Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/lsp/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
lsp "go.lsp.dev/protocol"
)

// TODO: use byte instead of string
func ParseAst(oldTree *sitter.Tree, content string) *sitter.Tree {
parser := sitter.NewParser()
parser.SetLanguage(gotemplate.GetLanguage())
Expand Down Expand Up @@ -69,7 +70,8 @@ func isPointLargerOrEq(a sitter.Point, b sitter.Point) bool {
return a.Row > b.Row
}

func (d *Document) ApplyChangesToAst(newContent string) {
func (d *Document) ApplyChangesToAst(editInput sitter.EditInput, newContent string) {
d.Ast.Edit(editInput)
d.Ast = ParseAst(nil, newContent)
}

Expand Down
17 changes: 16 additions & 1 deletion internal/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,30 @@ func (d *Document) ApplyChanges(changes []lsp.TextDocumentContentChangeEvent) {
for _, change := range changes {
start, end := util.PositionToIndex(change.Range.Start, content), util.PositionToIndex(change.Range.End, content)

newEnd := start + len(change.Text)

var buf bytes.Buffer

buf.Write(content[:start])
buf.Write([]byte(change.Text))
buf.Write(content[end:])

content = buf.Bytes()

editInput := sitter.EditInput{
StartIndex: uint32(start),
OldEndIndex: uint32(end),
NewEndIndex: uint32(newEnd),
StartPoint: util.PositionToPoint(change.Range.Start),
OldEndPoint: util.PositionToPoint(change.Range.End),
NewEndPoint: util.PositionToPoint(lsp.Position{Line: change.Range.Start.Line, Character: change.Range.Start.Character + uint32(len(change.Text))}),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably won't work for multiline changes

}

d.Ast.Edit(editInput)
}
d.Content = string(content)

d.ApplyChangesToAst(d.Content)
d.Ast = ParseAst(d.Ast, string(content))
d.SymbolTable = NewSymbolTable(d.Ast, []byte(d.Content))

d.lines = nil
Expand Down
52 changes: 52 additions & 0 deletions internal/lsp/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,55 @@ func TestDocumentStore(t *testing.T) {
assert.NotNil(doc)
assert.True(ok)
}

func TestApplyChanges(t *testing.T) {
assert := assert.New(t)

documentStore := NewDocumentStore()
documentStore.DidOpen(&protocol.DidOpenTextDocumentParams{
TextDocument: protocol.TextDocumentItem{
URI: uri.File("test.yaml"),
LanguageID: "helm",
Text: `{{ .Values.test }}`,
},
}, util.DefaultConfig)

doc, ok := documentStore.Get(uri.File("test.yaml"))
assert.True(ok)
assert.Equal("{{ .Values.test }}", doc.Content)

doc.ApplyChanges([]protocol.TextDocumentContentChangeEvent{
{Range: protocol.Range{Start: protocol.Position{Line: 0, Character: 18}, End: protocol.Position{Line: 0, Character: 18}}, Text: "\n"},
{Range: protocol.Range{Start: protocol.Position{Line: 1, Character: 0}, End: protocol.Position{Line: 1, Character: 0}}, Text: "\n"},
{Range: protocol.Range{Start: protocol.Position{Line: 1, Character: 0}, End: protocol.Position{Line: 1, Character: 0}}, Text: "spec:\n replicas: {{ .Values.replicaCount }}\n selector:\n matchLabels:\n {{- include \"hello-world.selectorLabels\" . | nindent 6 }}\n template:\n metadata:\n labels:"},
{Range: protocol.Range{Start: protocol.Position{Line: 8, Character: 13}, End: protocol.Position{Line: 9, Character: 0}}, Text: "\n \n"},
{Range: protocol.Range{Start: protocol.Position{Line: 9, Character: 6}, End: protocol.Position{Line: 9, Character: 0}}, Text: "{{- if .Values.serviceAccount.create -}}\napiVersion: v1\nkind: ServiceAccount\nmetadata:\n name: {{ include \"hello-world.serviceAccountName\" . }}\n labels:\n {{- include \"hello-world.labels\" . | nindent 4 }}\n {{- with .Values.serviceAccount.annotations }}\n annotations:\n {{- toYaml . | nindent 4 }}\n {{- end }}\n{{- end }}"},
{Range: protocol.Range{Start: protocol.Position{Line: 17, Character: 0}, End: protocol.Position{Line: 17, Character: 0}}, Text: ""},
{Range: protocol.Range{Start: protocol.Position{Line: 18, Character: 0}, End: protocol.Position{Line: 19, Character: 0}}, Text: ""},
})

print(doc.Content)
expected := `{{ .Values.test }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "hello-world.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "hello-world.serviceAccountName" . }}
labels:
{{- include "hello-world.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- end }}
{{- end }}
`

assert.Equal(expected, doc.Content)
}