ngalaiko / tree-sitter-go-template

Golang template grammar for tree-sitter
MIT License
74 stars 27 forks source link

Better support for yaml injections #4

Closed baptman21 closed 9 months ago

baptman21 commented 10 months ago

Currently the grammar generates a lot of individual (text) blocks that if you use the injections are parsed separately. This means that the following will parse two texts, causing in both cases a color problem

text: "{{ .Values.text }}"
another: 42

Ignoring the template part, the text is parsed into two texts blocks :

text: "

And

"
another: 42

With the injection.combined option of tree-sitter all the text is passed to yaml fused, providing a better render.

To improve the perfs the grammar was also slightly modified to fuse text blocks that follow each other into one. The following

text: {}

Will now be parsed as:

(template
  (text))

The tests have been updated for this case.

Edit: turns out that my optimization for the (text) node did not help because the main perf problem I was experiencing was due to the incremental selection of tree sitter. If you put everything on the same node the incremental selection is very slow. To fix this for helm files mainly I added a subblock to split text around the : character. It gives the same render with the combined injections but is faster in large (> 2k lines without much templating and mostly text) files.

PS: I know the repo is not maintained anymore so this PR does not need to be merged but at least there will be visibility.

qvalentin commented 9 months ago

Hi @baptman21, I merged https://github.com/ngalaiko/tree-sitter-go-template/pull/7 and thought that would improve the diff, but unfortunately there are some merge conflicts now, can you check if you can resolve them?

baptman21 commented 9 months ago

Hello, I used the default formatter of my lsp so it might not be the same as your changes, I'll rebase and redo the changes tomorrow so we get a better diff.

baptman21 commented 9 months ago

For now with the changes made to the repo, we don't seem to need the PR anymore I experimented and somehow with the current version and yaml injections with the combined type it is working perfectly, therefore the changes to the grammar don't seem necessary anymore.

I will close this PR for now and open another to add the injections configuration for helm in the doc.