-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move more template context implementions to usecases
- Loading branch information
Showing
12 changed files
with
191 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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/tree-sitter/gotemplate" | ||
sitter "github.com/smacker/go-tree-sitter" | ||
lsp "go.lsp.dev/protocol" | ||
) | ||
|
||
type BuiltInObjectsFeature struct { | ||
*GenericTemplateContextFeature | ||
} | ||
|
||
func NewBuiltInObjectsFeature(genericDocumentUseCase *GenericDocumentUseCase) *BuiltInObjectsFeature { | ||
return &BuiltInObjectsFeature{ | ||
GenericTemplateContextFeature: &GenericTemplateContextFeature{genericDocumentUseCase}, | ||
} | ||
} | ||
|
||
func (f *BuiltInObjectsFeature) AppropriateForNode(currentNodeType string, parentNodeType string, node *sitter.Node) bool { | ||
if !(parentNodeType == gotemplate.NodeTypeField && currentNodeType == gotemplate.NodeTypeIdentifier) && currentNodeType != gotemplate.NodeTypeDot && currentNodeType != gotemplate.NodeTypeDotSymbol { | ||
return false | ||
} | ||
|
||
allowedBuiltIns := []string{"Chart", "Values"} | ||
|
||
templateContext, _ := f.getTemplateContext() | ||
if 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, err := f.getTemplateContext() | ||
if err != nil { | ||
return []lsp.Location{}, err | ||
} | ||
|
||
locations := f.getReferencesFromSymbolTable(templateContext) | ||
return append(locations, f.getDefinitionLocations(templateContext)...), err | ||
} | ||
|
||
func (f *BuiltInObjectsFeature) getDefinitionLocations(templateContext lsplocal.TemplateContext) []lsp.Location { | ||
locations := []lsp.Location{} | ||
|
||
switch templateContext[0] { | ||
case "Values": | ||
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}} | ||
} | ||
|
||
return []lsp.Location{} | ||
} | ||
|
||
func (f *BuiltInObjectsFeature) Hover() (string, error) { | ||
templateContext, _ := f.getTemplateContext() | ||
docs, error := f.builtInOjectDocsLookup(templateContext[0], helmdocs.BuiltInObjects) | ||
return docs.Doc, error | ||
} | ||
|
||
func (f *BuiltInObjectsFeature) Definition() (result []lsp.Location, err error) { | ||
templateContext, err := f.getTemplateContext() | ||
if err != nil { | ||
return []lsp.Location{}, err | ||
} | ||
return f.getDefinitionLocations(templateContext), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package languagefeatures | ||
|
||
import ( | ||
"fmt" | ||
|
||
helmdocs "github.com/mrjosh/helm-ls/internal/documentation/helm" | ||
lsplocal "github.com/mrjosh/helm-ls/internal/lsp" | ||
"github.com/mrjosh/helm-ls/internal/util" | ||
lsp "go.lsp.dev/protocol" | ||
) | ||
|
||
type GenericTemplateContextFeature struct { | ||
*GenericDocumentUseCase | ||
} | ||
|
||
func (f *GenericTemplateContextFeature) getTemplateContext() (lsplocal.TemplateContext, error) { | ||
templateContext := f.GenericDocumentUseCase.Document.SymbolTable.GetTemplateContext(lsplocal.GetRangeForNode(f.Node)) | ||
if len(templateContext) == 0 || templateContext == nil { | ||
return lsplocal.TemplateContext{}, fmt.Errorf("no template context found") | ||
} | ||
return templateContext, nil | ||
} | ||
|
||
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 { | ||
locations = append(locations, util.RangeToLocation(doc.URI, referenceRange)) | ||
} | ||
} | ||
|
||
return locations | ||
} | ||
|
||
func (f *GenericTemplateContextFeature) builtInOjectDocsLookup(key string, docs []helmdocs.HelmDocumentation) (helmdocs.HelmDocumentation, error) { | ||
for _, item := range docs { | ||
if key == item.Name { | ||
return item, nil | ||
} | ||
} | ||
return helmdocs.HelmDocumentation{}, fmt.Errorf("key %s not found on built-in object", key) | ||
} |
Oops, something went wrong.