Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Jul 1, 2024
1 parent 21152f0 commit 90ae567
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 20 deletions.
6 changes: 2 additions & 4 deletions internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
15 changes: 15 additions & 0 deletions internal/handler/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 0 additions & 9 deletions internal/handler/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handler
import (
"context"
"os"
"strings"
"testing"

"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
Expand Down Expand Up @@ -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
}
16 changes: 14 additions & 2 deletions internal/language_features/built_in_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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":

Check failure on line 66 in internal/language_features/built_in_objects.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

string `Chart` has 3 occurrences, make it a constant (goconst)
return []lsp.Location{{URI: f.Chart.ChartMetadata.URI}}
Expand All @@ -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
}

Expand All @@ -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
}
2 changes: 2 additions & 0 deletions internal/language_features/generic_template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions internal/lsp/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions internal/lsp/symbol_table_template_context_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 90ae567

Please sign in to comment.