all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dmitry@gutov.dev>
To: Yuan Fu <casouri@gmail.com>, 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: Tue, 28 Nov 2023 17:31:05 +0200	[thread overview]
Message-ID: <ab88c85d-25e1-bab1-5e0d-cf6a8977ec11@gutov.dev> (raw)
In-Reply-To: <720463fd-c6d8-4c31-8240-7017984084a4@gmail.com>

On 28/11/2023 08:55, Yuan Fu wrote:
> 
> 
> On 11/26/23 6:22 PM, Dmitry Gutov wrote:
>> On 27/11/2023 03:47, Yuan Fu wrote:
>>> 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.
>>
>> Sorry, two counter-examples right away:
>>
>> Type 'elsewhere();' and RET -> the next line is indented 1 level 
>> extra, at least until you type some more and then have the line 
>> reindented either with pressing TAB or adding semicolon.
>>
>> Type 'for (;;) {}' and RET -> same.
>>
>> The first case is easy to guard against (just check that the next char 
>> is either space of opening paren), but the second one less so. OTOH, 
>> the second case is likely to have a parse tree without errors, so if 
>> we also check for that... the heuristic might work.
> 
> Well, darn it. And you're right, the second case is a bit hard to 
> check... Well I guess for the moment we can remove this heuristic. (I 
> tried a bit, and checking for no errors is not so easy.)

Maybe it's possible to salvage this heuristic a bit, at least for 
"else", and as long as it's followed by "}" somewhere (e.g. when 
electric-pair-mode is used).

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 31a9d0fc886..20689dc1862 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -373,8 +373,17 @@ c-ts-mode--indent-styles
             ;; point on the empty line to be indented; this rule
             ;; does that.
             ((and no-node
+                 ;; Could be a matcher 'prev-sibling-p'.
+                 (lambda (_ parent bol &rest _)
+                   (equal
+                    "ERROR"
+                    (treesit-node-type
+                     (treesit-node-prev-sibling
+                      (treesit-node-first-child-for-pos
+                       parent bol)
+                      t))))
                   (c-ts-mode--prev-line-match
-                  ,(rx (or "if" "else" "while" "do" "for"))))
+                  ,(rx "else" symbol-end)))
              prev-line c-ts-mode-indent-offset)

             ((parent-is "translation_unit") column-0 0)

The rest of the nodes (if/while/do/for) don't result in parse errors 
here, as long as the condition in parentheses is typed out correctly.

I tried some additional clauses looking for "previous sibling", checking 
whether it's for_statement, etc, which ends with "expression statement", 
and that one is empty... but it a fair amount of code which will likely 
miss other edge cases anyway. Or breaks when the grammar changes.





  parent reply	other threads:[~2023-11-28 15:31 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
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 [this message]
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=ab88c85d-25e1-bab1-5e0d-cf6a8977ec11@gutov.dev \
    --to=dmitry@gutov.dev \
    --cc=67417@debbugs.gnu.org \
    --cc=arteen@linux.ucla.edu \
    --cc=casouri@gmail.com \
    --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.