Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Mar 4, 2024
1 parent 6403cf4 commit f692f2d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions internal/lsp/yaml_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ import (
"github.com/smacker/go-tree-sitter/yaml"
)

func getRangeForNode(node *sitter.Node) sitter.Range {
return sitter.Range{
StartPoint: node.StartPoint(),
EndPoint: node.EndPoint(),
StartByte: node.StartByte(),
EndByte: node.EndByte(),
}
}

func getTextNodeRanges(gotemplateNode *sitter.Node) []sitter.Range {
textNodes := []sitter.Range{}

for i := 0; i < int(gotemplateNode.ChildCount()); i++ {
child := gotemplateNode.Child(i)
if child.Type() == gotemplate.NodeTypeText {
textNodes = append(textNodes, sitter.Range{
StartPoint: child.StartPoint(),
EndPoint: child.EndPoint(),
StartByte: child.StartByte(),
EndByte: child.EndByte(),
})
textNodes = append(textNodes, getRangeForNode(child))
} else {
textNodes = append(textNodes, getTextNodeRanges(child)...)
}
Expand All @@ -39,10 +43,13 @@ func ParseYamlAst(gotemplateTree *sitter.Tree, content string) *sitter.Tree {
func TrimTemplate(gotemplateTree *sitter.Tree, content string) string {
ranges := getTextNodeRanges(gotemplateTree.RootNode())
result := make([]byte, len(content))
for i := 0; i < len(result); i++ {
for i := range result {
if content[i] == '\n' {
result[i] = byte('\n')
continue
} else if content[i] == '\r' {
result[i] = byte('\r')
continue
}
result[i] = byte(' ')
}
Expand Down

0 comments on commit f692f2d

Please sign in to comment.