all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: Dmitry Gutov <dmitry@gutov.dev>, Eli Zaretskii <eliz@gnu.org>,
	Arteen Abrishami <arteen@linux.ucla.edu>
Cc: 67417@debbugs.gnu.org
Subject: bug#67417: 29.1.50; c-ts-mode syntax issues with no brackets
Date: Sun, 26 Nov 2023 17:47:49 -0800	[thread overview]
Message-ID: <8f8aa9c5-95cb-44e5-b41e-4cf58f024624@gmail.com> (raw)
In-Reply-To: <2ba91823-bf3c-4d5d-e556-1622135ab2fd@gutov.dev>


On 11/24/23 10:26 AM, Dmitry Gutov wrote:
> On 24/11/2023 09:23, Eli Zaretskii wrote:
>>> Date: Thu, 23 Nov 2023 12:55:31 -0800
>>> From:  Arteen Abrishami via "Bug reports for GNU Emacs,
>>>   the Swiss army knife of text editors"<bug-gnu-emacs@gnu.org>
>>>
>>> This is specifically for the usage of `c-ts-mode` and is not a problem
>>> in `c-mode`. Sometimes, when you type something like:
>>>
>>> else
>>> break
>>>
>>> it won't indent the "break" until you type a semicolon. In this below
>>> scenario, it does not indent the break at all, but `c-mode` does, and
>>> switching from `c-mode` to `c-ts-mode` with correct indentation leaves
>>> it fixed, but `c-ts-mode` cannot detect or fix it itself.
>>>
>>> You can put it into a `.c` buffer all by itself and see:
>>>
>>> ```
>>> unsigned
>>> heap_pop(struct heapq * heap)
>>> {
>>>    if (heap->sz == 0)
>>>      return -1;
>>>
>>>    unsigned ret_val = heap->vals[0];
>>>    heap->vals[0] = heap->vals[heap->len];
>>>    heap->len -= 1;
>>>    unsigned i = 0;
>>>    unsigned lc;
>>>
>>>    while ((lc = HEAPQ_L_CHILD(i)) < heap->len)
>>>      {
>>>        unsigned rc = HEAPQ_R_CHILD(i);
>>>        /* no right child for our guy, special case */
>>>        if (rc == heap->len)
>>>          {
>>>            if (heap->vals[lc] < heap->vals[i])
>>>              SWAP(heap->vals[lc], heap->vals[i]);
>>>            break;
>>>          }
>>>
>>>        if (heap->vals[lc] < heap->vals[i])
>>>          {
>>>            SWAP(heap->vals[lc], heap->vals[i]);
>>>            i = lc;
>>>          }
>>>        else if (heap->vals[rc] < heap->vals[i])
>>>          {
>>>            SWAP(heap->vals[rc], heap->vals[i]);
>>>            i = rc;
>>>          }
>>>        else
>>>        break;
>>>             }
>>> }
>>> ```
>>>
>>> The very last break on the else without brackets around it will not 
>>> indent.c
>> Yuan, any comments?
>>
>> My personal take on this is that as long as typing the required
>> semi-colons fixes the indentation, we are okay in these cases, but if
>> we can do better (i.e. if the problem is not that tree-sitter returns
>> a tree with an error node), we should fix this even without relying on
>> the electric semi-colon.
>>
>> In the specific example above, it looks like tree-sitter does succeed
>> in parsing and shows a valid tree:
>>
>>        alternative:
>>         (else_clause else
>>          (break_statement break ;)))))
>>
>> So I wonder why we don't indent the "break;" part here.
>
> In my testing, it indents fine when after "else" there is either:
>
>  * some char(s) followed by closing curly
>  * or (optionally) some char(s) followed by semicolon
>
> When there is _no_ code between "else" and the closing curly, it 
> already indents fine in my testing (whether the semicolon is added or 
> not).
>
> Without either, the text after "else" isn't parsed as "alternative:" 
> -- it's parsed as a sibling of the "else" node. And, most 
> unfortunately, when "else" is followed by a closing curly, it's just 
> parsed as (ERROR else), so simply pressing RET does not indent the 
> empty line properly even when one is working with electric-pair-mode 
> enabled.
>
> I'd personally consider the last one a more definite bug in the 
> grammar, but maybe there is some good reason for it. I haven't found 
> anything relevant in the bug tracker.
>
> BTW, it seems like the latest C grammar changed how else without 
> braces is parsed, so "break" isn't reindented even with semicolon at 
> the end.

I pushed two commits which should fix the indentation for "break" after 
"else", and indentation for empty lines after if/else/for/while in 
general. The fix for the general case doesn't use the parse tree, since 
the parse tree is often incomplete when you type if (...) and hit 
return. Instead it uses a plain regexp match to see if the previous line 
starts with if/else/for/while. This seems like a reasonable heuristic to 
use before user types more things, at which point more accurate 
indentation rules would be used, since the parse tree should be more 
complete then.

Yuan






  reply	other threads:[~2023-11-27  1:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-23 20:55 bug#67417: 29.1.50; c-ts-mode syntax issues with no brackets Arteen Abrishami via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-24  7:23 ` Eli Zaretskii
2023-11-24 18:26   ` Dmitry Gutov
2023-11-27  1:47     ` Yuan Fu [this message]
2023-11-27  2:22       ` Dmitry Gutov
2023-11-28  6:55         ` Yuan Fu
2023-11-28 14:27           ` Eli Zaretskii
2023-11-28 15:31           ` Dmitry Gutov
2023-12-01  7:58             ` Yuan Fu
2023-12-09  8:27               ` Eli Zaretskii
2023-12-10  9:29                 ` Yuan Fu
2023-12-11  0:29                   ` Dmitry Gutov

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=8f8aa9c5-95cb-44e5-b41e-4cf58f024624@gmail.com \
    --to=casouri@gmail.com \
    --cc=67417@debbugs.gnu.org \
    --cc=arteen@linux.ucla.edu \
    --cc=dmitry@gutov.dev \
    --cc=eliz@gnu.org \
    /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.