Skip to content

Commit

Permalink
[FIX] yaml trimming for if else works now
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Nov 1, 2023
1 parent 8496d3a commit 702c7fd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
41 changes: 19 additions & 22 deletions internal/adapter/yamlls/trimTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,27 @@ func prettyPrintNode(node *sitter.Node, previous []byte, result []byte) {

switch node.Type() {
case "if_action":
for i := 0; i < int(childCount); i++ {
logger.Debug("FieldName", node.FieldNameForChild(i))
child := node.Child(i)
logger.Println("FieldNameForChild in in_action: ", node.FieldNameForChild(i))

if child.Type() == "end" {
earaseTemplate(child, previous, result)
earaseTemplate(child.NextSibling(), previous, result)
earaseTemplate(child.PrevSibling(), previous, result)
break
} else if "condition" == node.FieldNameForChild(i) {
if_action_condition := child
if_action_condition_content := child.Content(previous)
logger.Println("if_action_condition_content: ", if_action_condition_content)
earaseTemplate(if_action_condition, previous, result)
earaseTemplate(if_action_condition.NextSibling(), previous, result)
if if_action_condition.PrevSibling() != nil && if_action_condition.PrevSibling().Type() == "if" {
earaseTemplate(if_action_condition.PrevSibling(), previous, result)
earaseTemplate(if_action_condition.PrevSibling().PrevSibling(), previous, result)
}
} else {
prettyPrintNode(child, previous, result)
curser := sitter.NewTreeCursor(node)
curser.GoToFirstChild()
for curser.GoToNextSibling() {
if curser.CurrentFieldName() == "condition" {
earaseTemplate(curser.CurrentNode(), previous, result)
earaseTemplate(curser.CurrentNode().NextSibling(), previous, result)
continue
}
switch curser.CurrentNode().Type() {
case "if", "else if":
earaseTemplate(curser.CurrentNode(), previous, result)
earaseTemplate(curser.CurrentNode().PrevSibling(), previous, result)
case "end":
earaseTemplate(curser.CurrentNode(), previous, result)
earaseTemplate(curser.CurrentNode().PrevSibling(), previous, result)
earaseTemplate(curser.CurrentNode().NextSibling(), previous, result)
default:
prettyPrintNode(curser.CurrentNode(), previous, result)
}
}
curser.Close()

case "block_action", "with_action", "range_action":
for i := 0; i < int(childCount); i++ {
Expand Down
36 changes: 36 additions & 0 deletions internal/adapter/yamlls/trimTemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,42 @@ metadata:
`,
},
{
documentText: `
{{ if eq .Values.replicaCout 1 }}
{{- $kube := "" -}}
apiVersion: v1
kind: Service
bka: dsa
metadata:
name: {{ include "hello-world.fullname" . }}
labels:
{{- include "hello-world.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
{{ end }}
`,
trimmedText: `
apiVersion: v1
kind: Service
bka: dsa
metadata:
name: {{ include "hello-world.fullname" . }}
labels:
{{- include "hello-world.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
`,
},
}

func TestTrimTemplate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/ast_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func IsInElseBranch(node *sitter.Node) bool {
}
}
}

curser.Close()
}
return IsInElseBranch(parent)
}
Expand Down

0 comments on commit 702c7fd

Please sign in to comment.