unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Theodor Thornhill <theo@thornhill.no>
Cc: Yuan Fu <casouri@gmail.com>,
	62412@debbugs.gnu.org, geza.herman@gmail.com
Subject: bug#62412: 29.0.60; strange c++ indentation behavior with tree sitter
Date: Sun, 26 Mar 2023 15:25:15 +0200	[thread overview]
Message-ID: <m11qlbtzk4.fsf@yahoo.es> (raw)
In-Reply-To: <46A6C705-97DA-4D17-999B-0536F01EFCA7@thornhill.no> (Theodor Thornhill's message of "Fri, 24 Mar 2023 21:04:24 +0100")

Theodor Thornhill <theo@thornhill.no> writes:

> I'll look more deeply into the cause of this, but the rule is covering some preproc directives iirc.
>
> Unfortunately tree-sitter behaves better when auto pairs is used. I
> would advise people to use electric-pairs-mode (if that's the correct
> name, on mobile now) to avoid these sorts of issues.
>

Yes, I think that having an error node in the indentation rules is not a
good idea.  It can cause unexpected issues like the one described in
this thread.  I'd explore the idea of removing error nodes from the
rules before resorting to tweak the electric or pairing features of
Emacs.

Let's look into the problem that the introduction of the error node
tried to solve, the indentation of preprocessor directives.  Starting
with this code:

int
main()
{
  |
}

if I type '#', automatic indentation does not happen because, at that
stage, the AST doesn't recognize the full preprocessor directive (the
node in the AST is an error node).  If I continue writing the
preprocessor directive (say, "#ifdef DEBUG"), the preprocessor node is
created correctly, but it would require a manual TAB to go to column 0
because we haven't inserted any electric character while we completed
the directive.  The same manual TAB is required by c++-mode, so I
wouldn't see this as a regression.

However, there might be still minor divergences between c++-mode and
c++-ts-mode.  For example:

int
main()
{
#if
}

This in-progress code would indent correctly in c++-mode, but on
c-ts-mode the node is an error node, so we won't reliably know that it
should indent to column 0.  If we want to fix these minor divergences, I
see two possible approaches:

- Investigate if the C/C++ grammars can be improved to cover these cases
  better.

- Without changing the grammars, could we insert our own preprocessor
  nodes in the AST tree by checking if the first non-whitespace
  character of the line is the beginning of #assert, #define, #include,
  #if, #ifndef, #elif, etc.?





      parent reply	other threads:[~2023-03-26 13:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-23 20:43 bug#62412: 29.0.60; strange c++ indentation behavior with tree sitter Herman, Geza
2023-03-24 18:17 ` Yuan Fu
2023-03-24 20:04   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-03-25  8:53     ` João Távora
2023-03-25 10:19       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-03-25 10:28         ` João Távora
2023-03-25 10:26       ` Herman, Géza
2023-03-25 11:43         ` João Távora
2023-03-25 13:48           ` Herman, Géza
2023-03-25 16:23             ` João Távora
2023-03-25 17:41               ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-12  0:03                 ` Stefan Kangas
2023-03-25 17:47               ` Herman, Géza
2023-03-25 17:53                 ` João Távora
2023-03-26 13:54           ` Herman, Géza
2023-03-26 13:25     ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m11qlbtzk4.fsf@yahoo.es \
    --to=bug-gnu-emacs@gnu.org \
    --cc=62412@debbugs.gnu.org \
    --cc=casouri@gmail.com \
    --cc=geza.herman@gmail.com \
    --cc=mardani29@yahoo.es \
    --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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).