0

How does one use local treesit parsers/multiple parsers to fontify different regions in a buffer? I haven't been able to find any tree-sitter modes that are currently using local parsers to see how it is supposed to work.

According to treesit-local-parsers-on, if I'm reading it correctly, I should be able to specify local parsers by adding a treesit-parser property to an overlay. The doc also says the local parsers should have an embedded tag. So, I've tried doing that, eg. in a dockerfile-ts-mode buffer

RUN          \
    # comment
    echo foo \
    bar

Adding an overlay with a treesit-parser for bash to the body of the RUN command (with region selected),

(let ((ov (make-overlay (region-beginning) (region-end))))
  (overlay-put
   ov 'treesit-parser
   (treesit-parser-create 'bash nil nil 'embedded)))

But, I'm not sure what to do next - calling treesit-font-lock-fontify-region doesn't seem to do anything with the bash parser.

Anyone know how to get multiple parsers working together? And if so, is there a way to specify regions to be injected with local parsers in the treesit-font-lock-rules?

[[it looks like nvim-treesitter has a nice mechanism to handle this]]

1 Answer 1

1

Have you read chapter 37.6 Parsing Text in Multiple Languages of the Emacs Lisp manual? It gives plenty of details and even some examples.

Specifically, you should call treesit-parser-set-included-ranges to set up the embedded parser instead of creating an overlay directly. Alternatively you can call treesit-range-rules to turn queries into embedded parsers automatically.

1
  • Thankyou! treesit-range-rules looks perfect, I'm dumb missing that section - it's actually much more tutorial-like than a lot of the manual - super useful
    – Rorschach
    Commented Oct 7, 2023 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.