From: Yuan Fu <casouri@gmail.com>
To: "Jørgen Kvalsvik" <j@lambda.is>
Cc: Eli Zaretskii <eliz@gnu.org>,
Theodor Thornhill <theo@thornhill.no>,
74507-done@debbugs.gnu.org
Subject: bug#74507: [PATCH] Indent compounds c-ts-mode when { is not BOL
Date: Sun, 1 Dec 2024 18:13:19 -0800 [thread overview]
Message-ID: <C741EECF-2170-4294-A9F1-D2B6D74A45CD@gmail.com> (raw)
In-Reply-To: <67232cd8-0557-4801-be76-76a965a8c60e@lambda.is>
> On Dec 1, 2024, at 2:46 PM, Jørgen Kvalsvik <j@lambda.is> wrote:
>
> On 12/1/24 22:48, Yuan Fu wrote:
>>> On Dec 1, 2024, at 1:51 AM, Jørgen Kvalsvik <j@lambda.is> wrote:
>>>
>>> On 12/1/24 10:25, Yuan Fu wrote:
>>>>> On Nov 30, 2024, at 12:49 PM, Jørgen Kvalsvik <j@lambda.is> wrote:
>>>>>
>>>>> On 11/30/24 01:16, Yuan Fu wrote:
>>>>>>> On Nov 29, 2024, at 1:05 AM, Jørgen Kvalsvik <j@lambda.is> wrote:
>>>>>>>
>>>>>>> On 11/29/24 08:57, Eli Zaretskii wrote:
>>>>>>>>> Cc: theo@thornhill.no,casouri@gmail.com
>>>>>>>>> From: Jørgen Kvalsvik <j@lambda.is>
>>>>>>>>> Date: Sun, 24 Nov 2024 10:15:12 +0100
>>>>>>>>>
>>>>>>>>> * lisp/progmodes/c-ts-mode.el (c-ts-mode--parent-is-not-top-compound):
>>>>>>>>> New function.
>>>>>>>>> (c-ts-mode--indent-styles): Use it.
>>>>>>>>> * test/lisp/progmodes/c-ts-mode-resources/indent.erts: New compound
>>>>>>>>> statement test.
>>>>>>>> Please make sure the lines here are not too long (see CONTRIBUTE for
>>>>>>>> details).
>>>>>>>>> +(defun c-ts-mode--parent-is-not-top-compound (_n parent &rest _)
>>>>>>>>> + "Matches when PARENT is not the top level compound statement,
>>>>>>>>> +the {} that immediately follows the signature."
>>>>>>>> The first line of a doc string should be a single complete sentence.
>>>>>>>> Thanks.
>>>>>>>
>>>>>>> Ok. Yuan, would you like me to submit a revision?
>>>>>> Since your assignment is already done, let’s just apply your patch, and I’ll rebase my changes on top of yours. So yeah, do send the revision patch, thanks!
>>>>>> Yuan
>>>>>
>>>>> Certainly - I posted it on the bug tracker.
>>>> Thanks Jørgen. What did you use to generate the patch? For some reason I can’t apply it. My git skill isn’t that great so it could be my problem. If you can apply it fine maybe you can share the command you used?
>>>
>>> I used git format-patch HEAD~1, and I just tested applying it to master with `git am 0001-Indent-compounds-in-c-ts-mode-when-is-not-BOL.patch' which worked.
>>>
>>>> BTW, the commit title is missing. When you add the title, you can also add the bug number. For example:
>>>> Improve c-ts-mode indentation for macros (bug#74507)
>>>> Yuan
>>>
>>> Sure. I've attached a new patch with the bug in it. I tested it and it applies cleanly with `git am 0001-Improve-c-ts-mode-compound-indents-bug-74507.patch'
>>> <0001-Improve-c-ts-mode-compound-indents-bug-74507.patch>
>> Thanks. This patch applied without a problem.
>> I reworked your heuristic into a function rule for better organization, and changed the condition slightly:
>> (defun c-ts-mode--macro-heuristic-rules (node parent &rest _)
>> "Heuristic indent rule for control flow macros.
>> Eg,
>> #define IOTA(var, n) for (int var = 0; var != (n); ++var)
>> int main()
>> {
>> IOTA (v, 10) {
>> printf(\"%d \", v); <-- Here we want to indent
>> counter++; <-- Use baseline rule to align
>> } to prev sibling
>> Checked by \"Compound Statement after code (Bug#74507)\" test.
>> NODE and PARENT are the same as other indent rules."
>> (when (and (treesit-node-match-p parent "compound_statement")
>> (treesit-node-match-p (treesit-node-prev-sibling parent)
>> "expression_statement"))
>> (let ((parent-bol
>> (lambda () (save-excursion
>> (goto-char (treesit-node-start parent))
>> (back-to-indentation)
>> (point)))))
>> (cond
>> ;; Closing brace.
>> ((treesit-node-match-p node "}")
>> (cons (funcall parent-bol) 0))
>> ;; First sibling.
>> ((treesit-node-eq (treesit-node-child parent 0 'named) node)
>> (cons (funcall parent-bol)
>> c-ts-mode-indent-offset))))))
>> Instead of checking whether PARENT is top-level compound_statement, I make it check whether the previous sibling is an expression_statement. This way the heuristic works when the macro isn’t at top-level too. Here’s the updated test:
>> #define IOTA(var, n) for (int var = 0; var != (n); ++var)
>> int main()
>> {
>> IOTA (v, 10) {
>> printf("%d ", v);
>> }
>> const char *msg = "Hello, world!"; {
>> puts("Hello, world!");
>> }
>> for (int i = 0;
>> i < 10;
>> i++) {
>> IOTA (v, 10) {
>> printf("%d ", v);
>> }
>> }
>> {
>> IOTA (v, 10) {
>> printf("%d ", v);
>> }
>> }
>> }
>> The only test case that doesn’t pass right now is this one:
>> const char *msg = "Hello, world!"; {
>> puts("Hello, world!");
>> }
>> Is this a real use-case?
>> Yuan
>
> Maybe - I included it to show that the problem was not tied to it being a macro, but rather { not being the first thing on the line. It would be nice to support it, and c-mode also indents it properly.
>
> That being said, it is probably rare enough in practice to prio it less.
Ok, I pushed your patch and my indentation rework to master. I end up removing the test mentioned above since it’s not a real use-case, in favor of making the heuristic indent rule more specific.
Yuan
next prev parent reply other threads:[~2024-12-02 2:13 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-24 9:15 bug#74507: [PATCH] Indent compounds c-ts-mode when { is not BOL Jørgen Kvalsvik
2024-11-29 4:56 ` Yuan Fu
2024-11-29 7:54 ` Eli Zaretskii
2024-11-29 9:04 ` Jørgen Kvalsvik
2024-11-29 7:57 ` Eli Zaretskii
2024-11-29 9:05 ` Jørgen Kvalsvik
2024-11-30 0:16 ` Yuan Fu
2024-11-30 20:49 ` Jørgen Kvalsvik
2024-12-01 9:25 ` Yuan Fu
2024-12-01 9:51 ` Jørgen Kvalsvik
2024-12-01 21:48 ` Yuan Fu
2024-12-01 22:46 ` Jørgen Kvalsvik
2024-12-02 2:13 ` Yuan Fu [this message]
2024-11-30 15:39 ` bug#74507: [PATCH] Indent compounds in " Jørgen Kvalsvik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=C741EECF-2170-4294-A9F1-D2B6D74A45CD@gmail.com \
--to=casouri@gmail.com \
--cc=74507-done@debbugs.gnu.org \
--cc=eliz@gnu.org \
--cc=j@lambda.is \
--cc=theo@thornhill.no \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.