From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Vincenzo Pupillo Newsgroups: gmane.emacs.bugs Subject: bug#71874: 30.0.60; [PATCH] Fontify doxygen comments in c-ts-mode,c++-ts-mode and java-ts-mode Date: Mon, 01 Jul 2024 12:48:33 +0200 Message-ID: <9826503.eNJFYEL58v@3-191.divsi.unimi.it> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart21879956.4csPzL39Zc" Content-Transfer-Encoding: 7Bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19642"; mail-complaints-to="usenet@ciao.gmane.io" To: 71874@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jul 01 12:49:15 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1sOEaw-0004sI-Ez for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 01 Jul 2024 12:49:14 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sOEan-0007Kw-26; Mon, 01 Jul 2024 06:49:05 -0400 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 1sOEak-0007KZ-PV for bug-gnu-emacs@gnu.org; Mon, 01 Jul 2024 06:49:02 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sOEak-0005b3-Gh for bug-gnu-emacs@gnu.org; Mon, 01 Jul 2024 06:49:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sOEak-0004u4-Gb for bug-gnu-emacs@gnu.org; Mon, 01 Jul 2024 06:49:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Vincenzo Pupillo Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 01 Jul 2024 10:49:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 71874 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: Bug Emacs Original-Received: via spool by submit@debbugs.gnu.org id=B.171983093018826 (code B ref -1); Mon, 01 Jul 2024 10:49:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 1 Jul 2024 10:48:50 +0000 Original-Received: from localhost ([127.0.0.1]:60229 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sOEaX-0004tW-75 for submit@debbugs.gnu.org; Mon, 01 Jul 2024 06:48:50 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:44194) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sOEaU-0004tM-Ud for submit@debbugs.gnu.org; Mon, 01 Jul 2024 06:48:48 -0400 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 1sOEaN-0007Gi-5e for bug-gnu-emacs@gnu.org; Mon, 01 Jul 2024 06:48:41 -0400 Original-Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sOEaK-0005X1-Ip for bug-gnu-emacs@gnu.org; Mon, 01 Jul 2024 06:48:38 -0400 Original-Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-a72b3e1c14cso320871466b.1 for ; Mon, 01 Jul 2024 03:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719830915; x=1720435715; darn=gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=rZH55IERme7j2260849oFZf51OWXo6GKePg5kifUZXE=; b=LQdgQPtDJ/WIJkEVQYBg80+SN24SHnAfWz28XWdxLKqCod7KWo5DwPygme7oVJBJGL 5y4UNNqlbmA8BtZmo+p2CgnuVW2Gf/k1mWJqkPmr7AzDgHV15mVRfJldj/W1IWuqKOtQ H56PzFTioiwnQn5H5eA9Tc9KCshKwlM+GxMk6LmvwfUZe9o3NJmPFWoPt9/g01VT/sBV vWgYKnrDlhbioMGMndlnUXzu80TE4yfx/sLf+HD5+Hd+N36k6Cdfz+OY52JCylFXaW60 F0A+BFhP/CwRACp2JVB5VW3DT3NZEIRTogVasBUUFpGRo6JO25z9Nh0Vfv0V/byAxUMr RXfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719830915; x=1720435715; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rZH55IERme7j2260849oFZf51OWXo6GKePg5kifUZXE=; b=KUZXK6mK2NCMcDQq/Fcc673yd+9i2kC677wApWEnuZLL5VtSl7HcbEktNIoYc7MIew pTPGg4CBqaz/PiFuF7kO+X9BjohMeczehJ7rZ7gj7aKP076CyB0BXGeDZ4eAQvRQt4Mf OOYo4a02aHPyMZKEeObNbbYRJYRuMN+FHpsaPENFFBSXrp2prF2hYA0itbteXuM5MfB2 C3Itg9KAn5cvghNUFPu9FmVGnz/Phv6RIP9Jsh3jL4BPCAQA1cDTP63Zqj40RYlnc0vP 4TgMmrvhLnhsExiwvkBTnZ8DBNmRXRisIaqdeusdoj3HuTt6y57MEniLD+Aa0f5WoCXO MXrQ== X-Gm-Message-State: AOJu0YxWFHgyfA1YG5jCpNxhUALdoob+08PF3Ta45BBBogZtbb4jhvLj zfsdECEuD/ChlbIp7AeegZJky+J0Snaw7yrF97DKHVtTf1PtIj2XVG3k5Q== X-Google-Smtp-Source: AGHT+IGaz43zZn1PBiT9H2dIKAENJJOV9T+T3eflpFP8IxV80Y2eHlXnDEVJV7kw6o4y3HSClSMmcg== X-Received: by 2002:a17:907:7211:b0:a6f:e456:4207 with SMTP id a640c23a62f3a-a75144a2799mr447023566b.61.1719830914518; Mon, 01 Jul 2024 03:48:34 -0700 (PDT) Original-Received: from 3-191.divsi.unimi.it (3-191.divsi.unimi.it. [159.149.3.191]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a72ab065735sm323056466b.135.2024.07.01.03.48.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jul 2024 03:48:34 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::62d; envelope-from=v.pupillo@gmail.com; helo=mail-ej1-x62d.google.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:288242 Archived-At: This is a multi-part message in MIME format. --nextPart21879956.4csPzL39Zc Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="utf-8" Ciao, This patch adds support for comment fontification using the doxygen parser: https://github.com/tree-sitter-grammars/tree-sitter-doxygen This is the same parser used by nvim treesitter and seems to be quite stable. The attached screenshot shows the result. This patch also works with emacs 31. Thanks. Vincenzo --nextPart21879956.4csPzL39Zc Content-Disposition: attachment; filename="0001-Fontify-doxygen-support-to-c-ts-mode-c-ts-mode-and-j.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="x-UTF_8J"; name="0001-Fontify-doxygen-support-to-c-ts-mode-c-ts-mode-and-j.patch" >From 298a7676c924fdc0642dcf867f3ac0da82409968 Mon Sep 17 00:00:00 2001 From: Vincenzo Pupillo Date: Mon, 1 Jul 2024 12:34:01 +0200 Subject: [PATCH] Fontify doxygen support to c-ts-mode, c++-ts-mode and java-ts-mode Add doxygen support to 'c-ts-mode', 'c++-ts-mode' and 'java-ts-mode' using tree-sitter-doxygen from github.com/tree-sitter-grammars. * lisp/progmodes/c-ts-common.el (c-ts-mode-doxygen-comment-font-lock-settings): Add font locking rules for doxygen comment. * lisp/progmodes/c-ts-mode.el (c-ts-mode--feature-list): Add 'document' feature. * lisp/progmodes/c-ts-mode.el (c-ts-mode--doxygen-comment-regex): New regular expression for doxygen comments. * lisp/progmodes/c-ts-mode.el (c-ts-mode): Add support for doxygen parser. * lisp/progmodes/c-ts-mode.el (c++-ts-mode): Add support for doxygen parser. * lisp/progmodes/java-ts-mode.el (java-ts-mode): Add support for doxygen parser. --- lisp/progmodes/c-ts-common.el | 22 +++++ lisp/progmodes/c-ts-mode.el | 126 +++++++++++++++++--------- lisp/progmodes/java-ts-mode.el | 159 ++++++++++++++++++--------------- 3 files changed, 193 insertions(+), 114 deletions(-) diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 3882a697c48..a1f257ee09a 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el @@ -348,6 +348,28 @@ c-ts-common-comment-indent-new-line (delete-region (line-beginning-position) (point)) (insert whitespaces))))) +;; Font locking using doxygen parser +(defvar c-ts-mode-doxygen-comment-font-lock-settings + (treesit-font-lock-rules + :language 'doxygen + :feature 'document + :override t + '((document) @font-lock-doc-face) + + :language 'doxygen + :override t + :feature 'keyword + '((tag_name) @font-lock-constant-face + (storageclass) @font-lock-constant-face) + + :language 'doxygen + :override t + :feature 'definition + '((tag (identifier) @font-lock-variable-name-face) + (function (identifier) @font-lock-function-name-face) + (function_link) @font-lock-function-name-face)) + "Tree-sitter font lock rules for doxygen like comment styles.") + ;;; Statement indent (defvar c-ts-common-indent-offset nil diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 2ac163d7a7e..201ff09593a 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -63,6 +63,9 @@ ;; will set up Emacs to use the C/C++ modes defined here for other ;; files, provided that you have the corresponding parser grammar ;; libraries installed. +;; +;; If the tree-sitter doxygen grammar is available, then the comment +;; blocks will be highlighted according to this grammar. ;;; Code: @@ -539,7 +542,7 @@ c-ts-mode--top-level-label-matcher ;;; Font-lock (defvar c-ts-mode--feature-list - '(( comment definition) + '(( comment document definition) ( keyword preprocessor string type) ( assignment constant escape-sequence label literal) ( bracket delimiter error function operator property variable)) @@ -591,6 +594,10 @@ c-ts-mode--for-each-tail-regexp "LIVE_BUFFER" "FRAME")) "A regexp matching all the variants of the FOR_EACH_* macro.") +(defvar c-ts-mode--doxygen-comment-regex + (rx (| "/**" "/*!" "//!" "///")) + "A regexp that matches all doxygen comment styles.") + (defun c-ts-mode--font-lock-settings (mode) "Tree-sitter font-lock settings. MODE is either `c' or `cpp'." @@ -1319,30 +1326,47 @@ c-ts-mode (when c-ts-mode-emacs-sources-support (treesit-parser-create 'c nil nil 'for-each)) - (treesit-parser-create 'c) - ;; Comments. - (setq-local comment-start "/* ") - (setq-local comment-end " */") - ;; Indent. - (setq-local treesit-simple-indent-rules - (c-ts-mode--get-indent-style 'c)) - ;; Font-lock. - (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'c)) - ;; Navigation. - (setq-local treesit-defun-tactic 'top-level) - (treesit-major-mode-setup) - - ;; Emacs source support: handle DEFUN and FOR_EACH_* gracefully. - (when c-ts-mode-emacs-sources-support - (setq-local add-log-current-defun-function - #'c-ts-mode--emacs-current-defun-name) - - (setq-local treesit-range-settings - (treesit-range-rules 'c-ts-mode--emacs-set-ranges)) - - (setq-local treesit-language-at-point-function - (lambda (_pos) 'c)) - (treesit-font-lock-recompute-features '(emacs-devel))))) + (let ((primary-parser (treesit-parser-create 'c))) + ;; Comments. + (setq-local comment-start "/* ") + (setq-local comment-end " */") + ;; Indent. + (setq-local treesit-simple-indent-rules + (c-ts-mode--get-indent-style 'c)) + ;; Font-lock. + (setq-local treesit-font-lock-settings + (c-ts-mode--font-lock-settings 'c)) + ;; Navigation. + (setq-local treesit-defun-tactic 'top-level) + (treesit-major-mode-setup) + + ;; Emacs source support: handle DEFUN and FOR_EACH_* gracefully. + (when c-ts-mode-emacs-sources-support + (setq-local add-log-current-defun-function + #'c-ts-mode--emacs-current-defun-name) + + (setq-local treesit-range-settings + (treesit-range-rules 'c-ts-mode--emacs-set-ranges)) + + (setq-local treesit-language-at-point-function + (lambda (_pos) 'c)) + (treesit-font-lock-recompute-features '(emacs-devel))) + + ;; Inject doxygen parser for comment. + (when (treesit-ready-p 'doxygen) + (setq-local treesit-primary-parser primary-parser) + (setq-local treesit-font-lock-settings + (append + treesit-font-lock-settings + c-ts-mode-doxygen-comment-font-lock-settings)) + (setq-local treesit-range-settings + (treesit-range-rules + :embed 'doxygen + :host 'c + :local t + `(((comment) @cap + (:match + ,c-ts-mode--doxygen-comment-regex @cap))))))))) (derived-mode-add-parents 'c-ts-mode '(c-mode)) @@ -1370,24 +1394,40 @@ c++-ts-mode :after-hook (c-ts-mode-set-modeline) (when (treesit-ready-p 'cpp) - - (treesit-parser-create 'cpp) - - ;; Syntax. - (setq-local syntax-propertize-function - #'c-ts-mode--syntax-propertize) - - ;; Indent. - (setq-local treesit-simple-indent-rules - (c-ts-mode--get-indent-style 'cpp)) - - ;; Font-lock. - (setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'cpp)) - (treesit-major-mode-setup) - - (when c-ts-mode-emacs-sources-support - (setq-local add-log-current-defun-function - #'c-ts-mode--emacs-current-defun-name)))) + (let ((primary-parser (treesit-parser-create 'cpp))) + + ;; Syntax. + (setq-local syntax-propertize-function + #'c-ts-mode--syntax-propertize) + + ;; Indent. + (setq-local treesit-simple-indent-rules + (c-ts-mode--get-indent-style 'cpp)) + + ;; Font-lock. + (setq-local treesit-font-lock-settings + (c-ts-mode--font-lock-settings 'cpp)) + (treesit-major-mode-setup) + + (when c-ts-mode-emacs-sources-support + (setq-local add-log-current-defun-function + #'c-ts-mode--emacs-current-defun-name)) + + ;; Inject doxygen parser for comment. + (when (treesit-ready-p 'doxygen) + (setq-local treesit-primary-parser primary-parser) + (setq-local treesit-font-lock-settings + (append + treesit-font-lock-settings + c-ts-mode-doxygen-comment-font-lock-settings)) + (setq-local treesit-range-settings + (treesit-range-rules + :embed 'doxygen + :host 'cpp + :local t + `(((comment) @cap + (:match + ,c-ts-mode--doxygen-comment-regex @cap))))))))) (derived-mode-add-parents 'c++-ts-mode '(c++-mode)) diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index bb4a7df3340..04aaf43ac66 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -24,6 +24,8 @@ ;;; Commentary: ;; +;; If the tree-sitter doxygen grammar is available, then the comment +;; blocks will be highlighted according to this grammar. ;;; Code: @@ -312,7 +314,7 @@ java-ts-mode--defun-name (defvar java-ts-mode--feature-list - '(( comment definition ) + '(( comment document definition ) ( constant keyword string type) ( annotation expression literal) ( bracket delimiter operator))) @@ -326,76 +328,91 @@ java-ts-mode (unless (treesit-ready-p 'java) (error "Tree-sitter for Java isn't available")) - (treesit-parser-create 'java) - - ;; Comments. - (c-ts-common-comment-setup) - - ;; Indent. - (setq-local c-ts-common-indent-type-regexp-alist - `((block . ,(rx (or "class_body" - "array_initializer" - "constructor_body" - "annotation_type_body" - "interface_body" - "lambda_expression" - "enum_body" - "switch_block" - "record_declaration_body" - "block"))) - (close-bracket . "}") - (if . "if_statement") - (else . ("if_statement" . "alternative")) - (for . "for_statement") - (while . "while_statement") - (do . "do_statement"))) - (setq-local c-ts-common-indent-offset 'java-ts-mode-indent-offset) - (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules) - - ;; Electric - (setq-local electric-indent-chars - (append "{}():;," electric-indent-chars)) - - ;; Navigation. - (setq-local treesit-defun-type-regexp - (regexp-opt '("method_declaration" - "class_declaration" - "record_declaration" - "interface_declaration" - "enum_declaration" - "import_declaration" - "package_declaration" - "module_declaration" - "constructor_declaration"))) - (setq-local treesit-defun-name-function #'java-ts-mode--defun-name) - - (setq-local treesit-thing-settings - `((java - (sexp ,(rx (or "annotation" - "parenthesized_expression" - "argument_list" - "identifier" - "modifiers" - "block" - "body" - "literal" - "access" - "reference" - "_type" - "true" - "false"))) - (sentence ,(rx (or "statement" - "local_variable_declaration" - "field_declaration" - "module_declaration" - "package_declaration" - "import_declaration"))) - (text ,(regexp-opt '("line_comment" - "block_comment" - "text_block")))))) - - ;; Font-lock. - (setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings) + (let ((primary-parser (treesit-parser-create 'java))) + + ;; Comments. + (c-ts-common-comment-setup) + + ;; Indent. + (setq-local c-ts-common-indent-type-regexp-alist + `((block . ,(rx (or "class_body" + "array_initializer" + "constructor_body" + "annotation_type_body" + "interface_body" + "lambda_expression" + "enum_body" + "switch_block" + "record_declaration_body" + "block"))) + (close-bracket . "}") + (if . "if_statement") + (else . ("if_statement" . "alternative")) + (for . "for_statement") + (while . "while_statement") + (do . "do_statement"))) + (setq-local c-ts-common-indent-offset 'java-ts-mode-indent-offset) + (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules) + + ;; Electric + (setq-local electric-indent-chars + (append "{}():;," electric-indent-chars)) + + ;; Navigation. + (setq-local treesit-defun-type-regexp + (regexp-opt '("method_declaration" + "class_declaration" + "record_declaration" + "interface_declaration" + "enum_declaration" + "import_declaration" + "package_declaration" + "module_declaration" + "constructor_declaration"))) + (setq-local treesit-defun-name-function #'java-ts-mode--defun-name) + + (setq-local treesit-thing-settings + `((java + (sexp ,(rx (or "annotation" + "parenthesized_expression" + "argument_list" + "identifier" + "modifiers" + "block" + "body" + "literal" + "access" + "reference" + "_type" + "true" + "false"))) + (sentence ,(rx (or "statement" + "local_variable_declaration" + "field_declaration" + "module_declaration" + "package_declaration" + "import_declaration"))) + (text ,(regexp-opt '("line_comment" + "block_comment" + "text_block")))))) + + ;; Font-lock. + (setq-local treesit-font-lock-settings + java-ts-mode--font-lock-settings) + + ;; Inject doxygen parser for comment. + (when (treesit-ready-p 'doxygen) + (setq-local treesit-primary-parser primary-parser) + (setq-local treesit-font-lock-settings + (append treesit-font-lock-settings + c-ts-mode-doxygen-comment-font-lock-settings)) + (setq-local treesit-range-settings + (treesit-range-rules + :embed 'doxygen + :host 'java + :local t + `(((block_comment) @cap (:match "/\\*\\*" @cap))))))) + (setq-local treesit-font-lock-feature-list java-ts-mode--feature-list) ;; Imenu. -- 2.45.2 --nextPart21879956.4csPzL39Zc--