From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill Newsgroups: gmane.emacs.devel Subject: Re: Standardizing tree-sitter fontification features Date: Mon, 05 Dec 2022 12:30:30 +0100 Message-ID: <87r0xedr55.fsf@thornhill.no> References: <2AEA8AB6-593E-4D89-AB05-0C8EB2BCE327@gmail.com> <5B7EE037-C85A-411C-B42B-1E234EE66DFA@gmail.com> <64C676F2-C4A0-4D28-8FE7-5FB250BF1B9B@gmail.com> <87sfhuxm5g.fsf@thornhill.no> <4FC19A2A-E4B3-4BB4-B718-8BB0BE70B04B@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20011"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Yuan Fu , emacs-devel To: Mattias =?utf-8?Q?Engdeg=C3=A5rd?= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Dec 05 12:31:17 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p29gr-00053X-9J for ged-emacs-devel@m.gmane-mx.org; Mon, 05 Dec 2022 12:31:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p29gI-0002Ue-96; Mon, 05 Dec 2022 06:30:42 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p29gD-0002QW-Ab for emacs-devel@gnu.org; Mon, 05 Dec 2022 06:30:40 -0500 Original-Received: from out2.migadu.com ([188.165.223.204]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p29gA-0005UZ-IL for emacs-devel@gnu.org; Mon, 05 Dec 2022 06:30:36 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1670239831; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iinz0OKvXeL/OEjLVpoVat0pl8s5AdKPuN4cCcoeKcM=; b=wTMPSG5wCrtgx/ARGr/8dIUWV+1OCqw0BS7q4YSOdLnbM7UCLZ9FHAV6C2W4Qkzg4FCfZk 4u/XPXuGsYsMZUMQrMGJ2vbgzyWB1umF1D8w9d3lqVvzSvByccH3lV5wLJegqTFdxYeflV H5eCxwbHjntDHj3VYo7kMhV7sJZYU0I9Qqur7CIQHt2J4FeD1ayuBmtrft/YVIkWKaAF/H 8w28/DtchjjGpYxOO5OuYWwdMFlInqQiNqrz+ikzZ0X9tBq7R/mTe/xB73WC53S3jJ1sog I23yg1geZY+hEBF1ZEgYmv9pPfq4JbhkBpsQNsWoZJn6rhniaQ/xlSLwWZnsqQ== In-Reply-To: <4FC19A2A-E4B3-4BB4-B718-8BB0BE70B04B@gmail.com> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=188.165.223.204; envelope-from=theo@thornhill.no; helo=out2.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:300924 Archived-At: Mattias Engdeg=C3=A5rd writes: > 5 dec. 2022 kl. 09.58 skrev Theodor Thornhill : > >> 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 "|=3D" "~" ">>" ">>>" "<<" "::" "?" "&=3D") "C operators for tree-sitter font-locking.") =20 +(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 n= ode)) + "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