better expression handling in lsp

This commit is contained in:
Martino Ferrari
2026-02-02 16:09:50 +01:00
parent bd845aa859
commit 12615aa6d2
2 changed files with 15 additions and 9 deletions

View File

@@ -679,7 +679,7 @@ func HandleCompletion(params CompletionParams) *CompletionList {
prefix := lineStr[:col] prefix := lineStr[:col]
// Case 3: Variable completion // Case 3: Variable completion
varRegex := regexp.MustCompile(`([@$])([a-zA-Z0-9_]*)$`) varRegex := regexp.MustCompile(`([@])([a-zA-Z0-9_]*)$`)
if matches := varRegex.FindStringSubmatch(prefix); matches != nil { if matches := varRegex.FindStringSubmatch(prefix); matches != nil {
container := Tree.GetNodeContaining(path, parser.Position{Line: params.Position.Line + 1, Column: col + 1}) container := Tree.GetNodeContaining(path, parser.Position{Line: params.Position.Line + 1, Column: col + 1})
if container == nil { if container == nil {
@@ -1785,7 +1785,7 @@ func findSignalPeers(target *index.ProjectNode) []*index.ProjectNode {
func evaluate(val parser.Value, ctx *index.ProjectNode) parser.Value { func evaluate(val parser.Value, ctx *index.ProjectNode) parser.Value {
switch v := val.(type) { switch v := val.(type) {
case *parser.VariableReferenceValue: case *parser.VariableReferenceValue:
name := strings.TrimLeft(v.Name, "@$") name := strings.TrimLeft(v.Name, "@")
if info := Tree.ResolveVariable(ctx, name); info != nil { if info := Tree.ResolveVariable(ctx, name); info != nil {
if info.Def.DefaultValue != nil { if info.Def.DefaultValue != nil {
return evaluate(info.Def.DefaultValue, ctx) return evaluate(info.Def.DefaultValue, ctx)
@@ -1805,8 +1805,14 @@ func evaluate(val parser.Value, ctx *index.ProjectNode) parser.Value {
func compute(left parser.Value, op parser.Token, right parser.Value) parser.Value { func compute(left parser.Value, op parser.Token, right parser.Value) parser.Value {
if op.Type == parser.TokenConcat { if op.Type == parser.TokenConcat {
s1 := valueToString(left, nil) getRaw := func(v parser.Value) string {
s2 := valueToString(right, nil) if s, ok := v.(*parser.StringValue); ok {
return s.Value
}
return valueToString(v, nil)
}
s1 := getRaw(left)
s2 := getRaw(right)
return &parser.StringValue{Value: s1 + s2, Quoted: true} return &parser.StringValue{Value: s1 + s2, Quoted: true}
} }

View File

@@ -352,12 +352,12 @@ package schema
t.Error("Expected @MyVar in suggestions for =") t.Error("Expected @MyVar in suggestions for =")
} }
// 2. Triggered by $ // 2. Triggered by @
// "Field = $" // "Field = @"
lsp.Documents[uri] = ` lsp.Documents[uri] = `
#var MyVar: uint = 10 #var MyVar: uint = 10
+App = { +App = {
Field = $ Field = @
} }
` `
params2 := lsp.CompletionParams{ params2 := lsp.CompletionParams{
@@ -366,7 +366,7 @@ package schema
} }
list2 := lsp.HandleCompletion(params2) list2 := lsp.HandleCompletion(params2)
if list2 == nil { if list2 == nil {
t.Fatal("Expected suggestions for $") t.Fatal("Expected suggestions for @")
} }
found = false found = false
for _, item := range list2.Items { for _, item := range list2.Items {
@@ -376,7 +376,7 @@ package schema
} }
} }
if !found { if !found {
t.Error("Expected MyVar in suggestions for $") t.Error("Expected MyVar in suggestions for @")
} }
}) })
} }