Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ast): incremental parsing of treesitter ast #91

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
@@ -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())
@@ -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)
}

17 changes: 16 additions & 1 deletion internal/lsp/document.go
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions internal/lsp/document_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

Unchanged files with check annotations Beta

)
// ApplyEdit implements protocol.Client.
func (y Connector) ApplyEdit(ctx context.Context, params *protocol.ApplyWorkspaceEditParams) (result bool, err error) {

Check failure on line 10 in internal/adapter/yamlls/client.go

GitHub Actions / lint (1.21.5, ubuntu-latest)

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return true, nil
}
// LogMessage implements protocol.Client.
func (y Connector) LogMessage(ctx context.Context, params *protocol.LogMessageParams) (err error) {

Check failure on line 15 in internal/adapter/yamlls/client.go

GitHub Actions / lint (1.21.5, ubuntu-latest)

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return nil
}
// Progress implements protocol.Client.
func (y Connector) Progress(ctx context.Context, params *protocol.ProgressParams) (err error) {

Check failure on line 20 in internal/adapter/yamlls/client.go

GitHub Actions / lint (1.21.5, ubuntu-latest)

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return nil
}
}
go func() {
io.Copy(os.Stderr, strderr)

Check failure on line 67 in internal/adapter/yamlls/yamlls.go

GitHub Actions / lint (1.21.5, ubuntu-latest)

Error return value of `io.Copy` is not checked (errcheck)
}()
yamllsConnector := Connector{documents: documents, config: yamllsConfiguration, client: client}
locations := []lsp.Location{}
switch templateContext[0] {
case "Values":

Check failure on line 60 in internal/language_features/built_in_objects.go

GitHub Actions / lint (1.21.5, ubuntu-latest)

string `Values` has 4 occurrences, make it a constant (goconst)
for _, valueFile := range f.Chart.ValuesFiles.AllValuesFiles() {
locations = append(locations, lsp.Location{URI: valueFile.URI})
}
return locations
case "Chart":

Check failure on line 66 in internal/language_features/built_in_objects.go

GitHub Actions / lint (1.21.5, ubuntu-latest)

string `Chart` has 3 occurrences, make it a constant (goconst)
return []lsp.Location{{URI: f.Chart.ChartMetadata.URI}}
}
return sitter.Point{Row: position.Line, Column: position.Character}
}
func RangeToLocation(URI uri.URI, range_ sitter.Range) lsp.Location {

Check failure on line 17 in internal/util/points.go

GitHub Actions / lint (1.21.5, ubuntu-latest)

var-naming: don't use underscores in Go names; func parameter range_ should be range (revive)
return lsp.Location{
URI: URI,
Range: lsp.Range{