[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun
From: |
Yuan Fu |
Subject: |
bug#59662: 29.0.50; [PATCH] Add treesit--indent-defun |
Date: |
Wed, 30 Nov 2022 17:07:48 -0800 |
> On Nov 30, 2022, at 3:23 PM, Yuan Fu <casouri@gmail.com> wrote:
>
>
> Theodor Thornhill <theo@thornhill.no> writes:
>
>> Eli Zaretskii <eliz@gnu.org> writes:
>>
>>>> Cc: eliz@gnu.org, casouri@gmail.org
>>>> From: Theodor Thornhill <theo@thornhill.no>
>>>> Date: Mon, 28 Nov 2022 20:32:01 +0100
>>>>
>>>> I've added a function to treesit.el, for use in
>>>> treesit-major-mode-setup. Because the treesit-defun-type-regexp gives
>>>> us "defuns" for free, we can set fill-paragraph-function to this new
>>>> function, thus enabling quick formatting, and some sensible default for
>>>> fill-paragraph. It aims to mirror c-indent-defun.
>>>>
>>>> I think this is a nice way to get this functionality for free, but I'm
>>>> not 100% whether this is considered ok or not.
>>>
>>> Sounds good, but why did you think it wouldn't be OK? Anything here that
>>> doesn't meet the eye?
>>>
>>
>> Not really, but see below answer.
>>
>>>> An alternative could be to add a 'treesit-mode-map' where we can
>>>> auto-enable such constructs.
>>>
>>> I think this is less desirable.
>>>
>>> Yuan, WDYT?
>>>
>>>> @@ -1698,7 +1713,10 @@ treesit-major-mode-setup
>>>> ;; Navigation.
>>>> (when treesit-defun-type-regexp
>>>> (setq-local beginning-of-defun-function #'treesit-beginning-of-defun)
>>>> - (setq-local end-of-defun-function #'treesit-end-of-defun)))
>>>> + (setq-local end-of-defun-function #'treesit-end-of-defun))
>>>> + ;; Filling
>>>> + (when (and treesit-defun-type-regexp treesit-simple-indent-rules)
>>>> + (setq-local fill-paragraph-function #'treesit--indent-defun)))
>>>
>>> I'm a bit confused: if the function's name is treesit--indent-defun, and it
>>> uses treesit-indent-region to do its job, why do we assign it to
>>> fill-paragraph-function, which is supposed to _fill_, not to _indent_?
>>
>> This is why I was thinking it would maybe be better to put it into a
>> treesit-mode-map that major-modes can inherit from, thus binding it to
>> things such as C-c C-q. The reason I put it in filling was because that
>> is a common key to press in everything _but_ prog-modes. And prog-modes
>> seem to mostly just turn it off if not inside of comments etc. This
>> would behave just like that, except we would reformat/reindent/refill
>> code.
>>
>> In a way filling _is_ formatting/reindenting, at least that's how I look
>> at it.
>
> I see, so you want to implement C-c C-q in c-mode. But why don’t we
> make treesit--indent-defun a command and bind it in C-c C-q in major modes?
> Filling is not indent IMO. If you fill a list in elisp code it wraps long
> lines
> instead of indent (IIRC).
>
> Also, this is really not tree-sitter specific, it doesn’t require any
> tree-sitter feature to accomplish: indent-defun only needs mark-defun
> and indent-region, both are supported by practically any major mode.
>
> Normally this kind of thing goes into lisp.el, alongside commands like
> fill-paragraph, indent-region, beginning-of-defun, etc, and claim a
> global keybinding. But maybe we only want it to live under C-c prefix,
> in that case I guess we can bind it in prog-mode-map, under C-c C-q?
>
> I’m thinking something like
>
> (defun prog-mode-indent-defun ()
> (interactive)
> (mark-defun)
> (indent-region (region-beginning) (region-end)))
>
> (defvar prog-mode-map
> (...
> (define-key map (kbd "C-c C-q") #'prog-mode-indent-defun)))
Tho I’d prefer we bind it in major mode maps first. Adding anything to
prog-mode-map should go through emacs-devel (and I don’t know if we want to add
any bindings to prog-mode-map), of course.
Yuan