From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#59251: 29.0.50; Fix-some-inconsistencies-in-ts-modes Date: Sun, 13 Nov 2022 21:42:19 +0100 Message-ID: <87k03ya8no.fsf@thornhill.no> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22329"; mail-complaints-to="usenet@ciao.gmane.io" Cc: casouri@gmail.com To: 59251@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Nov 13 21:43:21 2022 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 1ouJp1-0005co-RN for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 13 Nov 2022 21:43:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ouJol-0002gl-Mj; Sun, 13 Nov 2022 15:43:03 -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 1ouJok-0002eU-BG for bug-gnu-emacs@gnu.org; Sun, 13 Nov 2022 15:43:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ouJok-0002Ay-31 for bug-gnu-emacs@gnu.org; Sun, 13 Nov 2022 15:43:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ouJoj-0001Z8-Pg for bug-gnu-emacs@gnu.org; Sun, 13 Nov 2022 15:43:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 13 Nov 2022 20:43:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 59251 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16683721605982 (code B ref -1); Sun, 13 Nov 2022 20:43:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 13 Nov 2022 20:42:40 +0000 Original-Received: from localhost ([127.0.0.1]:48451 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ouJoN-0001YQ-Tf for submit@debbugs.gnu.org; Sun, 13 Nov 2022 15:42:40 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:37890) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ouJoL-0001YH-4K for submit@debbugs.gnu.org; Sun, 13 Nov 2022 15:42:38 -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 1ouJoK-0002Ya-L8 for bug-gnu-emacs@gnu.org; Sun, 13 Nov 2022 15:42:36 -0500 Original-Received: from out0.migadu.com ([2001:41d0:2:267::]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ouJoG-00028V-51 for bug-gnu-emacs@gnu.org; Sun, 13 Nov 2022 15:42: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=1668372146; 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; bh=1AQ4Gw0rpCBGtGHTLfY154loMPe0yKRejh4hDidDazQ=; b=Ld2TMi9nRclfQ/TZT6nEefASGBOY8+v/WgGngP2hRuZNGvlh4DjtQLtKbJsb6puSYhvyYm cYcg2pIy5cOYYRkhEmlUJMS8UA/is61/wFQtF2ZJgE/jKZ5ttAZecL6mA+osCBwtC7uHPF 6e4Hpr3vguqqni8AFQSIIJIpuUUqtxm3RZpzK63KeaTJEj+7hRgwyJLWG4hBsQJEq62Fyb JezPcvfD4eiNd13bmWAgS5Hkeb6cT0fouFDduoQYOlaH0ajTtydXVbbac1Y7zGZINez4Nj bItKsWUg4xOWyzdpOUehGk6/r9ABuP4FVNFDttxJgAjxQTtFOzrGt25drySwlw== X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:2:267::; envelope-from=theo@thornhill.no; helo=out0.migadu.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-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:247816 Archived-At: --=-=-= Content-Type: text/plain Hi Yuan! I've make some changes to my *-ts-modes, and would appreciate a second opinion as well as a push to the feature/tree-sitter branch :-) I've extracted some of the relevant mode-init variables into defcustoms so that users can tweak their behavior. It also seems I forgot some electric indent configs in some of the other files, so I added that aswell. BTW, should we do the same thing we did with js-mode - add a js-ts-mode and don't change the js.el file at all? We could either go the same route as c-ts-mode, where we define both c and c++ in the same file, or keep them separate. What do you think? Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Fix-some-inconsistencies-in-ts-modes.patch >From 0c2181a0bd3dbcf8c896404932fbd8eb2d2ee897 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sun, 13 Nov 2022 21:37:01 +0100 Subject: [PATCH] Fix some inconsistencies in *-ts-modes * lisp/progmodes/c-ts-mode.el (c-ts-mode-comment-style): New defcustom. (c-ts-mode-font-lock-feature-list): New defcustom. (c-ts-mode--base-mode): Extract comment-* so that we can separate between C and C++. Use the new font-lock-feature-list defcustom. (c-ts-mode, c++-ts-mode): Set comment-* variables. * lisp/progmodes/css-ts-mode.el (css-ts-mode): Add electric-indent. * lisp/progmodes/java-ts-mode.el (java-ts-mode-font-lock-feature-list): New defcustom. (java-ts-mode): Add electric-indent and use the font-lock-feature-list defcustom. * lisp/progmodes/json-ts-mode.el (json-ts-mode-font-lock-feature-list): New defcustom. (json-ts-mode--font-lock-settings): Make much more granular font lock features. (json-ts-mode): Add electric-indent and use the font-lock-feature-list defcustom. * lisp/progmodes/ts-mode.el (ts-mode-font-lock-feature-list): New defcustom. (ts-mode--font-lock-settings): Whitespace cleanup. (ts-mode): Add electric-indent and use the font-lock-feature-list defcustom. --- lisp/progmodes/c-ts-mode.el | 42 ++++++++++++++++++++++------- lisp/progmodes/css-ts-mode.el | 4 +++ lisp/progmodes/java-ts-mode.el | 21 ++++++++++++--- lisp/progmodes/json-ts-mode.el | 49 ++++++++++++++++++++++++---------- lisp/progmodes/ts-mode.el | 30 ++++++++++++++++++--- 5 files changed, 116 insertions(+), 30 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 5617ea7d7c..75ab5ed737 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -31,6 +31,25 @@ (require 'treesit) (require 'rx) +(defcustom c-ts-mode-comment-style 'multi-line + "The comment style as decided by `comment-styles'" + :type 'symbol + :safe 'symbolp + :group 'c) + +(defcustom c-ts-mode-font-lock-feature-list + '((comment preprocessor operator constant string literal keyword) + (type definition expression statement) + ()) + "Language constructs to font-lock. + +See `treesit-font-lock-feature-list' for description of how to +use this variable. Supported features are as defined in +`c-ts-mode--font-lock-settings'." + :type 'list + :safe 'listp + :group 'c) + (defcustom c-ts-mode-indent-offset 2 "Number of spaces for each indentation step in `c-ts-mode'." :type 'integer @@ -399,11 +418,6 @@ c-ts-mode--base-mode :group 'c :syntax-table c-ts-mode--syntax-table - ;; Comments. - (setq-local comment-start "// ") - (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") - (setq-local comment-end "") - ;; Navigation. (setq-local treesit-defun-type-regexp (rx (or "specifier" @@ -415,16 +429,14 @@ c-ts-mode--base-mode ;; Electric (setq-local electric-indent-chars - (append "{}():;," electric-indent-chars)) + (append "{}():;," electric-indent-chars)) ;; Imenu. (setq-local imenu-create-index-function #'c-ts-mode--imenu) (setq-local which-func-functions nil) (setq-local treesit-font-lock-feature-list - '((comment preprocessor operator constant string literal keyword) - (type definition expression statement) - (error)))) + c-ts-mode-font-lock-feature-list)) ;;;###autoload (define-derived-mode c-ts-mode c-ts-mode--base-mode "C" @@ -436,6 +448,12 @@ c-ts-mode (treesit-parser-create 'c) + ;; Comments. + (setq-local comment-start "/* ") + (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") + (setq-local comment-end " */") + (setq-local comment-style c-ts-mode-comment-style) + (setq-local treesit-simple-indent-rules (c-ts-mode--set-indent-style 'c)) @@ -452,6 +470,12 @@ c++-ts-mode (unless (treesit-ready-p nil 'cpp) (error "Tree-sitter for C++ isn't available")) + ;; Comments. + (setq-local comment-start "// ") + (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") + (setq-local comment-end "") + (setq-local comment-style c-ts-mode-comment-style) + (treesit-parser-create 'cpp) (setq-local treesit-simple-indent-rules diff --git a/lisp/progmodes/css-ts-mode.el b/lisp/progmodes/css-ts-mode.el index e22bf99c6c..8cb6074043 100644 --- a/lisp/progmodes/css-ts-mode.el +++ b/lisp/progmodes/css-ts-mode.el @@ -114,6 +114,10 @@ css-ts-mode ;; Indent. (setq-local treesit-simple-indent-rules css-ts-mode--indent-rules) + ;; Electric + (setq-local electric-indent-chars + (append "{}():;," electric-indent-chars)) + ;; Navigation. (setq-local treesit-defun-type-regexp "rule_set") ;; Font-lock. diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index 5ec7da9f5c..8aed2db9c6 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -31,6 +31,19 @@ (require 'treesit) (require 'rx) +(defcustom java-ts-mode-font-lock-feature-list + '((basic comment keyword constant string operator) + (type definition expression literal annotation) + ()) + "Language constructs to font-lock. + +See `treesit-font-lock-feature-list' for description of how to +use this variable. Supported features are as defined in +`java-ts-mode--font-lock-settings'." + :type 'list + :safe 'listp + :group 'java) + (defcustom java-ts-mode-indent-offset 4 "Number of spaces for each indentation step in `java-ts-mode'." :type 'integer @@ -268,6 +281,10 @@ java-ts-mode ;; Indent. (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 (rx (or "declaration"))) @@ -275,9 +292,7 @@ java-ts-mode ;; Font-lock. (setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings) (setq-local treesit-font-lock-feature-list - '((basic comment keyword constant string operator) - (type definition expression literal annotation) - ())) + java-ts-mode-font-lock-feature-list) ;; Imenu. (setq-local imenu-create-index-function #'java-ts-mode--imenu) diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el index 686640f98a..140ad5b523 100644 --- a/lisp/progmodes/json-ts-mode.el +++ b/lisp/progmodes/json-ts-mode.el @@ -31,6 +31,17 @@ (require 'treesit) (require 'rx) +(defcustom json-ts-mode-font-lock-feature-list + '((comment string number) (constant pair) ()) + "Language constructs to font-lock. + +See `treesit-font-lock-feature-list' for description of how to +use this variable. Supported features are as defined in +`json-ts-mode--font-lock-settings'." + :type 'list + :safe 'listp + :group 'json) + (defcustom json-ts-mode-indent-offset 2 "Number of spaces for each indentation step in `json-ts-mode'." :type 'integer @@ -67,20 +78,26 @@ json-ts--indent-rules (defvar json-ts-mode--font-lock-settings (treesit-font-lock-rules :language 'json - :feature 'minimal + :feature 'comment :override t - `((pair - key: (_) @font-lock-string-face) - - (string) @font-lock-string-face - - (number) @font-lock-constant-face - - [(null) (true) (false)] @font-lock-constant-face - - (escape_sequence) @font-lock-constant-face - - (comment) @font-lock-comment-face)) + '((comment) @font-lock-comment-face) + :language 'json + :feature 'string + :override t + '((escape_sequence) @font-lock-constant-face + (string) @font-lock-string-face) + :language 'json + :feature 'number + :override t + '((number) @font-lock-constant-face) + :language 'json + :feature 'constant + :override t + '([(null) (true) (false)] @font-lock-constant-face) + :language 'json + :feature 'pair + :override t + `((pair key: (_) @font-lock-variable-name-face))) "Font-lock settings for JSON.") (defun json-ts-mode--imenu-1 (node) @@ -127,6 +144,10 @@ json-ts-mode (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") (setq-local comment-end "") + ;; Electric + (setq-local electric-indent-chars + (append "{}():;," electric-indent-chars)) + ;; Indent. (setq-local treesit-simple-indent-rules json-ts--indent-rules) @@ -137,7 +158,7 @@ json-ts-mode ;; Font-lock. (setq-local treesit-font-lock-settings json-ts-mode--font-lock-settings) (setq-local treesit-font-lock-feature-list - '((minimal) () ())) + json-ts-mode-font-lock-feature-list) ;; Imenu. (setq-local imenu-create-index-function #'json-ts-mode--imenu) diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el index f4dfa2ae95..73f8c38238 100644 --- a/lisp/progmodes/ts-mode.el +++ b/lisp/progmodes/ts-mode.el @@ -28,6 +28,19 @@ (require 'rx) (require 'js) +(defcustom ts-mode-font-lock-feature-list + '((comment declaration) + (string keyword identifier expression constant) + (property pattern jsx)) + "Language constructs to font-lock. + +See `treesit-font-lock-feature-list' for description of how to +use this variable. Supported features are as defined in +`ts-mode--font-lock-settings'." + :type 'list + :safe 'listp + :group 'typescript) + (defcustom ts-mode-indent-offset 2 "Number of spaces for each indentation step in `ts-mode'." :type 'integer @@ -100,7 +113,6 @@ ts-mode--keywords (defvar ts-mode--font-lock-settings (treesit-font-lock-rules - :language 'tsx :override t :feature 'comment @@ -271,29 +283,39 @@ ts-mode ((treesit-ready-p nil 'tsx) ;; Tree-sitter. (treesit-parser-create 'tsx) + ;; Comments. (setq-local comment-start "// ") (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") (setq-local comment-end "") + + ;; Electric + (setq-local electric-indent-chars + (append "{}():;," electric-indent-chars)) + ;; Indent. (setq-local treesit-simple-indent-rules ts-mode--indent-rules) + ;; Navigation. (setq-local treesit-defun-type-regexp (rx (or "class_declaration" "method_definition" "function_declaration" "lexical_declaration"))) + ;; Font-lock. (setq-local treesit-font-lock-settings ts-mode--font-lock-settings) (setq-local treesit-font-lock-feature-list - '((comment declaration) - (string keyword identifier expression constant) - (property pattern jsx))) + ts-mode-font-lock-feature-list) + ;; Imenu. (setq-local imenu-create-index-function #'js--treesit-imenu) + ;; Which-func (use imenu). (setq-local which-func-functions nil) + (treesit-major-mode-setup)) + ;; Elisp. (t (js-mode) -- 2.34.1 --=-=-=--