unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Theodor Thornhill <theo@thornhill.no>
To: Eli Zaretskii <eliz@gnu.org>
Cc: casouri@gmail.com, emacs-devel@gnu.org, monnier@iro.umontreal.ca
Subject: Re: Tree sitter support for C-like languages
Date: Sun, 13 Nov 2022 10:40:26 +0100	[thread overview]
Message-ID: <87v8njw5th.fsf@thornhill.no> (raw)
In-Reply-To: <83pmdrkyj7.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 4255 bytes --]

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Theodor Thornhill <theo@thornhill.no>
>> Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org, monnier@iro.umontreal.ca
>> Date: Sat, 12 Nov 2022 21:14:21 +0100
>> 
>> Yuan Fu <casouri@gmail.com> writes:
>> 
>> >> See new patch here - following Stefans keen eye ;-)
>> >
>> > Applied and pushed, thanks ;-)
>> 
>> Great news!  Thanks, all!
>
> Thanks.  The new C mode looks good, but I have a couple of issues with
> it.
>

Great - thanks for looking.  I actually have answers too!

> First, something strange is going on when I type new code.  Here's a
> recipe:
>
>    emacs -Q
>    C-x C-f newfile.c RET
>    M-x c-ts-mode RET
>    Type:
>
> int
> foo (void)
> {
>
> At this point, "int" is in font-lock-warning-face -- why?
>

If you enable 'treesit-inspect-mode' and put point on 'int', you will
see it report the 'ERROR' node.  This node is font locked like that
because of the font lock rule I added for that case.  I think we can
remove it, but it does serve some useful purpose.


> Next, with point after the brace, type RET -- this doesn't indent 2
> spaces, as I'd expect -- why?  Typing TAB to indent doesn't help,
> either.
>

This is because tree-sitter doesn't know what to do with it. if you
rather type:

```
int
foo (void)
{}
```

It will know that it has a complete node and indent accordingly if you
press RET while inside the braces.

       (no-node parent-bol c-ts-mode-indent-offset)

Now this indentation should happen as you want, even though we are in an
error state syntax-wise.  At least after you do what you state just below


> I then type "int bar = 0;".  Typing RET after that doesn't indent,
> either.
>

This is for the same reason.  Adding the closing brace would fix that,
or the rule I mentioned.  Now my code is indented like this:

```
int
foo ()
{
  int bar = 0;
```

> But if I add an empty line at BOB, the fontification becomes as
> expected, and doesn't go back to font-lock-warning-face even if I then
> remove that empty line.
>

This is likely due to either treesit or tree-sitter or tree-sitter-c not
dealing properly with the root node.  Maybe Yuan has some insight here?

> Type } to close the function.  I now have this:
>
> int
> foo (void)
> {
>   int bar = 0;
> }
>
> But "int" is still in font-lock-warning-face -- why?
>

I think the best solution is just to remove the

```
   :language mode
   :override t
   :feature 'error
   '((ERROR) @font-lock-warning-face)
```

> Next, I type this:
>
> struct foo {
>   int bar;
> };
>
> The result is that all of the struct, except the closing brace, is in
> font-lock-warning-face -- why?  Again, adding an empty line before
> that fixes fontifications, and the fontification stays correct even
> after removing that empty line.
>
> If I type
>
> struct bar
>   {
>     int foo;
>   };
>

Same thing.  Let's just remove it.  I'll add a patch below, feel free to
install it.

> then the opening brace and "int foo;" are in font-lock-warning-face.
>
> Next, if I type M-;, I get a C++-style comment delimiter "//".  It
> sounds like this is the only style of comments supported?  More
> generally, if I compare c-basic-common-init and c-common-init from CC
> Mode with c-ts-mode, I see that the former has much more
> initializations than the latter.  So I think we should audit what CC
> Mode does here and see what else is relevant.  Alternatively, we could
> consider c-ts-mode be a minor mode of CC Mode, which only changes the
> fontification, the indentation, and the navigation parts.
>

I can take a look at that this evening - and see what else I can come up
with.  I agree with the comment style

> Thanks.
>
> P.S. If these problems are non-trivial, it might be best to file a bug
> report for each one.  But the last issue, the one about doing more
> stuff like CC Mode does, is something we should discuss here, I think,
> since this is basic design, and similar issues could exist for other
> modes whose *-ts-mode variants were installed on the branch.

Your issues are two-fold.  The warning face is super easy, but the
indenting of error nodes may need a change of perspective.  Tree-sitter
works best when syntax is correct, even though it handles errors pretty
well.

See patch


Theo



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Remove-error-node-font-locking.patch --]
[-- Type: text/x-diff, Size: 1429 bytes --]

From 8a21833d36239ed61d808064faa78d19d6fc5517 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Sun, 13 Nov 2022 10:39:56 +0100
Subject: [PATCH] Remove error node font locking

* lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings)
(c-ts-mode--base-mode): Error node font locking causes too much noise.
---
 lisp/progmodes/c-ts-mode.el | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 5617ea7d7c..7e7b554943 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -326,11 +326,7 @@ c-ts-mode--font-lock-settings
    :feature 'statement
    '((expression_statement (identifier) @font-lock-variable-name-face)
      (labeled_statement
-      label: (statement_identifier) @font-lock-type-face))
-   :language mode
-   :override t
-   :feature 'error
-   '((ERROR) @font-lock-warning-face)))
+      label: (statement_identifier) @font-lock-type-face))))
 
 (defun c-ts-mode--imenu-1 (node)
   "Helper for `c-ts-mode--imenu'.
@@ -424,7 +420,7 @@ c-ts-mode--base-mode
   (setq-local treesit-font-lock-feature-list
               '((comment preprocessor operator constant string literal keyword)
                 (type definition expression statement)
-                (error))))
+                ())))
 
 ;;;###autoload
 (define-derived-mode c-ts-mode c-ts-mode--base-mode "C"
-- 
2.34.1


  reply	other threads:[~2022-11-13  9:40 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-10 17:45 Tree sitter support for C-like languages Theodor Thornhill via Emacs development discussions.
2022-11-10 18:03 ` Stefan Monnier
2022-11-10 18:18   ` Eli Zaretskii
2022-11-10 18:19   ` Theodor Thornhill
2022-11-10 22:58 ` Yuan Fu
2022-11-11  5:48   ` Theodor Thornhill
2022-11-11  6:01   ` Theodor Thornhill via Emacs development discussions.
2022-11-12  5:43     ` Yuan Fu
2022-11-12  6:13       ` Po Lu
2022-11-12  6:17         ` Yuan Fu
2022-11-12  6:43           ` Po Lu
2022-11-12  6:16       ` Theodor Thornhill
2022-11-12  6:25         ` Yuan Fu
2022-11-12  6:37           ` Theodor Thornhill
2022-11-12  8:08         ` Eli Zaretskii
2022-11-12  8:42           ` Theodor Thornhill
2022-11-12  7:22       ` Theodor Thornhill via Emacs development discussions.
2022-11-12  8:05       ` Eli Zaretskii
2022-11-12  8:43         ` Theodor Thornhill
2022-11-12 12:21     ` Eli Zaretskii
2022-11-12 19:38       ` Theodor Thornhill via Emacs development discussions.
2022-11-12 19:46         ` Stefan Kangas
2022-11-12 20:03           ` Theodor Thornhill
2022-11-12 19:51         ` Eli Zaretskii
2022-11-12 20:05           ` Theodor Thornhill via Emacs development discussions.
2022-11-12 20:08             ` Yuan Fu
2022-11-12 20:14               ` Theodor Thornhill
2022-11-13  9:13                 ` Eli Zaretskii
2022-11-13  9:40                   ` Theodor Thornhill [this message]
2022-11-13  9:56                     ` Eli Zaretskii
2022-11-13 10:13                       ` Theodor Thornhill
2022-11-13 12:55                         ` Eli Zaretskii
2022-11-13 13:02                           ` Theodor Thornhill
2022-11-13 13:08                             ` Eli Zaretskii
2022-11-13 13:37                               ` Theodor Thornhill
2022-11-14  1:23                             ` Dmitry Gutov
2022-11-14  0:22                       ` Yuan Fu
2022-11-14  1:26                         ` Dmitry Gutov
2022-11-14  8:35                           ` Yuan Fu
2022-11-14 13:24                             ` Eli Zaretskii
2022-11-14 18:31                               ` Yuan Fu
2022-11-14 19:54                             ` Dmitry Gutov
2022-11-15 10:56                               ` Yuan Fu
2022-11-15 12:30                                 ` Dmitry Gutov
2022-11-14  3:48                         ` Stefan Monnier
2022-11-14  8:23                           ` Yuan Fu
2022-11-14 12:46                             ` Stefan Monnier
2022-11-14 13:20                             ` Eli Zaretskii
2022-11-14 18:29                               ` Yuan Fu
2022-11-14 18:45                                 ` Eli Zaretskii
2022-11-14 19:51                                   ` Yuan Fu
2022-11-14 20:10                                     ` Eli Zaretskii
2022-11-14 21:57                                       ` Yuan Fu
2022-11-15  3:27                                         ` Eli Zaretskii
2022-11-15 10:51                                           ` Yuan Fu
2022-11-15 11:37                                             ` Theodor Thornhill
2022-11-15 15:03                                             ` Eli Zaretskii
2022-11-15 16:01                                               ` Stefan Monnier
2022-11-15 16:59                                                 ` Eli Zaretskii
2022-11-15 18:18                                                   ` Yuan Fu
2022-11-15 18:38                                                     ` Eli Zaretskii
2022-11-16  7:58                                                       ` Yuan Fu
2022-11-16 13:16                                                         ` Eli Zaretskii
2022-11-16 13:29                                                           ` Po Lu
2022-11-16 17:29                                                             ` Yuan Fu
2022-11-15 18:27                                                   ` Visuwesh
2022-11-15 18:36                                                     ` Yuan Fu
2022-11-14 12:55                         ` Eli Zaretskii
2022-11-11  0:43 ` Randy Taylor
2022-11-11  5:50   ` Theodor Thornhill
2022-11-11 13:37     ` Stefan Monnier
2022-11-11 15:09       ` Theodor Thornhill
2022-11-11 15:54     ` Randy Taylor
2022-11-13  8:37       ` Theodor Thornhill
2022-11-13 13:03         ` Randy Taylor
2022-11-16 17:51 ` Yuan Fu
2022-11-16 20:02   ` Theodor Thornhill
2022-11-16 20:10     ` Yuan Fu
2022-11-16 20:25       ` Theodor Thornhill
2022-11-16 20:58     ` Yuan Fu
2022-11-21  9:28       ` Yuan Fu
2022-11-21 11:15         ` Theodor Thornhill
2022-11-23  1:55           ` Yuan Fu

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=87v8njw5th.fsf@thornhill.no \
    --to=theo@thornhill.no \
    --cc=casouri@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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).