Skip to content

Commit

Permalink
usecase selection
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Apr 12, 2024
1 parent 75bed53 commit 271cac9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 82 deletions.
2 changes: 1 addition & 1 deletion internal/handler/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check failure on line 37 in internal/handler/definition.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

unused-parameter: parameter 'position' seems to be unused, consider removing or renaming it as _ (revive)
var (
relevantChildNode = genericDocumentUseCase.Node
parentNode = relevantChildNode.Parent()
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/generic_document_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ 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 {
logger.Error("Error getting chart info for file", params.TextDocument.URI, err)
}
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,
Expand Down
41 changes: 0 additions & 41 deletions internal/handler/project_files.go

This file was deleted.

26 changes: 8 additions & 18 deletions internal/handler/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
43 changes: 27 additions & 16 deletions internal/language_features/includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 21 in internal/language_features/includes.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

unused-parameter: parameter 'currentNodeType' seems to be unused, consider removing or renaming it as _ (revive)
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},
}
}

Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions internal/language_features/template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions internal/language_features/usecases.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 271cac9

Please sign in to comment.