bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun


From: Theodor Thornhill
Subject: bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun
Date: Thu, 08 Dec 2022 09:56:44 +0100

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Theodor Thornhill <theo@thornhill.no>
>> Cc: casouri@gmail.com, 59662@debbugs.gnu.org, casouri@gmail.org
>> Date: Thu, 08 Dec 2022 09:11:28 +0100
>> 
>> > Bonus points for adding something that could use treesit.el
>> > capabilities, when those are available, instead of syntax-ppss, to
>> > determine when we are in a comment or in a string.  Or maybe Theo or
>> > Yuan could suggest such an addition.
>> 
>> This works perfectly as is in *-ts-modes.  At least for modes where
>> syntax-table is set correctly.  I don't think there's a reason to enable
>> treesit.el capabilities, unless there's a case where this doesn't work.
>
> 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\". ")


(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-available-p)
                 (treesit-ready-p (treesit-language-at (point)))
                 (string-match-p
                  treesit-comment-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)))))
```

The error handling here is naive, but is this something in the line of
what you're suggesting?  If so, I can tweak the error handling and
create a patch for it?  Unless you want to do that, Dmitry

>> +    (if (or (nth 8 (syntax-ppss))
>> +            (re-search-forward comment-start-skip (line-end-position) t))
>
> which falls back on regexp search if syntax-ppss cannot help.  I'm
> asking why not try tree-sitter before we fall back on syntax-ppss or
> regexp search?
>
> Does that make sense?  If not, why not?
>

It does.

>> If this ends up in master, should I backport some similar feature for
>> the *ts-modes?
>
> Why do we need this on the release branch?  A general
> indent-or-fill-comment feature for all the progmodes didn't exist
> until now, which is why we intend to install this on master.

Yeah, sure :-)

Theo





reply via email to

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