GNU bug report logs -
#59662
29.0.50; [PATCH] Add treesit--indent-defun
Previous Next
Reported by: Theodor Thornhill <theo <at> thornhill.no>
Date: Mon, 28 Nov 2022 19:33:01 UTC
Severity: wishlist
Tags: patch
Found in version 29.0.50
Done: Yuan Fu <casouri <at> gmail.com>
Bug is archived. No further changes may be made.
Full log
Message #169 received at 59662 <at> debbugs.gnu.org (full text, mbox):
Eli Zaretskii <eliz <at> gnu.org> writes:
>> From: Theodor Thornhill <theo <at> thornhill.no>
>> Cc: dgutov <at> yandex.ru, monnier <at> iro.umontreal.ca, larsi <at> gnus.org,
>> 59662 <at> debbugs.gnu.org, casouri <at> gmail.org
>> Date: Thu, 08 Dec 2022 09:56:44 +0100
>>
>> Eli Zaretskii <eliz <at> gnu.org> writes:
>>
>> > My point is that tree-sitter in many cases parses the program better
>> > than syntax-ppss. So if we can use its information about comments and
>> > strings, why not do that? It's exactly the same logic that Dmitry
>> > used:
>> >
>>
>> Something like this? If we add a defvar such as the below, similar to
>> treesit-defun-type-regexp we can use that in the function below:
>>
>> ```
>> (defvar-local treesit-comment-type-regexp nil
>> "A regexp that matches the node type of comment nodes.
>>
>> For example, \"(line|block)_comment\". ")
>
> The intent is that major modes set this variable? SGTM.
Yeah, that was the idea.
>
>> (if (or (and (treesit-available-p)
>> (treesit-ready-p (treesit-language-at (point)))
>
> I'd imagine that this kind of test should have an API, so the
> treesit-ready-p call should not be made explicitly? Yuan, WDYT?
>
> Also, isn't it enough to check whether the buffer has a tree-sitter
> parser or something?
>
>> The error handling here is naive, but is this something in the line of
>> what you're suggesting?
>
> Yes, it is what I had in mind, but see above.
What about this? I changed it to make a point that we should fill if
inside of a string or comment, and indent if inside a code block. We
one such variable for each type, or in both as suggested below.
```
(defvar-local treesit-text-type-regexp nil
"A regexp that matches the node type of textual nodes.
For example, \"(line|block)_comment\" in the case of a comment,
or \"string_literal\" in the case of a string.")
(defun prog-reindent-defun (&optional argument)
"Refill paragraph or reindent the definition that the point is on.
If the point is in a string, or in a comment, or there is a
comment on the current line, fill the paragraph that the point is
in or is on the same line.
Otherwise, reindent the definition around or below point."
(interactive "P")
(save-excursion
(if (or (and (treesit-parser-list)
(string-match-p
treesit-text-type-regexp
(treesit-node-type (treesit-node-at (point)))))
(nth 8 (syntax-ppss))
(re-search-forward comment-start-skip (line-end-position) t))
(if (memq fill-paragraph-function '(t nil))
(lisp-fill-paragraph argument)
(funcall fill-paragraph-function argument))
(end-of-defun)
(let ((end (point)))
(beginning-of-defun)
(indent-region (point) end)))))
```
This bug report was last modified 2 years and 158 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.