emacs-tree-sitter / elisp-tree-sitter

Emacs Lisp bindings for tree-sitter
https://emacs-tree-sitter.github.io
MIT License
815 stars 73 forks source link

Nested template strings aren’t (can't be?) highlighted properly #236

Open guillaumebrunerie opened 1 year ago

guillaumebrunerie commented 1 year ago

The code below (Javascript) doesn’t get highlighted properly. We should have all stringN be highlighted with string color, but only string1 and string4 are.

const a = `string1 ${var1 + `string2${`string3` + var2}`}string4`;

What is supposed to be happening is that the whole string gets the string face, then the template substitution gets the embedded face, then the nested string gets (again) the string face, the nested substitution gets the embedded face, and so on. The problem is that (as far as I understand) the priority of the queries is defined by the ordering in the queries file, but here we want both queries to have the same priority (more exactly we want shorter matches to have priority above longer matches). I'm not even sure if that's something that is supposed to work in upstream tree-sitter?

Anyway, I managed to fix it somehow (not tested very much) by ordering the matches by length (using cl-stable-sort) before applying the text properties, but I'm not sure if that's what we're supposed to do? There could also be performance issues by sorting the matches at every keystroke (although I tried a bit on a 3500 lines file and couldn’t see any issue so far).

Any thoughts?