[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: tree-sitter user rules.
From: |
Yuan Fu |
Subject: |
Re: tree-sitter user rules. |
Date: |
Mon, 27 Jan 2025 17:51:00 -0800 |
> On Jan 17, 2025, at 10:52 AM, Ergus <spacibba@aol.com> wrote:
>
> 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?
Hey Ergus, have you considered this:
(setq-local treesit-simple-indent-rules
`((rust . ,(append (your rules)
(alist-get ‘rust treesit-simple-indent-rules)))))
In the major mode hook? That’s the way of customization I had in mind when
writing the indentation facility.
I do agree that we can have a function to simplify it. Similar to
treesit-add-font-lock-rules, we can add a treesit-add-simple-indent-rules, that
also hands adding rules before/after the existing rules, or a certain rule.
Yuan