* Allow indentation styles by use of function in tree-sitter
@ 2022-11-06 12:52 Theodor Thornhill
2022-11-07 0:06 ` Yuan Fu
0 siblings, 1 reply; 4+ messages in thread
From: Theodor Thornhill @ 2022-11-06 12:52 UTC (permalink / raw)
To: emacs-devel; +Cc: casouri
[-- Attachment #1: Type: text/plain, Size: 499 bytes --]
Hi Yuan!
I was thinking that a quick way to enable indentation styles and
customizable indentation styles is to also accept a function in
tree-sitter-simple-indent.
What do you think of the suggested patch?
That would allow a defcustom such as
```
(defcustom c-ts-mode-indent-rule-function #'c-ts-mode--some-specific-style
"Indentation style of choice"
:group 'c)
```
and in the major-mode init:
```
(setq-local treesit-simple-indent-rules c-ts-mode-indent-rule-function)
```
--
Theo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-treesit-simple-indent-rules-accept-function.patch --]
[-- Type: text/x-diff, Size: 1553 bytes --]
From 9dc7d1151eb15880285d4015efa1579f9e8817ed Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Sun, 6 Nov 2022 13:48:39 +0100
Subject: [PATCH] Make treesit-simple-indent-rules accept function
* lisp/treesit.el (treesit-simple-indent): Make treesit-simple-indent
accept a function that returns a list of the same type as before.
(treesit-simple-indent-rules): Document said change.
---
lisp/treesit.el | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 84be69d8b7..18c9abd658 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -882,7 +882,8 @@ treesit--indent-verbose
"If non-nil, log progress when indenting.")
(defvar-local treesit-simple-indent-rules nil
- "A list of indent rule settings.
+ "A list, or a function that returns a list of indent rule settings.
+
Each indent rule setting should be (LANGUAGE . RULES),
where LANGUAGE is a language symbol, and RULES is a list of
@@ -1292,7 +1293,9 @@ treesit-simple-indent
(cons nil nil))
(let* ((language (treesit-node-language parent))
(rules (alist-get language
- treesit-simple-indent-rules)))
+ (if (functionp treesit-simple-indent-rules)
+ (funcall treesit-simple-indent-rules)
+ treesit-simple-indent-rules))))
(cl-loop for rule in rules
for pred = (nth 0 rule)
for anchor = (nth 1 rule)
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: Allow indentation styles by use of function in tree-sitter
2022-11-06 12:52 Allow indentation styles by use of function in tree-sitter Theodor Thornhill
@ 2022-11-07 0:06 ` Yuan Fu
2022-11-07 8:00 ` Theodor Thornhill
0 siblings, 1 reply; 4+ messages in thread
From: Yuan Fu @ 2022-11-07 0:06 UTC (permalink / raw)
To: Theodor Thornhill; +Cc: emacs-devel
> On Nov 6, 2022, at 4:52 AM, Theodor Thornhill <theo@thornhill.no> wrote:
>
>
> Hi Yuan!
>
> I was thinking that a quick way to enable indentation styles and
> customizable indentation styles is to also accept a function in
> tree-sitter-simple-indent.
>
> What do you think of the suggested patch?
>
> That would allow a defcustom such as
> ```
> (defcustom c-ts-mode-indent-rule-function #'c-ts-mode--some-specific-style
> "Indentation style of choice"
> :group 'c)
> ```
>
> and in the major-mode init:
> ```
> (setq-local treesit-simple-indent-rules c-ts-mode-indent-rule-function)
> ```
Hmmm, does assigning treesit-indent-function fit the bill?
Yuan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Allow indentation styles by use of function in tree-sitter
2022-11-07 0:06 ` Yuan Fu
@ 2022-11-07 8:00 ` Theodor Thornhill
2022-11-07 9:13 ` Yuan Fu
0 siblings, 1 reply; 4+ messages in thread
From: Theodor Thornhill @ 2022-11-07 8:00 UTC (permalink / raw)
To: Yuan Fu; +Cc: emacs-devel
Yuan Fu <casouri@gmail.com> writes:
>> On Nov 6, 2022, at 4:52 AM, Theodor Thornhill <theo@thornhill.no> wrote:
>>
>>
>> Hi Yuan!
>>
>> I was thinking that a quick way to enable indentation styles and
>> customizable indentation styles is to also accept a function in
>> tree-sitter-simple-indent.
>>
>> What do you think of the suggested patch?
>>
>> That would allow a defcustom such as
>> ```
>> (defcustom c-ts-mode-indent-rule-function #'c-ts-mode--some-specific-style
>> "Indentation style of choice"
>> :group 'c)
>> ```
>>
>> and in the major-mode init:
>> ```
>> (setq-local treesit-simple-indent-rules c-ts-mode-indent-rule-function)
>> ```
>
> Hmmm, does assigning treesit-indent-function fit the bill?
>
I might be misunderstanding what you mean, but I don't want to change
the architecture, I just want the user to set their own as a
customization option in a major mode. The function will just return the
same stuff as it does now, but with a function as an available option
that will be a little more flexible. See [0] for an example. The user
can just set [1] to change the indent style. A quick workaround could
be that each major mode just does this:
```
(setq-local treesit-simple-indent-rules
(funcall c-ts-mode-indent-rule-function))
```
But that is a little less nice :-)
[0]: https://git.sr.ht/~theo/tree-sitter-modes/tree/master/item/c-ts-mode.el#L66-160
[1]: https://git.sr.ht/~theo/tree-sitter-modes/tree/master/item/c-ts-mode.el#L35-37
--
Theo
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Allow indentation styles by use of function in tree-sitter
2022-11-07 8:00 ` Theodor Thornhill
@ 2022-11-07 9:13 ` Yuan Fu
0 siblings, 0 replies; 4+ messages in thread
From: Yuan Fu @ 2022-11-07 9:13 UTC (permalink / raw)
To: Theodor Thornhill; +Cc: emacs-devel
> On Nov 7, 2022, at 12:00 AM, Theodor Thornhill <theo@thornhill.no> wrote:
>
> Yuan Fu <casouri@gmail.com> writes:
>
>>> On Nov 6, 2022, at 4:52 AM, Theodor Thornhill <theo@thornhill.no> wrote:
>>>
>>>
>>> Hi Yuan!
>>>
>>> I was thinking that a quick way to enable indentation styles and
>>> customizable indentation styles is to also accept a function in
>>> tree-sitter-simple-indent.
>>>
>>> What do you think of the suggested patch?
>>>
>>> That would allow a defcustom such as
>>> ```
>>> (defcustom c-ts-mode-indent-rule-function #'c-ts-mode--some-specific-style
>>> "Indentation style of choice"
>>> :group 'c)
>>> ```
>>>
>>> and in the major-mode init:
>>> ```
>>> (setq-local treesit-simple-indent-rules c-ts-mode-indent-rule-function)
>>> ```
>>
>> Hmmm, does assigning treesit-indent-function fit the bill?
>>
>
> I might be misunderstanding what you mean, but I don't want to change
> the architecture, I just want the user to set their own as a
> customization option in a major mode. The function will just return the
> same stuff as it does now, but with a function as an available option
> that will be a little more flexible. See [0] for an example. The user
> can just set [1] to change the indent style. A quick workaround could
> be that each major mode just does this:
> ```
> (setq-local treesit-simple-indent-rules
> (funcall c-ts-mode-indent-rule-function))
> ```
> But that is a little less nice :-)
Ah, I see what you mean. I think funcall is perfectly fine. I also think you can just define those styles as variables and use symbol-value.
Yuan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-11-07 9:13 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-06 12:52 Allow indentation styles by use of function in tree-sitter Theodor Thornhill
2022-11-07 0:06 ` Yuan Fu
2022-11-07 8:00 ` Theodor Thornhill
2022-11-07 9:13 ` Yuan Fu
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).