Skip to content

Commit

Permalink
not working refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Apr 5, 2024
1 parent 925f4be commit 4489389
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
13 changes: 2 additions & 11 deletions internal/lsp/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,11 @@ func (s *SymbolTable) GetIncludeReference(symbol string) ([]sitter.Range, bool)

func (s *SymbolTable) parseTree(ast *sitter.Tree, content []byte) {
rootNode := ast.RootNode()

v := Visitors{
symbolTable: s,
visitors: []Visitor{
&ValuesVisitor{
currentContext: []string{},
stashedContext: [][]string{},
symbolTable: s,
content: content,
},
&IncludeDefinitionsVisitor{
symbolTable: s,
content: content,
},
NewValuesVisitor(s, content),
NewIncludeDefinitionsVisitor(s, content),
},
}

Expand Down
7 changes: 7 additions & 0 deletions internal/lsp/symbol_table_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ type IncludeDefinitionsVisitor struct {
content []byte
}

func NewIncludeDefinitionsVisitor(symbolTable *SymbolTable, content []byte) *IncludeDefinitionsVisitor {
return &IncludeDefinitionsVisitor{
symbolTable: symbolTable,
content: content,
}
}

func (v *IncludeDefinitionsVisitor) Enter(node *sitter.Node) {
if node.Type() == gotemplate.NodeTypeDefineAction {
content := node.ChildByFieldName("name").Content(v.content)
Expand Down
43 changes: 43 additions & 0 deletions internal/lsp/symbol_table_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,41 @@ type ValuesVisitor struct {
content []byte
}

func NewValuesVisitor(symbolTable *SymbolTable, content []byte) *ValuesVisitor {
return &ValuesVisitor{
currentContext: []string{},
stashedContext: [][]string{},
symbolTable: symbolTable,
content: content,
}
}

func (v *ValuesVisitor) PushContext(context string) {
v.currentContext = append(v.currentContext, context)
}

func (v *ValuesVisitor) PushContextMany(context []string) {
v.currentContext = append(v.currentContext, context...)
}

func (v *ValuesVisitor) PopContext() {
v.currentContext = v.currentContext[:len(v.currentContext)-1]
}

func (v *ValuesVisitor) PopContextN(n int) {
v.currentContext = v.currentContext[:len(v.currentContext)-n]
}

func (v *ValuesVisitor) StashContext() {
v.stashedContext = append(v.stashedContext, v.currentContext)
v.currentContext = []string{}
}

func (v *ValuesVisitor) RestoreStashedContext() {
v.currentContext = v.stashedContext[len(v.stashedContext)-1]
v.stashedContext = v.stashedContext[:len(v.stashedContext)-1]
}

func (v *ValuesVisitor) Enter(node *sitter.Node) {
switch node.Type() {
case gotemplate.NodeTypeDot:
Expand All @@ -27,6 +62,7 @@ func (v *ValuesVisitor) Enter(node *sitter.Node) {
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
v.stashedContext = append(v.stashedContext, v.currentContext)
v.currentContext = []string{}
// v.StashContext()
}
}
}
Expand All @@ -38,6 +74,7 @@ func (v *ValuesVisitor) Exit(node *sitter.Node) {
if operandNode.Type() == gotemplate.NodeTypeVariable && operandNode.Content(v.content) == "$" {
v.currentContext = v.stashedContext[len(v.stashedContext)-1]
v.stashedContext = v.stashedContext[:len(v.stashedContext)-1]
// v.RestoreStashedContext()
}
}
}
Expand All @@ -50,32 +87,38 @@ func (v *ValuesVisitor) EnterContextShift(node *sitter.Node, suffix string) {
case gotemplate.NodeTypeField:
content := node.ChildByFieldName("name").Content(v.content) + suffix
v.currentContext = append(v.currentContext, content)
// v.PushContext(content)
case gotemplate.NodeTypeSelectorExpression:
s := getContextForSelectorExpression(node, v.content)
if len(s) > 0 {
s[len(s)-1] = s[len(s)-1] + suffix
if s[0] == "$" {
v.stashedContext = append(v.stashedContext, v.currentContext)
v.currentContext = []string{}
// v.StashContext()
s = s[1:]
}
}
v.currentContext = append(v.currentContext, s...)
// v.PushContextMany(s)
}
}

func (v *ValuesVisitor) ExitContextShift(node *sitter.Node) {
switch node.Type() {
case gotemplate.NodeTypeField, gotemplate.NodeTypeFieldIdentifier:
v.currentContext = v.currentContext[:len(v.currentContext)-1]
// v.PopContext()
case gotemplate.NodeTypeSelectorExpression:
s := getContextForSelectorExpression(node, v.content)
if len(s) > 0 && s[0] == "$" {
v.currentContext = v.stashedContext[len(v.stashedContext)-1]
v.stashedContext = v.stashedContext[:len(v.stashedContext)-1]
s = s[1:]

Check failure on line 117 in internal/lsp/symbol_table_values.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

SA4006: this value of `s` is never used (staticcheck)
// v.RestoreStashedContext()
} else {
v.currentContext = v.currentContext[:len(v.currentContext)-len(s)]
// v.PopContextN(len(s))
}
}
}
Expand Down

0 comments on commit 4489389

Please sign in to comment.