From 271cac937b1e9f1ad409e5a7593aab814f245de4 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Fri, 12 Apr 2024 17:55:42 +0200 Subject: [PATCH] usecase selection --- internal/handler/definition.go | 2 +- internal/handler/generic_document_usecase.go | 8 ++-- internal/handler/project_files.go | 41 ------------------ internal/handler/references.go | 26 ++++------- internal/language_features/includes.go | 43 ++++++++++++------- .../language_features/template_context.go | 10 ++++- internal/language_features/usecases.go | 16 +++++++ 7 files changed, 64 insertions(+), 82 deletions(-) delete mode 100644 internal/handler/project_files.go create mode 100644 internal/language_features/usecases.go diff --git a/internal/handler/definition.go b/internal/handler/definition.go index 679fde9b..2e2cac0f 100644 --- a/internal/handler/definition.go +++ b/internal/handler/definition.go @@ -34,7 +34,7 @@ func (h *langHandler) Definition(ctx context.Context, params *lsp.DefinitionPara return result, nil } -func (h *langHandler) definitionAstParsing(genericDocumentUseCase languagefeatures.GenericDocumentUseCase, chart *charts.Chart, doc *lsplocal.Document, position lsp.Position) ([]lsp.Location, error) { +func (h *langHandler) definitionAstParsing(genericDocumentUseCase *languagefeatures.GenericDocumentUseCase, chart *charts.Chart, doc *lsplocal.Document, position lsp.Position) ([]lsp.Location, error) { var ( relevantChildNode = genericDocumentUseCase.Node parentNode = relevantChildNode.Parent() diff --git a/internal/handler/generic_document_usecase.go b/internal/handler/generic_document_usecase.go index 8b4b2e77..4f8c6783 100644 --- a/internal/handler/generic_document_usecase.go +++ b/internal/handler/generic_document_usecase.go @@ -9,10 +9,10 @@ import ( lsp "go.lsp.dev/protocol" ) -func (h *langHandler) NewGenericDocumentUseCase(params lsp.TextDocumentPositionParams) (languagefeatures.GenericDocumentUseCase, error) { +func (h *langHandler) NewGenericDocumentUseCase(params lsp.TextDocumentPositionParams) (*languagefeatures.GenericDocumentUseCase, error) { doc, ok := h.documents.Get(params.TextDocument.URI) if !ok { - return languagefeatures.GenericDocumentUseCase{}, errors.New("Could not get document: " + params.TextDocument.URI.Filename()) + return &languagefeatures.GenericDocumentUseCase{}, errors.New("Could not get document: " + params.TextDocument.URI.Filename()) } chart, err := h.chartStore.GetChartForDoc(params.TextDocument.URI) if err != nil { @@ -20,14 +20,14 @@ func (h *langHandler) NewGenericDocumentUseCase(params lsp.TextDocumentPositionP } node := h.getNode(doc, params.Position) if node == nil { - return languagefeatures.GenericDocumentUseCase{}, errors.New("Could not get node for: " + params.TextDocument.URI.Filename()) + return &languagefeatures.GenericDocumentUseCase{}, errors.New("Could not get node for: " + params.TextDocument.URI.Filename()) } parentNode := node.Parent() var parentNodeType string if parentNode != nil { parentNodeType = parentNode.Type() } - return languagefeatures.GenericDocumentUseCase{ + return &languagefeatures.GenericDocumentUseCase{ Document: doc, DocumentStore: h.documents, Chart: chart, diff --git a/internal/handler/project_files.go b/internal/handler/project_files.go deleted file mode 100644 index 3816c178..00000000 --- a/internal/handler/project_files.go +++ /dev/null @@ -1,41 +0,0 @@ -package handler - -import ( - "errors" - "os" - "path/filepath" - - "github.com/mrjosh/helm-ls/pkg/chartutil" - lsp "go.lsp.dev/protocol" - "go.lsp.dev/uri" -) - -type ProjectFiles struct { - ValuesFile string - ChartFile string - RootURI uri.URI -} - -func (p ProjectFiles) GetValuesFileURI() lsp.DocumentURI { - return "file://" + lsp.DocumentURI(p.ValuesFile) -} -func (p ProjectFiles) GetChartFileURI() lsp.DocumentURI { - return "file://" + lsp.DocumentURI(p.ChartFile) -} - -func NewProjectFiles(rootURI uri.URI, valuesFileName string) ProjectFiles { - - if valuesFileName == "" { - valuesFileName = chartutil.ValuesfileName - } - valuesFileName = filepath.Join(rootURI.Filename(), valuesFileName) - if _, err := os.Stat(valuesFileName); errors.Is(err, os.ErrNotExist) { - valuesFileName = filepath.Join(rootURI.Filename(), "values.yml") - } - - return ProjectFiles{ - ValuesFile: valuesFileName, - ChartFile: filepath.Join(rootURI.Filename(), chartutil.ChartfileName), - RootURI: rootURI, - } -} diff --git a/internal/handler/references.go b/internal/handler/references.go index a7cc0843..80477444 100644 --- a/internal/handler/references.go +++ b/internal/handler/references.go @@ -4,7 +4,6 @@ import ( "context" languagefeatures "github.com/mrjosh/helm-ls/internal/language_features" - "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" lsp "go.lsp.dev/protocol" ) @@ -14,26 +13,17 @@ func (h *langHandler) References(ctx context.Context, params *lsp.ReferenceParam return nil, err } - parentNode := genericDocumentUseCase.Node.Parent() - pt := parentNode.Type() - ct := genericDocumentUseCase.Node.Type() - - logger.Println("pt", pt, "ct", ct) - logger.Println(genericDocumentUseCase.NodeContent()) - - if pt == gotemplate.NodeTypeDefineAction && ct == gotemplate.NodeTypeInterpretedStringLiteral { - includesDefinitionFeature := languagefeatures.NewIncludesDefinitionFeature(genericDocumentUseCase) - return includesDefinitionFeature.References() + usecases := []languagefeatures.ReferencesUseCase{ + languagefeatures.NewIncludesDefinitionFeature(genericDocumentUseCase), + languagefeatures.NewIncludesCallFeature(genericDocumentUseCase), + languagefeatures.NewValuesFeature(genericDocumentUseCase), } - if pt == gotemplate.NodeTypeArgumentList { - includesCallFeature := languagefeatures.NewIncludesCallFeature(genericDocumentUseCase) - return includesCallFeature.References() + for _, usecase := range usecases { + if usecase.AppropriateForNode(genericDocumentUseCase.NodeType, genericDocumentUseCase.ParentNodeType, genericDocumentUseCase.Node) { + return usecase.References() + } } - if (pt == gotemplate.NodeTypeField && ct == gotemplate.NodeTypeIdentifier) || ct == gotemplate.NodeTypeFieldIdentifier || ct == gotemplate.NodeTypeField { - valuesFeature := languagefeatures.NewValuesFeature(genericDocumentUseCase) - return valuesFeature.References() - } return nil, nil } diff --git a/internal/language_features/includes.go b/internal/language_features/includes.go index f1640950..0168392a 100644 --- a/internal/language_features/includes.go +++ b/internal/language_features/includes.go @@ -4,32 +4,47 @@ import ( lsp "go.lsp.dev/protocol" lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" "github.com/mrjosh/helm-ls/internal/util" + sitter "github.com/smacker/go-tree-sitter" ) type IncludesFeature struct { - GenericDocumentUseCase + *GenericDocumentUseCase } -// should be called on {{ include "name" . }} type IncludesCallFeature struct { - IncludesFeature + *IncludesFeature +} + +// should be called on {{ include "name" . }} +func (f *IncludesCallFeature) AppropriateForNode(currentNodeType string, parentNodeType string, node *sitter.Node) bool { + if parentNodeType != gotemplate.NodeTypeArgumentList { + return false + } + functionCallNode := node.Parent().Parent() + _, err := lsplocal.ParseIncludeFunctionCall(functionCallNode, []byte(f.GenericDocumentUseCase.Document.Content)) + return err == nil } -// should be called on {{ define "name" }} type IncludesDefinitionFeature struct { - IncludesFeature + *IncludesFeature } -func NewIncludesCallFeature(genericDocumentUseCase GenericDocumentUseCase) *IncludesCallFeature { +// should be called on {{ define "name" }} +func (f *IncludesDefinitionFeature) AppropriateForNode(currentNodeType string, parentNodeType string, node *sitter.Node) bool { + return parentNodeType == gotemplate.NodeTypeDefineAction && currentNodeType == gotemplate.NodeTypeInterpretedStringLiteral +} + +func NewIncludesCallFeature(genericDocumentUseCase *GenericDocumentUseCase) *IncludesCallFeature { return &IncludesCallFeature{ - IncludesFeature: IncludesFeature{genericDocumentUseCase}, + IncludesFeature: &IncludesFeature{genericDocumentUseCase}, } } -func NewIncludesDefinitionFeature(genericDocumentUseCase GenericDocumentUseCase) *IncludesDefinitionFeature { +func NewIncludesDefinitionFeature(genericDocumentUseCase *GenericDocumentUseCase) *IncludesDefinitionFeature { return &IncludesDefinitionFeature{ - IncludesFeature: IncludesFeature{genericDocumentUseCase}, + IncludesFeature: &IncludesFeature{genericDocumentUseCase}, } } @@ -39,21 +54,17 @@ func (f *IncludesCallFeature) References() (result []lsp.Location, err error) { return []lsp.Location{}, err } - locations := f.getReferenceLocations(includeName) - return locations, nil + return f.getReferenceLocations(includeName), nil } func (f *IncludesCallFeature) getIncludeName() (string, error) { functionCallNode := f.Node.Parent().Parent() - includeName, err := lsplocal.ParseIncludeFunctionCall(functionCallNode, []byte(f.GenericDocumentUseCase.Document.Content)) - return includeName, err + return lsplocal.ParseIncludeFunctionCall(functionCallNode, []byte(f.GenericDocumentUseCase.Document.Content)) } func (f *IncludesDefinitionFeature) References() (result []lsp.Location, err error) { includeName := util.RemoveQuotes(f.GenericDocumentUseCase.NodeContent()) - - locations := f.getReferenceLocations(includeName) - return locations, nil + return f.getReferenceLocations(includeName), nil } func (f *IncludesFeature) getReferenceLocations(includeName string) []lsp.Location { diff --git a/internal/language_features/template_context.go b/internal/language_features/template_context.go index 8ba7c7f6..20f31789 100644 --- a/internal/language_features/template_context.go +++ b/internal/language_features/template_context.go @@ -6,19 +6,25 @@ import ( lsp "go.lsp.dev/protocol" lsplocal "github.com/mrjosh/helm-ls/internal/lsp" + "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate" "github.com/mrjosh/helm-ls/internal/util" + sitter "github.com/smacker/go-tree-sitter" ) type TemplateContextFeature struct { - GenericDocumentUseCase + *GenericDocumentUseCase } -func NewValuesFeature(genericDocumentUseCase GenericDocumentUseCase) *TemplateContextFeature { +func NewValuesFeature(genericDocumentUseCase *GenericDocumentUseCase) *TemplateContextFeature { return &TemplateContextFeature{ GenericDocumentUseCase: genericDocumentUseCase, } } +func (f *TemplateContextFeature) AppropriateForNode(currentNodeType string, parentNodeType string, node *sitter.Node) bool { + return (parentNodeType == gotemplate.NodeTypeField && currentNodeType == gotemplate.NodeTypeIdentifier) || currentNodeType == gotemplate.NodeTypeFieldIdentifier || currentNodeType == gotemplate.NodeTypeField +} + func (f *TemplateContextFeature) References() (result []lsp.Location, err error) { includeName, err := f.getTemplateContext() if err != nil { diff --git a/internal/language_features/usecases.go b/internal/language_features/usecases.go new file mode 100644 index 00000000..d5b2c801 --- /dev/null +++ b/internal/language_features/usecases.go @@ -0,0 +1,16 @@ +package languagefeatures + +import ( + sitter "github.com/smacker/go-tree-sitter" + lsp "go.lsp.dev/protocol" +) + +// interface for use cases +type UseCase interface { + AppropriateForNode(currentNodeType string, parentNodeType string, node *sitter.Node) bool +} + +type ReferencesUseCase interface { + UseCase + References() (result []lsp.Location, err error) +}