Tree Sitter?

Decided to look up tree sitter after hearing a bit about it. Sounds like a big improvement to syntax highlighting.

Have a few questions about it though:

  • Is there an up-to-date guide out there to install and configure doom?
  • Does it help with indentation as well?
  • Does it conflict with lsp-mode?
  • Is it worth it?
2 Likes

Better! @jeetelongname has been working on a module, see here.

Henrik already reviewed it, I think there’s one remaining issue regarding lazy loading that needs to be solved before it can be merged.

It can help with indentation, but I’m not sure it comes out of the box in the module, but iirc there’s a package.

It doesn’t conflict with lsp-mode, and imo it’s totally worth it for the better syntax highlighting alone, but it also has other useful features like ast aware text objects and folding. Basically all upsides so far.

There is also this tracking post, but I’m not sure how up to date it is.

1 Like

It very much is not lol, I have not touched it in a while but it should still have some interesting stuff

Just to add to the “is it worth it” point.

Tree sitter is very much a general tool, so anyone can jack into the ast it generates and do some really cool syntax aware stuff! for example tree-edit uses a mix of the TS ast and logic programming to be able to strutually destructure, navigate and restructure the AST. In a sense it allows for structural selection, allowing you to wrap code with other code structures and a heck of a lot more! and this is not the end of it, in a sense the sky’s the limit

1 Like

Nice! Looking forward to @jeetelongname’s module!

In the meantime, I use this minimal setup to get syntax highlighting with tree sitter. It is taken from Henrik’s doom config. He created an extra module and pinned the versions: doom-emacs-private/modules/ui/tree-sitter at master · hlissner/doom-emacs-private · GitHub the module.

I only used it with TypeScript so far and the syntax highlighting is much nicer.

$DOOMDIR/packages.el:

(package! tree-sitter)
(package! tree-sitter-langs)

$DOOMDIR/config.el:

;;; Tree Sitter

(use-package! tree-sitter
   :hook (prog-mode . turn-on-tree-sitter-mode)
   :hook (tree-sitter-after-on . tree-sitter-hl-mode)
   :config
   (require 'tree-sitter-langs)
   ;; This makes every node a link to a section of code
   (setq tree-sitter-debug-jump-buttons t
         ;; and this highlights the entire sub tree in your code
         tree-sitter-debug-highlight-jump-region t))

3 Likes

That advice is no longer needed. I would just take the use-package! blocks from the module it should work the same. (maybe need to add in a :hook here and a (require tree-sitter-langs) there but overall it should be copy and paste

1 Like

Thank you. So only this in the config for minimal tree sitter/syntax highlighting?

(use-package! tree-sitter
   :hook (tree-sitter-after-on . tree-sitter-hl-mode)
   :config
   ;; This makes every node a link to a section of code
   (setq tree-sitter-debug-jump-buttons t
         ;; and this highlights the entire sub tree in your code
         tree-sitter-debug-highlight-jump-region t))

I would also add to that snippet

(use-package! tree-sitter
   :hook (prog-mode . turn-on-tree-sitter-mode)
   :hook (tree-sitter-after-on . tree-sitter-hl-mode)
   :config
   (require 'tree-sitter-langs)
   ;; This makes every node a link to a section of code
   (setq tree-sitter-debug-jump-buttons t
         ;; and this highlights the entire sub tree in your code
         tree-sitter-debug-highlight-jump-region t))

but overall yes

3 Likes

Thank you. I updated my other post.

I also have this snippet added that Henrik gave me once on discord:

(defadvice! doom-tree-sitter-fail-gracefully-a (orig-fn &rest args)
      "Don't break with errors when current major mode lacks tree-sitter support"
      :around #'tree-sitter-mode
      (condition-case e
          (apply orign-fn args)
        (error
         (unless (string-match-p (concat "^Cannot find shared library\\|"
                                         "^No language registered\\|"
                                         "cannot open sharaed object file")
                                 (error-message-string e))
           (signal (car e) (cadr e)))))))

maybe it doesn’t make sense in the module/your configuration snippets but it can be useful for others that encountered these errors.

The advice is of course to be added in the use-package! after :config

The reason this advice is not needed is because if your using turn-on-tree-sitter-mode like in the snippet, it already ignores errors

here is them implementation

(defun turn-on-tree-sitter-mode ()
  "Turn on `tree-sitter-mode' in a buffer, if possible."
  ;; FIX: Ignore only known errors. Log the rest, at least.
  (ignore-errors
    (tree-sitter-mode 1)))

I would argue that you should not need to use tree-sitter-mode as a hook because tree sitter already has a better hook function that accounts for this, the only times I need it when activating in a mode that does not have a hook. to demote all errors feels harder to debug.

3 Likes

BTW, I know its had some headway on the discord but the module has been merged into master! all of this is now moot because all you need to do is enable the module and add flags to the lang modules you want!

6 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.