Skip to content

Commit

Permalink
chore: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed May 9, 2024
1 parent a1d2abc commit 6630f69
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion internal/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (h *langHandler) Completion(ctx context.Context, params *lsp.CompletionPara
usecases := []languagefeatures.CompletionUseCase{
languagefeatures.NewTemplateContextFeature(genericDocumentUseCase),
languagefeatures.NewFunctionCallFeature(genericDocumentUseCase),
languagefeatures.NewTextFeature(ctx, genericDocumentUseCase, h.yamllsConnector, params.TextDocumentPositionParams),
languagefeatures.NewTextFeature(ctx, genericDocumentUseCase, h.yamllsConnector, &params.TextDocumentPositionParams),
languagefeatures.NewIncludesCallFeature(genericDocumentUseCase),
}

Expand Down
3 changes: 2 additions & 1 deletion internal/language_features/function_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func NewFunctionCallFeature(genericDocumentUseCase *GenericDocumentUseCase) *Fun
}

func (f *FunctionCallFeature) AppropriateForNode() bool {
return f.NodeType == gotemplate.NodeTypeIdentifier && f.ParentNodeType == gotemplate.NodeTypeFunctionCall
return f.NodeType == gotemplate.NodeTypeIdentifier &&
f.ParentNodeType == gotemplate.NodeTypeFunctionCall
}

func (f *FunctionCallFeature) Hover() (string, error) {
Expand Down
11 changes: 0 additions & 11 deletions internal/language_features/generic_document_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,3 @@ type GenericDocumentUseCase struct {
func (u *GenericDocumentUseCase) NodeContent() string {
return u.Node.Content([]byte(u.Document.Content))
}

// Allows for using a custom node selection
func (u GenericDocumentUseCase) WithNode(node *sitter.Node) *GenericDocumentUseCase {
u.Node = node
u.NodeType = node.Type()
u.ParentNode = node.Parent()
if u.ParentNode != nil {
u.ParentNodeType = u.ParentNode.Type()
}
return &u
}
6 changes: 3 additions & 3 deletions internal/language_features/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type TextFeature struct {
*GenericDocumentUseCase
textDocumentPosition lsp.TextDocumentPositionParams
textDocumentPosition *lsp.TextDocumentPositionParams
ctx context.Context
yamllsConnector *yamlls.Connector
}
Expand All @@ -21,7 +21,7 @@ func NewTextFeature(
ctx context.Context,
genericDocumentUseCase *GenericDocumentUseCase,
yamllsConnector *yamlls.Connector,
textDocumentPosition lsp.TextDocumentPositionParams,
textDocumentPosition *lsp.TextDocumentPositionParams,
) *TextFeature {
return &TextFeature{
GenericDocumentUseCase: genericDocumentUseCase,
Expand All @@ -37,7 +37,7 @@ func (f *TextFeature) AppropriateForNode() bool {

func (f *TextFeature) Completion() (result *lsp.CompletionList, err error) {
comletions := f.yamllsCompletions(&lsp.CompletionParams{
TextDocumentPositionParams: f.textDocumentPosition,
TextDocumentPositionParams: *f.textDocumentPosition,
})

return protocol.CompletionResults{Items: comletions}.WithSnippets(godocs.TextSnippets).ToList(), nil
Expand Down
17 changes: 13 additions & 4 deletions internal/lsp/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ func (t TemplateContext) Tail() TemplateContext {
return t[1:]
}

func (t TemplateContext) IsVariable() bool {
return len(t) > 0 && t[0] == "$"
}

func (t TemplateContext) AppendSuffix(suffix string) TemplateContext {
t[len(t)-1] = t[len(t)-1] + suffix
return t
}

type SymbolTable struct {
contexts map[string][]sitter.Range
contextsReversed map[sitter.Range]TemplateContext
Expand All @@ -26,10 +35,10 @@ type SymbolTable struct {

func NewSymbolTable(ast *sitter.Tree, content []byte) *SymbolTable {
s := &SymbolTable{
contexts: make(map[string][]sitter.Range),
contextsReversed: make(map[sitter.Range]TemplateContext),
includeDefinitions: make(map[string][]sitter.Range),
includeUseages: make(map[string][]sitter.Range),
contexts: map[string][]sitter.Range{},
contextsReversed: map[sitter.Range]TemplateContext{},
includeDefinitions: map[string][]sitter.Range{},
includeUseages: map[string][]sitter.Range{},
}
s.parseTree(ast, content)
return s
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/symbol_table_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ func ParseIncludeFunctionCall(node *sitter.Node, content []byte) (string, error)
return util.RemoveQuotes(firstArgument.Content(content)), nil
}

func (v *IncludeDefinitionsVisitor) Exit(node *sitter.Node) {}
func (v *IncludeDefinitionsVisitor) EnterContextShift(node *sitter.Node, suffix string) {}
func (v *IncludeDefinitionsVisitor) ExitContextShift(node *sitter.Node) {}
func (v *IncludeDefinitionsVisitor) Exit(_ *sitter.Node) {}
func (v *IncludeDefinitionsVisitor) EnterContextShift(_ *sitter.Node, _ string) {}
func (v *IncludeDefinitionsVisitor) ExitContextShift(_ *sitter.Node) {}
11 changes: 6 additions & 5 deletions internal/lsp/symbol_table_template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func (v *TemplateContextVisitor) Enter(node *sitter.Node) {
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
v.StashContext()
}
v.symbolTable.AddTemplateContext(append(getContextForSelectorExpression(operandNode, v.content), ""), GetRangeForNode(node.Child(int(node.ChildCount())-1)))
v.symbolTable.AddTemplateContext(append(getContextForSelectorExpression(operandNode, v.content), ""),
GetRangeForNode(node.Child(int(node.ChildCount())-1)))
case gotemplate.NodeTypeSelectorExpression:
operandNode := node.ChildByFieldName("operand")
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
Expand Down Expand Up @@ -93,10 +94,10 @@ func (v *TemplateContextVisitor) EnterContextShift(node *sitter.Node, suffix str
case gotemplate.NodeTypeSelectorExpression, gotemplate.NodeTypeUnfinishedSelectorExpression:
s := getContextForSelectorExpression(node, v.content)
if len(s) > 0 {
s[len(s)-1] = s[len(s)-1] + suffix
if s[0] == "$" {
s = s.AppendSuffix(suffix)
if s.IsVariable() {
v.StashContext()
s = s[1:]
s = s.Tail()
}
}
v.PushContextMany(s)
Expand All @@ -109,7 +110,7 @@ func (v *TemplateContextVisitor) ExitContextShift(node *sitter.Node) {
v.PopContext()
case gotemplate.NodeTypeSelectorExpression, gotemplate.NodeTypeUnfinishedSelectorExpression:
s := getContextForSelectorExpression(node, v.content)
if len(s) > 0 && s[0] == "$" {
if s.IsVariable() {
v.RestoreStashedContext()
} else {
v.PopContextN(len(s))
Expand Down
4 changes: 0 additions & 4 deletions internal/protocol/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ func (h HoverResultsWithFiles) Format(rootURI uri.URI) string {
return formatted
}

func (h *HoverResultsWithFiles) Add(hoverResult HoverResultWithFile) {
*h = append(*h, hoverResult)
}

func BuildHoverResponse(value string, wordRange lsp.Range) *lsp.Hover {
content := lsp.MarkupContent{
Kind: lsp.Markdown,
Expand Down

0 comments on commit 6630f69

Please sign in to comment.