Skip to content

Commit

Permalink
fix: extract findDefinitionForRange
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jul 1, 2024
1 parent 90ae567 commit 9219696
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 0 additions & 2 deletions internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"os"
"strings"
"testing"

"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
Expand All @@ -13,7 +12,6 @@ import (
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
"go.lsp.dev/protocol"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
)
Expand Down
15 changes: 7 additions & 8 deletions internal/lsp/symbol_table_template_context_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lsp
import (
"fmt"

"github.com/mrjosh/helm-ls/internal/util"
sitter "github.com/smacker/go-tree-sitter"
)

Expand All @@ -23,14 +22,14 @@ func (s *SymbolTable) ResolveVariablesInTemplateContext(templateContext Template
return templateContext, fmt.Errorf("variable %s not found", variableName)
}

for _, definition := range variableDefinitions {
if util.RangeContainsRange(definition.Scope, pointRange) {
prefix := getPrefixTemplateContextForVariable(definition)

return s.ResolveVariablesInTemplateContext(append(prefix, templateContext.Tail()...), pointRange)
}
definition, err := findDefinitionForRange(variableDefinitions, pointRange)
if err != nil {
return templateContext, fmt.Errorf("variable %s not found %e", variableName, err)
}
return templateContext, fmt.Errorf("variable %s not found", variableName)

prefix := getPrefixTemplateContextForVariable(definition)

return s.ResolveVariablesInTemplateContext(append(prefix, templateContext.Tail()...), pointRange)
}

func getPrefixTemplateContextForVariable(definition VariableDefinition) TemplateContext {
Expand Down
21 changes: 16 additions & 5 deletions internal/lsp/symbol_table_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ func (s *SymbolTable) getVariableDefinition(name string, accessRange sitter.Rang
if !ok || len(definitions) == 0 {
return VariableDefinition{}, fmt.Errorf("variable %s not found", name)
}
for _, definition := range definitions {
if util.RangeContainsRange(definition.Scope, accessRange) {
return definition, nil
}

definition, err := findDefinitionForRange(definitions, accessRange)
if err != nil {
return VariableDefinition{}, fmt.Errorf("variable %s not found: %s", name, err.Error())
}
return VariableDefinition{}, fmt.Errorf("variable %s not found", name)

return definition, nil
}

func (s *SymbolTable) GetVariableDefinitionForNode(node *sitter.Node, content []byte) (VariableDefinition, error) {
Expand All @@ -67,6 +68,7 @@ func (s *SymbolTable) GetVariableReferencesForNode(node *sitter.Node, content []
return []sitter.Range{}, err
}
usages := s.variableUsages[name]

for _, usage := range usages {
if util.RangeContainsRange(definition.Scope, usage) {
ranges = append(ranges, usage)
Expand All @@ -87,3 +89,12 @@ func getVariableName(node *sitter.Node, content []byte) (string, error) {
}
return node.Content(content), nil
}

func findDefinitionForRange(definitions []VariableDefinition, accessRange sitter.Range) (VariableDefinition, error) {
for _, definition := range definitions {
if util.RangeContainsRange(definition.Scope, accessRange) {
return definition, nil
}
}
return VariableDefinition{}, fmt.Errorf("variable not found")
}
2 changes: 1 addition & 1 deletion internal/lsp/symbol_table_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestGetVariableDefinition(t *testing.T) {
EndByte: 68,
},
expectedValue: ``,
expectedError: fmt.Errorf("variable $x not found"),
expectedError: fmt.Errorf("variable $x not found: variable not found"),
},
}
for _, tC := range testCases {
Expand Down

0 comments on commit 9219696

Please sign in to comment.