Skip to content

Commit

Permalink
fix: move some code
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed May 4, 2024
1 parent a04a38f commit 2bf9763
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
55 changes: 0 additions & 55 deletions internal/language_features/template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/protocol"
"github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/mrjosh/helm-ls/pkg/chart"
Expand Down Expand Up @@ -122,57 +121,3 @@ func (f *TemplateContextFeature) getMetadataField(v *chart.Metadata, fieldName s
field := reflect.Indirect(r).FieldByName(fieldName)
return util.FormatToYAML(field, fieldName)
}

func (f *TemplateContextFeature) Completion() (result *lsp.CompletionList, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
return nil, err
}

if len(templateContext) == 0 {
result := helmdocs.BuiltInObjects
return protocol.NewCompletionResults(result).ToLSP(), nil
}

if len(templateContext) == 1 {
result, ok := helmdocs.BuiltInOjectVals[templateContext[0]]
if !ok {
result := helmdocs.BuiltInObjects
return protocol.NewCompletionResults(result).ToLSP(), nil
}
return protocol.NewCompletionResults(result).ToLSP(), nil
}

switch templateContext[0] {
case "Values":
return f.valuesCompletion(templateContext)
case "Chart", "Release", "Files", "Capabilities", "Template":
// TODO: make this more fine, by checking the length
result, ok := helmdocs.BuiltInOjectVals[templateContext[0]]
if !ok {
result := helmdocs.BuiltInObjects
return protocol.NewCompletionResults(result).ToLSP(), nil
}
return protocol.NewCompletionResults(result).ToLSP(), nil

}

return nil, nil
}

func (f *TemplateContextFeature) valuesCompletion(templateContext lsplocal.TemplateContext) (*lsp.CompletionList, error) {
m := make(map[string]lsp.CompletionItem)
for _, queriedValuesFiles := range f.Chart.ResolveValueFiles(templateContext.Tail(), f.ChartStore) {
for _, valuesFile := range queriedValuesFiles.ValuesFiles.AllValuesFiles() {
for _, item := range util.GetValueCompletion(valuesFile.Values, queriedValuesFiles.Selector) {
m[item.InsertText] = item
}
}
}
completions := []lsp.CompletionItem{}
for _, item := range m {
completions = append(completions, item)
}

return &lsp.CompletionList{Items: completions, IsIncomplete: false}, nil
}
63 changes: 63 additions & 0 deletions internal/language_features/template_context_completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package languagefeatures

import (
helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/protocol"
"github.com/mrjosh/helm-ls/internal/util"
lsp "go.lsp.dev/protocol"
)

func (f *TemplateContextFeature) Completion() (result *lsp.CompletionList, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
return nil, err
}

if len(templateContext) == 0 {
result := helmdocs.BuiltInObjects
return protocol.NewCompletionResults(result).ToLSP(), nil
}

if len(templateContext) == 1 {
result, ok := helmdocs.BuiltInOjectVals[templateContext[0]]
if !ok {
result := helmdocs.BuiltInObjects
return protocol.NewCompletionResults(result).ToLSP(), nil
}
return protocol.NewCompletionResults(result).ToLSP(), nil
}

switch templateContext[0] {
case "Values":
return f.valuesCompletion(templateContext)
case "Chart", "Release", "Files", "Capabilities", "Template":
// TODO: make this more fine, by checking the length
result, ok := helmdocs.BuiltInOjectVals[templateContext[0]]
if !ok {
result := helmdocs.BuiltInObjects
return protocol.NewCompletionResults(result).ToLSP(), nil
}
return protocol.NewCompletionResults(result).ToLSP(), nil

}

return nil, nil
}

func (f *TemplateContextFeature) valuesCompletion(templateContext lsplocal.TemplateContext) (*lsp.CompletionList, error) {
m := make(map[string]lsp.CompletionItem)
for _, queriedValuesFiles := range f.Chart.ResolveValueFiles(templateContext.Tail(), f.ChartStore) {
for _, valuesFile := range queriedValuesFiles.ValuesFiles.AllValuesFiles() {
for _, item := range util.GetValueCompletion(valuesFile.Values, queriedValuesFiles.Selector) {
m[item.InsertText] = item
}
}
}
completions := []lsp.CompletionItem{}
for _, item := range m {
completions = append(completions, item)
}

return &lsp.CompletionList{Items: completions, IsIncomplete: false}, nil
}
File renamed without changes.

0 comments on commit 2bf9763

Please sign in to comment.