emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tree-sitter user rules.


From: Ergus
Subject: tree-sitter user rules.
Date: Fri, 17 Jan 2025 19:52:40 +0100

Hi:

I have been hacking a bit treesitter to add a few custom indentation
rules that behave "weird" in my system.

At the end I have this workaround, but I am wondering if we could add
something less hacky in order to allow the user insert some rules over
existing styles:

```
(defun my/treesit-simple-indent (oldfun node parent bol)
    "Allow indent with custom rules without hacks in the predefined rules

It is possible to change the treesit-indent-function BUT the python mode
breaks because there is some hacky condition assigning the indentation
function in the tree-sitter library."
    (or (when-let* ((rules (alist-get (treesit-node-language parent)
                                      my/treesit-indent-rules))
                    (treesit-simple-indent-rules my/treesit-indent-rules)
                    (result (funcall oldfun node parent bol))
                    ((or (car result)
                         (cdr result))))
          result)
        (funcall oldfun node parent bol)))

  ;; Add an advise to check my indentations before the default ones.
  ;; I cannot use another function and set it to treesit-indent-function
  ;; because some code in tressitter.el apparently checks that
  ;; (eq treesit-indent-function '#treesit-simple-indent)
  (advice-add 'treesit-simple-indent :around #'my/treesit-simple-indent)

  ;; The for example for rust:
  (defun my/rust-ts-mode-hook ()
    "Hook to improve indentation in rust mode"
    (setq-local tab-width 4)

    (setq-local my/treesit-indent-rules
                `((rust . (((and (parent-is "function_item")
                                 (node-is "block")) parent-bol 0))))))
  (add-hook 'rust-ts-mode-hook #'my/rust-ts-mode-hook)

```

I propose to add a variable equivalent to my/treesit-indent-rules and
make treesit-simple-indent try to get a match from there before, else
rely on treesit-simple-indent-rules

WDYT?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]