From 90ae5672390ee7749fc26eefc84088d32fe10bb8 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Mon, 1 Jul 2024 15:58:46 +0200 Subject: [PATCH] fix: cleanup --- internal/handler/completion_main_test.go | 6 ++---- internal/handler/helpers_test.go | 15 +++++++++++++++ internal/handler/references_test.go | 9 --------- internal/language_features/built_in_objects.go | 16 ++++++++++++++-- .../generic_template_context.go | 2 ++ internal/lsp/symbol_table.go | 1 + .../symbol_table_template_context_variables.go | 14 +++++++++----- 7 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 internal/handler/helpers_test.go diff --git a/internal/handler/completion_main_test.go b/internal/handler/completion_main_test.go index 46b1504f..1ee2f7e4 100644 --- a/internal/handler/completion_main_test.go +++ b/internal/handler/completion_main_test.go @@ -188,10 +188,8 @@ func TestCompletionMainSingleLines(t *testing.T) { for _, tt := range testCases { t.Run(tt.templateWithMark, func(t *testing.T) { - // seen chars up to ^ - col := strings.Index(tt.templateWithMark, "^") - buf := strings.Replace(tt.templateWithMark, "^", "", 1) - pos := protocol.Position{Line: 0, Character: uint32(col)} + pos, buf := getPositionForMarkedTestLine(tt.templateWithMark) + // to get the correct values file ../../testdata/example/values.yaml fileURI := uri.File("../../testdata/example/templates/completion-test.yaml") diff --git a/internal/handler/helpers_test.go b/internal/handler/helpers_test.go new file mode 100644 index 00000000..f9fd365f --- /dev/null +++ b/internal/handler/helpers_test.go @@ -0,0 +1,15 @@ +package handler + +import ( + "strings" + + "go.lsp.dev/protocol" +) + +// Takes a string with a mark (^) in it and returns the position and the string without the mark +func getPositionForMarkedTestLine(buf string) (protocol.Position, string) { + col := strings.Index(buf, "^") + buf = strings.Replace(buf, "^", "", 1) + pos := protocol.Position{Line: 0, Character: uint32(col)} + return pos, buf +} diff --git a/internal/handler/references_test.go b/internal/handler/references_test.go index 039bd24e..ba106d66 100644 --- a/internal/handler/references_test.go +++ b/internal/handler/references_test.go @@ -3,7 +3,6 @@ package handler import ( "context" "os" - "strings" "testing" "github.com/mrjosh/helm-ls/internal/adapter/yamlls" @@ -241,11 +240,3 @@ func TestRefercesSingleLines(t *testing.T) { }) } } - -// Takes a string with a mark (^) in it and returns the position and the string without the mark -func getPositionForMarkedTestLine(buf string) (protocol.Position, string) { - col := strings.Index(buf, "^") - buf = strings.Replace(buf, "^", "", 1) - pos := protocol.Position{Line: 0, Character: uint32(col)} - return pos, buf -} diff --git a/internal/language_features/built_in_objects.go b/internal/language_features/built_in_objects.go index dbeaf044..c7c026be 100644 --- a/internal/language_features/built_in_objects.go +++ b/internal/language_features/built_in_objects.go @@ -32,18 +32,24 @@ func (f *BuiltInObjectsFeature) AppropriateForNode() bool { if err != nil || len(templateContext) != 1 { return false } + for _, allowedBuiltIn := range allowedBuiltIns { if templateContext[0] == allowedBuiltIn { return true } } + return false } func (f *BuiltInObjectsFeature) References() (result []lsp.Location, err error) { - templateContext, _ := f.getTemplateContext() + templateContext, err := f.getTemplateContext() + if err != nil { + return []lsp.Location{}, err + } locations := f.getReferencesFromSymbolTable(templateContext) + return append(locations, f.getDefinitionLocations(templateContext)...), err } @@ -55,6 +61,7 @@ func (f *BuiltInObjectsFeature) getDefinitionLocations(templateContext lsplocal. for _, valueFile := range f.Chart.ValuesFiles.AllValuesFiles() { locations = append(locations, lsp.Location{URI: valueFile.URI}) } + return locations case "Chart": return []lsp.Location{{URI: f.Chart.ChartMetadata.URI}} @@ -64,9 +71,13 @@ func (f *BuiltInObjectsFeature) getDefinitionLocations(templateContext lsplocal. } func (f *BuiltInObjectsFeature) Hover() (string, error) { - templateContext, _ := f.getTemplateContext() + templateContext, err := f.getTemplateContext() + if err != nil { + return "", err + } docs, err := f.builtInOjectDocsLookup(templateContext[0], helmdocs.BuiltInObjects) + return docs.Doc, err } @@ -75,5 +86,6 @@ func (f *BuiltInObjectsFeature) Definition() (result []lsp.Location, err error) if err != nil { return []lsp.Location{}, err } + return f.getDefinitionLocations(templateContext), nil } diff --git a/internal/language_features/generic_template_context.go b/internal/language_features/generic_template_context.go index 13bf565b..77dffc70 100644 --- a/internal/language_features/generic_template_context.go +++ b/internal/language_features/generic_template_context.go @@ -19,6 +19,7 @@ func (f *GenericTemplateContextFeature) getTemplateContext() (lsplocal.TemplateC func (f *GenericTemplateContextFeature) getReferencesFromSymbolTable(templateContext lsplocal.TemplateContext) []lsp.Location { locations := []lsp.Location{} + for _, doc := range f.GenericDocumentUseCase.DocumentStore.GetAllDocs() { referenceRanges := doc.SymbolTable.GetTemplateContextRanges(templateContext) for _, referenceRange := range referenceRanges { @@ -35,5 +36,6 @@ func (f *GenericTemplateContextFeature) builtInOjectDocsLookup(key string, docs return item, nil } } + return helmdocs.HelmDocumentation{}, fmt.Errorf("key %s not found on built-in object", key) } diff --git a/internal/lsp/symbol_table.go b/internal/lsp/symbol_table.go index 9689b53e..741e11c6 100644 --- a/internal/lsp/symbol_table.go +++ b/internal/lsp/symbol_table.go @@ -86,6 +86,7 @@ func (s *SymbolTable) AddTemplateContext(templateContext TemplateContext, pointR // we can just remove it from the template context templateContext = templateContext.Tail() } + s.contexts[templateContext.Format()] = append(s.contexts[templateContext.Format()], pointRange) sliceCopy := make(TemplateContext, len(templateContext)) copy(sliceCopy, templateContext) diff --git a/internal/lsp/symbol_table_template_context_variables.go b/internal/lsp/symbol_table_template_context_variables.go index ed7a428e..7ac139b0 100644 --- a/internal/lsp/symbol_table_template_context_variables.go +++ b/internal/lsp/symbol_table_template_context_variables.go @@ -25,14 +25,18 @@ func (s *SymbolTable) ResolveVariablesInTemplateContext(templateContext Template for _, definition := range variableDefinitions { if util.RangeContainsRange(definition.Scope, pointRange) { - - prefix := NewTemplateContext(definition.Value) - if definition.VariableType == VariableTypeRangeValue && len(prefix) > 0 { - prefix[len(prefix)-1] = prefix[len(prefix)-1] + "[]" - } + prefix := getPrefixTemplateContextForVariable(definition) return s.ResolveVariablesInTemplateContext(append(prefix, templateContext.Tail()...), pointRange) } } return templateContext, fmt.Errorf("variable %s not found", variableName) } + +func getPrefixTemplateContextForVariable(definition VariableDefinition) TemplateContext { + prefix := NewTemplateContext(definition.Value) + if definition.VariableType == VariableTypeRangeValue && len(prefix) > 0 { + prefix[len(prefix)-1] = prefix[len(prefix)-1] + "[]" + } + return prefix +}