Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d9befe101 | ||
|
|
abb262d9cf | ||
|
|
eca451b5a3 | ||
|
|
54af50d6d6 | ||
|
|
ce0fc3650d | ||
|
|
ce021e85f4 | ||
|
|
bc6613ec01 | ||
|
|
59aefc5bc2 | ||
|
|
fac58675d5 | ||
|
|
22f6c149db | ||
|
|
6d8b60fcfa | ||
|
|
54ecffc803 | ||
|
|
0731d63adf | ||
|
|
434d74b07f |
No files matched your search
+18
-1
@@ -11,7 +11,22 @@
|
||||
- Integration: Updated TextMate grammar and IntelliJ plugin (highlighting + keywords).
|
||||
- Documentation: New "Properties" section in `docs/OOP.md`.
|
||||
|
||||
- Docs: Scopes and Closures guidance
|
||||
- Language: Restricted Setter Visibility
|
||||
- Support for `private set` and `protected set` modifiers on `var` fields and properties.
|
||||
- Allows members to be publicly readable but only writable from within the declaring class or its subclasses.
|
||||
- Enforcement at runtime: throws `AccessException` on unauthorized writes.
|
||||
- Supported only for declarations in class bodies (fields and properties).
|
||||
- Documentation: New "Restricted Setter Visibility" section in `docs/OOP.md`.
|
||||
|
||||
- Language: Late-initialized `val` fields in classes
|
||||
- Support for declaring `val` without an immediate initializer in class bodies.
|
||||
- Compulsory initialization: every late-init `val` must be assigned at least once within the class body or an `init` block.
|
||||
- Write-once enforcement: assigning to a `val` is allowed only if its current value is `Unset`.
|
||||
- Access protection: reading a late-init `val` before it is assigned returns the `Unset` singleton; using `Unset` for most operations throws an `UnsetException`.
|
||||
- Extension properties do not support late-init.
|
||||
- Documentation: New "Late-initialized `val` fields" and "The `Unset` singleton" sections in `docs/OOP.md`.
|
||||
|
||||
- Docs: OOP improvements
|
||||
- New page: `docs/scopes_and_closures.md` detailing `ClosureScope` resolution order, recursion‑safe helpers (`chainLookupIgnoreClosure`, `chainLookupWithMembers`, `baseGetIgnoreClosure`), cycle prevention, and capturing lexical environments for callbacks (`snapshotForClosure`).
|
||||
- Updated: `docs/advanced_topics.md` (link to the new page), `docs/parallelism.md` (closures in `launch`/`flow`), `docs/OOP.md` (visibility from closures with preserved `currentClassCtx`), `docs/exceptions_handling.md` (compatibility alias `SymbolNotFound`).
|
||||
- Tutorial: added quick link to Scopes and Closures.
|
||||
@@ -83,6 +98,8 @@ All notable changes to this project will be documented in this file.
|
||||
- Mutually exclusive: `--check` and `--in-place` together now produce an error and exit with code 1.
|
||||
- Multi-file stdout prints headers `--- <path> ---` per file.
|
||||
- `lyng --help` shows `fmt`; `lyng fmt --help` displays dedicated help.
|
||||
- Fix: Property accessors (`get`, `set`, `private set`, `protected set`) are now correctly indented relative to the property declaration.
|
||||
- Fix: Indentation now correctly carries over into blocks that start on extra‑indented lines (e.g., nested `if` statements or property accessor bodies).
|
||||
|
||||
- CLI: Preserved legacy script invocation fast-paths:
|
||||
- `lyng script.lyng [args...]` executes the script directly.
|
||||
|
||||
Executable
+256
@@ -0,0 +1,256 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
#
|
||||
|
||||
set -e
|
||||
|
||||
# Configuration
|
||||
DOCS_DIR="docs"
|
||||
OUTPUT_DIR="distributables"
|
||||
TEMP_DIR="build/temp_docs"
|
||||
MERGED_MD="$TEMP_DIR/merged.md"
|
||||
OUTPUT_HTML="$OUTPUT_DIR/lyng_documentation.html"
|
||||
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
mkdir -p "$TEMP_DIR"
|
||||
|
||||
# Files that should come first in specific order
|
||||
PRIORITY_FILES=(
|
||||
"tutorial.md"
|
||||
"OOP.md"
|
||||
"advanced_topics.md"
|
||||
"declaring_arguments.md"
|
||||
"scopes_and_closures.md"
|
||||
"exceptions_handling.md"
|
||||
"when.md"
|
||||
"parallelism.md"
|
||||
"Testing.md"
|
||||
)
|
||||
|
||||
# Files that should come next (reference)
|
||||
REFERENCE_FILES=(
|
||||
"Collection.md"
|
||||
"Iterable.md"
|
||||
"Iterator.md"
|
||||
"List.md"
|
||||
"Set.md"
|
||||
"Map.md"
|
||||
"Array.md"
|
||||
"Buffer.md"
|
||||
"RingBuffer.md"
|
||||
"Range.md"
|
||||
"Real.md"
|
||||
"Regex.md"
|
||||
"math.md"
|
||||
"time.md"
|
||||
)
|
||||
|
||||
# Files that are about integration/tools
|
||||
INTEGRATION_FILES=(
|
||||
"serialization.md"
|
||||
"json_and_kotlin_serialization.md"
|
||||
"embedding.md"
|
||||
"lyng_cli.md"
|
||||
"lyng.io.fs.md"
|
||||
"formatter.md"
|
||||
"EfficientIterables.md"
|
||||
)
|
||||
|
||||
# Tracking processed files to avoid duplicates
|
||||
PROCESSED_PATHS=()
|
||||
|
||||
is_excluded() {
|
||||
local full_path="$1"
|
||||
if grep -q "excludeFromIndex" "$full_path"; then
|
||||
return 0 # true in bash
|
||||
fi
|
||||
return 1 # false
|
||||
}
|
||||
|
||||
process_file() {
|
||||
local rel_path="$1"
|
||||
local full_path="$DOCS_DIR/$rel_path"
|
||||
|
||||
if [[ ! -f "$full_path" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
if is_excluded "$full_path"; then
|
||||
echo "Skipping excluded: $rel_path"
|
||||
return
|
||||
fi
|
||||
|
||||
# Check for duplicates
|
||||
for p in "${PROCESSED_PATHS[@]}"; do
|
||||
if [[ "$p" == "$rel_path" ]]; then
|
||||
return
|
||||
fi
|
||||
done
|
||||
PROCESSED_PATHS+=("$rel_path")
|
||||
|
||||
echo "Processing: $rel_path"
|
||||
|
||||
# 1. Add an anchor for the file based on its path
|
||||
local anchor_name=$(echo "$rel_path" | sed 's/\//_/g')
|
||||
echo "<div id=\"$anchor_name\"></div>" >> "$MERGED_MD"
|
||||
echo "" >> "$MERGED_MD"
|
||||
|
||||
# 2. Append content with fixed links
|
||||
# - [text](file.md) -> [text](#file.md)
|
||||
# - [text](dir/file.md) -> [text](#dir_file.md)
|
||||
# - [text](file.md#anchor) -> [text](#anchor)
|
||||
# - Fix image links: [alt](../images/...) -> [alt](images/...) if needed, but none found yet.
|
||||
|
||||
cat "$full_path" | \
|
||||
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md\)/"[$1](#" . ($2 =~ s|\/|_|gr) . ".md)"/ge' | \
|
||||
perl -pe 's/\[([^\]]+)\]\(([^)]+)\.md#([^)]+)\)/[$1](#$3)/g' >> "$MERGED_MD"
|
||||
|
||||
echo -e "\n\n---\n\n" >> "$MERGED_MD"
|
||||
}
|
||||
|
||||
# Start with an empty merged file
|
||||
echo "% Lyng Language Documentation" > "$MERGED_MD"
|
||||
echo "" >> "$MERGED_MD"
|
||||
|
||||
# 1. Process priority files
|
||||
for f in "${PRIORITY_FILES[@]}"; do
|
||||
process_file "$f"
|
||||
done
|
||||
|
||||
# 2. Process reference files
|
||||
for f in "${REFERENCE_FILES[@]}"; do
|
||||
process_file "$f"
|
||||
done
|
||||
|
||||
# 3. Process integration files
|
||||
for f in "${INTEGRATION_FILES[@]}"; do
|
||||
process_file "$f"
|
||||
done
|
||||
|
||||
# 4. Process remaining files in docs root
|
||||
for f in "$DOCS_DIR"/*.md; do
|
||||
rel_f=${f#"$DOCS_DIR/"}
|
||||
process_file "$rel_f"
|
||||
done
|
||||
|
||||
# 5. Process remaining files in subdirs (like samples)
|
||||
find "$DOCS_DIR" -name "*.md" | sort | while read -r f; do
|
||||
rel_f=${f#"$DOCS_DIR/"}
|
||||
process_file "$rel_f"
|
||||
done
|
||||
|
||||
echo "Running pandoc to generate $OUTPUT_HTML..."
|
||||
|
||||
# Use a basic but clean CSS
|
||||
pandoc "$MERGED_MD" -o "$OUTPUT_HTML" \
|
||||
--toc --toc-depth=2 \
|
||||
--standalone \
|
||||
--embed-resources \
|
||||
--metadata title="Lyng Language Documentation" \
|
||||
--css <(echo "
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Helvetica, Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
max-width: 1000px;
|
||||
margin: 0 auto;
|
||||
padding: 2em;
|
||||
color: #24292e;
|
||||
background-color: #fff;
|
||||
}
|
||||
code {
|
||||
background-color: rgba(27,31,35,0.05);
|
||||
padding: 0.2em 0.4em;
|
||||
border-radius: 3px;
|
||||
font-family: SFMono-Regular, Consolas, \"Liberation Mono\", Menlo, monospace;
|
||||
font-size: 85%;
|
||||
}
|
||||
pre {
|
||||
background-color: #f6f8fa;
|
||||
padding: 16px;
|
||||
overflow: auto;
|
||||
border-radius: 3px;
|
||||
line-height: 1.45;
|
||||
}
|
||||
pre code {
|
||||
background-color: transparent;
|
||||
padding: 0;
|
||||
font-size: 100%;
|
||||
}
|
||||
h1 {
|
||||
border-bottom: 1px solid #eaecef;
|
||||
padding-bottom: 0.3em;
|
||||
margin-top: 24px;
|
||||
margin-bottom: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
h2 {
|
||||
border-bottom: 1px solid #eaecef;
|
||||
padding-bottom: 0.3em;
|
||||
margin-top: 24px;
|
||||
margin-bottom: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
hr {
|
||||
height: 0.25em;
|
||||
padding: 0;
|
||||
margin: 24px 0;
|
||||
background-color: #e1e4e8;
|
||||
border: 0;
|
||||
}
|
||||
blockquote {
|
||||
padding: 0 1em;
|
||||
color: #6a737d;
|
||||
border-left: 0.25em solid #dfe2e5;
|
||||
margin: 0 0 16px 0;
|
||||
}
|
||||
nav#TOC {
|
||||
background: #f9f9f9;
|
||||
padding: 1em;
|
||||
border: 1px solid #eee;
|
||||
margin-bottom: 2.5em;
|
||||
border-radius: 6px;
|
||||
}
|
||||
nav#TOC ul {
|
||||
list-style: none;
|
||||
padding-left: 1.5em;
|
||||
}
|
||||
nav#TOC > ul {
|
||||
padding-left: 0;
|
||||
}
|
||||
table {
|
||||
border-spacing: 0;
|
||||
border-collapse: collapse;
|
||||
margin-top: 0;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
table th, table td {
|
||||
padding: 6px 13px;
|
||||
border: 1px solid #dfe2e5;
|
||||
}
|
||||
table tr {
|
||||
background-color: #fff;
|
||||
border-top: 1px solid #c6cbd1;
|
||||
}
|
||||
table tr:nth-child(2n) {
|
||||
background-color: #f6f8fa;
|
||||
}
|
||||
")
|
||||
|
||||
echo "-------------------------------------------------------"
|
||||
echo "Done! Documentation generated successfully."
|
||||
echo "Location: $OUTPUT_HTML"
|
||||
echo "-------------------------------------------------------"
|
||||
+7
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,3 +29,9 @@ tasks.register<org.gradle.api.DefaultTask>("runIde") {
|
||||
description = "Run IntelliJ IDEA with the Lyng plugin (:lyng-idea)"
|
||||
dependsOn(":lyng-idea:runIde")
|
||||
}
|
||||
|
||||
tasks.register<Exec>("generateDocs") {
|
||||
group = "documentation"
|
||||
description = "Generates a single-file documentation HTML using bin/generate_docs.sh"
|
||||
commandLine("./bin/generate_docs.sh")
|
||||
}
|
||||
+123
@@ -100,6 +100,28 @@ class Counter {
|
||||
- **No Backing Fields**: There is no magic `field` identifier. If you need to store state, you must declare a separate (usually `private`) field.
|
||||
- **Type Inference**: You can omit the type declaration if it can be inferred or if you don't need strict typing.
|
||||
|
||||
### Lazy Properties with `cached`
|
||||
|
||||
When you want to define a property that is computed only once (on demand) and then remembered, use the built-in `cached` function. This is more efficient than a regular property with `get()` if the computation is expensive, as it avoids re-calculating the value on every access.
|
||||
|
||||
```kotlin
|
||||
class DataService(val id: Int) {
|
||||
// The lambda passed to cached is only executed once, the first time data() is called.
|
||||
val data = cached {
|
||||
println("Fetching data for " + id)
|
||||
// Perform expensive operation
|
||||
"Record " + id
|
||||
}
|
||||
}
|
||||
|
||||
val service = DataService(42)
|
||||
// No printing yet
|
||||
println(service.data()) // Prints "Fetching data for 42", then returns "Record 42"
|
||||
println(service.data()) // Returns "Record 42" immediately (no second fetch)
|
||||
```
|
||||
|
||||
Note that `cached` returns a lambda, so you access the value by calling it like a method: `service.data()`. This is a powerful pattern for lazy-loading resources, caching results of database queries, or delaying expensive computations until they are truly needed.
|
||||
|
||||
## Instance initialization: init block
|
||||
|
||||
In addition to the primary constructor arguments, you can provide an `init` block that runs on each instance creation. This is useful for more complex initializations, side effects, or setting up fields that depend on multiple constructor parameters.
|
||||
@@ -153,6 +175,44 @@ statements discussed later, there could be default values, ellipsis, etc.
|
||||
|
||||
Note that unlike **Kotlin**, which uses `=` for named arguments, Lyng uses `:` to avoid ambiguity with assignment expressions.
|
||||
|
||||
### Late-initialized `val` fields
|
||||
|
||||
You can declare a `val` field without an immediate initializer if you provide an assignment for it within an `init` block or the class body. This is useful when the initial value depends on logic that cannot be expressed in a single expression.
|
||||
|
||||
```kotlin
|
||||
class DataProcessor(data: Object) {
|
||||
val result: Object
|
||||
|
||||
init {
|
||||
// Complex initialization logic
|
||||
result = transform(data)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Key rules for late-init `val`:
|
||||
- **Compile-time Check**: The compiler ensures that every `val` declared without an initializer in a class body has at least one assignment within that class body (including `init` blocks). Failing to do so results in a syntax error.
|
||||
- **Write-Once**: A `val` can only be assigned once. Even if it was declared without an initializer, once it is assigned a value (e.g., in `init`), any subsequent assignment will throw an `IllegalAssignmentException`.
|
||||
- **Access before Initialization**: If you attempt to read a late-init `val` before it has been assigned (for example, by calling a method in `init` that reads the field before its assignment), it will hold a special `Unset` value. Using `Unset` for most operations (like arithmetic or method calls) will throw an `UnsetException`.
|
||||
- **No Extensions**: Extension properties do not support late initialization as they do not have per-instance storage. Extension `val`s must always have an initializer or a `get()` accessor.
|
||||
|
||||
### The `Unset` singleton
|
||||
|
||||
The `Unset` singleton represents a field that has been declared but not yet initialized. While it can be compared and converted to a string, most other operations on it are forbidden to prevent accidental use of uninitialized data.
|
||||
|
||||
```kotlin
|
||||
class T {
|
||||
val x
|
||||
fun check() {
|
||||
if (x == Unset) println("Not ready")
|
||||
}
|
||||
init {
|
||||
check() // Prints "Not ready"
|
||||
x = 42
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Methods
|
||||
|
||||
Functions defined inside a class body are methods, and unless declared
|
||||
@@ -372,6 +432,69 @@ Are declared with var
|
||||
assert( p.isSpecial == true )
|
||||
>>> void
|
||||
|
||||
### Restricted Setter Visibility
|
||||
|
||||
You can restrict the visibility of a `var` field's or property's setter by using `private set` or `protected set` modifiers. This allows the member to be publicly readable but only writable from within the class or its subclasses.
|
||||
|
||||
#### On Fields
|
||||
|
||||
```kotlin
|
||||
class SecretCounter {
|
||||
var count = 0
|
||||
private set // Can be read anywhere, but written only in SecretCounter
|
||||
|
||||
fun increment() { count++ }
|
||||
}
|
||||
|
||||
val c = SecretCounter()
|
||||
println(c.count) // OK
|
||||
c.count = 10 // Throws AccessException
|
||||
c.increment() // OK
|
||||
```
|
||||
|
||||
#### On Properties
|
||||
|
||||
You can also apply restricted visibility to custom property setters:
|
||||
|
||||
```kotlin
|
||||
class Person(private var _age: Int) {
|
||||
var age
|
||||
get() = _age
|
||||
private set(v) { if (v >= 0) _age = v }
|
||||
}
|
||||
```
|
||||
|
||||
#### Protected Setters and Inheritance
|
||||
|
||||
A `protected set` allows subclasses to modify a field that is otherwise read-only to the public:
|
||||
|
||||
```kotlin
|
||||
class Base {
|
||||
var state = "initial"
|
||||
protected set
|
||||
}
|
||||
|
||||
class Derived : Base() {
|
||||
fun changeState(newVal) {
|
||||
state = newVal // OK: protected access from subclass
|
||||
}
|
||||
}
|
||||
|
||||
val d = Derived()
|
||||
println(d.state) // OK: "initial"
|
||||
d.changeState("updated")
|
||||
println(d.state) // OK: "updated"
|
||||
d.state = "bad" // Throws AccessException: public write not allowed
|
||||
```
|
||||
|
||||
### Key Rules and Limitations
|
||||
|
||||
- **Only for `var`**: Restricted setter visibility cannot be used with `val` declarations, as they are inherently read-only. Attempting to use it with `val` results in a syntax error.
|
||||
- **Class Body Only**: These modifiers can only be used on members declared within the class body. They are not supported for primary constructor parameters.
|
||||
- **`private set`**: The setter is only accessible within the same class context (specifically, when `this` is an instance of that class).
|
||||
- **`protected set`**: The setter is accessible within the declaring class and all its transitive subclasses.
|
||||
- **Multiple Inheritance**: In MI scenarios, visibility is checked against the class that actually declared the member. Qualified access (e.g., `this@Base.field = value`) also respects restricted setter visibility.
|
||||
|
||||
### Private fields
|
||||
|
||||
Private fields are visible only _inside the class instance_:
|
||||
|
||||
+60
-5
@@ -109,7 +109,62 @@ scope.eval("val y = inc(41); log('Answer:', y)")
|
||||
|
||||
You can register multiple names (aliases) at once: `addFn<ObjInt>("inc", "increment") { ... }`.
|
||||
|
||||
### 5) Read variable values back in Kotlin
|
||||
### 5) Add Kotlin‑backed fields
|
||||
|
||||
If you need a simple field (with a value) instead of a computed property, use `createField`. This adds a field to the class that will be present in all its instances.
|
||||
|
||||
```kotlin
|
||||
val myClass = ObjClass("MyClass")
|
||||
|
||||
// Add a read-only field (constant)
|
||||
myClass.createField("version", ObjString("1.0.0"), isMutable = false)
|
||||
|
||||
// Add a mutable field with an initial value
|
||||
myClass.createField("count", ObjInt(0), isMutable = true)
|
||||
|
||||
scope.addConst("MyClass", myClass)
|
||||
```
|
||||
|
||||
In Lyng:
|
||||
```lyng
|
||||
val instance = MyClass()
|
||||
println(instance.version) // -> "1.0.0"
|
||||
instance.count = 5
|
||||
println(instance.count) // -> 5
|
||||
```
|
||||
|
||||
### 6) Add Kotlin‑backed properties
|
||||
|
||||
Properties in Lyng are pure accessors (getters and setters) and do not have automatic backing fields. You can add them to a class using `addProperty`.
|
||||
|
||||
```kotlin
|
||||
val myClass = ObjClass("MyClass")
|
||||
var internalValue: Long = 10
|
||||
|
||||
myClass.addProperty(
|
||||
name = "value",
|
||||
getter = {
|
||||
// Return current value as a Lyng object
|
||||
ObjInt(internalValue)
|
||||
},
|
||||
setter = { newValue ->
|
||||
// newValue is passed as a Lyng object (the first and only argument)
|
||||
internalValue = (newValue as ObjInt).value
|
||||
}
|
||||
)
|
||||
|
||||
scope.addConst("MyClass", myClass)
|
||||
```
|
||||
|
||||
Usage in Lyng:
|
||||
```lyng
|
||||
val instance = MyClass()
|
||||
println(instance.value) // -> 10
|
||||
instance.value = 42
|
||||
println(instance.value) // -> 42
|
||||
```
|
||||
|
||||
### 7) Read variable values back in Kotlin
|
||||
|
||||
The simplest approach: evaluate an expression that yields the value and convert it.
|
||||
|
||||
@@ -124,7 +179,7 @@ val kotlinName = scope.eval("name").toKotlin(scope) // -> "Lyng rocks!"
|
||||
|
||||
Advanced: you can also grab a variable record directly via `scope.get(name)` and work with its `Obj` value, but evaluating `"name"` is often clearer and enforces Lyng semantics consistently.
|
||||
|
||||
### 6) Execute scripts with parameters; call Lyng functions from Kotlin
|
||||
### 8) Execute scripts with parameters; call Lyng functions from Kotlin
|
||||
|
||||
There are two convenient patterns.
|
||||
|
||||
@@ -157,7 +212,7 @@ val result = resultObj.toKotlin(scope) // -> 42
|
||||
|
||||
If you need to pass complex data (lists, maps), construct the corresponding Lyng `Obj` types (`ObjList`, `ObjMap`, etc.) and pass them in `Arguments`.
|
||||
|
||||
### 7) Create your own packages and import them in Lyng
|
||||
### 9) Create your own packages and import them in Lyng
|
||||
|
||||
Lyng supports packages that are imported from scripts. You can register packages programmatically via `ImportManager` or by providing source texts that declare `package ...`.
|
||||
|
||||
@@ -212,7 +267,7 @@ val s = scope.eval("s").toKotlin(scope) // -> 144
|
||||
|
||||
You can also register from parsed `Source` instances via `addSourcePackages(source)`.
|
||||
|
||||
### 8) Executing from files, security, and isolation
|
||||
### 10) Executing from files, security, and isolation
|
||||
|
||||
- To run code from a file, read it and pass to `scope.eval(text)` or compile with `Compiler.compile(Source(fileName, text))`.
|
||||
- `ImportManager` takes an optional `SecurityManager` if you need to restrict what packages or operations are available. By default, `Script.defaultImportManager` allows everything suitable for embedded use; clamp it down in sandboxed environments.
|
||||
@@ -223,7 +278,7 @@ You can also register from parsed `Source` instances via `addSourcePackages(sour
|
||||
val isolated = net.sergeych.lyng.Scope.new()
|
||||
```
|
||||
|
||||
### 9) Tips and troubleshooting
|
||||
### 11) Tips and troubleshooting
|
||||
|
||||
- All values that cross the boundary must be Lyng `Obj` instances. Convert Kotlin values explicitly (e.g., `ObjInt`, `ObjReal`, `ObjString`).
|
||||
- Use `toKotlin(scope)` to get Kotlin values back. Collections convert to Kotlin collections recursively.
|
||||
|
||||
@@ -68,6 +68,26 @@ Tip: If a closure unexpectedly cannot see an outer local, check whether an inter
|
||||
- The `visited` sets used for cycle detection are tiny and short‑lived; in typical scripts the overhead is negligible.
|
||||
- If profiling shows hotspots, consider limiting ancestry depth in your custom helpers or using small fixed arrays instead of hash sets—only for extremely hot code paths.
|
||||
|
||||
## Practical Example: `cached`
|
||||
|
||||
The `cached` function (defined in `lyng.stdlib`) is a classic example of using closures to maintain state. It wraps a builder into a zero-argument function that computes once and remembers the result:
|
||||
|
||||
```kotlin
|
||||
fun cached(builder) {
|
||||
var calculated = false
|
||||
var value = null
|
||||
{ // This lambda captures `calculated`, `value`, and `builder`
|
||||
if( !calculated ) {
|
||||
value = builder()
|
||||
calculated = true
|
||||
}
|
||||
value
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Because Lyng now correctly isolates closures for each evaluation of a lambda literal, using `cached` inside a class instance works as expected: each instance maintains its own private `calculated` and `value` state, even if they share the same property declaration.
|
||||
|
||||
## Dos and Don’ts
|
||||
- Do use `chainLookupIgnoreClosure` / `chainLookupWithMembers` for ancestry traversals.
|
||||
- Do maintain the resolution order above for predictable behavior.
|
||||
|
||||
+45
-1
@@ -1497,7 +1497,7 @@ See [math functions](math.md). Other general purpose functions are:
|
||||
| print(args...) | Open for overriding, it prints to stdout without newline. |
|
||||
| flow {} | create flow sequence, see [parallelism] |
|
||||
| delay, launch, yield | see [parallelism] |
|
||||
| cached(builder) | remembers builder() on first invocation and return it then |
|
||||
| cached(builder) | [Lazy evaluation with `cached`](#lazy-evaluation-with-cached) |
|
||||
| let, also, apply, run | see above, flow controls |
|
||||
|
||||
(1)
|
||||
@@ -1545,6 +1545,50 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
|
||||
|
||||
[Regex]: Regex.md
|
||||
|
||||
## Lazy evaluation with `cached`
|
||||
|
||||
Sometimes you have an expensive computation that you only want to perform if and when it is actually needed, and then remember (cache) the result for all future calls. Lyng provides the `cached(builder)` function for this purpose.
|
||||
|
||||
It is extremely simple to use: you pass it a block (lambda) that performs the computation, and it returns a zero-argument function that manages the caching for you.
|
||||
|
||||
### Basic Example
|
||||
|
||||
```kotlin
|
||||
val expensive = cached {
|
||||
println("Performing expensive calculation...")
|
||||
2 + 2
|
||||
}
|
||||
|
||||
println(expensive()) // Prints "Performing expensive calculation..." then "4"
|
||||
println(expensive()) // Prints only "4" (result is cached)
|
||||
```
|
||||
|
||||
### Benefits and Simplicity
|
||||
|
||||
1. **Lazy Execution:** The code inside the `cached` block doesn't run until you actually call the resulting function.
|
||||
2. **Automatic State Management:** You don't need to manually check if a value has been computed or store it in a separate variable.
|
||||
3. **Closures and Class Support:** `cached` works perfectly with closures. If you use it inside a class, it will correctly capture the instance variables, and each instance will have its own independent cache.
|
||||
|
||||
### Use Case: Lazy Properties in Classes
|
||||
|
||||
This is the most common use case for `cached`. It allows you to define expensive "fields" that are only computed if someone actually uses them:
|
||||
|
||||
```kotlin
|
||||
class User(val id: Int) {
|
||||
// The details will be fetched only once, on demand
|
||||
val details = cached {
|
||||
println("Fetching details for user " + id)
|
||||
// Db.query is a hypothetical example
|
||||
Db.query("SELECT * FROM users WHERE id = " + id)
|
||||
}
|
||||
}
|
||||
|
||||
val u = User(101)
|
||||
// ... nothing happens yet ...
|
||||
val d = u.details() // Computation happens here
|
||||
val sameD = u.details() // Returns the same result immediately
|
||||
```
|
||||
|
||||
## Multiple Inheritance (quick start)
|
||||
|
||||
Lyng supports multiple inheritance (MI) with simple, predictable rules. For a full reference see OOP notes, this is a quick, copy‑paste friendly overview.
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
# What's New in Lyng
|
||||
|
||||
This document highlights the latest additions and improvements to the Lyng language and its ecosystem.
|
||||
|
||||
## Language Features
|
||||
|
||||
### Class Properties with Accessors
|
||||
Classes now support properties with custom `get()` and `set()` accessors. Properties in Lyng do **not** have automatic backing fields; they are pure accessors.
|
||||
|
||||
```lyng
|
||||
class Person(private var _age: Int) {
|
||||
// Read-only property
|
||||
val ageCategory get() = if (_age < 18) "Minor" else "Adult"
|
||||
|
||||
// Read-write property
|
||||
var age: Int
|
||||
get() = _age
|
||||
set(v) {
|
||||
if (v >= 0) _age = v
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Private and Protected Setters
|
||||
You can now restrict the visibility of a property's or field's setter using `private set` or `protected set`. This allows members to be publicly readable but only writable from within the declaring class or its subclasses.
|
||||
|
||||
```lyng
|
||||
class Counter {
|
||||
var count = 0
|
||||
private set // Field with private setter
|
||||
|
||||
fun increment() { count++ }
|
||||
}
|
||||
|
||||
class AdvancedCounter : Counter {
|
||||
var totalOperations = 0
|
||||
protected set // Settable here and in further subclasses
|
||||
}
|
||||
|
||||
let c = Counter()
|
||||
c.increment() // OK
|
||||
// c.count = 10 // Error: setter is private
|
||||
```
|
||||
|
||||
### Late-initialized `val` Fields
|
||||
`val` fields in classes can be declared without an immediate initializer, provided they are assigned exactly once. If accessed before initialization, they hold the special `Unset` singleton.
|
||||
|
||||
```lyng
|
||||
class Service {
|
||||
val logger
|
||||
|
||||
fun check() {
|
||||
if (logger == Unset) println("Not initialized yet")
|
||||
}
|
||||
|
||||
init {
|
||||
logger = Logger("Service")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Named Arguments and Named Splats
|
||||
Function calls now support named arguments using the `name: value` syntax. If the variable name matches the parameter name, you can use the `name:` shorthand.
|
||||
|
||||
```lyng
|
||||
fun greet(name, greeting = "Hello") {
|
||||
println("$greeting, $name!")
|
||||
}
|
||||
|
||||
val name = "Alice"
|
||||
greet(name:) // Shorthand for greet(name: name)
|
||||
greet(greeting: "Hi", name: "Bob")
|
||||
|
||||
let params = { name: "Charlie", greeting: "Hey")
|
||||
greet(...params) // Named splat expansion
|
||||
```
|
||||
|
||||
### Multiple Inheritance (MI)
|
||||
Lyng now supports multiple inheritance using the C3 Method Resolution Order (MRO). Use `this@Type` or casts for disambiguation.
|
||||
|
||||
```lyng
|
||||
class A { fun foo() = "A" }
|
||||
class B { fun foo() = "B" }
|
||||
|
||||
class Derived : A, B {
|
||||
fun test() {
|
||||
println(foo()) // Resolves to A.foo (leftmost)
|
||||
println(this@B.foo()) // Qualified dispatch to B.foo
|
||||
}
|
||||
}
|
||||
|
||||
let d = Derived()
|
||||
println((d as B).foo()) // Disambiguation via cast
|
||||
```
|
||||
|
||||
## Tooling and Infrastructure
|
||||
|
||||
### CLI: Formatting Command
|
||||
A new `fmt` subcommand has been added to the Lyng CLI.
|
||||
|
||||
```bash
|
||||
lyng fmt MyFile.lyng # Print formatted code to stdout
|
||||
lyng fmt --in-place MyFile.lyng # Format file in-place
|
||||
lyng fmt --check MyFile.lyng # Check if file needs formatting
|
||||
```
|
||||
|
||||
### IDEA Plugin: Autocompletion
|
||||
Experimental lightweight autocompletion is now available in the IntelliJ plugin. It features type-aware member suggestions and inheritance-aware completion.
|
||||
|
||||
You can enable it in **Settings | Lyng Formatter | Enable Lyng autocompletion**.
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,7 +21,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "net.sergeych.lyng"
|
||||
version = "0.0.4-SNAPSHOT"
|
||||
version = "0.0.5-SNAPSHOT"
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(17)
|
||||
|
||||
+6
-4
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -117,6 +117,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
d.name,
|
||||
if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
|
||||
)
|
||||
is MiniEnumDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,6 +168,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
d.ctorFields.forEach { addTypeSegments(it.type) }
|
||||
d.classFields.forEach { addTypeSegments(it.type) }
|
||||
}
|
||||
is MiniEnumDecl -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,9 +185,9 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
fun keyForKind(k: SymbolKind) = when (k) {
|
||||
SymbolKind.Function -> LyngHighlighterColors.FUNCTION
|
||||
SymbolKind.Class, SymbolKind.Enum -> LyngHighlighterColors.TYPE
|
||||
SymbolKind.Param -> LyngHighlighterColors.PARAMETER
|
||||
SymbolKind.Val -> LyngHighlighterColors.VALUE
|
||||
SymbolKind.Var -> LyngHighlighterColors.VARIABLE
|
||||
SymbolKind.Parameter -> LyngHighlighterColors.PARAMETER
|
||||
SymbolKind.Value -> LyngHighlighterColors.VALUE
|
||||
SymbolKind.Variable -> LyngHighlighterColors.VARIABLE
|
||||
}
|
||||
|
||||
// Track covered ranges to not override later heuristics
|
||||
|
||||
+38
-418
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,18 +26,16 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.patterns.PlatformPatterns
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.util.ProcessingContext
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.Compiler
|
||||
import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
|
||||
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
|
||||
import net.sergeych.lyng.idea.util.DocsBootstrap
|
||||
import net.sergeych.lyng.idea.util.IdeLenientImportProvider
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.idea.util.TextCtx
|
||||
import net.sergeych.lyng.miniast.*
|
||||
|
||||
@@ -65,6 +63,12 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
StdlibDocsBootstrap.ensure()
|
||||
val file: PsiFile = parameters.originalFile
|
||||
if (file.language != LyngLanguage) return
|
||||
|
||||
// Disable completion inside comments
|
||||
val pos = parameters.position
|
||||
val et = pos.node.elementType
|
||||
if (et == LyngTokenTypes.LINE_COMMENT || et == LyngTokenTypes.BLOCK_COMMENT) return
|
||||
|
||||
// Feature toggle: allow turning completion off from settings
|
||||
val settings = LyngFormatterSettings.getInstance(file.project)
|
||||
if (!settings.enableLyngCompletionExperimental) return
|
||||
@@ -94,7 +98,8 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
}
|
||||
|
||||
// Build MiniAst (cached) for both global and member contexts to enable local class/val inference
|
||||
val mini = buildMiniAstCached(file, text)
|
||||
val mini = LyngAstManager.getMiniAst(file)
|
||||
val binding = LyngAstManager.getBinding(file)
|
||||
|
||||
// Delegate computation to the shared engine to keep behavior in sync with tests
|
||||
val engineItems = try {
|
||||
@@ -121,13 +126,13 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
// Try inferring return/receiver class around the dot
|
||||
val inferred =
|
||||
// Prefer MiniAst-based inference (return type from member call or receiver type)
|
||||
guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported)
|
||||
?: guessReceiverClassViaMini(mini, text, memberDotPos, imported)
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDotPos, imported, binding)
|
||||
?:
|
||||
guessReturnClassFromMemberCallBefore(text, memberDotPos, imported)
|
||||
?: guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported)
|
||||
?: guessReturnClassAcrossKnownCallees(text, memberDotPos, imported)
|
||||
?: guessReceiverClass(text, memberDotPos, imported)
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
|
||||
|
||||
if (inferred != null) {
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback inferred receiver/return class='$inferred' — offering its members")
|
||||
@@ -159,6 +164,8 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
.withInsertHandler(ParenInsertHandler)
|
||||
Kind.Class_ -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Class)
|
||||
Kind.Enum -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Enum)
|
||||
Kind.Value -> LookupElementBuilder.create(ci.name)
|
||||
.withIcon(AllIcons.Nodes.Field)
|
||||
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
|
||||
@@ -177,18 +184,18 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
add("lyng.stdlib")
|
||||
}.toList()
|
||||
val inferredClass =
|
||||
guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported)
|
||||
?: guessReceiverClassViaMini(mini, text, memberDotPos, imported)
|
||||
?: guessReturnClassFromMemberCallBefore(text, memberDotPos, imported)
|
||||
?: guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported)
|
||||
?: guessReturnClassAcrossKnownCallees(text, memberDotPos, imported)
|
||||
?: guessReceiverClass(text, memberDotPos, imported)
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
|
||||
if (!inferredClass.isNullOrBlank()) {
|
||||
val ext = BuiltinDocRegistry.extensionMemberNamesFor(inferredClass)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${'$'}{ext}")
|
||||
for (name in ext) {
|
||||
if (existing.contains(name)) continue
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name)
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name, mini)
|
||||
if (resolved != null) {
|
||||
when (val member = resolved.second) {
|
||||
is MiniMemberFunDecl -> {
|
||||
@@ -232,12 +239,12 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
add("lyng.stdlib")
|
||||
}.toList()
|
||||
val inferred =
|
||||
guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported)
|
||||
?: guessReceiverClassViaMini(mini, text, memberDotPos, imported)
|
||||
?: guessReturnClassFromMemberCallBefore(text, memberDotPos, imported)
|
||||
?: guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported)
|
||||
?: guessReturnClassAcrossKnownCallees(text, memberDotPos, imported)
|
||||
?: guessReceiverClass(text, memberDotPos, imported)
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
|
||||
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
|
||||
if (inferred != null) {
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Enrichment inferred class='$inferred' — offering its members")
|
||||
offerMembers(emit, imported, inferred, sourceText = text, mini = mini)
|
||||
@@ -267,7 +274,8 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
.withIcon(kindIcon)
|
||||
.withTypeText(typeOf(d.type), true)
|
||||
}
|
||||
else -> LookupElementBuilder.create(name)
|
||||
is MiniEnumDecl -> LookupElementBuilder.create(name)
|
||||
.withIcon(AllIcons.Nodes.Enum)
|
||||
}
|
||||
emit(builder)
|
||||
}
|
||||
@@ -294,9 +302,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
, sourceText: String,
|
||||
mini: MiniScript? = null
|
||||
) {
|
||||
// Ensure modules are seeded in the registry (triggers lazy stdlib build too)
|
||||
for (m in imported) BuiltinDocRegistry.docsForModule(m)
|
||||
val classes = DocLookupUtils.aggregateClasses(imported)
|
||||
val classes = DocLookupUtils.aggregateClasses(imported, mini)
|
||||
if (DEBUG_COMPLETION) {
|
||||
val keys = classes.keys.joinToString(", ")
|
||||
log.info("[LYNG_DEBUG] offerMembers: imported=${imported} classes=[${keys}] target=${className}")
|
||||
@@ -315,7 +321,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
}
|
||||
// If MiniAst didn't populate members (empty), try to scan class body text for member signatures
|
||||
if (localClass.members.isEmpty()) {
|
||||
val scanned = scanLocalClassMembersFromText(mini, text = sourceText, cls = localClass)
|
||||
val scanned = DocLookupUtils.scanLocalClassMembersFromText(mini, text = sourceText, cls = localClass)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Local scan for class ${localClass.name}: found ${scanned.size} members -> ${scanned.keys}")
|
||||
for ((name, sig) in scanned) {
|
||||
when (sig.kind) {
|
||||
@@ -420,7 +426,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
.firstOrNull { it.type != null } ?: rep
|
||||
val builder = LookupElementBuilder.create(name)
|
||||
.withIcon(icon)
|
||||
.withTypeText(typeOf((chosen as MiniMemberValDecl).type), true)
|
||||
.withTypeText(typeOf(chosen.type), true)
|
||||
emit(builder)
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
@@ -541,392 +547,6 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
|
||||
// --- MiniAst-based inference helpers ---
|
||||
|
||||
private fun previousIdentifierBeforeDot(text: String, dotPos: Int): String? {
|
||||
var i = dotPos - 1
|
||||
// skip whitespace
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
val end = i + 1
|
||||
while (i >= 0 && TextCtx.isIdentChar(text[i])) i--
|
||||
val start = i + 1
|
||||
return if (start < end) text.substring(start, end) else null
|
||||
}
|
||||
|
||||
private fun guessReceiverClassViaMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>): String? {
|
||||
if (mini == null) return null
|
||||
val ident = previousIdentifierBeforeDot(text, dotPos) ?: return null
|
||||
// 1) Local val/var in the file
|
||||
val valDecl = mini.declarations.filterIsInstance<MiniValDecl>().firstOrNull { it.name == ident }
|
||||
val typeFromVal = valDecl?.type?.let { simpleClassNameOf(it) }
|
||||
if (!typeFromVal.isNullOrBlank()) return typeFromVal
|
||||
// If initializer exists, try to sniff ClassName(
|
||||
val initR = valDecl?.initRange
|
||||
if (initR != null) {
|
||||
val src = mini.range.start.source
|
||||
val s = src.offsetOf(initR.start)
|
||||
val e = src.offsetOf(initR.end).coerceAtMost(text.length)
|
||||
if (s in 0..e && e <= text.length) {
|
||||
val init = text.substring(s, e)
|
||||
Regex("([A-Za-z_][A-Za-z0-9_]*)\\s*\\(").find(init)?.let { m ->
|
||||
val cls = m.groupValues[1]
|
||||
return cls
|
||||
}
|
||||
}
|
||||
}
|
||||
// 2) Parameters in any function (best-effort without scope mapping)
|
||||
val paramType = mini.declarations.filterIsInstance<MiniFunDecl>()
|
||||
.asSequence()
|
||||
.flatMap { it.params.asSequence() }
|
||||
.firstOrNull { it.name == ident }?.type
|
||||
val typeFromParam = simpleClassNameOf(paramType)
|
||||
if (!typeFromParam.isNullOrBlank()) return typeFromParam
|
||||
return null
|
||||
}
|
||||
|
||||
private fun guessReturnClassFromMemberCallBeforeMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>): String? {
|
||||
if (mini == null) return null
|
||||
var i = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
// back to matching '('
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && TextCtx.isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
// Ensure member call: dot before callee
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k < 0 || text[k] != '.') return null
|
||||
val prevDot = k
|
||||
// Resolve receiver class via MiniAst (ident like `x`)
|
||||
val receiverClass = guessReceiverClassViaMini(mini, text, prevDot, imported) ?: return null
|
||||
// If receiver class is a locally declared class, resolve member on it
|
||||
val localClass = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == receiverClass }
|
||||
if (localClass != null) {
|
||||
val mm = localClass.members.firstOrNull { it.name == callee }
|
||||
if (mm != null) {
|
||||
val rt = when (mm) {
|
||||
is MiniMemberFunDecl -> mm.returnType
|
||||
is MiniMemberValDecl -> mm.type
|
||||
else -> null
|
||||
}
|
||||
return simpleClassNameOf(rt)
|
||||
} else {
|
||||
// Try to scan class body text for method signature and extract return type
|
||||
val sigs = scanLocalClassMembersFromText(mini, text, localClass)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Local scan for return type in ${receiverClass}.${callee}: candidates=${sigs.keys}")
|
||||
val sig = sigs[callee]
|
||||
if (sig != null && sig.typeText != null) return sig.typeText
|
||||
}
|
||||
}
|
||||
// Else fallback to registry-based resolution (covers imported classes)
|
||||
return DocLookupUtils.resolveMemberWithInheritance(imported, receiverClass, callee)?.second?.let { m ->
|
||||
val rt = when (m) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
simpleClassNameOf(rt)
|
||||
}
|
||||
}
|
||||
|
||||
private data class ScannedSig(val kind: String, val params: List<String>?, val typeText: String?)
|
||||
|
||||
private fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> {
|
||||
val src = mini.range.start.source
|
||||
val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start)
|
||||
val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length)
|
||||
if (start !in 0..end) return emptyMap()
|
||||
val body = text.substring(start, end)
|
||||
val map = LinkedHashMap<String, ScannedSig>()
|
||||
// fun name(params): Type
|
||||
val funRe = Regex("(?m)^\\s*fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(([^)]*)\\)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?")
|
||||
for (m in funRe.findAll(body)) {
|
||||
val name = m.groupValues.getOrNull(1) ?: continue
|
||||
val params = m.groupValues.getOrNull(2)?.split(',')?.mapNotNull { it.trim().takeIf { it.isNotEmpty() } } ?: emptyList()
|
||||
val type = m.groupValues.getOrNull(3)?.takeIf { it.isNotBlank() }
|
||||
map[name] = ScannedSig("fun", params, type)
|
||||
}
|
||||
// val/var name: Type
|
||||
val valRe = Regex("(?m)^\\s*(val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?")
|
||||
for (m in valRe.findAll(body)) {
|
||||
val kind = m.groupValues.getOrNull(1) ?: continue
|
||||
val name = m.groupValues.getOrNull(2) ?: continue
|
||||
val type = m.groupValues.getOrNull(3)?.takeIf { it.isNotBlank() }
|
||||
map.putIfAbsent(name, ScannedSig(kind, null, type))
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
private fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
// 1) Try call-based: ClassName(...).
|
||||
DocLookupUtils.guessClassFromCallBefore(text, dotPos, imported)?.let { return it }
|
||||
|
||||
// 2) Literal heuristics based on the immediate char before '.'
|
||||
var i = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
if (i >= 0) {
|
||||
when (text[i]) {
|
||||
'"' -> {
|
||||
// Either regular or triple-quoted string; both map to String
|
||||
return "String"
|
||||
}
|
||||
']' -> return "List" // very rough heuristic
|
||||
'}' -> return "Dict" // map/dictionary literal heuristic
|
||||
')' -> {
|
||||
// Parenthesized expression: walk back to matching '(' and inspect inner expression
|
||||
var j = i - 1
|
||||
var depth = 0
|
||||
while (j >= 0) {
|
||||
when (text[j]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
j--
|
||||
}
|
||||
if (j >= 0 && text[j] == '(') {
|
||||
val innerS = (j + 1).coerceAtLeast(0)
|
||||
val innerE = i.coerceAtMost(text.length)
|
||||
if (innerS < innerE) {
|
||||
val inner = text.substring(innerS, innerE).trim()
|
||||
if (inner.startsWith('"') && inner.endsWith('"')) return "String"
|
||||
if (inner.startsWith('[') && inner.endsWith(']')) return "List"
|
||||
if (inner.startsWith('{') && inner.endsWith('}')) return "Dict"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Numeric literal: support decimal, hex (0x..), and scientific notation (1e-3)
|
||||
var j = i
|
||||
var hasDigits = false
|
||||
var hasDot = false
|
||||
var hasExp = false
|
||||
// Walk over digits, letters for hex, dots, and exponent markers
|
||||
while (j >= 0) {
|
||||
val ch = text[j]
|
||||
if (ch.isDigit()) { hasDigits = true; j-- ; continue }
|
||||
if (ch == '.') { hasDot = true; j-- ; continue }
|
||||
if (ch == 'e' || ch == 'E') { hasExp = true; j-- ; // optional sign directly before digits
|
||||
if (j >= 0 && (text[j] == '+' || text[j] == '-')) j--
|
||||
continue
|
||||
}
|
||||
if (ch in listOf('x','X')) { // part of 0x prefix
|
||||
j--
|
||||
continue
|
||||
}
|
||||
if (ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd' || ch == 'f' ||
|
||||
ch == 'A' || ch == 'B' || ch == 'C' || ch == 'D' || ch == 'F') {
|
||||
// hex digit in 0x...
|
||||
j--
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
// Now check for 0x/0X prefix
|
||||
val k = j
|
||||
val isHex = k >= 1 && text[k] == '0' && (text[k+1] == 'x' || text[k+1] == 'X')
|
||||
if (hasDigits) {
|
||||
return if (isHex) "Int" else if (hasDot || hasExp) "Real" else "Int"
|
||||
}
|
||||
|
||||
// 3) this@Type or as Type
|
||||
val identRange = TextCtx.wordRangeAt(text, i + 1)
|
||||
if (identRange != null) {
|
||||
val ident = text.substring(identRange.startOffset, identRange.endOffset)
|
||||
// if it's "as Type", we want Type
|
||||
var k2 = TextCtx.prevNonWs(text, identRange.startOffset - 1)
|
||||
if (k2 >= 1 && text[k2] == 's' && text[k2 - 1] == 'a' && (k2 - 1 == 0 || !text[k2 - 2].isLetterOrDigit())) {
|
||||
return ident
|
||||
}
|
||||
// if it's "this@Type", we want Type
|
||||
if (k2 >= 0 && text[k2] == '@') {
|
||||
val k3 = TextCtx.prevNonWs(text, k2 - 1)
|
||||
if (k3 >= 3 && text.substring(k3 - 3, k3 + 1) == "this") {
|
||||
return ident
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to infer the class of the return value of the member call immediately before the dot.
|
||||
* Example: `Path(".." ).lines().<caret>` → detects `lines()` on receiver class `Path` and returns `Iterator`.
|
||||
*/
|
||||
private fun guessReturnClassFromMemberCallBefore(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
var i = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
if (i < 0) return null
|
||||
// We expect a call just before the dot, i.e., ')' ... '.'
|
||||
if (text[i] != ')') return null
|
||||
// Walk back to matching '('
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
val ch = text[i]
|
||||
when (ch) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
// Identify callee identifier just before '('
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && TextCtx.isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
// Ensure it's a member call (there must be a dot immediately before the callee, ignoring spaces)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k < 0 || text[k] != '.') return null
|
||||
val prevDot = k
|
||||
// Infer receiver class at the previous dot
|
||||
val receiverClass = guessReceiverClass(text, prevDot, imported) ?: return null
|
||||
// Resolve the callee as a member of receiver class, including inheritance
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, receiverClass, callee) ?: return null
|
||||
val member = resolved.second
|
||||
val returnType = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(returnType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Infer return class of a top-level call right before the dot: e.g., `files().<caret>`.
|
||||
* We extract callee name and resolve it among imported modules' top-level functions.
|
||||
*/
|
||||
private fun guessReturnClassFromTopLevelCallBefore(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
var i = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
// Walk back to matching '('
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
val ch = text[i]
|
||||
when (ch) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
// Extract callee ident before '('
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && TextCtx.isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
// If it's a member call, bail out (handled in member-call inference)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k >= 0 && text[k] == '.') return null
|
||||
|
||||
// Resolve top-level function in imported modules
|
||||
for (mod in imported) {
|
||||
val decls = BuiltinDocRegistry.docsForModule(mod)
|
||||
val fn = decls.asSequence().filterIsInstance<MiniFunDecl>().firstOrNull { it.name == callee }
|
||||
if (fn != null) return simpleClassNameOf(fn.returnType)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback: if we can at least extract a callee name before the dot and it exists across common classes,
|
||||
* derive its return type using cross-class lookup (Iterable/Iterator/List preference). This ignores the receiver.
|
||||
* Example: `something.lines().<caret>` where `something` type is unknown, but `lines()` commonly returns Iterator<String>.
|
||||
*/
|
||||
private fun guessReturnClassAcrossKnownCallees(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
var i = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
// Walk back to matching '('
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
val ch = text[i]
|
||||
when (ch) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
// Extract callee ident before '('
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && TextCtx.isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
// Try cross-class resolution
|
||||
val resolved = DocLookupUtils.findMemberAcrossClasses(imported, callee) ?: return null
|
||||
val member = resolved.second
|
||||
val returnType = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(returnType)
|
||||
}
|
||||
|
||||
/** Convert a MiniTypeRef to a simple class name as used by docs (e.g., Iterator from Iterator<String>). */
|
||||
private fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) {
|
||||
null -> null
|
||||
is MiniTypeName -> t.segments.lastOrNull()?.name
|
||||
is MiniGenericType -> simpleClassNameOf(t.base)
|
||||
is MiniFunctionType -> null
|
||||
is MiniTypeVar -> null
|
||||
}
|
||||
|
||||
private fun buildMiniAst(text: String): MiniScript? {
|
||||
return try {
|
||||
val sink = MiniAstBuilder()
|
||||
val provider = IdeLenientImportProvider.create()
|
||||
val src = Source("<ide>", text)
|
||||
runBlocking { Compiler.compileWithMini(src, provider, sink) }
|
||||
sink.build()
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
// Cached per PsiFile by document modification stamp
|
||||
private val MINI_KEY = Key.create<MiniScript>("lyng.mini.cache")
|
||||
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
|
||||
|
||||
private fun buildMiniAstCached(file: PsiFile, text: String): MiniScript? {
|
||||
val doc = file.viewProvider.document ?: return null
|
||||
val stamp = doc.modificationStamp
|
||||
val prevStamp = file.getUserData(STAMP_KEY)
|
||||
val cached = file.getUserData(MINI_KEY)
|
||||
if (cached != null && prevStamp != null && prevStamp == stamp) return cached
|
||||
val built = buildMiniAst(text)
|
||||
// Cache even null? avoid caching failures; only cache non-null
|
||||
if (built != null) {
|
||||
file.putUserData(MINI_KEY, built)
|
||||
file.putUserData(STAMP_KEY, stamp)
|
||||
}
|
||||
return built
|
||||
}
|
||||
|
||||
private fun offerParamsInScope(emit: (com.intellij.codeInsight.lookup.LookupElement) -> Unit, mini: MiniScript, text: String, caret: Int) {
|
||||
val src = mini.range.start.source
|
||||
// Find function whose body contains caret or whose whole range contains caret
|
||||
@@ -949,7 +569,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
// Lenient textual import extractor (duplicated from QuickDoc privately)
|
||||
private fun extractImportsFromText(text: String): List<String> {
|
||||
val result = LinkedHashSet<String>()
|
||||
val re = Regex("(?m)^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)")
|
||||
val re = Regex("^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)", RegexOption.MULTILINE)
|
||||
re.findAll(text).forEach { m ->
|
||||
val raw = m.groupValues.getOrNull(1)?.trim().orEmpty()
|
||||
if (raw.isNotEmpty()) {
|
||||
|
||||
+149
-69
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -69,59 +69,164 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
val ident = text.substring(idRange.startOffset, idRange.endOffset)
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: ident='$ident' at ${idRange.startOffset}..${idRange.endOffset} in ${file.name}")
|
||||
|
||||
// Build MiniAst for this file (fast and resilient). Best-effort; on failure continue with registry lookup only.
|
||||
// Build MiniAst for this file (fast and resilient). Best-effort; on failure continue with partial AST.
|
||||
val sink = MiniAstBuilder()
|
||||
// Use lenient import provider so unresolved imports (e.g., lyng.io.fs) don't break docs
|
||||
val provider = IdeLenientImportProvider.create()
|
||||
val src = Source("<ide>", text)
|
||||
var mini: MiniScript? = try {
|
||||
val mini = try {
|
||||
runBlocking { Compiler.compileWithMini(src, provider, sink) }
|
||||
sink.build()
|
||||
} catch (t: Throwable) {
|
||||
// Do not bail out completely: we still can resolve built-in and imported docs (e.g., println)
|
||||
if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: compileWithMini failed: ${t.message}")
|
||||
null
|
||||
}
|
||||
val haveMini = mini != null
|
||||
if (mini == null) {
|
||||
// Ensure we have a dummy script object to avoid NPE in downstream helpers that expect a MiniScript
|
||||
mini = MiniScript(MiniRange(Pos(src, 1, 1), Pos(src, 1, 1)))
|
||||
}
|
||||
if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: compileWithMini produced partial AST: ${t.message}")
|
||||
sink.build()
|
||||
} ?: MiniScript(MiniRange(Pos(src, 1, 1), Pos(src, 1, 1)))
|
||||
val source = src
|
||||
|
||||
// Try resolve to: function param at position, function/class/val declaration at position
|
||||
// 1) Check declarations whose name range contains offset
|
||||
if (haveMini) for (d in mini.declarations) {
|
||||
val s = source.offsetOf(d.nameStart)
|
||||
val e = (s + d.name.length).coerceAtMost(text.length)
|
||||
if (offset in s until e) {
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched decl '${d.name}' kind=${d::class.simpleName}")
|
||||
return renderDeclDoc(d)
|
||||
// 1) Use unified declaration detection
|
||||
DocLookupUtils.findDeclarationAt(mini, offset, ident)?.let { (name, kind) ->
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched declaration '$name' kind=$kind")
|
||||
// Find the actual declaration object to render
|
||||
for (d in mini.declarations) {
|
||||
if (d.name == name && source.offsetOf(d.nameStart) <= offset && source.offsetOf(d.nameStart) + d.name.length > offset) {
|
||||
return renderDeclDoc(d)
|
||||
}
|
||||
// Handle members if it was a member
|
||||
if (d is MiniClassDecl) {
|
||||
for (m in d.members) {
|
||||
if (m.name == name && source.offsetOf(m.nameStart) <= offset && source.offsetOf(m.nameStart) + m.name.length > offset) {
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
for (cf in d.ctorFields) {
|
||||
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(d.name, mv)
|
||||
}
|
||||
}
|
||||
for (cf in d.classFields) {
|
||||
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(d.name, mv)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (d is MiniEnumDecl) {
|
||||
if (d.entries.contains(name) && offset >= source.offsetOf(d.range.start) && offset <= source.offsetOf(d.range.end)) {
|
||||
// For enum constant, we don't have detailed docs in MiniAst yet, but we can render a title
|
||||
return "<div class='doc-title'>enum constant ${d.name}.${name}</div>"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 2) Check parameters of functions
|
||||
if (haveMini) for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fn.params) {
|
||||
val s = source.offsetOf(p.nameStart)
|
||||
val e = (s + p.name.length).coerceAtMost(text.length)
|
||||
if (offset in s until e) {
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched param '${p.name}' in fun '${fn.name}'")
|
||||
return renderParamDoc(fn, p)
|
||||
// Check parameters
|
||||
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fn.params) {
|
||||
if (p.name == name && source.offsetOf(p.nameStart) <= offset && source.offsetOf(p.nameStart) + p.name.length > offset) {
|
||||
return renderParamDoc(fn, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3) Member-context resolution first (dot immediately before identifier): handle literals and calls
|
||||
|
||||
// 3) usages in current file via Binder (resolves local variables, parameters, and classes)
|
||||
try {
|
||||
val binding = net.sergeych.lyng.binding.Binder.bind(text, mini)
|
||||
val ref = binding.references.firstOrNull { offset in it.start until it.end }
|
||||
if (ref != null) {
|
||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
||||
if (sym != null) {
|
||||
// Find local declaration that matches this symbol
|
||||
val ds = mini.declarations.firstOrNull { decl ->
|
||||
val s = source.offsetOf(decl.nameStart)
|
||||
decl.name == sym.name && s == sym.declStart
|
||||
}
|
||||
if (ds != null) return renderDeclDoc(ds)
|
||||
|
||||
// Check parameters
|
||||
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fn.params) {
|
||||
val s = source.offsetOf(p.nameStart)
|
||||
if (p.name == sym.name && s == sym.declStart) {
|
||||
return renderParamDoc(fn, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check class members (fields/functions)
|
||||
for (cls in mini.declarations.filterIsInstance<MiniClassDecl>()) {
|
||||
for (m in cls.members) {
|
||||
val s = source.offsetOf(m.nameStart)
|
||||
if (m.name == sym.name && s == sym.declStart) {
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
for (cf in cls.ctorFields) {
|
||||
val s = source.offsetOf(cf.nameStart)
|
||||
if (cf.name == sym.name && s == sym.declStart) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(cls.name, mv)
|
||||
}
|
||||
}
|
||||
for (cf in cls.classFields) {
|
||||
val s = source.offsetOf(cf.nameStart)
|
||||
if (cf.name == sym.name && s == sym.declStart) {
|
||||
// Render as a member val
|
||||
val mv = MiniMemberValDecl(
|
||||
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
|
||||
name = cf.name,
|
||||
mutable = cf.mutable,
|
||||
type = cf.type,
|
||||
doc = null,
|
||||
nameStart = cf.nameStart
|
||||
)
|
||||
return renderMemberValDoc(cls.name, mv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: local binder resolution failed: ${e.message}")
|
||||
}
|
||||
// 4) Member-context resolution first (dot immediately before identifier): handle literals and calls
|
||||
run {
|
||||
val dotPos = TextCtx.findDotLeft(text, idRange.startOffset)
|
||||
?: TextCtx.findDotLeft(text, offset)
|
||||
if (dotPos != null) {
|
||||
// Build imported modules (MiniAst-derived if available, else lenient from text) and ensure stdlib is present
|
||||
var importedModules = if (haveMini) DocLookupUtils.canonicalImportedModules(mini) else emptyList()
|
||||
if (importedModules.isEmpty()) {
|
||||
val fromText = extractImportsFromText(text)
|
||||
importedModules = if (fromText.isEmpty()) listOf("lyng.stdlib") else fromText
|
||||
}
|
||||
if (!importedModules.contains("lyng.stdlib")) importedModules = importedModules + "lyng.stdlib"
|
||||
val importedModules = DocLookupUtils.canonicalImportedModules(mini, text)
|
||||
|
||||
// Try literal and call-based receiver inference around the dot
|
||||
val i = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
@@ -192,21 +297,14 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
}
|
||||
|
||||
// 4) As a fallback, if the caret is on an identifier text that matches any declaration name, show that
|
||||
if (haveMini) mini.declarations.firstOrNull { it.name == ident }?.let {
|
||||
mini.declarations.firstOrNull { it.name == ident }?.let {
|
||||
log.info("[LYNG_DEBUG] QuickDoc: fallback by name '${it.name}' kind=${it::class.simpleName}")
|
||||
return renderDeclDoc(it)
|
||||
}
|
||||
|
||||
// 4) Consult BuiltinDocRegistry for imported modules (top-level and class members)
|
||||
// Canonicalize import names using ImportManager, as users may write shortened names (e.g., "io.fs")
|
||||
var importedModules = if (haveMini) DocLookupUtils.canonicalImportedModules(mini) else emptyList()
|
||||
// If MiniAst failed or captured no imports, try a lightweight textual import scan
|
||||
if (importedModules.isEmpty()) {
|
||||
val fromText = extractImportsFromText(text)
|
||||
if (fromText.isNotEmpty()) {
|
||||
importedModules = fromText
|
||||
}
|
||||
}
|
||||
var importedModules = DocLookupUtils.canonicalImportedModules(mini, text)
|
||||
// Always include stdlib as a fallback context
|
||||
if (!importedModules.contains("lyng.stdlib")) importedModules = importedModules + "lyng.stdlib"
|
||||
// 4a) try top-level decls
|
||||
@@ -225,8 +323,9 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
}
|
||||
// Also allow values/consts
|
||||
docs.filterIsInstance<MiniValDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
|
||||
// And classes
|
||||
// And classes/enums
|
||||
docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
|
||||
docs.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
|
||||
}
|
||||
// Defensive fallback: if nothing found and it's a well-known stdlib function, render minimal inline docs
|
||||
if (ident == "println" || ident == "print") {
|
||||
@@ -313,25 +412,6 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Very lenient import extractor for cases when MiniAst is unavailable.
|
||||
* Looks for lines like `import xxx.yyy` and returns canonical module names
|
||||
* (prefixing with `lyng.` if missing).
|
||||
*/
|
||||
private fun extractImportsFromText(text: String): List<String> {
|
||||
val result = LinkedHashSet<String>()
|
||||
val re = Regex("(?m)^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)")
|
||||
re.findAll(text).forEach { m ->
|
||||
val raw = m.groupValues.getOrNull(1)?.trim().orEmpty()
|
||||
if (raw.isNotEmpty()) {
|
||||
val canon = if (raw.startsWith("lyng.")) raw else "lyng.$raw"
|
||||
result.add(canon)
|
||||
}
|
||||
}
|
||||
return result.toList()
|
||||
}
|
||||
|
||||
// External docs registrars discovery via reflection to avoid hard dependencies on optional modules
|
||||
private val externalDocsLoaded: Boolean by lazy { tryLoadExternalDocs() }
|
||||
|
||||
private fun ensureExternalDocsRegistered() { @Suppress("UNUSED_EXPRESSION") externalDocsLoaded }
|
||||
@@ -373,15 +453,15 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
val title = when (d) {
|
||||
is MiniFunDecl -> "function ${d.name}${signatureOf(d)}"
|
||||
is MiniClassDecl -> "class ${d.name}"
|
||||
is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }"
|
||||
is MiniValDecl -> if (d.mutable) "var ${d.name}${typeOf(d.type)}" else "val ${d.name}${typeOf(d.type)}"
|
||||
else -> d.name
|
||||
}
|
||||
// Show full detailed documentation, not just the summary
|
||||
val raw = d.doc?.raw
|
||||
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
|
||||
val sb = StringBuilder()
|
||||
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc!!))
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
@@ -402,7 +482,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
|
||||
val sb = StringBuilder()
|
||||
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc!!))
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
@@ -415,7 +495,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
val doc: String? = if (raw.isNullOrBlank()) null else MarkdownRenderer.render(raw)
|
||||
val sb = StringBuilder()
|
||||
sb.append("<div class='doc-title'>").append(htmlEscape(title)).append("</div>")
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc!!))
|
||||
if (!doc.isNullOrBlank()) sb.append(styledMarkdown(doc))
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,6 +35,8 @@ import com.intellij.psi.codeStyle.CodeStyleManager
|
||||
import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.computeDesiredIndent
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.findFirstNonWs
|
||||
|
||||
class LyngEnterHandler : EnterHandlerDelegate {
|
||||
private val log = Logger.getInstance(LyngEnterHandler::class.java)
|
||||
@@ -80,16 +82,39 @@ class LyngEnterHandler : EnterHandlerDelegate {
|
||||
val trimmed = prevText.trimStart()
|
||||
// consider only code part before // comment
|
||||
val code = trimmed.substringBefore("//").trim()
|
||||
if (code == "}") {
|
||||
// Previously we reindented the enclosed block on Enter after a lone '}'.
|
||||
// Per new behavior, this action is now bound to typing '}' instead.
|
||||
// Keep Enter flow limited to indenting the new line only.
|
||||
if (code == "}" || code == "*/") {
|
||||
// Adjust indent for the previous line if it's a block or comment closer
|
||||
val prevStart = doc.getLineStartOffset(prevLine)
|
||||
if (file.context == null) {
|
||||
try {
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, prevStart)
|
||||
} catch (e: Exception) {
|
||||
log.warn("Failed to adjust line indent for previous line: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for previous line: manual application
|
||||
val desiredPrev = computeDesiredIndent(project, doc, prevLine)
|
||||
val lineStartPrev = doc.getLineStartOffset(prevLine)
|
||||
val lineEndPrev = doc.getLineEndOffset(prevLine)
|
||||
val firstNonWsPrev = findFirstNonWs(doc, lineStartPrev, lineEndPrev)
|
||||
val currentIndentLenPrev = firstNonWsPrev - lineStartPrev
|
||||
if (doc.getText(TextRange(lineStartPrev, lineStartPrev + currentIndentLenPrev)) != desiredPrev) {
|
||||
WriteCommandAction.runWriteCommandAction(project) {
|
||||
doc.replaceString(lineStartPrev, lineStartPrev + currentIndentLenPrev, desiredPrev)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Adjust indent for the current (new) line
|
||||
val currentStart = doc.getLineStartOffsetSafe(currentLine)
|
||||
val csm = CodeStyleManager.getInstance(project)
|
||||
csm.adjustLineIndent(file, currentStart)
|
||||
if (file.context == null) {
|
||||
try {
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, currentStart)
|
||||
} catch (e: Exception) {
|
||||
log.warn("Failed to adjust line indent for current line: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: if the platform didn't physically insert indentation, compute it from our formatter and apply
|
||||
val lineStart = doc.getLineStartOffset(currentLine)
|
||||
@@ -159,35 +184,6 @@ class LyngEnterHandler : EnterHandlerDelegate {
|
||||
caret.moveToOffset(target)
|
||||
}
|
||||
|
||||
private fun computeDesiredIndent(project: Project, doc: Document, line: Int): String {
|
||||
val options = CodeStyle.getIndentOptions(project, doc)
|
||||
val start = 0
|
||||
val end = doc.getLineEndOffset(line)
|
||||
val snippet = doc.getText(TextRange(start, end))
|
||||
val isBlankLine = doc.getLineText(line).trim().isEmpty()
|
||||
val snippetForCalc = if (isBlankLine) snippet + "x" else snippet
|
||||
val cfg = LyngFormatConfig(
|
||||
indentSize = options.INDENT_SIZE.coerceAtLeast(1),
|
||||
useTabs = options.USE_TAB_CHARACTER,
|
||||
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
|
||||
)
|
||||
val formatted = LyngFormatter.reindent(snippetForCalc, cfg)
|
||||
val lastNl = formatted.lastIndexOf('\n')
|
||||
val lastLine = if (lastNl >= 0) formatted.substring(lastNl + 1) else formatted
|
||||
val wsLen = lastLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) lastLine.length else it }
|
||||
return lastLine.substring(0, wsLen)
|
||||
}
|
||||
|
||||
private fun findFirstNonWs(doc: Document, start: Int, end: Int): Int {
|
||||
var i = start
|
||||
val text = doc.charsSequence
|
||||
while (i < end) {
|
||||
val ch = text[i]
|
||||
if (ch != ' ' && ch != '\t') break
|
||||
i++
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
private fun Document.safeLineNumber(offset: Int): Int =
|
||||
getLineNumber(offset.coerceIn(0, textLength))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,31 +32,66 @@ import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.computeDesiredIndent
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils.findFirstNonWs
|
||||
|
||||
class LyngTypedHandler : TypedHandlerDelegate() {
|
||||
private val log = Logger.getInstance(LyngTypedHandler::class.java)
|
||||
|
||||
override fun charTyped(c: Char, project: Project, editor: Editor, file: PsiFile): Result {
|
||||
if (file.language != LyngLanguage) return Result.CONTINUE
|
||||
if (c != '}') return Result.CONTINUE
|
||||
|
||||
if (c == '}') {
|
||||
val doc = editor.document
|
||||
PsiDocumentManager.getInstance(project).commitDocument(doc)
|
||||
|
||||
val doc = editor.document
|
||||
PsiDocumentManager.getInstance(project).commitDocument(doc)
|
||||
val offset = editor.caretModel.offset
|
||||
val line = doc.getLineNumber((offset - 1).coerceAtLeast(0))
|
||||
if (line < 0) return Result.CONTINUE
|
||||
|
||||
val offset = editor.caretModel.offset
|
||||
val line = doc.getLineNumber((offset - 1).coerceAtLeast(0))
|
||||
if (line < 0) return Result.CONTINUE
|
||||
|
||||
val rawLine = doc.getLineText(line)
|
||||
val code = rawLine.substringBefore("//").trim()
|
||||
if (code == "}") {
|
||||
val settings = LyngFormatterSettings.getInstance(project)
|
||||
if (settings.reindentClosedBlockOnEnter) {
|
||||
reindentClosedBlockAroundBrace(project, file, doc, line)
|
||||
val rawLine = doc.getLineText(line)
|
||||
val code = rawLine.substringBefore("//").trim()
|
||||
if (code == "}") {
|
||||
val settings = LyngFormatterSettings.getInstance(project)
|
||||
if (settings.reindentClosedBlockOnEnter) {
|
||||
reindentClosedBlockAroundBrace(project, file, doc, line)
|
||||
}
|
||||
// After block reindent, adjust line indent to what platform thinks (no-op in many cases)
|
||||
val lineStart = doc.getLineStartOffset(line)
|
||||
if (file.context == null) {
|
||||
try {
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
} catch (e: Exception) {
|
||||
log.warn("Failed to adjust line indent for current line: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (c == '/') {
|
||||
val doc = editor.document
|
||||
val offset = editor.caretModel.offset
|
||||
if (offset >= 2 && doc.getText(TextRange(offset - 2, offset)) == "*/") {
|
||||
PsiDocumentManager.getInstance(project).commitDocument(doc)
|
||||
val line = doc.getLineNumber(offset - 1)
|
||||
val lineStart = doc.getLineStartOffset(line)
|
||||
if (file.context == null) {
|
||||
try {
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
} catch (e: Exception) {
|
||||
log.warn("Failed to adjust line indent for comment: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
// Manual application fallback
|
||||
val desired = computeDesiredIndent(project, doc, line)
|
||||
val lineEnd = doc.getLineEndOffset(line)
|
||||
val firstNonWs = findFirstNonWs(doc, lineStart, lineEnd)
|
||||
val currentIndentLen = firstNonWs - lineStart
|
||||
if (doc.getText(TextRange(lineStart, lineStart + currentIndentLen)) != desired) {
|
||||
WriteCommandAction.runWriteCommandAction(project) {
|
||||
doc.replaceString(lineStart, lineStart + currentIndentLen, desired)
|
||||
}
|
||||
}
|
||||
}
|
||||
// After block reindent, adjust line indent to what platform thinks (no-op in many cases)
|
||||
val lineStart = doc.getLineStartOffset(line)
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
}
|
||||
return Result.CONTINUE
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,9 +24,8 @@ import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions
|
||||
import com.intellij.psi.codeStyle.lineIndent.LineIndentProvider
|
||||
import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.util.FormattingUtils
|
||||
|
||||
/**
|
||||
* Lightweight indentation provider for Lyng.
|
||||
@@ -45,8 +44,7 @@ class LyngLineIndentProvider : LineIndentProvider {
|
||||
val options = CodeStyle.getIndentOptions(project, doc)
|
||||
|
||||
val line = doc.getLineNumberSafe(offset)
|
||||
val indent = computeDesiredIndentFromCore(doc, line, options)
|
||||
return indent
|
||||
return FormattingUtils.computeDesiredIndent(project, doc, line)
|
||||
}
|
||||
|
||||
override fun isSuitableFor(language: Language?): Boolean = language == null || language == LyngLanguage
|
||||
@@ -79,25 +77,4 @@ class LyngLineIndentProvider : LineIndentProvider {
|
||||
return spaces / size
|
||||
}
|
||||
|
||||
private fun computeDesiredIndentFromCore(doc: Document, line: Int, options: IndentOptions): String {
|
||||
// Build a minimal text consisting of all previous lines and the current line.
|
||||
// Special case: when the current line is blank (newly created by Enter), compute the
|
||||
// indent as if there was a non-whitespace character at line start (append a sentinel).
|
||||
val start = 0
|
||||
val end = doc.getLineEndOffset(line)
|
||||
val snippet = doc.getText(TextRange(start, end))
|
||||
val isBlankLine = doc.getLineText(line).trim().isEmpty()
|
||||
val snippetForCalc = if (isBlankLine) snippet + "x" else snippet
|
||||
val cfg = LyngFormatConfig(
|
||||
indentSize = options.INDENT_SIZE.coerceAtLeast(1),
|
||||
useTabs = options.USE_TAB_CHARACTER,
|
||||
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
|
||||
)
|
||||
val formatted = LyngFormatter.reindent(snippetForCalc, cfg)
|
||||
// Grab the last line's leading whitespace as the indent for the current line
|
||||
val lastNl = formatted.lastIndexOf('\n')
|
||||
val lastLine = if (lastNl >= 0) formatted.substring(lastNl + 1) else formatted
|
||||
val wsLen = lastLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) lastLine.length else it }
|
||||
return lastLine.substring(0, wsLen)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -80,7 +80,14 @@ class LyngPreFormatProcessor : PreFormatProcessor {
|
||||
val lineStart = doc.getLineStartOffset(line)
|
||||
// adjustLineIndent delegates to our LineIndentProvider which computes
|
||||
// indentation from scratch; this is safe and idempotent
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
if (file.context == null) {
|
||||
try {
|
||||
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
|
||||
} catch (e: Exception) {
|
||||
// Log as debug because this can be called many times during reformat
|
||||
// and we don't want to spam warnings if it's a known platform issue with injections
|
||||
}
|
||||
}
|
||||
|
||||
// After indentation, update block/paren/bracket balances using the current line text
|
||||
val lineEnd = doc.getLineEndOffset(line)
|
||||
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.navigation
|
||||
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.navigation.ItemPresentation
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiNameIdentifierOwner
|
||||
import com.intellij.psi.impl.light.LightElement
|
||||
import com.intellij.util.IncorrectOperationException
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import javax.swing.Icon
|
||||
|
||||
/**
|
||||
* A light PSI element representing a Lyng declaration (function, class, enum, or variable).
|
||||
* Used for navigation and to provide a stable anchor for "Find Usages".
|
||||
*/
|
||||
class LyngDeclarationElement(
|
||||
private val nameElement: PsiElement,
|
||||
private val name: String,
|
||||
val kind: String = "declaration"
|
||||
) : LightElement(nameElement.manager, LyngLanguage), PsiNameIdentifierOwner {
|
||||
|
||||
override fun getName(): String = name
|
||||
|
||||
override fun setName(name: String): PsiElement {
|
||||
throw IncorrectOperationException("Renaming is not yet supported")
|
||||
}
|
||||
|
||||
override fun getNameIdentifier(): PsiElement = nameElement
|
||||
|
||||
override fun getNavigationElement(): PsiElement = nameElement
|
||||
|
||||
override fun getTextRange(): TextRange = nameElement.textRange
|
||||
|
||||
override fun getContainingFile(): PsiFile = nameElement.containingFile
|
||||
|
||||
override fun isValid(): Boolean = nameElement.isValid
|
||||
|
||||
override fun getPresentation(): ItemPresentation {
|
||||
return object : ItemPresentation {
|
||||
override fun getPresentableText(): String = name
|
||||
override fun getLocationString(): String {
|
||||
val file = containingFile
|
||||
val document = PsiDocumentManager.getInstance(file.project).getDocument(file)
|
||||
val line = if (document != null) document.getLineNumber(textRange.startOffset) + 1 else "?"
|
||||
val column = if (document != null) {
|
||||
val lineStart = document.getLineStartOffset(document.getLineNumber(textRange.startOffset))
|
||||
textRange.startOffset - lineStart + 1
|
||||
} else "?"
|
||||
return "${file.name}:$line:$column"
|
||||
}
|
||||
override fun getIcon(unused: Boolean): Icon {
|
||||
return when (kind) {
|
||||
"Function" -> AllIcons.Nodes.Function
|
||||
"Class" -> AllIcons.Nodes.Class
|
||||
"Enum" -> AllIcons.Nodes.Enum
|
||||
"EnumConstant" -> AllIcons.Nodes.Enum
|
||||
"Variable" -> AllIcons.Nodes.Variable
|
||||
"Value" -> AllIcons.Nodes.Field
|
||||
"Parameter" -> AllIcons.Nodes.Parameter
|
||||
"Initializer" -> AllIcons.Nodes.Method
|
||||
else -> AllIcons.Nodes.Property
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String = "$kind:$name"
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is LyngDeclarationElement) return false
|
||||
return name == other.name && nameElement == other.nameElement
|
||||
}
|
||||
|
||||
override fun isEquivalentTo(another: PsiElement?): Boolean {
|
||||
if (this === another) return true
|
||||
if (another == nameElement) return true
|
||||
if (another is LyngDeclarationElement) {
|
||||
return name == another.name && nameElement == another.nameElement
|
||||
}
|
||||
return super.isEquivalentTo(another)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = nameElement.hashCode()
|
||||
result = 31 * result + name.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.navigation
|
||||
|
||||
import com.intellij.lang.cacheBuilder.DefaultWordsScanner
|
||||
import com.intellij.lang.cacheBuilder.WordsScanner
|
||||
import com.intellij.lang.findUsages.FindUsagesProvider
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import net.sergeych.lyng.idea.highlight.LyngLexer
|
||||
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
|
||||
class LyngFindUsagesProvider : FindUsagesProvider {
|
||||
override fun getWordsScanner(): WordsScanner {
|
||||
return DefaultWordsScanner(
|
||||
LyngLexer(),
|
||||
TokenSet.create(LyngTokenTypes.IDENTIFIER),
|
||||
TokenSet.create(LyngTokenTypes.LINE_COMMENT, LyngTokenTypes.BLOCK_COMMENT),
|
||||
TokenSet.create(LyngTokenTypes.STRING)
|
||||
)
|
||||
}
|
||||
|
||||
override fun canFindUsagesFor(psiElement: PsiElement): Boolean {
|
||||
return psiElement is LyngDeclarationElement || isDeclaration(psiElement)
|
||||
}
|
||||
|
||||
private fun isDeclaration(element: PsiElement): Boolean {
|
||||
val file = element.containingFile ?: return false
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return false
|
||||
val offset = element.textRange.startOffset
|
||||
val name = element.text ?: ""
|
||||
return DocLookupUtils.findDeclarationAt(mini, offset, name) != null
|
||||
}
|
||||
|
||||
override fun getHelpId(psiElement: PsiElement): String? = null
|
||||
|
||||
override fun getType(element: PsiElement): String {
|
||||
if (element is LyngDeclarationElement) return element.kind
|
||||
val file = element.containingFile ?: return "Lyng declaration"
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return "Lyng declaration"
|
||||
val info = DocLookupUtils.findDeclarationAt(mini, element.textRange.startOffset, element.text ?: "")
|
||||
return info?.second ?: "Lyng declaration"
|
||||
}
|
||||
|
||||
override fun getDescriptiveName(element: PsiElement): String {
|
||||
if (element is LyngDeclarationElement) {
|
||||
val file = element.containingFile
|
||||
val document = PsiDocumentManager.getInstance(file.project).getDocument(file)
|
||||
val line = if (document != null) document.getLineNumber(element.textRange.startOffset) + 1 else "?"
|
||||
val column = if (document != null) {
|
||||
val lineStart = document.getLineStartOffset(document.getLineNumber(element.textRange.startOffset))
|
||||
element.textRange.startOffset - lineStart + 1
|
||||
} else "?"
|
||||
return "${element.name} (${file.name}:$line:$column)"
|
||||
}
|
||||
val file = element.containingFile ?: return element.text ?: "unknown"
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return element.text ?: "unknown"
|
||||
val info = DocLookupUtils.findDeclarationAt(mini, element.textRange.startOffset, element.text ?: "")
|
||||
|
||||
val document = PsiDocumentManager.getInstance(file.project).getDocument(file)
|
||||
val line = if (document != null) document.getLineNumber(element.textRange.startOffset) + 1 else "?"
|
||||
val column = if (document != null) {
|
||||
val lineStart = document.getLineStartOffset(document.getLineNumber(element.textRange.startOffset))
|
||||
element.textRange.startOffset - lineStart + 1
|
||||
} else "?"
|
||||
|
||||
val name = info?.first ?: element.text ?: "unknown"
|
||||
return "$name (${file.name}:$line:$column)"
|
||||
}
|
||||
|
||||
override fun getNodeText(element: PsiElement, useFullName: Boolean): String {
|
||||
return (element as? LyngDeclarationElement)?.name ?: element.text ?: "unknown"
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.navigation
|
||||
|
||||
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiElement
|
||||
|
||||
/**
|
||||
* Ensures Ctrl+B (Go to Definition) works on Lyng identifiers by resolving through LyngPsiReference.
|
||||
*/
|
||||
class LyngGotoDeclarationHandler : GotoDeclarationHandler {
|
||||
override fun getGotoDeclarationTargets(sourceElement: PsiElement?, offset: Int, editor: Editor?): Array<PsiElement>? {
|
||||
if (sourceElement == null) return null
|
||||
|
||||
val allTargets = mutableListOf<PsiElement>()
|
||||
|
||||
// Find reference at the element or its parent (sometimes the identifier token is wrapped)
|
||||
val ref = sourceElement.reference ?: sourceElement.parent?.reference
|
||||
if (ref is LyngPsiReference) {
|
||||
val resolved = ref.multiResolve(false)
|
||||
allTargets.addAll(resolved.mapNotNull { it.element })
|
||||
} else {
|
||||
// Manual check if not picked up by reference (e.g. if contributor didn't run yet)
|
||||
val manualRef = LyngPsiReference(sourceElement)
|
||||
val manualResolved = manualRef.multiResolve(false)
|
||||
allTargets.addAll(manualResolved.mapNotNull { it.element })
|
||||
}
|
||||
|
||||
if (allTargets.isEmpty()) return null
|
||||
|
||||
// If there is only one target and it's equivalent to the source, return null.
|
||||
// This allows IDEA to treat it as a declaration site and trigger "Show Usages".
|
||||
if (allTargets.size == 1) {
|
||||
val target = allTargets[0]
|
||||
if (target == sourceElement || target.isEquivalentTo(sourceElement)) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
return allTargets.toTypedArray()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.navigation
|
||||
|
||||
import com.intellij.icons.AllIcons
|
||||
import com.intellij.ide.IconProvider
|
||||
import com.intellij.psi.PsiElement
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
import javax.swing.Icon
|
||||
|
||||
class LyngIconProvider : IconProvider() {
|
||||
override fun getIcon(element: PsiElement, flags: Int): Icon? {
|
||||
val file = element.containingFile ?: return null
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return null
|
||||
|
||||
val info = DocLookupUtils.findDeclarationAt(mini, element.textRange.startOffset, element.text ?: "")
|
||||
if (info != null) {
|
||||
return when (info.second) {
|
||||
"Function" -> AllIcons.Nodes.Function
|
||||
"Class" -> AllIcons.Nodes.Class
|
||||
"Enum" -> AllIcons.Nodes.Enum
|
||||
"EnumConstant" -> AllIcons.Nodes.Enum
|
||||
"Variable" -> AllIcons.Nodes.Variable
|
||||
"Value" -> AllIcons.Nodes.Field
|
||||
"Parameter" -> AllIcons.Nodes.Parameter
|
||||
"Initializer" -> AllIcons.Nodes.Method
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.navigation
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.search.FilenameIndex
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.idea.util.TextCtx
|
||||
import net.sergeych.lyng.miniast.*
|
||||
|
||||
class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength)) {
|
||||
|
||||
override fun multiResolve(incompleteCode: Boolean): Array<ResolveResult> {
|
||||
val file = element.containingFile
|
||||
val text = file.text
|
||||
val offset = element.textRange.startOffset
|
||||
val name = element.text ?: ""
|
||||
val results = mutableListOf<ResolveResult>()
|
||||
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: return emptyArray()
|
||||
val binding = LyngAstManager.getBinding(file)
|
||||
|
||||
// 1. Member resolution (obj.member)
|
||||
val dotPos = TextCtx.findDotLeft(text, offset)
|
||||
if (dotPos != null) {
|
||||
val imported = DocLookupUtils.canonicalImportedModules(mini, text)
|
||||
val receiverClass = DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, imported, binding)
|
||||
?: DocLookupUtils.guessReceiverClass(text, dotPos, imported, mini)
|
||||
|
||||
if (receiverClass != null) {
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, receiverClass, name, mini)
|
||||
if (resolved != null) {
|
||||
val owner = resolved.first
|
||||
val member = resolved.second
|
||||
|
||||
// We need to find the actual PSI element for this member
|
||||
val targetFile = findFileForClass(file.project, owner) ?: file
|
||||
val targetMini = LyngAstManager.getMiniAst(targetFile)
|
||||
if (targetMini != null) {
|
||||
val targetSrc = targetMini.range.start.source
|
||||
val off = targetSrc.offsetOf(member.nameStart)
|
||||
targetFile.findElementAt(off)?.let {
|
||||
val kind = when(member) {
|
||||
is MiniMemberFunDecl -> "Function"
|
||||
is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value"
|
||||
is MiniInitDecl -> "Initializer"
|
||||
}
|
||||
results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we couldn't resolve exactly, we might still want to search globally but ONLY for members
|
||||
if (results.isEmpty()) {
|
||||
results.addAll(resolveGlobally(file.project, name, membersOnly = true))
|
||||
}
|
||||
} else {
|
||||
// 2. Local resolution via Binder
|
||||
if (binding != null) {
|
||||
val ref = binding.references.firstOrNull { offset >= it.start && offset < it.end }
|
||||
if (ref != null) {
|
||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
||||
if (sym != null && sym.declStart >= 0) {
|
||||
file.findElementAt(sym.declStart)?.let {
|
||||
results.add(PsiElementResolveResult(LyngDeclarationElement(it, sym.name, sym.kind.name)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Global project scan
|
||||
// Only search globally if we haven't found a strong local match
|
||||
if (results.isEmpty()) {
|
||||
results.addAll(resolveGlobally(file.project, name))
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Filter results to exclude duplicates
|
||||
// Use a more robust de-duplication that prefers the raw element if multiple refer to the same thing
|
||||
val filtered = mutableListOf<ResolveResult>()
|
||||
for (res in results) {
|
||||
val el = res.element ?: continue
|
||||
val nav = if (el is LyngDeclarationElement) el.navigationElement else el
|
||||
if (filtered.none { existing ->
|
||||
val exEl = existing.element
|
||||
val exNav = if (exEl is LyngDeclarationElement) exEl.navigationElement else exEl
|
||||
exNav == nav || (exNav != null && exNav.isEquivalentTo(nav))
|
||||
}) {
|
||||
filtered.add(res)
|
||||
}
|
||||
}
|
||||
|
||||
return filtered.toTypedArray()
|
||||
}
|
||||
|
||||
private fun findFileForClass(project: Project, className: String): PsiFile? {
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
|
||||
// 1. Try file with matching name first (optimization)
|
||||
val matchingFiles = FilenameIndex.getFilesByName(project, "$className.lyng", GlobalSearchScope.projectScope(project))
|
||||
for (file in matchingFiles) {
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to full project scan
|
||||
val allFiles = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
||||
for (vFile in allFiles) {
|
||||
val file = psiManager.findFile(vFile) ?: continue
|
||||
if (matchingFiles.contains(file)) continue // already checked
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
override fun resolve(): PsiElement? {
|
||||
val results = multiResolve(false)
|
||||
if (results.isEmpty()) return null
|
||||
val target = results[0].element ?: return null
|
||||
// If the target is equivalent to our source element, return the source element itself.
|
||||
// This is crucial for IDEA to recognize we are already at the declaration site
|
||||
// and trigger "Show Usages" instead of performing a no-op navigation.
|
||||
if (target == element || target.isEquivalentTo(element)) {
|
||||
return element
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
private fun resolveGlobally(project: Project, name: String, membersOnly: Boolean = false): List<ResolveResult> {
|
||||
val results = mutableListOf<ResolveResult>()
|
||||
val files = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
||||
val psiManager = PsiManager.getInstance(project)
|
||||
|
||||
for (vFile in files) {
|
||||
val file = psiManager.findFile(vFile) ?: continue
|
||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
||||
val src = mini.range.start.source
|
||||
|
||||
fun addIfMatch(dName: String, nameStart: net.sergeych.lyng.Pos, dKind: String) {
|
||||
if (dName == name) {
|
||||
val off = src.offsetOf(nameStart)
|
||||
file.findElementAt(off)?.let {
|
||||
results.add(PsiElementResolveResult(LyngDeclarationElement(it, dName, dKind)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (d in mini.declarations) {
|
||||
if (!membersOnly) {
|
||||
val dKind = when(d) {
|
||||
is net.sergeych.lyng.miniast.MiniFunDecl -> "Function"
|
||||
is net.sergeych.lyng.miniast.MiniClassDecl -> "Class"
|
||||
is net.sergeych.lyng.miniast.MiniEnumDecl -> "Enum"
|
||||
is net.sergeych.lyng.miniast.MiniValDecl -> if (d.mutable) "Variable" else "Value"
|
||||
}
|
||||
addIfMatch(d.name, d.nameStart, dKind)
|
||||
}
|
||||
|
||||
// Check members of classes and enums
|
||||
val members = when(d) {
|
||||
is MiniClassDecl -> d.members
|
||||
is MiniEnumDecl -> DocLookupUtils.enumToSyntheticClass(d).members
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
for (m in members) {
|
||||
val mKind = when(m) {
|
||||
is net.sergeych.lyng.miniast.MiniMemberFunDecl -> "Function"
|
||||
is net.sergeych.lyng.miniast.MiniMemberValDecl -> if (m.mutable) "Variable" else "Value"
|
||||
is net.sergeych.lyng.miniast.MiniInitDecl -> "Initializer"
|
||||
}
|
||||
addIfMatch(m.name, m.nameStart, mKind)
|
||||
}
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
override fun getVariants(): Array<Any> = emptyArray()
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.navigation
|
||||
|
||||
import com.intellij.patterns.PlatformPatterns
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.util.ProcessingContext
|
||||
import net.sergeych.lyng.idea.LyngLanguage
|
||||
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
|
||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||
|
||||
class LyngPsiReferenceContributor : PsiReferenceContributor() {
|
||||
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
|
||||
registrar.registerReferenceProvider(
|
||||
PlatformPatterns.psiElement().withLanguage(LyngLanguage),
|
||||
object : PsiReferenceProvider() {
|
||||
override fun getReferencesByElement(
|
||||
element: PsiElement,
|
||||
context: ProcessingContext
|
||||
): Array<PsiReference> {
|
||||
if (element.node.elementType == LyngTokenTypes.IDENTIFIER) {
|
||||
val file = element.containingFile
|
||||
val mini = LyngAstManager.getMiniAst(file)
|
||||
if (mini != null) {
|
||||
val offset = element.textRange.startOffset
|
||||
val name = element.text ?: ""
|
||||
if (DocLookupUtils.findDeclarationAt(mini, offset, name) != null) {
|
||||
return PsiReference.EMPTY_ARRAY
|
||||
}
|
||||
}
|
||||
return arrayOf(LyngPsiReference(element))
|
||||
}
|
||||
return PsiReference.EMPTY_ARRAY
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.util
|
||||
|
||||
import com.intellij.application.options.CodeStyle
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import net.sergeych.lyng.format.LyngFormatConfig
|
||||
import net.sergeych.lyng.format.LyngFormatter
|
||||
|
||||
object FormattingUtils {
|
||||
fun computeDesiredIndent(project: Project, doc: Document, line: Int): String {
|
||||
val options = CodeStyle.getIndentOptions(project, doc)
|
||||
val start = 0
|
||||
val end = doc.getLineEndOffset(line)
|
||||
val snippet = doc.getText(TextRange(start, end))
|
||||
val lineText = if (line < doc.lineCount) {
|
||||
val ls = doc.getLineStartOffset(line)
|
||||
val le = doc.getLineEndOffset(line)
|
||||
doc.getText(TextRange(ls, le))
|
||||
} else ""
|
||||
val isBlankLine = lineText.trim().isEmpty()
|
||||
val snippetForCalc = if (isBlankLine) snippet + "x" else snippet
|
||||
val cfg = LyngFormatConfig(
|
||||
indentSize = options.INDENT_SIZE.coerceAtLeast(1),
|
||||
useTabs = options.USE_TAB_CHARACTER,
|
||||
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
|
||||
)
|
||||
val formatted = LyngFormatter.reindent(snippetForCalc, cfg)
|
||||
val lastNl = formatted.lastIndexOf('\n')
|
||||
val lastLine = if (lastNl >= 0) formatted.substring(lastNl + 1) else formatted
|
||||
val wsLen = lastLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) lastLine.length else it }
|
||||
return lastLine.substring(0, wsLen)
|
||||
}
|
||||
|
||||
fun findFirstNonWs(doc: Document, start: Int, end: Int): Int {
|
||||
var i = start
|
||||
val text = doc.charsSequence
|
||||
while (i < end) {
|
||||
val ch = text[i]
|
||||
if (ch != ' ' && ch != '\t') break
|
||||
i++
|
||||
}
|
||||
return i
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.idea.util
|
||||
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.psi.PsiFile
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import net.sergeych.lyng.Compiler
|
||||
import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.binding.Binder
|
||||
import net.sergeych.lyng.binding.BindingSnapshot
|
||||
import net.sergeych.lyng.miniast.MiniAstBuilder
|
||||
import net.sergeych.lyng.miniast.MiniScript
|
||||
|
||||
object LyngAstManager {
|
||||
private val MINI_KEY = Key.create<MiniScript>("lyng.mini.cache")
|
||||
private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache")
|
||||
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
|
||||
|
||||
fun getMiniAst(file: PsiFile): MiniScript? {
|
||||
val doc = file.viewProvider.document ?: return null
|
||||
val stamp = doc.modificationStamp
|
||||
val prevStamp = file.getUserData(STAMP_KEY)
|
||||
val cached = file.getUserData(MINI_KEY)
|
||||
if (cached != null && prevStamp != null && prevStamp == stamp) return cached
|
||||
|
||||
val text = doc.text
|
||||
val sink = MiniAstBuilder()
|
||||
val built = try {
|
||||
val provider = IdeLenientImportProvider.create()
|
||||
val src = Source(file.name, text)
|
||||
runBlocking { Compiler.compileWithMini(src, provider, sink) }
|
||||
sink.build()
|
||||
} catch (_: Throwable) {
|
||||
sink.build()
|
||||
}
|
||||
|
||||
if (built != null) {
|
||||
file.putUserData(MINI_KEY, built)
|
||||
file.putUserData(STAMP_KEY, stamp)
|
||||
// Invalidate binding too
|
||||
file.putUserData(BINDING_KEY, null)
|
||||
}
|
||||
return built
|
||||
}
|
||||
|
||||
fun getBinding(file: PsiFile): BindingSnapshot? {
|
||||
val doc = file.viewProvider.document ?: return null
|
||||
val stamp = doc.modificationStamp
|
||||
val prevStamp = file.getUserData(STAMP_KEY)
|
||||
val cached = file.getUserData(BINDING_KEY)
|
||||
|
||||
if (cached != null && prevStamp != null && prevStamp == stamp) return cached
|
||||
|
||||
val mini = getMiniAst(file) ?: return null
|
||||
val text = doc.text
|
||||
val binding = try {
|
||||
Binder.bind(text, mini)
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
|
||||
if (binding != null) {
|
||||
file.putUserData(BINDING_KEY, binding)
|
||||
// stamp is already set by getMiniAst
|
||||
}
|
||||
return binding
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<!--
|
||||
~ Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
~ Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
@@ -96,6 +96,12 @@
|
||||
<!-- If targeting SDKs with stable RawText API, the EP below can be enabled instead: -->
|
||||
<!-- <copyPastePreProcessor implementation="net.sergeych.lyng.idea.editor.LyngCopyPastePreProcessor"/> -->
|
||||
|
||||
<!-- Navigation and Find Usages -->
|
||||
<psi.referenceContributor language="Lyng" implementation="net.sergeych.lyng.idea.navigation.LyngPsiReferenceContributor"/>
|
||||
<gotoDeclarationHandler implementation="net.sergeych.lyng.idea.navigation.LyngGotoDeclarationHandler"/>
|
||||
<lang.findUsagesProvider language="Lyng" implementationClass="net.sergeych.lyng.idea.navigation.LyngFindUsagesProvider"/>
|
||||
<iconProvider implementation="net.sergeych.lyng.idea.navigation.LyngIconProvider"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
<actions/>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
group = "net.sergeych"
|
||||
version = "1.1.0-beta2"
|
||||
version = "1.1.0-rc"
|
||||
|
||||
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
||||
|
||||
|
||||
@@ -20,5 +20,7 @@ package net.sergeych.lyng
|
||||
sealed class CodeContext {
|
||||
class Module(@Suppress("unused") val packageName: String?): CodeContext()
|
||||
class Function(val name: String): CodeContext()
|
||||
class ClassBody(val name: String): CodeContext()
|
||||
class ClassBody(val name: String): CodeContext() {
|
||||
val pendingInitializations = mutableMapOf<String, Pos>()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
package net.sergeych.lyng
|
||||
|
||||
import ObjEnumClass
|
||||
import net.sergeych.lyng.Compiler.Companion.compile
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
@@ -139,9 +138,16 @@ class Compiler(
|
||||
private var lastParsedBlockRange: MiniRange? = null
|
||||
|
||||
private suspend fun <T> inCodeContext(context: CodeContext, f: suspend () -> T): T {
|
||||
return try {
|
||||
codeContexts.add(context)
|
||||
f()
|
||||
codeContexts.add(context)
|
||||
try {
|
||||
val res = f()
|
||||
if (context is CodeContext.ClassBody) {
|
||||
if (context.pendingInitializations.isNotEmpty()) {
|
||||
val (name, pos) = context.pendingInitializations.entries.first()
|
||||
throw ScriptError(pos, "val '$name' must be initialized in the class body or init block")
|
||||
}
|
||||
}
|
||||
return res
|
||||
} finally {
|
||||
codeContexts.removeLast()
|
||||
}
|
||||
@@ -361,7 +367,21 @@ class Compiler(
|
||||
val rvalue = parseExpressionLevel(level + 1)
|
||||
?: throw ScriptError(opToken.pos, "Expecting expression")
|
||||
|
||||
lvalue = op.generate(opToken.pos, lvalue!!, rvalue)
|
||||
val res = op.generate(opToken.pos, lvalue!!, rvalue)
|
||||
if (opToken.type == Token.Type.ASSIGN) {
|
||||
val ctx = codeContexts.lastOrNull()
|
||||
if (ctx is CodeContext.ClassBody) {
|
||||
val target = lvalue
|
||||
val name = when (target) {
|
||||
is LocalVarRef -> target.name
|
||||
is FastLocalVarRef -> target.name
|
||||
is FieldRef -> if (target.target is LocalVarRef && target.target.name == "this") target.name else null
|
||||
else -> null
|
||||
}
|
||||
if (name != null) ctx.pendingInitializations.remove(name)
|
||||
}
|
||||
}
|
||||
lvalue = res
|
||||
}
|
||||
return lvalue
|
||||
}
|
||||
@@ -493,24 +513,32 @@ class Compiler(
|
||||
// there could be terminal operators or keywords:// variable to read or like
|
||||
when (t.value) {
|
||||
in stopKeywords -> {
|
||||
if (operand != null) throw ScriptError(t.pos, "unexpected keyword")
|
||||
// Allow certain statement-like constructs to act as expressions
|
||||
// when they appear in expression position (e.g., `if (...) ... else ...`).
|
||||
// Other keywords should be handled by the outer statement parser.
|
||||
when (t.value) {
|
||||
"if" -> {
|
||||
val s = parseIfStatement()
|
||||
operand = StatementRef(s)
|
||||
}
|
||||
"when" -> {
|
||||
val s = parseWhenStatement()
|
||||
operand = StatementRef(s)
|
||||
}
|
||||
else -> {
|
||||
// Do not consume the keyword as part of a term; backtrack
|
||||
// and return null so outer parser handles it.
|
||||
cc.previous()
|
||||
return null
|
||||
if (t.value == "init" && !(codeContexts.lastOrNull() is CodeContext.ClassBody && cc.peekNextNonWhitespace().type == Token.Type.LBRACE)) {
|
||||
// Soft keyword: init is only a keyword in class body when followed by {
|
||||
cc.previous()
|
||||
operand = parseAccessor()
|
||||
} else {
|
||||
if (operand != null) throw ScriptError(t.pos, "unexpected keyword")
|
||||
// Allow certain statement-like constructs to act as expressions
|
||||
// when they appear in expression position (e.g., `if (...) ... else ...`).
|
||||
// Other keywords should be handled by the outer statement parser.
|
||||
when (t.value) {
|
||||
"if" -> {
|
||||
val s = parseIfStatement()
|
||||
operand = StatementRef(s)
|
||||
}
|
||||
|
||||
"when" -> {
|
||||
val s = parseWhenStatement()
|
||||
operand = StatementRef(s)
|
||||
}
|
||||
|
||||
else -> {
|
||||
// Do not consume the keyword as part of a term; backtrack
|
||||
// and return null so outer parser handles it.
|
||||
cc.previous()
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -629,35 +657,30 @@ class Compiler(
|
||||
|
||||
val body = parseBlock(skipLeadingBrace = true)
|
||||
|
||||
var closure: Scope? = null
|
||||
|
||||
val callStatement = statement {
|
||||
// and the source closure of the lambda which might have other thisObj.
|
||||
val context = this.applyClosure(closure!!)
|
||||
// Execute lambda body in a closure-aware context. Blocks inside the lambda
|
||||
// will create child scopes as usual, so re-declarations inside loops work.
|
||||
if (argsDeclaration == null) {
|
||||
// no args: automatic var 'it'
|
||||
val l = args.list
|
||||
val itValue: Obj = when (l.size) {
|
||||
// no args: it == void
|
||||
0 -> ObjVoid
|
||||
// one args: it is this arg
|
||||
1 -> l[0]
|
||||
// more args: it is a list of args
|
||||
else -> ObjList(l.toMutableList())
|
||||
return ValueFnRef { closureScope ->
|
||||
statement {
|
||||
// and the source closure of the lambda which might have other thisObj.
|
||||
val context = this.applyClosure(closureScope)
|
||||
// Execute lambda body in a closure-aware context. Blocks inside the lambda
|
||||
// will create child scopes as usual, so re-declarations inside loops work.
|
||||
if (argsDeclaration == null) {
|
||||
// no args: automatic var 'it'
|
||||
val l = args.list
|
||||
val itValue: Obj = when (l.size) {
|
||||
// no args: it == void
|
||||
0 -> ObjVoid
|
||||
// one args: it is this arg
|
||||
1 -> l[0]
|
||||
// more args: it is a list of args
|
||||
else -> ObjList(l.toMutableList())
|
||||
}
|
||||
context.addItem("it", false, itValue, recordType = ObjRecord.Type.Argument)
|
||||
} else {
|
||||
// assign vars as declared the standard way
|
||||
argsDeclaration.assignToContext(context, defaultAccessType = AccessType.Val)
|
||||
}
|
||||
context.addItem("it", false, itValue, recordType = ObjRecord.Type.Argument)
|
||||
} else {
|
||||
// assign vars as declared the standard way
|
||||
argsDeclaration.assignToContext(context, defaultAccessType = AccessType.Val)
|
||||
}
|
||||
body.execute(context)
|
||||
}
|
||||
|
||||
return ValueFnRef { x ->
|
||||
closure = x
|
||||
callStatement.asReadonly
|
||||
body.execute(context)
|
||||
}.asReadonly
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1373,7 +1396,7 @@ class Compiler(
|
||||
}
|
||||
|
||||
"init" -> {
|
||||
if (codeContexts.lastOrNull() is CodeContext.ClassBody) {
|
||||
if (codeContexts.lastOrNull() is CodeContext.ClassBody && cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||
val block = parseBlock()
|
||||
lastParsedBlockRange?.let { range ->
|
||||
miniSink?.onInitDecl(MiniInitDecl(MiniRange(id.pos, range.end), id.pos))
|
||||
@@ -1750,6 +1773,9 @@ class Compiler(
|
||||
}
|
||||
|
||||
private fun parseEnumDeclaration(): Statement {
|
||||
val startPos = pendingDeclStart ?: cc.currentPos()
|
||||
val doc = pendingDeclDoc ?: consumePendingDoc()
|
||||
pendingDeclDoc = null
|
||||
val nameToken = cc.requireToken(Token.Type.ID)
|
||||
// so far only simplest enums:
|
||||
val names = mutableListOf<String>()
|
||||
@@ -1777,6 +1803,16 @@ class Compiler(
|
||||
}
|
||||
} while (true)
|
||||
|
||||
miniSink?.onEnumDecl(
|
||||
MiniEnumDecl(
|
||||
range = MiniRange(startPos, cc.currentPos()),
|
||||
name = nameToken.value,
|
||||
entries = names,
|
||||
doc = doc,
|
||||
nameStart = nameToken.pos
|
||||
)
|
||||
)
|
||||
|
||||
return statement {
|
||||
ObjEnumClass.createSimpleEnum(nameToken.value, names).also {
|
||||
addItem(nameToken.value, false, it, recordType = ObjRecord.Type.Enum)
|
||||
@@ -1879,7 +1915,7 @@ class Compiler(
|
||||
// create class
|
||||
val className = nameToken.value
|
||||
|
||||
// @Suppress("UNUSED_VARIABLE") val defaultAccess = if (isStruct) AccessType.Var else AccessType.Initialization
|
||||
// @Suppress("UNUSED_VARIABLE") val defaultAccess = if (isStruct) AccessType.Variable else AccessType.Initialization
|
||||
// @Suppress("UNUSED_VARIABLE") val defaultVisibility = Visibility.Public
|
||||
|
||||
// create instance constructor
|
||||
@@ -2689,7 +2725,7 @@ class Compiler(
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.isId("get") || next.isId("set")) {
|
||||
if (next.isId("get") || next.isId("set") || next.isId("private") || next.isId("protected")) {
|
||||
isProperty = true
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
@@ -2707,7 +2743,13 @@ class Compiler(
|
||||
if (!isProperty && eqToken.type != Token.Type.ASSIGN) {
|
||||
if (!isMutable && (declaringClassNameCaptured == null) && (extTypeName == null))
|
||||
throw ScriptError(start, "val must be initialized")
|
||||
else {
|
||||
else if (!isMutable && declaringClassNameCaptured != null && extTypeName == null) {
|
||||
// lateinit val in class: track it
|
||||
(codeContexts.lastOrNull() as? CodeContext.ClassBody)?.pendingInitializations?.put(name, start)
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
setNull = true
|
||||
} else {
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
setNull = true
|
||||
@@ -2746,8 +2788,8 @@ class Compiler(
|
||||
// if (isDelegate) throw ScriptError(start, "static delegates are not yet implemented")
|
||||
currentInitScope += statement {
|
||||
val initValue = initialExpression?.execute(this)?.byValueCopy() ?: ObjNull
|
||||
(thisObj as ObjClass).createClassField(name, initValue, isMutable, visibility, pos)
|
||||
addItem(name, isMutable, initValue, visibility, ObjRecord.Type.Field)
|
||||
(thisObj as ObjClass).createClassField(name, initValue, isMutable, visibility, null, pos)
|
||||
addItem(name, isMutable, initValue, visibility, null, ObjRecord.Type.Field)
|
||||
ObjVoid
|
||||
}
|
||||
return NopStatement
|
||||
@@ -2756,11 +2798,11 @@ class Compiler(
|
||||
// Check for accessors if it is a class member
|
||||
var getter: Statement? = null
|
||||
var setter: Statement? = null
|
||||
var setterVisibility: Visibility? = null
|
||||
if (declaringClassNameCaptured != null || extTypeName != null) {
|
||||
while (true) {
|
||||
val t = cc.peekNextNonWhitespace()
|
||||
val t = cc.skipWsTokens()
|
||||
if (t.isId("get")) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume 'get'
|
||||
cc.requireToken(Token.Type.LPAREN)
|
||||
cc.requireToken(Token.Type.RPAREN)
|
||||
@@ -2776,7 +2818,6 @@ class Compiler(
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after get()")
|
||||
}
|
||||
} else if (t.isId("set")) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume 'set'
|
||||
cc.requireToken(Token.Type.LPAREN)
|
||||
val setArg = cc.requireToken(Token.Type.ID, "Expected setter argument name")
|
||||
@@ -2802,6 +2843,46 @@ class Compiler(
|
||||
} else {
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after set(...)")
|
||||
}
|
||||
} else if (t.isId("private") || t.isId("protected")) {
|
||||
val vis = if (t.isId("private")) Visibility.Private else Visibility.Protected
|
||||
val mark = cc.savePos()
|
||||
cc.next() // consume modifier
|
||||
if (cc.skipWsTokens().isId("set")) {
|
||||
cc.next() // consume 'set'
|
||||
setterVisibility = vis
|
||||
if (cc.skipWsTokens().type == Token.Type.LPAREN) {
|
||||
cc.next() // consume '('
|
||||
val setArg = cc.requireToken(Token.Type.ID, "Expected setter argument name")
|
||||
cc.requireToken(Token.Type.RPAREN)
|
||||
setter = if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||
cc.skipWsTokens()
|
||||
val body = parseBlock()
|
||||
statement(body.pos) { scope ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
body.execute(scope)
|
||||
}
|
||||
} else if (cc.peekNextNonWhitespace().type == Token.Type.ASSIGN) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(
|
||||
cc.current().pos,
|
||||
"Expected setter expression"
|
||||
)
|
||||
val st = (expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
||||
statement(st.pos) { scope ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
st.execute(scope)
|
||||
}
|
||||
} else {
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after set(...)")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cc.restorePos(mark)
|
||||
break
|
||||
}
|
||||
} else break
|
||||
}
|
||||
if (getter != null || setter != null) {
|
||||
@@ -2810,11 +2891,13 @@ class Compiler(
|
||||
throw ScriptError(start, "var property must have both get() and set()")
|
||||
}
|
||||
} else {
|
||||
if (setter != null)
|
||||
throw ScriptError(start, "val property cannot have a setter (name: $name)")
|
||||
if (setter != null || setterVisibility != null)
|
||||
throw ScriptError(start, "val property cannot have a setter or restricted visibility set (name: $name)")
|
||||
if (getter == null)
|
||||
throw ScriptError(start, "val property with accessors must have a getter (name: $name)")
|
||||
}
|
||||
} else if (setterVisibility != null && !isMutable) {
|
||||
throw ScriptError(start, "val field cannot have restricted visibility set (name: $name)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2831,7 +2914,7 @@ class Compiler(
|
||||
val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
|
||||
if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance")
|
||||
|
||||
context.addExtension(type, name, ObjRecord(prop, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Property))
|
||||
context.addExtension(type, name, ObjRecord(prop, isMutable = false, visibility = visibility, writeVisibility = setterVisibility, declaringClass = null, type = ObjRecord.Type.Property))
|
||||
|
||||
return@statement prop
|
||||
}
|
||||
@@ -2865,6 +2948,7 @@ class Compiler(
|
||||
isMutable,
|
||||
prop,
|
||||
visibility,
|
||||
setterVisibility,
|
||||
recordType = ObjRecord.Type.Property
|
||||
)
|
||||
ObjVoid
|
||||
@@ -2872,38 +2956,41 @@ class Compiler(
|
||||
ObjVoid
|
||||
} else {
|
||||
// We are in instance scope already: perform initialization immediately
|
||||
context.addItem(storageName, isMutable, prop, visibility, recordType = ObjRecord.Type.Property)
|
||||
context.addItem(storageName, isMutable, prop, visibility, setterVisibility, recordType = ObjRecord.Type.Property)
|
||||
prop
|
||||
}
|
||||
} else {
|
||||
if (declaringClassName != null && !isStatic) {
|
||||
val storageName = "$declaringClassName::$name"
|
||||
// If we are in class scope now (defining instance field), defer initialization to instance time
|
||||
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
|
||||
if (isClassScope) {
|
||||
val cls = context.thisObj as ObjClass
|
||||
// Defer: at instance construction, evaluate initializer in instance scope and store under mangled name
|
||||
val initStmt = statement(start) { scp ->
|
||||
val initValue = initialExpression?.execute(scp)?.byValueCopy() ?: ObjNull
|
||||
// Preserve mutability of declaration: do NOT use addOrUpdateItem here, as it creates mutable records
|
||||
scp.addItem(storageName, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
val isLateInitVal = !isMutable && initialExpression == null && getter == null && setter == null
|
||||
if (declaringClassName != null && !isStatic) {
|
||||
val storageName = "$declaringClassName::$name"
|
||||
// If we are in class scope now (defining instance field), defer initialization to instance time
|
||||
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
|
||||
if (isClassScope) {
|
||||
val cls = context.thisObj as ObjClass
|
||||
// Defer: at instance construction, evaluate initializer in instance scope and store under mangled name
|
||||
val initStmt = statement(start) { scp ->
|
||||
val initValue =
|
||||
initialExpression?.execute(scp)?.byValueCopy() ?: if (isLateInitVal) ObjUnset else ObjNull
|
||||
// Preserve mutability of declaration: do NOT use addOrUpdateItem here, as it creates mutable records
|
||||
scp.addItem(storageName, isMutable, initValue, visibility, setterVisibility, recordType = ObjRecord.Type.Field)
|
||||
ObjVoid
|
||||
}
|
||||
cls.instanceInitializers += initStmt
|
||||
ObjVoid
|
||||
} else {
|
||||
// We are in instance scope already: perform initialization immediately
|
||||
val initValue =
|
||||
initialExpression?.execute(context)?.byValueCopy() ?: if (isLateInitVal) ObjUnset else ObjNull
|
||||
// Preserve mutability of declaration: create record with correct mutability
|
||||
context.addItem(storageName, isMutable, initValue, visibility, setterVisibility, recordType = ObjRecord.Type.Field)
|
||||
initValue
|
||||
}
|
||||
cls.instanceInitializers += initStmt
|
||||
ObjVoid
|
||||
} else {
|
||||
// We are in instance scope already: perform initialization immediately
|
||||
// Not in class body: regular local/var declaration
|
||||
val initValue = initialExpression?.execute(context)?.byValueCopy() ?: ObjNull
|
||||
// Preserve mutability of declaration: create record with correct mutability
|
||||
context.addItem(storageName, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
context.addItem(name, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
initValue
|
||||
}
|
||||
} else {
|
||||
// Not in class body: regular local/var declaration
|
||||
val initValue = initialExpression?.execute(context)?.byValueCopy() ?: ObjNull
|
||||
context.addItem(name, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
initValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3206,7 +3293,10 @@ class Compiler(
|
||||
* The keywords that stop processing of expression term
|
||||
*/
|
||||
val stopKeywords =
|
||||
setOf("do", "break", "continue", "return", "if", "when", "do", "while", "for", "class")
|
||||
setOf(
|
||||
"break", "continue", "return", "if", "when", "do", "while", "for", "class",
|
||||
"private", "protected", "val", "var", "fun", "fn", "static", "init", "enum"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -324,15 +324,14 @@ private class Parser(fromPos: Pos) {
|
||||
}
|
||||
|
||||
'?' -> {
|
||||
when (currentChar.also { pos.advance() }) {
|
||||
':' -> Token("??", from, Token.Type.ELVIS)
|
||||
'?' -> Token("??", from, Token.Type.ELVIS)
|
||||
'.' -> Token("?.", from, Token.Type.NULL_COALESCE)
|
||||
'[' -> Token("?(", from, Token.Type.NULL_COALESCE_INDEX)
|
||||
'(' -> Token("?(", from, Token.Type.NULL_COALESCE_INVOKE)
|
||||
'{' -> Token("?{", from, Token.Type.NULL_COALESCE_BLOCKINVOKE)
|
||||
when (currentChar) {
|
||||
':' -> { pos.advance(); Token("?:", from, Token.Type.ELVIS) }
|
||||
'?' -> { pos.advance(); Token("??", from, Token.Type.ELVIS) }
|
||||
'.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) }
|
||||
'[' -> { pos.advance(); Token("?[", from, Token.Type.NULL_COALESCE_INDEX) }
|
||||
'(' -> { pos.advance(); Token("?(", from, Token.Type.NULL_COALESCE_INVOKE) }
|
||||
'{' -> { pos.advance(); Token("?{", from, Token.Type.NULL_COALESCE_BLOCKINVOKE) }
|
||||
else -> {
|
||||
pos.back()
|
||||
Token("?", from, Token.Type.QUESTION)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,6 +262,9 @@ open class Scope(
|
||||
|
||||
fun raiseClassCastError(msg: String): Nothing = raiseError(ObjClassCastException(this, msg))
|
||||
|
||||
fun raiseUnset(message: String = "property is unset (not initialized)"): Nothing =
|
||||
raiseError(ObjUnsetException(this, message))
|
||||
|
||||
@Suppress("unused")
|
||||
fun raiseSymbolNotFound(name: String): Nothing =
|
||||
raiseError(ObjSymbolNotDefinedException(this, "symbol is not defined: $name"))
|
||||
@@ -431,6 +434,7 @@ open class Scope(
|
||||
name: String,
|
||||
value: Obj,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
writeVisibility: Visibility? = null,
|
||||
recordType: ObjRecord.Type = ObjRecord.Type.Other
|
||||
): ObjRecord =
|
||||
objects[name]?.let {
|
||||
@@ -445,17 +449,18 @@ open class Scope(
|
||||
callScope.localBindings[name] = it
|
||||
}
|
||||
it
|
||||
} ?: addItem(name, true, value, visibility, recordType)
|
||||
} ?: addItem(name, true, value, visibility, writeVisibility, recordType)
|
||||
|
||||
fun addItem(
|
||||
name: String,
|
||||
isMutable: Boolean,
|
||||
value: Obj,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
writeVisibility: Visibility? = null,
|
||||
recordType: ObjRecord.Type = ObjRecord.Type.Other,
|
||||
declaringClass: net.sergeych.lyng.obj.ObjClass? = currentClassCtx
|
||||
): ObjRecord {
|
||||
val rec = ObjRecord(value, isMutable, visibility, declaringClass = declaringClass, type = recordType)
|
||||
val rec = ObjRecord(value, isMutable, visibility, writeVisibility, declaringClass = declaringClass, type = recordType)
|
||||
objects[name] = rec
|
||||
// Index this binding within the current frame to help resolve locals across suspension
|
||||
localBindings[name] = rec
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -60,6 +60,7 @@ class Script(
|
||||
|
||||
internal val rootScope: Scope = Scope(null).apply {
|
||||
ObjException.addExceptionsToContext(this)
|
||||
addConst("Unset", ObjUnset)
|
||||
addFn("print") {
|
||||
for ((i, a) in args.withIndex()) {
|
||||
if (i > 0) print(' ' + a.toString(this).value)
|
||||
@@ -302,6 +303,7 @@ class Script(
|
||||
addConst("Deferred", ObjDeferred.type)
|
||||
addConst("CompletableDeferred", ObjCompletableDeferred.type)
|
||||
addConst("Mutex", ObjMutex.type)
|
||||
addConst("Flow", ObjFlow.type)
|
||||
|
||||
addConst("Regex", ObjRegex.type)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,12 +25,9 @@ import net.sergeych.lyng.Source
|
||||
import net.sergeych.lyng.highlight.HighlightKind
|
||||
import net.sergeych.lyng.highlight.SimpleLyngHighlighter
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.miniast.MiniClassDecl
|
||||
import net.sergeych.lyng.miniast.MiniFunDecl
|
||||
import net.sergeych.lyng.miniast.MiniScript
|
||||
import net.sergeych.lyng.miniast.MiniValDecl
|
||||
import net.sergeych.lyng.miniast.*
|
||||
|
||||
enum class SymbolKind { Class, Enum, Function, Val, Var, Param }
|
||||
enum class SymbolKind { Class, Enum, Function, Value, Variable, Parameter }
|
||||
|
||||
data class Symbol(
|
||||
val id: Int,
|
||||
@@ -111,7 +108,16 @@ object Binder {
|
||||
for (cf in d.ctorFields) {
|
||||
val fs = source.offsetOf(cf.nameStart)
|
||||
val fe = fs + cf.name.length
|
||||
val kind = if (cf.mutable) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||
val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id)
|
||||
symbols += fieldSym
|
||||
classes.last().fields += fieldSym.id
|
||||
}
|
||||
// Class fields (val/var in class body, if any are reported here)
|
||||
for (cf in d.classFields) {
|
||||
val fs = source.offsetOf(cf.nameStart)
|
||||
val fe = fs + cf.name.length
|
||||
val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||
val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id)
|
||||
symbols += fieldSym
|
||||
classes.last().fields += fieldSym.id
|
||||
@@ -138,7 +144,7 @@ object Binder {
|
||||
for (p in d.params) {
|
||||
val ps = source.offsetOf(p.nameStart)
|
||||
val pe = ps + p.name.length
|
||||
val pk = SymbolKind.Param
|
||||
val pk = SymbolKind.Parameter
|
||||
val paramSym = Symbol(nextId++, p.name, pk, ps, pe, containerId = sym.id)
|
||||
fnScope.locals += paramSym.id
|
||||
symbols += paramSym
|
||||
@@ -147,7 +153,7 @@ object Binder {
|
||||
}
|
||||
is MiniValDecl -> {
|
||||
val (s, e) = nameOffsets(d.nameStart, d.name)
|
||||
val kind = if (d.mutable) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (d.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||
val ownerClass = classContaining(s)
|
||||
if (ownerClass != null) {
|
||||
// class field
|
||||
@@ -160,6 +166,12 @@ object Binder {
|
||||
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
|
||||
}
|
||||
}
|
||||
is MiniEnumDecl -> {
|
||||
val (s, e) = nameOffsets(d.nameStart, d.name)
|
||||
val sym = Symbol(nextId++, d.name, SymbolKind.Enum, s, e, containerId = null)
|
||||
symbols += sym
|
||||
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +203,7 @@ object Binder {
|
||||
.maxByOrNull { it.rangeEnd - it.rangeStart }
|
||||
if (containerFn != null) {
|
||||
val fnSymId = containerFn.id
|
||||
val kind = if (d.mutable) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (d.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||
val localSym = Symbol(nextId++, d.name, kind, s, e, containerId = fnSymId)
|
||||
symbols += localSym
|
||||
containerFn.locals += localSym.id
|
||||
@@ -231,7 +243,7 @@ object Binder {
|
||||
val inFn = functions.asSequence()
|
||||
.filter { it.rangeEnd > it.rangeStart && nameStart >= it.rangeStart && nameStart <= it.rangeEnd }
|
||||
.maxByOrNull { it.rangeEnd - it.rangeStart }
|
||||
val kind = if (kw.equals("var", true)) SymbolKind.Var else SymbolKind.Val
|
||||
val kind = if (kw.equals("var", true)) SymbolKind.Variable else SymbolKind.Value
|
||||
if (inFn != null) {
|
||||
val localSym = Symbol(nextId++, text.substring(nameStart, nameEnd), kind, nameStart, nameEnd, containerId = inFn.id)
|
||||
symbols += localSym
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,6 +24,24 @@ package net.sergeych.lyng.format
|
||||
*/
|
||||
object LyngFormatter {
|
||||
|
||||
private fun startsWithWord(s: String, w: String): Boolean =
|
||||
s.startsWith(w) && s.getOrNull(w.length)?.let { !it.isLetterOrDigit() && it != '_' } != false
|
||||
|
||||
private fun isPropertyAccessor(s: String): Boolean {
|
||||
val t = s.trim()
|
||||
if (t.isEmpty()) return false
|
||||
if (startsWithWord(t, "get") || startsWithWord(t, "set")) return true
|
||||
if (startsWithWord(t, "private")) {
|
||||
val rest = t.substring("private".length).trimStart()
|
||||
if (startsWithWord(rest, "set")) return true
|
||||
}
|
||||
if (startsWithWord(t, "protected")) {
|
||||
val rest = t.substring("protected".length).trimStart()
|
||||
if (startsWithWord(rest, "set")) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** Returns the input with indentation recomputed from scratch, line by line. */
|
||||
fun reindent(text: String, config: LyngFormatConfig = LyngFormatConfig()): String {
|
||||
// Normalize tabs to spaces globally before any transformation; results must contain no tabs
|
||||
@@ -34,18 +52,26 @@ object LyngFormatter {
|
||||
var parenBalance = 0
|
||||
var bracketBalance = 0
|
||||
var prevBracketContinuation = false
|
||||
var inBlockComment = false
|
||||
val extraIndents = mutableListOf<Int>()
|
||||
// We don't keep per-"[" base alignment; continuation rules define alignment.
|
||||
|
||||
fun codePart(s: String): String {
|
||||
val idx = s.indexOf("//")
|
||||
return if (idx >= 0) s.substring(0, idx) else s
|
||||
fun updateBalances(ch: Char) {
|
||||
when (ch) {
|
||||
'{' -> blockLevel++
|
||||
'}' -> if (blockLevel > 0) blockLevel--
|
||||
'(' -> parenBalance++
|
||||
')' -> if (parenBalance > 0) parenBalance--
|
||||
'[' -> bracketBalance++
|
||||
']' -> if (bracketBalance > 0) bracketBalance--
|
||||
}
|
||||
}
|
||||
|
||||
fun indentOf(level: Int, continuation: Int): String =
|
||||
// Always produce spaces; tabs are not allowed in resulting code
|
||||
" ".repeat(level * config.indentSize + continuation)
|
||||
|
||||
var awaitingSingleIndent = false
|
||||
var awaitingExtraIndent = 0
|
||||
fun isControlHeaderNoBrace(s: String): Boolean {
|
||||
val t = s.trim()
|
||||
if (t.isEmpty()) return false
|
||||
@@ -53,36 +79,46 @@ object LyngFormatter {
|
||||
val isIf = Regex("^if\\s*\\(.*\\)\\s*$").matches(t)
|
||||
val isElseIf = Regex("^else\\s+if\\s*\\(.*\\)\\s*$").matches(t)
|
||||
val isElse = t == "else"
|
||||
return isIf || isElseIf || isElse
|
||||
if (isIf || isElseIf || isElse) return true
|
||||
|
||||
// property accessors ending with ) or =
|
||||
if (isPropertyAccessor(t)) {
|
||||
return if (t.contains('=')) t.endsWith('=') else t.endsWith(')')
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
for ((i, rawLine) in lines.withIndex()) {
|
||||
val line = rawLine
|
||||
val code = codePart(line)
|
||||
val (parts, nextInBlockComment) = splitIntoParts(rawLine, inBlockComment)
|
||||
val code = parts.filter { it.type == PartType.Code }.joinToString("") { it.text }
|
||||
val trimmedStart = code.dropWhile { it == ' ' || it == '\t' }
|
||||
val trimmedLine = rawLine.trim()
|
||||
|
||||
// Compute effective indent level for this line
|
||||
var effectiveLevel = blockLevel
|
||||
if (trimmedStart.startsWith("}")) effectiveLevel = (effectiveLevel - 1).coerceAtLeast(0)
|
||||
val currentExtraIndent = extraIndents.sum()
|
||||
var effectiveLevel = blockLevel + currentExtraIndent
|
||||
|
||||
val isAccessor = !inBlockComment && isPropertyAccessor(trimmedStart)
|
||||
if (isAccessor) effectiveLevel += 1
|
||||
|
||||
if (inBlockComment) {
|
||||
if (!trimmedLine.startsWith("*/")) {
|
||||
effectiveLevel += 1
|
||||
}
|
||||
} else if (trimmedStart.startsWith("}")) {
|
||||
effectiveLevel = (effectiveLevel - 1).coerceAtLeast(0)
|
||||
}
|
||||
// else/catch/finally should align with the parent block level; no extra dedent here,
|
||||
// because the preceding '}' has already reduced [blockLevel] appropriately.
|
||||
|
||||
// Single-line control header (if/else/else if) without braces: indent the next
|
||||
// non-empty, non-'}', non-'else' line by one extra level
|
||||
val applyAwaiting = awaitingSingleIndent && trimmedStart.isNotEmpty() &&
|
||||
val applyAwaiting = awaitingExtraIndent > 0 && trimmedStart.isNotEmpty() &&
|
||||
!trimmedStart.startsWith("else") && !trimmedStart.startsWith("}")
|
||||
if (applyAwaiting) effectiveLevel += 1
|
||||
if (applyAwaiting) effectiveLevel += awaitingExtraIndent
|
||||
|
||||
val firstChar = trimmedStart.firstOrNull()
|
||||
// Do not apply continuation on a line that starts with a closer ')' or ']'
|
||||
val startsWithCloser = firstChar == ')' || firstChar == ']'
|
||||
// Kotlin-like rule: continuation persists while inside parentheses; it applies
|
||||
// even on the line that starts with ')'. For brackets, do not apply on the ']' line itself.
|
||||
// Continuation rules:
|
||||
// - For brackets: one-shot continuation for first element after '[', and while inside brackets
|
||||
// (except on the ']' line) continuation equals one unit.
|
||||
// - For parentheses: continuation depth scales with nested level; e.g., inside two nested
|
||||
// parentheses lines get 2 * continuationIndentSize. No continuation on a line that starts with ')'.
|
||||
// While inside parentheses, continuation applies scaled by nesting level
|
||||
val parenContLevels = if (parenBalance > 0 && firstChar != ')') parenBalance else 0
|
||||
val continuation = when {
|
||||
// One-shot continuation when previous line ended with '[' to align first element
|
||||
@@ -91,6 +127,7 @@ object LyngFormatter {
|
||||
bracketBalance > 0 && firstChar != ']' -> config.continuationIndentSize
|
||||
// While inside parentheses, continuation applies scaled by nesting level
|
||||
parenContLevels > 0 -> config.continuationIndentSize * parenContLevels
|
||||
trimmedStart.startsWith(".") -> config.indentSize
|
||||
else -> 0
|
||||
}
|
||||
|
||||
@@ -100,8 +137,8 @@ object LyngFormatter {
|
||||
}
|
||||
|
||||
// Replace leading whitespace with the exact target indent; but keep fully blank lines truly empty
|
||||
val contentStart = line.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) line.length else it }
|
||||
var content = line.substring(contentStart)
|
||||
val contentStart = rawLine.indexOfFirst { it != ' ' && it != '\t' }.let { if (it < 0) rawLine.length else it }
|
||||
var content = rawLine.substring(contentStart)
|
||||
// Collapse spaces right after an opening '[' to avoid "[ 1"; make it "[1"
|
||||
if (content.startsWith("[")) {
|
||||
content = "[" + content.drop(1).trimStart()
|
||||
@@ -115,104 +152,89 @@ object LyngFormatter {
|
||||
}
|
||||
// Determine base indent using structural level and continuation only (spaces only)
|
||||
val indentString = indentOf(effectiveLevel, continuation)
|
||||
if (content.isEmpty()) {
|
||||
// preserve truly blank line as empty to avoid trailing spaces on empty lines
|
||||
// (also keeps continuation blocks visually clean)
|
||||
// do nothing, just append nothing; newline will be appended below if needed
|
||||
} else {
|
||||
if (content.isNotEmpty()) {
|
||||
sb.append(indentString).append(content)
|
||||
}
|
||||
|
||||
// New line (keep EOF semantics similar to input)
|
||||
if (i < lines.lastIndex) sb.append('\n')
|
||||
|
||||
val oldBlockLevel = blockLevel
|
||||
// Update balances using this line's code content
|
||||
for (ch in code) when (ch) {
|
||||
'{' -> blockLevel++
|
||||
'}' -> if (blockLevel > 0) blockLevel--
|
||||
'(' -> parenBalance++
|
||||
')' -> if (parenBalance > 0) parenBalance--
|
||||
'[' -> bracketBalance++
|
||||
']' -> if (bracketBalance > 0) bracketBalance--
|
||||
for (part in parts) {
|
||||
if (part.type == PartType.Code) {
|
||||
for (ch in part.text) updateBalances(ch)
|
||||
}
|
||||
}
|
||||
val newBlockLevel = blockLevel
|
||||
if (newBlockLevel > oldBlockLevel) {
|
||||
val addedThisLine = (if (applyAwaiting) awaitingExtraIndent else 0) + (if (isAccessor) 1 else 0)
|
||||
repeat(newBlockLevel - oldBlockLevel) {
|
||||
extraIndents.add(addedThisLine)
|
||||
}
|
||||
} else if (newBlockLevel < oldBlockLevel) {
|
||||
repeat(oldBlockLevel - newBlockLevel) {
|
||||
if (extraIndents.isNotEmpty()) extraIndents.removeAt(extraIndents.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
// Update awaitingSingleIndent based on current line
|
||||
inBlockComment = nextInBlockComment
|
||||
|
||||
// Update awaitingExtraIndent based on current line
|
||||
if (applyAwaiting && trimmedStart.isNotEmpty()) {
|
||||
// we have just consumed the body line
|
||||
awaitingSingleIndent = false
|
||||
// we have just applied it.
|
||||
val endsWithBrace = code.trimEnd().endsWith("{")
|
||||
if (!endsWithBrace && isControlHeaderNoBrace(code)) {
|
||||
// It's another header, increment
|
||||
awaitingExtraIndent += if (isAccessor) 2 else 1
|
||||
} else {
|
||||
// It's the body, reset
|
||||
awaitingExtraIndent = 0
|
||||
}
|
||||
} else {
|
||||
// start awaiting if current line is a control header without '{'
|
||||
val endsWithBrace = code.trimEnd().endsWith("{")
|
||||
if (!endsWithBrace && isControlHeaderNoBrace(code)) {
|
||||
awaitingSingleIndent = true
|
||||
awaitingExtraIndent = if (isAccessor) 2 else 1
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare one-shot bracket continuation if the current line ends with '['
|
||||
// (first element line gets continuation even before balances update propagate).
|
||||
val endsWithBracket = code.trimEnd().endsWith("[")
|
||||
// Reset one-shot flag after we used it on this line
|
||||
if (prevBracketContinuation) prevBracketContinuation = false
|
||||
// Set for the next iteration if current line ends with '['
|
||||
// Record whether THIS line ends with an opening '[' so the NEXT line gets a one-shot
|
||||
// continuation indent for the first element.
|
||||
if (endsWithBracket) {
|
||||
// One-shot continuation for the very next line
|
||||
prevBracketContinuation = true
|
||||
} else {
|
||||
// Reset the one-shot flag if the previous line didn't end with '['
|
||||
// Reset the one-shot flag if it was used or if line doesn't end with '['
|
||||
prevBracketContinuation = false
|
||||
}
|
||||
}
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
/** Full format. Currently performs indentation only; spacing/wrapping can be added later. */
|
||||
fun format(text: String, config: LyngFormatConfig = LyngFormatConfig()): String {
|
||||
// Phase 1: indentation
|
||||
val indented = reindent(text, config)
|
||||
if (!config.applySpacing && !config.applyWrapping) return indented
|
||||
|
||||
// Phase 2: minimal, safe spacing (PSI-free). Skip block comments completely and
|
||||
// only apply spacing to the part before '//' on each line.
|
||||
// Phase 2: minimal, safe spacing (PSI-free).
|
||||
val lines = indented.split('\n')
|
||||
val out = StringBuilder(indented.length)
|
||||
var inBlockComment = false
|
||||
for ((i, rawLine) in lines.withIndex()) {
|
||||
var line = rawLine
|
||||
if (config.applySpacing) {
|
||||
if (inBlockComment) {
|
||||
// Pass-through until we see the end of the block comment on some line
|
||||
val end = line.indexOf("*/")
|
||||
if (end >= 0) {
|
||||
inBlockComment = false
|
||||
}
|
||||
} else {
|
||||
// If this line opens a block comment, apply spacing only before the opener
|
||||
val startIdx = line.indexOf("/*")
|
||||
val endIdx = line.indexOf("*/")
|
||||
if (startIdx >= 0 && (endIdx < 0 || endIdx < startIdx)) {
|
||||
val before = line.substring(0, startIdx)
|
||||
val after = line.substring(startIdx)
|
||||
val commentIdx = before.indexOf("//")
|
||||
val code = if (commentIdx >= 0) before.substring(0, commentIdx) else before
|
||||
val tail = if (commentIdx >= 0) before.substring(commentIdx) else ""
|
||||
val spaced = applyMinimalSpacing(code)
|
||||
line = (spaced + tail) + after
|
||||
inBlockComment = true
|
||||
val (parts, nextInBlockComment) = splitIntoParts(rawLine, inBlockComment)
|
||||
val sb = StringBuilder()
|
||||
for (part in parts) {
|
||||
if (part.type == PartType.Code) {
|
||||
sb.append(applyMinimalSpacingRules(part.text))
|
||||
} else {
|
||||
// Normal code line: respect single-line comments
|
||||
val commentIdx = line.indexOf("//")
|
||||
if (commentIdx >= 0) {
|
||||
val code = line.substring(0, commentIdx)
|
||||
val tail = line.substring(commentIdx)
|
||||
val spaced = applyMinimalSpacing(code)
|
||||
line = spaced + tail
|
||||
} else {
|
||||
line = applyMinimalSpacing(line)
|
||||
}
|
||||
sb.append(part.text)
|
||||
}
|
||||
}
|
||||
line = sb.toString()
|
||||
inBlockComment = nextInBlockComment
|
||||
}
|
||||
out.append(line.trimEnd())
|
||||
if (i < lines.lastIndex) out.append('\n')
|
||||
@@ -224,9 +246,6 @@ object LyngFormatter {
|
||||
return applyControlledWrapping(spacedText, config)
|
||||
}
|
||||
|
||||
private fun startsWithWord(s: String, w: String): Boolean =
|
||||
s.startsWith(w) && s.getOrNull(w.length)?.let { !it.isLetterOrDigit() && it != '_' } != false
|
||||
|
||||
/**
|
||||
* Reindents a slice of [text] specified by [range] and returns a new string with that slice replaced.
|
||||
* By default, preserves the base indent of the first line in the slice (so the block stays at
|
||||
@@ -369,7 +388,80 @@ object LyngFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyMinimalSpacing(code: String): String {
|
||||
private enum class PartType { Code, StringLiteral, BlockComment, LineComment }
|
||||
private data class Part(val text: String, val type: PartType)
|
||||
|
||||
/**
|
||||
* Split a line into parts: code, string literals, and comments.
|
||||
* Tracks [inBlockComment] state across lines.
|
||||
*/
|
||||
private fun splitIntoParts(
|
||||
text: String,
|
||||
inBlockCommentInitial: Boolean
|
||||
): Pair<List<Part>, Boolean> {
|
||||
val result = mutableListOf<Part>()
|
||||
var i = 0
|
||||
var last = 0
|
||||
var inBlockComment = inBlockCommentInitial
|
||||
var inString = false
|
||||
var quoteChar = ' '
|
||||
|
||||
while (i < text.length) {
|
||||
if (inBlockComment) {
|
||||
if (text.startsWith("*/", i)) {
|
||||
result.add(Part(text.substring(last, i + 2), PartType.BlockComment))
|
||||
inBlockComment = false
|
||||
i += 2
|
||||
last = i
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
} else if (inString) {
|
||||
if (text[i] == quoteChar) {
|
||||
var escapeCount = 0
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j] == '\\') {
|
||||
escapeCount++
|
||||
j--
|
||||
}
|
||||
if (escapeCount % 2 == 0) {
|
||||
inString = false
|
||||
result.add(Part(text.substring(last, i + 1), PartType.StringLiteral))
|
||||
last = i + 1
|
||||
}
|
||||
}
|
||||
i++
|
||||
} else {
|
||||
if (text.startsWith("//", i)) {
|
||||
if (i > last) result.add(Part(text.substring(last, i), PartType.Code))
|
||||
result.add(Part(text.substring(i), PartType.LineComment))
|
||||
last = text.length
|
||||
break
|
||||
} else if (text.startsWith("/*", i)) {
|
||||
if (i > last) result.add(Part(text.substring(last, i), PartType.Code))
|
||||
inBlockComment = true
|
||||
last = i
|
||||
i += 2
|
||||
} else if (text[i] == '"' || text[i] == '\'') {
|
||||
if (i > last) result.add(Part(text.substring(last, i), PartType.Code))
|
||||
inString = true
|
||||
quoteChar = text[i]
|
||||
last = i
|
||||
i++
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
if (last < text.length) {
|
||||
val leftover = text.substring(last)
|
||||
val type = if (inBlockComment) PartType.BlockComment else PartType.Code
|
||||
result.add(Part(leftover, type))
|
||||
}
|
||||
return result to inBlockComment
|
||||
}
|
||||
|
||||
private fun applyMinimalSpacingRules(code: String): String {
|
||||
var s = code
|
||||
// Ensure space before '(' for control-flow keywords
|
||||
s = s.replace(Regex("\\b(if|for|while)\\("), "$1 (")
|
||||
@@ -408,15 +500,16 @@ private fun applyMinimalSpacing(code: String): String {
|
||||
s = s.replace(Regex(", \\)"), ",)")
|
||||
s = s.replace(Regex(", \\]"), ",]")
|
||||
// Equality/boolean operators: ensure spaces around
|
||||
s = s.replace(Regex("\\s*(==|!=|<=|>=|&&|\\|\\|)\\s*"), " $1 ")
|
||||
// Assignment '=' (not part of '==', '!=', '<=', '>=', '=>'): collapse whitespace to single spaces around '='
|
||||
s = s.replace(Regex("(?<![=!<>])\\s*=\\s*(?![=>])"), " = ")
|
||||
// Multiply/Divide/Mod as binary: spaces around
|
||||
s = s.replace(Regex("\\s*([*/%])\\s*"), " $1 ")
|
||||
s = s.replace(Regex("\\s*(==|!=|<=|>=|&&|\\|\\||\\+=|-=|\\*=|/=|=>|->)\\s*"), " $1 ")
|
||||
// Assignment '=' (not part of '==', '!=', '<=', '>=', '=>', '+=', '-=', '*=', '/=', '->'): collapse whitespace to single spaces around '='
|
||||
s = s.replace(Regex("(?<![=!<>+\\-*/])\\s*=\\s*(?![=>])"), " = ")
|
||||
// Multiply/Divide/Mod as binary: spaces around. Avoid '*=', '/='
|
||||
s = s.replace(Regex("(?<![*])\\s*([*/%])\\s*(?![=])"), " $1 ")
|
||||
// Addition as binary: spaces around '+'. We try not to break '++' and '+=' here.
|
||||
s = s.replace(Regex("(?<![+])\\s*\\+\\s*(?![+=])"), " + ")
|
||||
// Subtraction as binary: add spaces when it looks binary. Keep unary '-' tight (after start or ( [ { = : ,)
|
||||
s = s.replace(Regex("(?<=[^\\s(\\[\\{=:,])-(?=[^=])"), " - ")
|
||||
// and avoid splitting '-=', '--', '->'
|
||||
s = s.replace(Regex("(?<=[^\\s(\\[\\{=:,])\\s*-\\s*(?=[^=\\->])"), " - ")
|
||||
// Colon in types/extends: remove spaces before, ensure one space after (keep '::' intact)
|
||||
s = s.replace(Regex("(?<!:)\\s*:(?!:)\\s*"), ": ")
|
||||
return s
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -42,10 +42,10 @@ private val fallbackKeywordIds = setOf(
|
||||
"and", "or", "not",
|
||||
// declarations & modifiers
|
||||
"fun", "fn", "class", "enum", "val", "var", "import", "package",
|
||||
"private", "protected", "static", "open", "extern",
|
||||
"private", "protected", "static", "open", "extern", "init", "get", "set", "by",
|
||||
// control flow and misc
|
||||
"if", "else", "when", "while", "do", "for", "try", "catch", "finally",
|
||||
"throw", "return", "break", "continue", "this", "null", "true", "false"
|
||||
"throw", "return", "break", "continue", "this", "null", "true", "false", "unset"
|
||||
)
|
||||
|
||||
/** Maps lexer token type (and sometimes value) to a [HighlightKind]. */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -113,7 +113,7 @@ object BuiltinDocRegistry : BuiltinDocSource {
|
||||
val src = try { rootLyng } catch (_: Throwable) { null } ?: return emptyList()
|
||||
val out = LinkedHashSet<String>()
|
||||
// Match lines like: fun String.trim(...) or val Int.isEven = ...
|
||||
val re = Regex("(?m)^\\s*(?:fun|val|var)\\s+${className}\\.([A-Za-z_][A-Za-z0-9_]*)\\b")
|
||||
val re = Regex("^\\s*(?:fun|val|var)\\s+${className}\\.([A-Za-z_][A-Za-z0-9_]*)\\b", RegexOption.MULTILINE)
|
||||
re.findAll(src).forEach { m ->
|
||||
val name = m.groupValues.getOrNull(1)?.trim()
|
||||
if (!name.isNullOrEmpty()) out.add(name)
|
||||
@@ -604,6 +604,11 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
||||
method(name = "printStackTrace", doc = StdlibInlineDocIndex.methodDoc("Exception", "printStackTrace") ?: "Print this exception and its stack trace to standard output.")
|
||||
}
|
||||
|
||||
mod.classDoc(name = "Enum", doc = StdlibInlineDocIndex.classDoc("Enum") ?: "Base class for all enums.") {
|
||||
method(name = "name", doc = "Returns the name of this enum constant.", returns = type("lyng.String"))
|
||||
method(name = "ordinal", doc = "Returns the ordinal of this enum constant.", returns = type("lyng.Int"))
|
||||
}
|
||||
|
||||
mod.classDoc(name = "String", doc = StdlibInlineDocIndex.classDoc("String") ?: "String helpers.") {
|
||||
// Only include inline-source method here; Kotlin-embedded methods are now documented via DocHelpers near definitions.
|
||||
method(name = "re", doc = StdlibInlineDocIndex.methodDoc("String", "re") ?: "Compile this string into a regular expression.", returns = type("lyng.Regex"))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,7 +33,7 @@ data class CompletionItem(
|
||||
val typeText: String? = null,
|
||||
)
|
||||
|
||||
enum class Kind { Function, Class_, Value, Method, Field }
|
||||
enum class Kind { Function, Class_, Enum, Value, Method, Field }
|
||||
|
||||
/**
|
||||
* Platform-free, lenient import provider that never fails on unknown packages.
|
||||
@@ -62,43 +62,33 @@ object CompletionEngineLight {
|
||||
StdlibDocsBootstrap.ensure()
|
||||
val prefix = prefixAt(text, caret)
|
||||
val mini = buildMiniAst(text)
|
||||
// Build imported modules as a UNION of MiniAst-derived and textual extraction, always including stdlib
|
||||
run {
|
||||
// no-op block to keep local scope tidy
|
||||
}
|
||||
val fromMini: List<String> = mini?.let { DocLookupUtils.canonicalImportedModules(it) } ?: emptyList()
|
||||
val fromText: List<String> = extractImportsFromText(text)
|
||||
val imported: List<String> = LinkedHashSet<String>().apply {
|
||||
fromMini.forEach { add(it) }
|
||||
fromText.forEach { add(it) }
|
||||
add("lyng.stdlib")
|
||||
}.toList()
|
||||
val imported: List<String> = DocLookupUtils.canonicalImportedModules(mini ?: return emptyList(), text)
|
||||
|
||||
val cap = 200
|
||||
val out = ArrayList<CompletionItem>(64)
|
||||
|
||||
// Member context detection: dot immediately before caret or before current word start
|
||||
val word = wordRangeAt(text, caret)
|
||||
val memberDot = findDotLeft(text, word?.first ?: caret)
|
||||
val word = DocLookupUtils.wordRangeAt(text, caret)
|
||||
val memberDot = DocLookupUtils.findDotLeft(text, word?.first ?: caret)
|
||||
if (memberDot != null) {
|
||||
// 0) Try chained member call return type inference
|
||||
guessReturnClassFromMemberCallBefore(text, memberDot, imported)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls)
|
||||
DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDot, imported, mini)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// 0a) Top-level call before dot
|
||||
guessReturnClassFromTopLevelCallBefore(text, memberDot, imported)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls)
|
||||
DocLookupUtils.guessReturnClassFromTopLevelCallBefore(text, memberDot, imported, mini)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// 0b) Across-known-callees (Iterable/Iterator/List preference)
|
||||
guessReturnClassAcrossKnownCallees(text, memberDot, imported)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls)
|
||||
DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDot, imported, mini)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// 1) Receiver inference fallback
|
||||
guessReceiverClass(text, memberDot, imported)?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls)
|
||||
(DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDot, imported) ?: DocLookupUtils.guessReceiverClass(text, memberDot, imported, mini))?.let { cls ->
|
||||
offerMembersAdd(out, prefix, imported, cls, mini)
|
||||
return out
|
||||
}
|
||||
// In member context and unknown receiver/return type: show nothing (no globals after dot)
|
||||
@@ -110,9 +100,11 @@ object CompletionEngineLight {
|
||||
val decls = m.declarations
|
||||
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
||||
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
||||
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
||||
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
||||
funs.forEach { offerDeclAdd(out, prefix, it) }
|
||||
classes.forEach { offerDeclAdd(out, prefix, it) }
|
||||
enums.forEach { offerDeclAdd(out, prefix, it) }
|
||||
vals.forEach { offerDeclAdd(out, prefix, it) }
|
||||
}
|
||||
|
||||
@@ -126,9 +118,11 @@ object CompletionEngineLight {
|
||||
val decls = BuiltinDocRegistry.docsForModule(mod)
|
||||
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
|
||||
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
|
||||
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
|
||||
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
|
||||
funs.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
classes.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
enums.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
vals.forEach { if (externalAdded < budget) { offerDeclAdd(out, prefix, it); externalAdded++ } }
|
||||
if (out.size >= cap || externalAdded >= budget) break
|
||||
}
|
||||
@@ -147,13 +141,14 @@ object CompletionEngineLight {
|
||||
add(CompletionItem(d.name, Kind.Function, tailText = tail, typeText = typeOf(d.returnType)))
|
||||
}
|
||||
is MiniClassDecl -> add(CompletionItem(d.name, Kind.Class_))
|
||||
is MiniEnumDecl -> add(CompletionItem(d.name, Kind.Enum))
|
||||
is MiniValDecl -> add(CompletionItem(d.name, Kind.Value, typeText = typeOf(d.type)))
|
||||
// else -> add(CompletionItem(d.name, Kind.Value))
|
||||
}
|
||||
}
|
||||
|
||||
private fun offerMembersAdd(out: MutableList<CompletionItem>, prefix: String, imported: List<String>, className: String) {
|
||||
val classes = DocLookupUtils.aggregateClasses(imported)
|
||||
private fun offerMembersAdd(out: MutableList<CompletionItem>, prefix: String, imported: List<String>, className: String, mini: MiniScript? = null) {
|
||||
val classes = DocLookupUtils.aggregateClasses(imported, mini)
|
||||
val visited = mutableSetOf<String>()
|
||||
val directMap = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
|
||||
val inheritedMap = LinkedHashMap<String, MutableList<MiniMemberDecl>>()
|
||||
@@ -242,187 +237,16 @@ object CompletionEngineLight {
|
||||
|
||||
// --- Inference helpers (text-only, PSI-free) ---
|
||||
|
||||
private fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
DocLookupUtils.guessClassFromCallBefore(text, dotPos, imported)?.let { return it }
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i >= 0) {
|
||||
when (text[i]) {
|
||||
'"' -> return "String"
|
||||
']' -> return "List"
|
||||
'}' -> return "Dict"
|
||||
')' -> {
|
||||
// Parenthesized expression: walk back to matching '(' and inspect the inner expression
|
||||
var j = i - 1
|
||||
var depth = 0
|
||||
while (j >= 0) {
|
||||
when (text[j]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
j--
|
||||
}
|
||||
if (j >= 0 && text[j] == '(') {
|
||||
val innerS = (j + 1).coerceAtLeast(0)
|
||||
val innerE = i.coerceAtMost(text.length)
|
||||
if (innerS < innerE) {
|
||||
val inner = text.substring(innerS, innerE).trim()
|
||||
if (inner.startsWith('"') && inner.endsWith('"')) return "String"
|
||||
if (inner.startsWith('[') && inner.endsWith(']')) return "List"
|
||||
if (inner.startsWith('{') && inner.endsWith('}')) return "Dict"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Numeric literal: decimal/int/hex/scientific
|
||||
var j = i
|
||||
var hasDigits = false
|
||||
var hasDot = false
|
||||
var hasExp = false
|
||||
while (j >= 0) {
|
||||
val ch = text[j]
|
||||
if (ch.isDigit()) { hasDigits = true; j--; continue }
|
||||
if (ch == '.') { hasDot = true; j--; continue }
|
||||
if (ch == 'e' || ch == 'E') { hasExp = true; j--; if (j >= 0 && (text[j] == '+' || text[j] == '-')) j--; continue }
|
||||
if (ch in listOf('x','X','a','b','c','d','f','A','B','C','D','F')) { j--; continue }
|
||||
break
|
||||
}
|
||||
if (hasDigits) return if (hasDot || hasExp) "Real" else "Int"
|
||||
|
||||
// 3) this@Type or as Type
|
||||
val identRange = wordRangeAt(text, i + 1)
|
||||
if (identRange != null) {
|
||||
val ident = text.substring(identRange.first, identRange.second)
|
||||
// if it's "as Type", we want Type
|
||||
var k = prevNonWs(text, identRange.first - 1)
|
||||
if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) {
|
||||
return ident
|
||||
}
|
||||
// if it's "this@Type", we want Type
|
||||
if (k >= 0 && text[k] == '@') {
|
||||
val k2 = prevNonWs(text, k - 1)
|
||||
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") {
|
||||
return ident
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun guessReturnClassFromMemberCallBefore(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k < 0 || text[k] != '.') return null
|
||||
val prevDot = k
|
||||
val receiverClass = guessReceiverClass(text, prevDot, imported) ?: return null
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, receiverClass, callee) ?: return null
|
||||
val member = resolved.second
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
|
||||
private fun guessReturnClassFromTopLevelCallBefore(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k >= 0 && text[k] == '.') return null // was a member call
|
||||
for (mod in imported) {
|
||||
val decls = BuiltinDocRegistry.docsForModule(mod)
|
||||
val fn = decls.asSequence().filterIsInstance<MiniFunDecl>().firstOrNull { it.name == callee }
|
||||
if (fn != null) return simpleClassNameOf(fn.returnType)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun guessReturnClassAcrossKnownCallees(text: String, dotPos: Int, imported: List<String>): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
val resolved = DocLookupUtils.findMemberAcrossClasses(imported, callee) ?: return null
|
||||
val member = resolved.second
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
|
||||
private fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) {
|
||||
null -> null
|
||||
is MiniTypeName -> t.segments.lastOrNull()?.name
|
||||
is MiniGenericType -> simpleClassNameOf(t.base)
|
||||
is MiniFunctionType -> null
|
||||
is MiniTypeVar -> null
|
||||
}
|
||||
|
||||
// --- MiniAst and small utils ---
|
||||
|
||||
private suspend fun buildMiniAst(text: String): MiniScript? = try {
|
||||
private suspend fun buildMiniAst(text: String): MiniScript? {
|
||||
val sink = MiniAstBuilder()
|
||||
val src = Source("<engine>", text)
|
||||
val provider = LenientImportProvider.create()
|
||||
Compiler.compileWithMini(src, provider, sink)
|
||||
sink.build()
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
return try {
|
||||
val src = Source("<engine>", text)
|
||||
val provider = LenientImportProvider.create()
|
||||
Compiler.compileWithMini(src, provider, sink)
|
||||
sink.build()
|
||||
} catch (_: Throwable) {
|
||||
sink.build()
|
||||
}
|
||||
}
|
||||
|
||||
private fun typeOf(t: MiniTypeRef?): String = when (t) {
|
||||
@@ -443,42 +267,8 @@ object CompletionEngineLight {
|
||||
private fun prefixAt(text: String, offset: Int): String {
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var i = (off - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && isIdentChar(text[i])) i--
|
||||
while (i >= 0 && DocLookupUtils.isIdentChar(text[i])) i--
|
||||
val start = i + 1
|
||||
return if (start in 0..text.length && start <= off) text.substring(start, off) else ""
|
||||
}
|
||||
|
||||
private fun wordRangeAt(text: String, offset: Int): Pair<Int, Int>? {
|
||||
if (text.isEmpty()) return null
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var s = off
|
||||
var e = off
|
||||
while (s > 0 && isIdentChar(text[s - 1])) s--
|
||||
while (e < text.length && isIdentChar(text[e])) e++
|
||||
return if (s < e) s to e else null
|
||||
}
|
||||
|
||||
private fun findDotLeft(text: String, offset: Int): Int? {
|
||||
var i = (offset - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return if (i >= 0 && text[i] == '.') i else null
|
||||
}
|
||||
|
||||
private fun prevNonWs(text: String, start: Int): Int {
|
||||
var i = start.coerceAtMost(text.length - 1)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return i
|
||||
}
|
||||
|
||||
private fun isIdentChar(c: Char): Boolean = c == '_' || c.isLetterOrDigit()
|
||||
|
||||
private fun extractImportsFromText(text: String): List<String> {
|
||||
val result = LinkedHashSet<String>()
|
||||
val re = Regex("(?m)^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)")
|
||||
re.findAll(text).forEach { m ->
|
||||
val raw = m.groupValues.getOrNull(1)?.trim().orEmpty()
|
||||
if (raw.isNotEmpty()) result.add(if (raw.startsWith("lyng.")) raw else "lyng.$raw")
|
||||
}
|
||||
return result.toList()
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,176 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Shared QuickDoc lookup helpers reusable outside the IDEA plugin.
|
||||
*/
|
||||
package net.sergeych.lyng.miniast
|
||||
|
||||
import net.sergeych.lyng.binding.BindingSnapshot
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
|
||||
object DocLookupUtils {
|
||||
fun findDeclarationAt(mini: MiniScript, offset: Int, name: String): Pair<String, String>? {
|
||||
val src = mini.range.start.source
|
||||
fun matches(p: net.sergeych.lyng.Pos, len: Int) = src.offsetOf(p).let { s -> offset >= s && offset < s + len }
|
||||
|
||||
for (d in mini.declarations) {
|
||||
if (matches(d.nameStart, d.name.length)) {
|
||||
val kind = when (d) {
|
||||
is MiniFunDecl -> "Function"
|
||||
is MiniClassDecl -> "Class"
|
||||
is MiniEnumDecl -> "Enum"
|
||||
is MiniValDecl -> if (d.mutable) "Variable" else "Value"
|
||||
}
|
||||
return d.name to kind
|
||||
}
|
||||
|
||||
val members = when (d) {
|
||||
is MiniFunDecl -> {
|
||||
for (p in d.params) {
|
||||
if (matches(p.nameStart, p.name.length)) return p.name to "Parameter"
|
||||
}
|
||||
emptyList()
|
||||
}
|
||||
|
||||
is MiniEnumDecl -> {
|
||||
// Enum entries don't have explicit nameStart yet, but we can check their text range if we had it.
|
||||
// For now, heuristic: if offset is within enum range and matches an entry name.
|
||||
// To be more precise, we should check that we are NOT at the 'enum' or enum name.
|
||||
if (offset >= src.offsetOf(d.range.start) && offset <= src.offsetOf(d.range.end)) {
|
||||
if (d.entries.contains(name) && !matches(d.nameStart, d.name.length)) {
|
||||
// verify we are actually at the entry word in text
|
||||
val off = src.offsetOf(d.range.start)
|
||||
val end = src.offsetOf(d.range.end)
|
||||
val text = src.text.substring(off, end)
|
||||
// This is still a bit loose but better
|
||||
return name to "EnumConstant"
|
||||
}
|
||||
}
|
||||
emptyList()
|
||||
}
|
||||
|
||||
is MiniClassDecl -> {
|
||||
for (cf in d.ctorFields) {
|
||||
if (matches(cf.nameStart, cf.name.length)) return cf.name to (if (cf.mutable) "Variable" else "Value")
|
||||
}
|
||||
for (cf in d.classFields) {
|
||||
if (matches(cf.nameStart, cf.name.length)) return cf.name to (if (cf.mutable) "Variable" else "Value")
|
||||
}
|
||||
d.members
|
||||
}
|
||||
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
for (m in members) {
|
||||
if (matches(m.nameStart, m.name.length)) {
|
||||
val kind = when (m) {
|
||||
is MiniMemberFunDecl -> "Function"
|
||||
is MiniMemberValDecl -> if (m.isStatic) "Value" else (if (m.mutable) "Variable" else "Value")
|
||||
is MiniInitDecl -> "Initializer"
|
||||
}
|
||||
return m.name to kind
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun findTypeByRange(mini: MiniScript?, name: String, startOffset: Int): MiniTypeRef? {
|
||||
if (mini == null) return null
|
||||
val src = mini.range.start.source
|
||||
|
||||
for (d in mini.declarations) {
|
||||
if (d.name == name && src.offsetOf(d.nameStart) == startOffset) {
|
||||
return when (d) {
|
||||
is MiniValDecl -> d.type
|
||||
is MiniFunDecl -> d.returnType
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
if (d is MiniFunDecl) {
|
||||
for (p in d.params) {
|
||||
if (p.name == name && src.offsetOf(p.nameStart) == startOffset) return p.type
|
||||
}
|
||||
}
|
||||
|
||||
if (d is MiniClassDecl) {
|
||||
for (m in d.members) {
|
||||
if (m.name == name && src.offsetOf(m.nameStart) == startOffset) {
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
for (cf in d.ctorFields) {
|
||||
if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
|
||||
}
|
||||
for (cf in d.classFields) {
|
||||
if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert MiniAst imports to fully-qualified module names expected by BuiltinDocRegistry.
|
||||
* Heuristics:
|
||||
* - If an import does not start with "lyng.", prefix it with "lyng." (e.g., "io.fs" -> "lyng.io.fs").
|
||||
* - Keep original if it already starts with "lyng.".
|
||||
* - Always include "lyng.stdlib" to make builtins available for docs.
|
||||
* - If [sourceText] is provided, uses textual extraction as a fallback if MiniAst has no imports.
|
||||
*/
|
||||
fun canonicalImportedModules(mini: MiniScript): List<String> {
|
||||
fun canonicalImportedModules(mini: MiniScript, sourceText: String? = null): List<String> {
|
||||
val raw = mini.imports.map { it.segments.joinToString(".") { s -> s.name } }
|
||||
val result = LinkedHashSet<String>()
|
||||
for (name in raw) {
|
||||
val canon = if (name.startsWith("lyng.")) name else "lyng.$name"
|
||||
result.add(canon)
|
||||
}
|
||||
|
||||
if (result.isEmpty() && sourceText != null) {
|
||||
result.addAll(extractImportsFromText(sourceText))
|
||||
}
|
||||
|
||||
// Always make stdlib available as a fallback context for common types,
|
||||
// even when there are no explicit imports in the file
|
||||
result.add("lyng.stdlib")
|
||||
return result.toList()
|
||||
}
|
||||
|
||||
fun aggregateClasses(importedModules: List<String>): Map<String, MiniClassDecl> {
|
||||
fun extractImportsFromText(text: String): List<String> {
|
||||
val result = LinkedHashSet<String>()
|
||||
val re = Regex("^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)", RegexOption.MULTILINE)
|
||||
re.findAll(text).forEach { m ->
|
||||
val raw = m.groupValues.getOrNull(1)?.trim().orEmpty()
|
||||
if (raw.isNotEmpty()) {
|
||||
val canon = if (raw.startsWith("lyng.")) raw else "lyng.$raw"
|
||||
result.add(canon)
|
||||
}
|
||||
}
|
||||
return result.toList()
|
||||
}
|
||||
|
||||
fun aggregateClasses(importedModules: List<String>, localMini: MiniScript? = null): Map<String, MiniClassDecl> {
|
||||
// Collect all class decls by name across modules, then merge duplicates by unioning members and bases.
|
||||
val buckets = LinkedHashMap<String, MutableList<MiniClassDecl>>()
|
||||
for (mod in importedModules) {
|
||||
@@ -32,6 +178,23 @@ object DocLookupUtils {
|
||||
for (cls in docs.filterIsInstance<MiniClassDecl>()) {
|
||||
buckets.getOrPut(cls.name) { mutableListOf() }.add(cls)
|
||||
}
|
||||
for (en in docs.filterIsInstance<MiniEnumDecl>()) {
|
||||
buckets.getOrPut(en.name) { mutableListOf() }.add(enumToSyntheticClass(en))
|
||||
}
|
||||
}
|
||||
|
||||
// Add local declarations
|
||||
localMini?.declarations?.forEach { d ->
|
||||
when (d) {
|
||||
is MiniClassDecl -> {
|
||||
buckets.getOrPut(d.name) { mutableListOf() }.add(d)
|
||||
}
|
||||
is MiniEnumDecl -> {
|
||||
val syn = enumToSyntheticClass(d)
|
||||
buckets.getOrPut(d.name) { mutableListOf() }.add(syn)
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
fun mergeClassDecls(name: String, list: List<MiniClassDecl>): MiniClassDecl {
|
||||
@@ -72,8 +235,8 @@ object DocLookupUtils {
|
||||
return result
|
||||
}
|
||||
|
||||
fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules)
|
||||
fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules, localMini)
|
||||
fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniMemberDecl>? {
|
||||
val cls = classes[name] ?: return null
|
||||
cls.members.firstOrNull { it.name == member }?.let { return name to it }
|
||||
@@ -86,12 +249,12 @@ object DocLookupUtils {
|
||||
return dfs(className, mutableSetOf())
|
||||
}
|
||||
|
||||
fun findMemberAcrossClasses(importedModules: List<String>, member: String): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules)
|
||||
fun findMemberAcrossClasses(importedModules: List<String>, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
|
||||
val classes = aggregateClasses(importedModules, localMini)
|
||||
// Preferred order for ambiguous common ops
|
||||
val preference = listOf("Iterable", "Iterator", "List")
|
||||
for (name in preference) {
|
||||
resolveMemberWithInheritance(importedModules, name, member)?.let { return it }
|
||||
resolveMemberWithInheritance(importedModules, name, member, localMini)?.let { return it }
|
||||
}
|
||||
for ((name, cls) in classes) {
|
||||
cls.members.firstOrNull { it.name == member }?.let { return name to it }
|
||||
@@ -104,12 +267,8 @@ object DocLookupUtils {
|
||||
* We walk left from the dot, find a matching `)` and then the identifier immediately before the `(`.
|
||||
* If that identifier matches a known class in any of the imported modules, return it.
|
||||
*/
|
||||
fun guessClassFromCallBefore(text: String, dotPos: Int, importedModules: List<String>): String? {
|
||||
fun guessClassFromCallBefore(text: String, dotPos: Int, importedModules: List<String>, localMini: MiniScript? = null): String? {
|
||||
var i = (dotPos - 1).coerceAtLeast(0)
|
||||
// Skip spaces
|
||||
while (i >= 0 && text[i].isWhitespace()) i++
|
||||
// Note: the previous line is wrong direction; correct implementation below
|
||||
i = (dotPos - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
// Walk back to matching '(' accounting nested parentheses
|
||||
@@ -134,9 +293,396 @@ object DocLookupUtils {
|
||||
if (start >= end) return null
|
||||
val name = text.substring(start, end)
|
||||
// Validate against imported classes
|
||||
val classes = aggregateClasses(importedModules)
|
||||
val classes = aggregateClasses(importedModules, localMini)
|
||||
return if (classes.containsKey(name)) name else null
|
||||
}
|
||||
|
||||
private fun isIdentChar(c: Char): Boolean = c == '_' || c.isLetterOrDigit()
|
||||
fun guessReceiverClassViaMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? {
|
||||
if (mini == null) return null
|
||||
val i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0) return null
|
||||
val wordRange = wordRangeAt(text, i + 1) ?: return null
|
||||
val ident = text.substring(wordRange.first, wordRange.second)
|
||||
|
||||
// 0) Use binding if available for precise resolution
|
||||
if (binding != null) {
|
||||
val ref = binding.references.firstOrNull { wordRange.first >= it.start && wordRange.first < it.end }
|
||||
if (ref != null) {
|
||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
||||
if (sym != null) {
|
||||
val type = findTypeByRange(mini, sym.name, sym.declStart)
|
||||
simpleClassNameOf(type)?.let { return it }
|
||||
}
|
||||
} else {
|
||||
// Check if it's a declaration (e.g. static access to a class)
|
||||
val sym = binding.symbols.firstOrNull { it.declStart == wordRange.first && it.name == ident }
|
||||
if (sym != null) {
|
||||
val type = findTypeByRange(mini, sym.name, sym.declStart)
|
||||
simpleClassNameOf(type)?.let { return it }
|
||||
// if it's a class/enum, return its name directly
|
||||
if (sym.kind == net.sergeych.lyng.binding.SymbolKind.Class || sym.kind == net.sergeych.lyng.binding.SymbolKind.Enum) return sym.name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1) Global declarations in current file (val/var/fun/class/enum)
|
||||
val d = mini.declarations.firstOrNull { it.name == ident }
|
||||
if (d != null) {
|
||||
return when (d) {
|
||||
is MiniClassDecl -> d.name
|
||||
is MiniEnumDecl -> d.name
|
||||
is MiniValDecl -> simpleClassNameOf(d.type)
|
||||
is MiniFunDecl -> simpleClassNameOf(d.returnType)
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Parameters in any function (best-effort fallback without scope mapping)
|
||||
for (fd in mini.declarations.filterIsInstance<MiniFunDecl>()) {
|
||||
for (p in fd.params) {
|
||||
if (p.name == ident) return simpleClassNameOf(p.type)
|
||||
}
|
||||
}
|
||||
|
||||
// 3) Recursive chaining: Base.ident.
|
||||
val dotBefore = findDotLeft(text, wordRange.first)
|
||||
if (dotBefore != null) {
|
||||
val receiverClass = guessReceiverClassViaMini(mini, text, dotBefore, imported, binding)
|
||||
?: guessReceiverClass(text, dotBefore, imported, mini)
|
||||
if (receiverClass != null) {
|
||||
val resolved = resolveMemberWithInheritance(imported, receiverClass, ident, mini)
|
||||
if (resolved != null) {
|
||||
val rt = when (val m = resolved.second) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
else -> null
|
||||
}
|
||||
return simpleClassNameOf(rt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4) Check if it's a known class (static access)
|
||||
val classes = aggregateClasses(imported, mini)
|
||||
if (classes.containsKey(ident)) return ident
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun guessReturnClassFromMemberCallBeforeMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? {
|
||||
if (mini == null) return null
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
// back to matching '('
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
// Ensure member call: dot before callee
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k < 0 || text[k] != '.') return null
|
||||
val prevDot = k
|
||||
// Resolve receiver class via MiniAst (ident like `x`)
|
||||
val receiverClass = guessReceiverClassViaMini(mini, text, prevDot, imported, binding) ?: return null
|
||||
// If receiver class is a locally declared class, resolve member on it
|
||||
val localClass = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == receiverClass }
|
||||
if (localClass != null) {
|
||||
val mm = localClass.members.firstOrNull { it.name == callee }
|
||||
if (mm != null) {
|
||||
val rt = when (mm) {
|
||||
is MiniMemberFunDecl -> mm.returnType
|
||||
is MiniMemberValDecl -> mm.type
|
||||
else -> null
|
||||
}
|
||||
return simpleClassNameOf(rt)
|
||||
} else {
|
||||
// Try to scan class body text for method signature and extract return type
|
||||
val sigs = scanLocalClassMembersFromText(mini, text, localClass)
|
||||
val sig = sigs[callee]
|
||||
if (sig != null && sig.typeText != null) return sig.typeText
|
||||
}
|
||||
}
|
||||
// Else fallback to registry-based resolution (covers imported classes)
|
||||
return resolveMemberWithInheritance(imported, receiverClass, callee, mini)?.second?.let { m ->
|
||||
val rt = when (m) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
simpleClassNameOf(rt)
|
||||
}
|
||||
}
|
||||
|
||||
data class ScannedSig(val kind: String, val params: List<String>?, val typeText: String?)
|
||||
|
||||
fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> {
|
||||
val src = mini.range.start.source
|
||||
val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start)
|
||||
val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length)
|
||||
if (start !in 0..end) return emptyMap()
|
||||
val body = text.substring(start, end)
|
||||
val map = LinkedHashMap<String, ScannedSig>()
|
||||
// fun name(params): Type
|
||||
val funRe = Regex("^\\s*fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(([^)]*)\\)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
|
||||
for (m in funRe.findAll(body)) {
|
||||
val name = m.groupValues.getOrNull(1) ?: continue
|
||||
val params = m.groupValues.getOrNull(2)?.split(',')?.mapNotNull { it.trim().takeIf { it.isNotEmpty() } } ?: emptyList()
|
||||
val type = m.groupValues.getOrNull(3)?.takeIf { it.isNotBlank() }
|
||||
map[name] = ScannedSig("fun", params, type)
|
||||
}
|
||||
// val/var name: Type
|
||||
val valRe = Regex("^\\s*(val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
|
||||
for (m in valRe.findAll(body)) {
|
||||
val kind = m.groupValues.getOrNull(1) ?: continue
|
||||
val name = m.groupValues.getOrNull(2) ?: continue
|
||||
val type = m.groupValues.getOrNull(3)?.takeIf { it.isNotBlank() }
|
||||
if (!map.containsKey(name)) map[name] = ScannedSig(kind, null, type)
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
guessClassFromCallBefore(text, dotPos, imported, mini)?.let { return it }
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i >= 0) {
|
||||
when (text[i]) {
|
||||
'"' -> return "String"
|
||||
']' -> return "List"
|
||||
'}' -> return "Dict"
|
||||
')' -> {
|
||||
// Parenthesized expression: walk back to matching '(' and inspect the inner expression
|
||||
var j = i - 1
|
||||
var depth = 0
|
||||
while (j >= 0) {
|
||||
when (text[j]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
j--
|
||||
}
|
||||
if (j >= 0 && text[j] == '(') {
|
||||
val innerS = (j + 1).coerceAtLeast(0)
|
||||
val innerE = i.coerceAtMost(text.length)
|
||||
if (innerS < innerE) {
|
||||
val inner = text.substring(innerS, innerE).trim()
|
||||
if (inner.startsWith('"') && inner.endsWith('"')) return "String"
|
||||
if (inner.startsWith('[') && inner.endsWith(']')) return "List"
|
||||
if (inner.startsWith('{') && inner.endsWith('}')) return "Dict"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Numeric literal: decimal/int/hex/scientific
|
||||
var j = i
|
||||
var hasDigits = false
|
||||
var hasDot = false
|
||||
var hasExp = false
|
||||
while (j >= 0) {
|
||||
val ch = text[j]
|
||||
if (ch.isDigit()) {
|
||||
hasDigits = true; j--; continue
|
||||
}
|
||||
if (ch == '.') {
|
||||
hasDot = true; j--; continue
|
||||
}
|
||||
if (ch == 'e' || ch == 'E') {
|
||||
hasExp = true; j--; if (j >= 0 && (text[j] == '+' || text[j] == '-')) j--; continue
|
||||
}
|
||||
if (ch in listOf('x', 'X', 'a', 'b', 'c', 'd', 'f', 'A', 'B', 'C', 'D', 'F')) {
|
||||
j--; continue
|
||||
}
|
||||
break
|
||||
}
|
||||
if (hasDigits) return if (hasDot || hasExp) "Real" else "Int"
|
||||
|
||||
// 3) this@Type or as Type
|
||||
val identRange = wordRangeAt(text, i + 1)
|
||||
if (identRange != null) {
|
||||
val ident = text.substring(identRange.first, identRange.second)
|
||||
// if it's "as Type", we want Type
|
||||
var k = prevNonWs(text, identRange.first - 1)
|
||||
if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) {
|
||||
return ident
|
||||
}
|
||||
// if it's "this@Type", we want Type
|
||||
if (k >= 0 && text[k] == '@') {
|
||||
val k2 = prevNonWs(text, k - 1)
|
||||
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") {
|
||||
return ident
|
||||
}
|
||||
}
|
||||
|
||||
// 4) Check if it's a known class (static access)
|
||||
val classes = aggregateClasses(imported, mini)
|
||||
if (classes.containsKey(ident)) return ident
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun guessReturnClassFromMemberCallBefore(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k < 0 || text[k] != '.') return null
|
||||
val prevDot = k
|
||||
val receiverClass = guessReceiverClass(text, prevDot, imported, mini) ?: return null
|
||||
val resolved = resolveMemberWithInheritance(imported, receiverClass, callee, mini) ?: return null
|
||||
val member = resolved.second
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
|
||||
fun guessReturnClassFromTopLevelCallBefore(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
var k = start - 1
|
||||
while (k >= 0 && text[k].isWhitespace()) k--
|
||||
if (k >= 0 && text[k] == '.') return null // was a member call
|
||||
for (mod in imported) {
|
||||
val decls = BuiltinDocRegistry.docsForModule(mod)
|
||||
val fn = decls.asSequence().filterIsInstance<MiniFunDecl>().firstOrNull { it.name == callee }
|
||||
if (fn != null) return simpleClassNameOf(fn.returnType)
|
||||
}
|
||||
// Also check local declarations
|
||||
mini?.declarations?.filterIsInstance<MiniFunDecl>()?.firstOrNull { it.name == callee }?.let { return simpleClassNameOf(it.returnType) }
|
||||
return null
|
||||
}
|
||||
|
||||
fun guessReturnClassAcrossKnownCallees(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
|
||||
var i = prevNonWs(text, dotPos - 1)
|
||||
if (i < 0 || text[i] != ')') return null
|
||||
i--
|
||||
var depth = 0
|
||||
while (i >= 0) {
|
||||
when (text[i]) {
|
||||
')' -> depth++
|
||||
'(' -> if (depth == 0) break else depth--
|
||||
}
|
||||
i--
|
||||
}
|
||||
if (i < 0 || text[i] != '(') return null
|
||||
var j = i - 1
|
||||
while (j >= 0 && text[j].isWhitespace()) j--
|
||||
val end = j + 1
|
||||
while (j >= 0 && isIdentChar(text[j])) j--
|
||||
val start = j + 1
|
||||
if (start >= end) return null
|
||||
val callee = text.substring(start, end)
|
||||
val resolved = findMemberAcrossClasses(imported, callee, mini) ?: return null
|
||||
val member = resolved.second
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
|
||||
fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) {
|
||||
null -> null
|
||||
is MiniTypeName -> t.segments.lastOrNull()?.name
|
||||
is MiniGenericType -> simpleClassNameOf(t.base)
|
||||
is MiniFunctionType -> null
|
||||
is MiniTypeVar -> null
|
||||
}
|
||||
|
||||
fun findDotLeft(text: String, offset: Int): Int? {
|
||||
var i = (offset - 1).coerceAtLeast(0)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return if (i >= 0 && text[i] == '.') i else null
|
||||
}
|
||||
|
||||
fun prevNonWs(text: String, start: Int): Int {
|
||||
var i = start.coerceAtMost(text.length - 1)
|
||||
while (i >= 0 && text[i].isWhitespace()) i--
|
||||
return i
|
||||
}
|
||||
|
||||
fun wordRangeAt(text: String, offset: Int): Pair<Int, Int>? {
|
||||
if (text.isEmpty()) return null
|
||||
val off = offset.coerceIn(0, text.length)
|
||||
var s = off
|
||||
var e = off
|
||||
while (s > 0 && isIdentChar(text[s - 1])) s--
|
||||
while (e < text.length && isIdentChar(text[e])) e++
|
||||
return if (s < e) s to e else null
|
||||
}
|
||||
|
||||
fun isIdentChar(c: Char): Boolean = c == '_' || c.isLetterOrDigit()
|
||||
|
||||
fun enumToSyntheticClass(en: MiniEnumDecl): MiniClassDecl {
|
||||
val staticMembers = mutableListOf<MiniMemberDecl>()
|
||||
// entries: List
|
||||
staticMembers.add(MiniMemberValDecl(en.range, "entries", false, null, null, en.nameStart, isStatic = true))
|
||||
// valueOf(name: String): Enum
|
||||
staticMembers.add(MiniMemberFunDecl(en.range, "valueOf", listOf(MiniParam("name", null, en.nameStart)), null, null, en.nameStart, isStatic = true))
|
||||
|
||||
// Also add each entry as a static member (const)
|
||||
for (entry in en.entries) {
|
||||
staticMembers.add(MiniMemberValDecl(en.range, entry, false, MiniTypeName(en.range, listOf(MiniTypeName.Segment(en.name, en.range)), false), null, en.nameStart, isStatic = true))
|
||||
}
|
||||
|
||||
return MiniClassDecl(
|
||||
range = en.range,
|
||||
name = en.name,
|
||||
bases = listOf("Enum"),
|
||||
bodyRange = null,
|
||||
doc = en.doc,
|
||||
nameStart = en.nameStart,
|
||||
members = staticMembers
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -137,6 +137,14 @@ data class MiniClassDecl(
|
||||
val members: List<MiniMemberDecl> = emptyList()
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniEnumDecl(
|
||||
override val range: MiniRange,
|
||||
override val name: String,
|
||||
val entries: List<String>,
|
||||
override val doc: MiniDoc?,
|
||||
override val nameStart: Pos
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniCtorField(
|
||||
val name: String,
|
||||
val mutable: Boolean,
|
||||
@@ -206,6 +214,7 @@ interface MiniAstSink {
|
||||
fun onValDecl(node: MiniValDecl) {}
|
||||
fun onInitDecl(node: MiniInitDecl) {}
|
||||
fun onClassDecl(node: MiniClassDecl) {}
|
||||
fun onEnumDecl(node: MiniEnumDecl) {}
|
||||
|
||||
fun onBlock(node: MiniBlock) {}
|
||||
fun onIdentifier(node: MiniIdentifier) {}
|
||||
@@ -275,6 +284,12 @@ class MiniAstBuilder : MiniAstSink {
|
||||
lastDoc = null
|
||||
}
|
||||
|
||||
override fun onEnumDecl(node: MiniEnumDecl) {
|
||||
val attach = node.copy(doc = node.doc ?: lastDoc)
|
||||
currentScript?.declarations?.add(attach)
|
||||
lastDoc = null
|
||||
}
|
||||
|
||||
override fun onBlock(node: MiniBlock) {
|
||||
blocks.addLast(node)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -149,6 +149,8 @@ open class Obj {
|
||||
// methods that to override
|
||||
|
||||
open suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if( other === this) return 0
|
||||
if( other === ObjNull ) return 2
|
||||
scope.raiseNotImplemented()
|
||||
}
|
||||
|
||||
@@ -414,7 +416,7 @@ open class Obj {
|
||||
|
||||
val decl = field.declaringClass
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(field.visibility, decl, caller))
|
||||
if (!canAccessMember(field.effectiveWriteVisibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't assign field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
|
||||
if (field.value is ObjProperty) {
|
||||
(field.value as ObjProperty).callSetter(scope, this, newValue, decl)
|
||||
@@ -663,6 +665,50 @@ object ObjNull : Obj() {
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("unset")
|
||||
object ObjUnset : Obj() {
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int = if (other === this) 0 else -1
|
||||
override fun equals(other: Any?): Boolean = other === this
|
||||
override fun toString(): String = "Unset"
|
||||
|
||||
override suspend fun readField(scope: Scope, name: String): ObjRecord = scope.raiseUnset()
|
||||
override suspend fun invokeInstanceMethod(
|
||||
scope: Scope,
|
||||
name: String,
|
||||
args: Arguments,
|
||||
onNotFoundResult: (suspend () -> Obj?)?
|
||||
): Obj = scope.raiseUnset()
|
||||
|
||||
override suspend fun getAt(scope: Scope, index: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) = scope.raiseUnset()
|
||||
override suspend fun callOn(scope: Scope): Obj = scope.raiseUnset()
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun minus(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun negate(scope: Scope): Obj = scope.raiseUnset()
|
||||
override suspend fun mul(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun div(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun mod(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun logicalNot(scope: Scope): Obj = scope.raiseUnset()
|
||||
override suspend fun logicalAnd(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun logicalOr(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun operatorMatch(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun bitAnd(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun bitOr(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun bitXor(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun shl(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun shr(scope: Scope, other: Obj): Obj = scope.raiseUnset()
|
||||
override suspend fun bitNot(scope: Scope): Obj = scope.raiseUnset()
|
||||
|
||||
override val objClass: ObjClass by lazy {
|
||||
object : ObjClass("Unset") {
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
|
||||
return ObjUnset
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: get rid of it. Maybe we ise some Lyng inheritance instead
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -345,6 +345,7 @@ open class ObjClass(
|
||||
initialValue: Obj,
|
||||
isMutable: Boolean = false,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
writeVisibility: Visibility? = null,
|
||||
pos: Pos = Pos.builtIn,
|
||||
declaringClass: ObjClass? = this
|
||||
) {
|
||||
@@ -353,7 +354,7 @@ open class ObjClass(
|
||||
if (existingInSelf != null && existingInSelf.isMutable == false)
|
||||
throw ScriptError(pos, "$name is already defined in $objClass")
|
||||
// Install/override in this class
|
||||
members[name] = ObjRecord(initialValue, isMutable, visibility, declaringClass = declaringClass)
|
||||
members[name] = ObjRecord(initialValue, isMutable, visibility, writeVisibility, declaringClass = declaringClass)
|
||||
// Structural change: bump layout version for PIC invalidation
|
||||
layoutVersion += 1
|
||||
}
|
||||
@@ -368,13 +369,14 @@ open class ObjClass(
|
||||
initialValue: Obj,
|
||||
isMutable: Boolean = false,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
writeVisibility: Visibility? = null,
|
||||
pos: Pos = Pos.builtIn
|
||||
) {
|
||||
initClassScope()
|
||||
val existing = classScope!!.objects[name]
|
||||
if (existing != null)
|
||||
throw ScriptError(pos, "$name is already defined in $objClass or one of its supertypes")
|
||||
classScope!!.addItem(name, isMutable, initialValue, visibility)
|
||||
classScope!!.addItem(name, isMutable, initialValue, visibility, writeVisibility)
|
||||
// Structural change: bump layout version for PIC invalidation
|
||||
layoutVersion += 1
|
||||
}
|
||||
@@ -383,14 +385,31 @@ open class ObjClass(
|
||||
name: String,
|
||||
isOpen: Boolean = false,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
writeVisibility: Visibility? = null,
|
||||
declaringClass: ObjClass? = this,
|
||||
code: suspend Scope.() -> Obj
|
||||
) {
|
||||
val stmt = statement { code() }
|
||||
createField(name, stmt, isOpen, visibility, Pos.builtIn, declaringClass)
|
||||
createField(name, stmt, isOpen, visibility, writeVisibility, Pos.builtIn, declaringClass)
|
||||
}
|
||||
|
||||
fun addConst(name: String, value: Obj) = createField(name, value, isMutable = false)
|
||||
|
||||
fun addProperty(
|
||||
name: String,
|
||||
getter: (suspend Scope.() -> Obj)? = null,
|
||||
setter: (suspend Scope.(Obj) -> Unit)? = null,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
writeVisibility: Visibility? = null,
|
||||
declaringClass: ObjClass? = this
|
||||
) {
|
||||
val g = getter?.let { statement { it() } }
|
||||
val s = setter?.let { statement { it(requiredArg(0)); ObjVoid } }
|
||||
val prop = ObjProperty(name, g, s)
|
||||
members[name] = ObjRecord(prop, false, visibility, writeVisibility, declaringClass, type = ObjRecord.Type.Property)
|
||||
layoutVersion += 1
|
||||
}
|
||||
|
||||
fun addClassConst(name: String, value: Obj) = createClassField(name, value)
|
||||
fun addClassFn(name: String, isOpen: Boolean = false, code: suspend Scope.() -> Obj) {
|
||||
createClassField(name, statement { code() }, isOpen)
|
||||
|
||||
@@ -1,4 +1,21 @@
|
||||
/*
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.sergeych.lyng.obj/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -18,7 +35,6 @@
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.obj.*
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
@@ -194,7 +194,9 @@ open class ObjException(
|
||||
"AccessException",
|
||||
"UnknownException",
|
||||
"NotFoundException",
|
||||
"IllegalOperationException"
|
||||
"IllegalOperationException",
|
||||
"UnsetException",
|
||||
"SyntaxError"
|
||||
)) {
|
||||
scope.addConst(name, getOrCreateExceptionClass(name))
|
||||
}
|
||||
@@ -245,3 +247,6 @@ class ObjIllegalOperationException(scope: Scope, message: String = "Operation is
|
||||
|
||||
class ObjNotFoundException(scope: Scope, message: String = "not found") :
|
||||
ObjException("NotFoundException", scope, message)
|
||||
|
||||
class ObjUnsetException(scope: Scope, message: String = "property is unset (not initialized)") :
|
||||
ObjException("UnsetException", scope, message)
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -97,8 +97,8 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
val decl = f.declaringClass
|
||||
if (scope.thisObj !== this || scope.currentClassCtx == null) {
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(f.visibility, decl, caller))
|
||||
ObjIllegalAssignmentException(
|
||||
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
|
||||
).raise()
|
||||
@@ -108,7 +108,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
prop.callSetter(scope, this, newValue, decl)
|
||||
return
|
||||
}
|
||||
if (!f.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (!f.isMutable && f.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (f.value.assign(scope, newValue) == null)
|
||||
f.value = newValue
|
||||
return
|
||||
@@ -131,8 +131,8 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
}
|
||||
if (scope.thisObj !== this || scope.currentClassCtx == null) {
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, declaring, caller))
|
||||
ObjIllegalAssignmentException(
|
||||
if (!canAccessMember(rec.effectiveWriteVisibility, declaring, caller))
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't assign to field $name (declared in ${declaring?.className ?: "?"})"
|
||||
).raise()
|
||||
@@ -142,7 +142,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
prop.callSetter(scope, this, newValue, declaring)
|
||||
return
|
||||
}
|
||||
if (!rec.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (!rec.isMutable && rec.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (rec.value.assign(scope, newValue) == null)
|
||||
rec.value = newValue
|
||||
return
|
||||
@@ -271,7 +271,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
|
||||
protected val comparableVars: Map<String, ObjRecord> by lazy {
|
||||
instanceScope.objects.filter {
|
||||
it.value.type.comparable
|
||||
it.value.type.comparable && (it.value.type != ObjRecord.Type.Field || it.value.isMutable)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,12 +350,12 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
|
||||
instance.instanceScope.objects[mangled]?.let { f ->
|
||||
val decl = f.declaringClass ?: startClass
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(f.visibility, decl, caller))
|
||||
ObjIllegalAssignmentException(
|
||||
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't assign to field $name (declared in ${decl.className})"
|
||||
).raise()
|
||||
if (!f.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (!f.isMutable && f.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (f.value.assign(scope, newValue) == null) f.value = newValue
|
||||
return
|
||||
}
|
||||
@@ -364,12 +364,12 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
|
||||
instance.instanceScope[name]?.let { f ->
|
||||
val decl = f.declaringClass ?: instance.objClass.findDeclaringClassOf(name)
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(f.visibility, decl, caller))
|
||||
ObjIllegalAssignmentException(
|
||||
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
|
||||
).raise()
|
||||
if (!f.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (!f.isMutable && f.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (f.value.assign(scope, newValue) == null) f.value = newValue
|
||||
return
|
||||
}
|
||||
@@ -377,8 +377,8 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
|
||||
val r = memberFromAncestor(name) ?: scope.raiseError("no such field: $name")
|
||||
val decl = r.declaringClass ?: startClass
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(r.visibility, decl, caller))
|
||||
ObjIllegalAssignmentException(scope, "can't assign to field $name (declared in ${decl.className})").raise()
|
||||
if (!canAccessMember(r.effectiveWriteVisibility, decl, caller))
|
||||
ObjAccessException(scope, "can't assign to field $name (declared in ${decl.className})").raise()
|
||||
if (!r.isMutable) scope.raiseError("can't assign to read-only field: $name")
|
||||
if (r.value.assign(scope, newValue) == null) r.value = newValue
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
}
|
||||
|
||||
else ->
|
||||
scope.raiseError("'+': can't concatenate $this with $other")
|
||||
scope.raiseError("'+': can't concatenate ${this.toString(scope)} with ${other.toString(scope)}")
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -26,12 +26,14 @@ data class ObjRecord(
|
||||
var value: Obj,
|
||||
val isMutable: Boolean,
|
||||
val visibility: Visibility = Visibility.Public,
|
||||
val writeVisibility: Visibility? = null,
|
||||
/** If non-null, denotes the class that declared this member (field/method). */
|
||||
val declaringClass: ObjClass? = null,
|
||||
var importedFrom: Scope? = null,
|
||||
val isTransient: Boolean = false,
|
||||
val type: Type = Type.Other,
|
||||
) {
|
||||
val effectiveWriteVisibility: Visibility get() = writeVisibility ?: visibility
|
||||
enum class Type(val comparable: Boolean = false,val serializable: Boolean = false) {
|
||||
Field(true, true),
|
||||
@Suppress("unused")
|
||||
|
||||
@@ -442,9 +442,9 @@ class ConstRef(private val record: ObjRecord) : ObjRef {
|
||||
* Reference to an object's field with optional chaining.
|
||||
*/
|
||||
class FieldRef(
|
||||
private val target: ObjRef,
|
||||
private val name: String,
|
||||
private val isOptional: Boolean,
|
||||
val target: ObjRef,
|
||||
val name: String,
|
||||
val isOptional: Boolean,
|
||||
) : ObjRef {
|
||||
// 4-entry PIC for reads/writes (guarded by PerfFlags.FIELD_PIC)
|
||||
// Reads
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,6 @@ package net.sergeych.lyng
|
||||
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
import net.sergeych.lyng.obj.ObjClass
|
||||
import net.sergeych.lyng.obj.ObjNull
|
||||
import net.sergeych.lyng.obj.ObjVoid
|
||||
|
||||
fun String.toSource(name: String = "eval"): Source = Source(name, this)
|
||||
@@ -46,9 +45,8 @@ abstract class Statement(
|
||||
abstract suspend fun execute(scope: Scope): Obj
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if( other == ObjNull || other == ObjVoid ) return 1
|
||||
if( other === this ) return 0
|
||||
return -1
|
||||
return -3
|
||||
}
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,10 +49,10 @@ class BindingHighlightTest {
|
||||
|
||||
val binding = Binder.bind(text, mini!!)
|
||||
|
||||
// Find the top-level symbol for counter and ensure it is mutable (Var)
|
||||
// Find the top-level symbol for counter and ensure it is mutable (Variable)
|
||||
val sym = binding.symbols.firstOrNull { it.name == "counter" }
|
||||
assertNotNull(sym, "Top-level var 'counter' must be registered as a symbol")
|
||||
assertEquals(SymbolKind.Var, sym.kind, "'counter' declared with var should be SymbolKind.Var")
|
||||
assertEquals(SymbolKind.Variable, sym.kind, "'counter' declared with var should be SymbolKind.Variable")
|
||||
|
||||
// Declaration position
|
||||
val declRange = sym.declStart to sym.declEnd
|
||||
@@ -82,7 +82,7 @@ class BindingHighlightTest {
|
||||
|
||||
val sym = binding.symbols.firstOrNull { it.name == "answer" }
|
||||
assertNotNull(sym, "Top-level val 'answer' must be registered as a symbol")
|
||||
assertEquals(SymbolKind.Val, sym.kind, "'answer' declared with val should be SymbolKind.Val")
|
||||
assertEquals(SymbolKind.Value, sym.kind, "'answer' declared with val should be SymbolKind.Value")
|
||||
|
||||
val declRange = sym.declStart to sym.declEnd
|
||||
val refs = binding.references.filter { it.symbolId == sym.id && (it.start to it.end) != declRange }
|
||||
@@ -119,7 +119,7 @@ class BindingHighlightTest {
|
||||
// Ensure we registered the local var/val symbol for `name`
|
||||
val nameSym = binding.symbols.firstOrNull { it.name == "name" }
|
||||
assertNotNull(nameSym, "Local variable 'name' should be registered as a symbol")
|
||||
assertEquals(SymbolKind.Var, nameSym.kind, "'name' is declared with var and must be SymbolKind.Var")
|
||||
assertEquals(SymbolKind.Variable, nameSym.kind, "'name' is declared with var and must be SymbolKind.Variable")
|
||||
|
||||
// Ensure there is at least one usage reference to `name` (not just the declaration)
|
||||
val nameRefs = binding.references.filter { it.symbolId == nameSym.id }
|
||||
@@ -165,7 +165,7 @@ class BindingHighlightTest {
|
||||
|
||||
val binding = Binder.bind(text, mini!!)
|
||||
|
||||
val nameSym = binding.symbols.firstOrNull { it.name == "name" && (it.kind == SymbolKind.Var || it.kind == SymbolKind.Val) }
|
||||
val nameSym = binding.symbols.firstOrNull { it.name == "name" && (it.kind == SymbolKind.Variable || it.kind == SymbolKind.Value) }
|
||||
assertNotNull(nameSym, "Local variable 'name' should be registered as a symbol")
|
||||
|
||||
// Find the specific usage inside string-literal invocation: "%s is directory"(name)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,7 +49,7 @@ class BindingTest {
|
||||
}
|
||||
"""
|
||||
)
|
||||
// Expect at least one Param symbol "a" and one Val symbol "x"
|
||||
// Expect at least one Parameter symbol "a" and one Value symbol "x"
|
||||
val aIds = snap.symbols.filter { it.name == "a" }.map { it.id }
|
||||
val xIds = snap.symbols.filter { it.name == "x" }.map { it.id }
|
||||
assertTrue(aIds.isNotEmpty())
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -131,4 +131,78 @@ class MiniAstTest {
|
||||
// Bases captured as plain names for now
|
||||
assertEquals(listOf("Base1", "Base2"), cd.bases)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun miniAst_captures_enum_entries_and_doc() = runTest {
|
||||
val code = """
|
||||
/** Enum E docs */
|
||||
enum E {
|
||||
A,
|
||||
B,
|
||||
C
|
||||
}
|
||||
"""
|
||||
val (_, sink) = compileWithMini(code)
|
||||
val mini = sink.build()
|
||||
assertNotNull(mini)
|
||||
val ed = mini!!.declarations.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == "E" }
|
||||
assertNotNull(ed)
|
||||
assertNotNull(ed.doc)
|
||||
assertTrue(ed.doc!!.raw.contains("Enum E docs"))
|
||||
assertEquals(listOf("A", "B", "C"), ed.entries)
|
||||
assertEquals("E", ed.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun enum_to_synthetic_class_members() = runTest {
|
||||
val code = """
|
||||
enum MyEnum { V1, V2 }
|
||||
"""
|
||||
val (_, sink) = compileWithMini(code)
|
||||
val mini = sink.build()
|
||||
assertNotNull(mini)
|
||||
|
||||
// I'll check via aggregateClasses by mocking the registry or just checking it includes Enum base.
|
||||
val stdlib = BuiltinDocRegistry.docsForModule("lyng.stdlib")
|
||||
val enumBase = stdlib.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "Enum" }
|
||||
assertNotNull(enumBase, "Enum base class should be in stdlib")
|
||||
assertTrue(enumBase.members.any { it.name == "name" })
|
||||
assertTrue(enumBase.members.any { it.name == "ordinal" })
|
||||
|
||||
// Check if aggregateClasses handles enums from local MiniScript
|
||||
val classes = DocLookupUtils.aggregateClasses(listOf("lyng.stdlib"), mini)
|
||||
val myEnum = classes["MyEnum"]
|
||||
assertNotNull(myEnum, "Local enum should be aggregated as a class")
|
||||
assertEquals(listOf("Enum"), myEnum.bases)
|
||||
assertTrue(myEnum.members.any { it.name == "entries" }, "Should have entries")
|
||||
assertTrue(myEnum.members.any { it.name == "valueOf" }, "Should have valueOf")
|
||||
assertTrue(myEnum.members.any { it.name == "V1" }, "Should have V1")
|
||||
assertTrue(myEnum.members.any { it.name == "V2" }, "Should have V2")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun complete_enum_members() = runTest {
|
||||
val code = """
|
||||
enum MyEnum { V1, V2 }
|
||||
val x = MyEnum.V1.<caret>
|
||||
"""
|
||||
val items = CompletionEngineLight.completeAtMarkerSuspend(code)
|
||||
val names = items.map { it.name }.toSet()
|
||||
assertTrue(names.contains("name"), "Should contain name from Enum base")
|
||||
assertTrue(names.contains("ordinal"), "Should contain ordinal from Enum base")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun complete_enum_class_members() = runTest {
|
||||
val code = """
|
||||
enum MyEnum { V1, V2 }
|
||||
val x = MyEnum.<caret>
|
||||
"""
|
||||
val items = CompletionEngineLight.completeAtMarkerSuspend(code)
|
||||
val names = items.map { it.name }.toSet()
|
||||
assertTrue(names.contains("entries"), "Should contain entries")
|
||||
assertTrue(names.contains("valueOf"), "Should contain valueOf")
|
||||
assertTrue(names.contains("V1"), "Should contain V1")
|
||||
assertTrue(names.contains("V2"), "Should contain V2")
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -396,4 +396,123 @@ class OOTest {
|
||||
assertThrows { "answer is 42".totalDigits }
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCacheInClass() = runTest {
|
||||
eval("""
|
||||
class T(salt) {
|
||||
private var c
|
||||
|
||||
init {
|
||||
println("create cached with "+salt)
|
||||
c = cached { salt + "." }
|
||||
}
|
||||
|
||||
fun getResult() = c()
|
||||
}
|
||||
|
||||
val t1 = T("foo")
|
||||
val t2 = T("bar")
|
||||
assertEquals("bar.", t2.getResult())
|
||||
assertEquals("foo.", t1.getResult())
|
||||
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLateInitValsInClasses() = runTest {
|
||||
assertFails {
|
||||
eval("""
|
||||
class T {
|
||||
val x
|
||||
}
|
||||
""")
|
||||
}
|
||||
|
||||
assertFails {
|
||||
eval("val String.late")
|
||||
}
|
||||
|
||||
eval("""
|
||||
// but we can "late-init" them in init block:
|
||||
class OK {
|
||||
val x
|
||||
|
||||
init {
|
||||
x = "foo"
|
||||
}
|
||||
}
|
||||
val ok = OK()
|
||||
assertEquals("foo", ok.x)
|
||||
|
||||
// they can't be reassigned:
|
||||
assertThrows(IllegalAssignmentException) {
|
||||
ok.x = "bar"
|
||||
}
|
||||
|
||||
// To test access before init, we need a trick:
|
||||
class AccessBefore {
|
||||
val x
|
||||
fun readX() = x
|
||||
init {
|
||||
assertEquals(x, Unset)
|
||||
// if we call readX() here, x is Unset.
|
||||
// Just reading it is fine, but using it should throw:
|
||||
assertThrows(UnsetException) { readX() + 1 }
|
||||
x = 42
|
||||
}
|
||||
}
|
||||
AccessBefore()
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPrivateSet() = runTest {
|
||||
eval("""
|
||||
class A {
|
||||
var y = 100
|
||||
private set
|
||||
fun setValue(newValue) { y = newValue }
|
||||
}
|
||||
assertEquals(100, A().y)
|
||||
assertThrows(AccessException) { A().y = 200 }
|
||||
val a = A()
|
||||
a.setValue(200)
|
||||
assertEquals(200, a.y)
|
||||
|
||||
class B(initial) {
|
||||
var y = initial
|
||||
protected set
|
||||
}
|
||||
class C(initial) : B(initial) {
|
||||
fun setBValue(v) { y = v }
|
||||
}
|
||||
val c = C(10)
|
||||
assertEquals(10, c.y)
|
||||
assertThrows(AccessException) { c.y = 20 }
|
||||
c.setBValue(30)
|
||||
assertEquals(30, c.y)
|
||||
|
||||
class D {
|
||||
private var _y = 0
|
||||
var y
|
||||
get() = _y
|
||||
private set(v) { _y = v }
|
||||
fun setY(v) { y = v }
|
||||
}
|
||||
val d = D()
|
||||
assertEquals(0, d.y)
|
||||
assertThrows(AccessException) { d.y = 10 }
|
||||
d.setY(20)
|
||||
assertEquals(20, d.y)
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testValPrivateSetError() = runTest {
|
||||
assertFails {
|
||||
eval("class E { val x = 1 private set }")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -331,6 +331,25 @@ class LyngFormatterTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun multiLineChainCalls() {
|
||||
val src = """
|
||||
somevar.where
|
||||
.isGreen()
|
||||
.isWarm()
|
||||
""".trimIndent()
|
||||
|
||||
val expected = """
|
||||
somevar.where
|
||||
.isGreen()
|
||||
.isWarm()
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 8)
|
||||
val out = LyngFormatter.reindent(src, cfg)
|
||||
assertEquals(expected, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun chainedCalls_and_MemberAccess() {
|
||||
val src = """
|
||||
@@ -413,6 +432,55 @@ class LyngFormatterTest {
|
||||
assertEquals(expected, LyngFormatter.format(out, cfg))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun multilineCommentIndentation() {
|
||||
val src = """
|
||||
/*
|
||||
This is
|
||||
a comment
|
||||
*/
|
||||
fun f() {
|
||||
/*
|
||||
Inner
|
||||
comment
|
||||
*/
|
||||
val x = 1 /* end of line
|
||||
comment */
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val expected = """
|
||||
/*
|
||||
This is
|
||||
a comment
|
||||
*/
|
||||
fun f() {
|
||||
/*
|
||||
Inner
|
||||
comment
|
||||
*/
|
||||
val x = 1 /* end of line
|
||||
comment */
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(indentSize = 4)
|
||||
val out = LyngFormatter.reindent(src, cfg)
|
||||
assertEquals(expected, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun multicharacterOperators_spacing() {
|
||||
val operators = listOf("+=", "-=", "*=", "/=", "=>", "==", "!=", "<=", ">=", "&&", "||", "->")
|
||||
for (op in operators) {
|
||||
val src = "a${op}b"
|
||||
val expected = "a $op b"
|
||||
val cfg = LyngFormatConfig(applySpacing = true)
|
||||
val out = LyngFormatter.format(src, cfg)
|
||||
assertEquals(expected, out, "Failed for operator $op")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun propertyBasedIdempotencyMedium() {
|
||||
// Compose slightly longer random snippets out of templates
|
||||
@@ -608,4 +676,178 @@ class LyngFormatterTest {
|
||||
// Idempotent
|
||||
assertEquals(expected, LyngFormatter.reindent(out, cfg))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stringLiterals_areNotNormalized() {
|
||||
val src = """
|
||||
val s = "a=b"
|
||||
val s2 = "a + b"
|
||||
val s3 = "a - b"
|
||||
val s4 = "a,b"
|
||||
val s5 = "if(x){}"
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(applySpacing = true)
|
||||
val formatted = LyngFormatter.format(src, cfg)
|
||||
|
||||
assertEquals(src, formatted, "String literals should not be changed by space normalization")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mixedCodeAndStrings_normalization() {
|
||||
val src = "val x=1+\"a+b\"+2"
|
||||
val expected = "val x = 1 + \"a+b\" + 2"
|
||||
|
||||
val cfg = LyngFormatConfig(applySpacing = true)
|
||||
val formatted = LyngFormatter.format(src, cfg)
|
||||
|
||||
assertEquals(expected, formatted)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reindent_ignoresBracesInStrings() {
|
||||
val src = """
|
||||
fun test() {
|
||||
val s = "{"
|
||||
val s2 = "}"
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(indentSize = 4)
|
||||
val formatted = LyngFormatter.reindent(src, cfg)
|
||||
|
||||
// If it fails, s2 will be indented differently
|
||||
assertEquals(src, formatted, "Braces in strings should not affect indentation")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blockComments_spacing_robustness() {
|
||||
val src = "val x=1/*a=b*/+2"
|
||||
val expected = "val x = 1/*a=b*/ + 2"
|
||||
|
||||
val cfg = LyngFormatConfig(applySpacing = true)
|
||||
val formatted = LyngFormatter.format(src, cfg)
|
||||
|
||||
assertEquals(expected, formatted)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun privateSet_indentation() {
|
||||
val src = """
|
||||
class A {
|
||||
var x = 1
|
||||
private set
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
// What we expect: private set should be indented relative to the property
|
||||
val expected = """
|
||||
class A {
|
||||
var x = 1
|
||||
private set
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
|
||||
val out = LyngFormatter.reindent(src, cfg)
|
||||
assertEquals(expected, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun propertyAccessors_indentation() {
|
||||
val src = """
|
||||
class A {
|
||||
var x
|
||||
get() = 1
|
||||
private set(v) {
|
||||
_x = v
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val expected = """
|
||||
class A {
|
||||
var x
|
||||
get() = 1
|
||||
private set(v) {
|
||||
_x = v
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
|
||||
val out = LyngFormatter.reindent(src, cfg)
|
||||
assertEquals(expected, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nestedAwaitingIndentation() {
|
||||
val src = """
|
||||
if (cond)
|
||||
if (other)
|
||||
fun f() {
|
||||
stmt
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val expected = """
|
||||
if (cond)
|
||||
if (other)
|
||||
fun f() {
|
||||
stmt
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
|
||||
val out = LyngFormatter.reindent(src, cfg)
|
||||
assertEquals(expected, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun propertyAccessor_followedByMethod() {
|
||||
val src = """
|
||||
var state: BarRequestState get() = getState()
|
||||
set(value) = setState(value)
|
||||
|
||||
fun save() { cell.value = this }
|
||||
""".trimIndent()
|
||||
|
||||
val expected = """
|
||||
var state: BarRequestState get() = getState()
|
||||
set(value) = setState(value)
|
||||
|
||||
fun save() { cell.value = this }
|
||||
""".trimIndent()
|
||||
|
||||
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
|
||||
val out = LyngFormatter.reindent(src, cfg)
|
||||
assertEquals(expected, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun propertyAccessor_dangling() {
|
||||
val src1 = """
|
||||
var x
|
||||
get()
|
||||
= 1
|
||||
""".trimIndent()
|
||||
val expected1 = """
|
||||
var x
|
||||
get()
|
||||
= 1
|
||||
""".trimIndent()
|
||||
assertEquals(expected1, LyngFormatter.reindent(src1, LyngFormatConfig(indentSize = 4)))
|
||||
|
||||
val src2 = """
|
||||
var x
|
||||
get() =
|
||||
1
|
||||
""".trimIndent()
|
||||
val expected2 = """
|
||||
var x
|
||||
get() =
|
||||
1
|
||||
""".trimIndent()
|
||||
assertEquals(expected2, LyngFormatter.reindent(src2, LyngFormatConfig(indentSize = 4)))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,10 +28,7 @@ import net.sergeych.lyng.binding.SymbolKind
|
||||
import net.sergeych.lyng.highlight.HighlightKind
|
||||
import net.sergeych.lyng.highlight.SimpleLyngHighlighter
|
||||
import net.sergeych.lyng.highlight.offsetOf
|
||||
import net.sergeych.lyng.miniast.MiniAstBuilder
|
||||
import net.sergeych.lyng.miniast.MiniClassDecl
|
||||
import net.sergeych.lyng.miniast.MiniFunDecl
|
||||
import net.sergeych.lyng.miniast.MiniTypeName
|
||||
import net.sergeych.lyng.miniast.*
|
||||
import org.w3c.dom.HTMLStyleElement
|
||||
|
||||
fun ensureBootstrapCodeBlocks(html: String): String {
|
||||
@@ -367,6 +364,7 @@ suspend fun applyLyngHighlightToTextAst(text: String): String {
|
||||
is MiniFunDecl -> putName(d.nameStart, d.name, "hl-fn")
|
||||
is MiniClassDecl -> putName(d.nameStart, d.name, "hl-class")
|
||||
is net.sergeych.lyng.miniast.MiniValDecl -> putName(d.nameStart, d.name, if (d.mutable) "hl-var" else "hl-val")
|
||||
is MiniEnumDecl -> putName(d.nameStart, d.name, "hl-class")
|
||||
}
|
||||
}
|
||||
// Imports: color each segment as directive/path
|
||||
@@ -404,6 +402,7 @@ suspend fun applyLyngHighlightToTextAst(text: String): String {
|
||||
}
|
||||
is net.sergeych.lyng.miniast.MiniValDecl -> addTypeSegments(d.type)
|
||||
is MiniClassDecl -> {}
|
||||
is MiniEnumDecl -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,9 +419,9 @@ suspend fun applyLyngHighlightToTextAst(text: String): String {
|
||||
fun classForKind(k: SymbolKind): String? = when (k) {
|
||||
SymbolKind.Function -> "hl-fn"
|
||||
SymbolKind.Class, SymbolKind.Enum -> "hl-class"
|
||||
SymbolKind.Param -> "hl-param"
|
||||
SymbolKind.Val -> "hl-val"
|
||||
SymbolKind.Var -> "hl-var"
|
||||
SymbolKind.Parameter -> "hl-param"
|
||||
SymbolKind.Value -> "hl-val"
|
||||
SymbolKind.Variable -> "hl-var"
|
||||
}
|
||||
for (ref in binding.references) {
|
||||
val key = ref.start to ref.end
|
||||
@@ -534,7 +533,8 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
|
||||
"package", "import", "fun", "fn", "class", "enum", "val", "var",
|
||||
"if", "else", "while", "do", "for", "when", "try", "catch", "finally",
|
||||
"throw", "return", "break", "continue", "in", "is", "as", "as?", "not",
|
||||
"true", "false", "null", "private", "protected", "open", "extern", "static"
|
||||
"true", "false", "null", "private", "protected", "open", "extern", "static",
|
||||
"init", "get", "set", "Unset", "by"
|
||||
)
|
||||
fun skipWs(idx0: Int): Int {
|
||||
var idx = idx0
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -127,7 +127,7 @@ fun ReferencePage() {
|
||||
Ul({ classes("mt-2") }) {
|
||||
d.members.forEach { m ->
|
||||
when (m) {
|
||||
is MiniMemberFunDecl, -> {
|
||||
is MiniMemberFunDecl -> {
|
||||
val params = m.params.joinToString(", ") { p ->
|
||||
val ts = typeOf(p.type)
|
||||
if (ts.isNotBlank()) "${p.name}${ts}" else p.name
|
||||
@@ -150,6 +150,17 @@ fun ReferencePage() {
|
||||
}
|
||||
}
|
||||
}
|
||||
is MiniEnumDecl -> {
|
||||
Div { Text("enum ${d.name}") }
|
||||
d.doc?.summary?.let { Small({ classes("text-muted") }) { Text(it) } }
|
||||
if (d.entries.isNotEmpty()) {
|
||||
Ul({ classes("mt-2") }) {
|
||||
d.entries.forEach { entry ->
|
||||
Li { Text(entry) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,7 +329,7 @@
|
||||
<!-- Top-left version ribbon -->
|
||||
<div class="corner-ribbon bg-danger text-white">
|
||||
<span style="margin-left: -5em">
|
||||
v1.1.0-beta1
|
||||
v1.1.0-beta2
|
||||
</span>
|
||||
</div>
|
||||
<!-- Fixed top navbar for the whole site -->
|
||||
|
||||
Reference in new issue
Block a user