From 702c7fd0473ca10ab682d95098a32f8c0eff9d33 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Wed, 1 Nov 2023 13:01:31 +0100 Subject: [PATCH] [FIX] yaml trimming for if else works now --- internal/adapter/yamlls/trimTemplate.go | 41 +++++++++----------- internal/adapter/yamlls/trimTemplate_test.go | 36 +++++++++++++++++ internal/lsp/ast_diagnostics.go | 2 +- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/internal/adapter/yamlls/trimTemplate.go b/internal/adapter/yamlls/trimTemplate.go index 4697390c..ebf0f1df 100644 --- a/internal/adapter/yamlls/trimTemplate.go +++ b/internal/adapter/yamlls/trimTemplate.go @@ -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++ { diff --git a/internal/adapter/yamlls/trimTemplate_test.go b/internal/adapter/yamlls/trimTemplate_test.go index 8df2d77f..6607ab40 100644 --- a/internal/adapter/yamlls/trimTemplate_test.go +++ b/internal/adapter/yamlls/trimTemplate_test.go @@ -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) { diff --git a/internal/lsp/ast_diagnostics.go b/internal/lsp/ast_diagnostics.go index de9b96b1..8c53cc92 100644 --- a/internal/lsp/ast_diagnostics.go +++ b/internal/lsp/ast_diagnostics.go @@ -20,7 +20,7 @@ func IsInElseBranch(node *sitter.Node) bool { } } } - + curser.Close() } return IsInElseBranch(parent) }