all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Theodor Thornhill <theo@thornhill.no>
To: "Mattias Engdegård" <mattias.engdegard@gmail.com>
Cc: Yuan Fu <casouri@gmail.com>, emacs-devel <emacs-devel@gnu.org>
Subject: Re: Standardizing tree-sitter fontification features
Date: Mon, 05 Dec 2022 12:30:30 +0100	[thread overview]
Message-ID: <87r0xedr55.fsf@thornhill.no> (raw)
In-Reply-To: <4FC19A2A-E4B3-4BB4-B718-8BB0BE70B04B@gmail.com>

Mattias Engdegård <mattias.engdegard@gmail.com> writes:

> 5 dec. 2022 kl. 09.58 skrev Theodor Thornhill <theo@thornhill.no>:
>
>> I agree - but in most tree-sitter languages it seems like there usually
>> is no distinction between them.  We need to implement some heuristics to
>> locate a comment above method etc, if I'm not mistaken.
>
> At least distinguish doc comments by their special syntax, such as `--
> !` or `/**`; it's better than nothing and only requires local
> analysis. A grammar tie-in to make sure they aren't misplaced is
> obviously better (and valuable) but it can be a later improvement.

Sure, but I don't think it's too hard.  We could do something like (on
emacs-29 branch):

diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index 2c42505ac9..abf67a4c14 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -123,13 +123,24 @@ java-ts-mode--operators
     "|=" "~" ">>" ">>>" "<<" "::" "?" "&=")
   "C operators for tree-sitter font-locking.")
 
+(defun java-ts-mode--font-lock-comment (node override start end &rest _)
+  (when (or (equal (treesit-node-type node) "block_comment")
+            (equal (treesit-node-type node) "line_comment"))
+    (let ((face (if (equal (treesit-node-type (treesit-node-next-sibling node))
+                           "method_declaration")
+                    'font-lock-doc-face
+                  'font-lock-comment-face)))
+      (treesit-fontify-with-override
+       (treesit-node-start node) (treesit-node-end node)
+       face override start end))))
+
 (defvar java-ts-mode--font-lock-settings
   (treesit-font-lock-rules
    :language 'java
    :override t
    :feature 'comment
-   `((line_comment) @font-lock-comment-face
-     (block_comment) @font-lock-comment-face)
+   `((line_comment) @java-ts-mode--font-lock-comment
+     (block_comment) @java-ts-mode--font-lock-comment)
    :language 'java
    :override t
    :feature 'constant


This naive function will work for comments directly above a method.  It
won't try to fix annotations and do other smartness.  The local analysis
is actually a little more complex because you need to extract the
comment text and scan it.  Is a more robust variant of this of interest?

Theo



  reply	other threads:[~2022-12-05 11:30 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-24 22:16 Standardizing tree-sitter fontification features Yuan Fu
2022-11-25  1:13 ` Randy Taylor
2022-11-25  6:15   ` Yuan Fu
2022-11-25 19:03     ` Randy Taylor
2022-11-25 20:55       ` Yuan Fu
2022-11-26  3:35         ` Randy Taylor
2022-12-05 21:17           ` Yuan Fu
2022-11-25  8:13   ` Eli Zaretskii
2022-11-25 19:14     ` Randy Taylor
2022-11-26 14:07     ` Stephen Leake
2022-11-25  2:56 ` Stefan Monnier
2022-11-25  6:34   ` Yuan Fu
2022-11-25 14:52     ` Stefan Monnier
2022-11-26 14:03 ` Stephen Leake
2022-11-26 14:29   ` [SPAM UNSURE] " Stephen Leake
2022-11-26 22:05     ` [SPAM UNSURE] " Yuan Fu
     [not found] ` <2AEA8AB6-593E-4D89-AB05-0C8EB2BCE327@gmail.com>
2022-12-03  1:12   ` Yuan Fu
2022-12-03 14:34     ` Mattias Engdegård
2022-12-05  8:58       ` Theodor Thornhill
2022-12-05 10:26         ` Mattias Engdegård
2022-12-05 11:30           ` Theodor Thornhill [this message]
2022-12-05 21:02             ` 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

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

  git send-email \
    --in-reply-to=87r0xedr55.fsf@thornhill.no \
    --to=theo@thornhill.no \
    --cc=casouri@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=mattias.engdegard@gmail.com \
    /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.