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#59617: 29.0.50; Add indent styles to all tree-sitter modes Date: Sat, 26 Nov 2022 19:59:34 +0100 Message-ID: <87r0xp4k4p.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="6529"; mail-complaints-to="usenet@ciao.gmane.io" Cc: casouri@gmail.com To: 59617@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 26 20:00:20 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 1oz0PT-0001Sr-TK for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 26 Nov 2022 20:00:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oz0PJ-0005Xf-6R; Sat, 26 Nov 2022 14:00:09 -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 1oz0PH-0005XF-9B for bug-gnu-emacs@gnu.org; Sat, 26 Nov 2022 14:00:07 -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 1oz0PG-0001TQ-ND for bug-gnu-emacs@gnu.org; Sat, 26 Nov 2022 14:00:06 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oz0PC-0003mT-Gl; Sat, 26 Nov 2022 14:00:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: casouri@gmail.com, bug-gnu-emacs@gnu.org Resent-Date: Sat, 26 Nov 2022 19:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 59617 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: casouri@gmail.com Original-Received: via spool by submit@debbugs.gnu.org id=B.166948920014495 (code B ref -1); Sat, 26 Nov 2022 19:00:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 26 Nov 2022 19:00:00 +0000 Original-Received: from localhost ([127.0.0.1]:41434 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oz0PA-0003lj-47 for submit@debbugs.gnu.org; Sat, 26 Nov 2022 14:00:00 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:45108) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oz0P4-0003lb-ND for submit@debbugs.gnu.org; Sat, 26 Nov 2022 13:59:58 -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 1oz0P4-00059g-5E for bug-gnu-emacs@gnu.org; Sat, 26 Nov 2022 13:59:54 -0500 Original-Received: from out-218.mta0.migadu.com ([2001:41d0:1004:224b::da]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oz0P0-0001OW-PA for bug-gnu-emacs@gnu.org; Sat, 26 Nov 2022 13:59:53 -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=1669489182; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=hHqJtJ5OUR8kaaSdhXfT+EXWKhoS56BfvXytuUkztzw=; b=dAKjnYkcM/1WulJeDOt++zr0abpsISLPX05VkAV0ID9k3YfKiDWKryeh3meCqkM1q9rEC3 r6TAJzkqogA0Pn3k3GdKBV2zmdJuajuNW+VOrWooz9NSCQOJhyPrjQ9HwVxTc31UNn/knI fZvS5PQHfXEX6nPU5LCQbjnTQRSxqUt0CHA8Z8niKeg3cOMgJTpVlbXkezmtlWt17EiLJo TNxpyeqYlZ3+b7ocCrD4/kCwKKxtZ3UUZ5V6aMmZ8zF2NMbWDKCu0R7cSclLHCmfglrCBG dikK8bbG+bwvHs/OAOyWyF3eLfQMkjfTPgS9VNT+bVKTC8V34hMbGuAWmZwmbg== X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:1004:224b::da; envelope-from=theo@thornhill.no; helo=out-218.mta0.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_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:249127 Archived-At: --=-=-= Content-Type: text/plain Hi Yuan and others! I've added in the ability for users to set their own indentation style should that be needed. I just followed what I already did in c-ts-mode and c++-ts-mode, so I believe this shouldn't be too controversial :-) Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-indent-styles-to-all-tree-sitter-modes.patch >From 5f5695916b71af4f9e778f4bd5c51edb2c85067e Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sat, 26 Nov 2022 19:54:47 +0100 Subject: [PATCH] Add indent styles to all tree-sitter modes Enable users to provide their own function to control the indentation style. Even though we don't supply any extra styles for indentation, this should give the users an escape hatch should the provided style not suit their needs. * lisp/progmodes/c-ts-mode.el (c-ts-mode-indent-style): Fix typo. * lisp/progmodes/csharp-mode.el (csharp-ts-mode-indent-style) (csharp-ts-mode--set-indent-style, csharp-ts-mode): New defcustom and helper. * lisp/progmodes/java-ts-mode.el (java-ts-mode-indent-style) (java-ts-mode--set-indent-style, java-ts-mode): New defcustom and helper. * lisp/progmodes/js.el (js-ts-mode-indent-style) (js--treesit-set-indent-style, js-ts-mode): New defcustom and helper. * lisp/progmodes/json-ts-mode.el (json-ts-mode-indent-style) (json-ts-mode--set-indent-style, json-ts-mode): Enable custom indent style. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode-indent-style) (typescript-ts-mode--set-indent-style, typescript-ts-mode): Enable custom indent style. * lisp/progmodes/css.el (css-ts-mode-indent-style) (css--treesit-set-indent-style, css-ts-mode): New defcustom and helper. --- lisp/progmodes/c-ts-mode.el | 2 +- lisp/progmodes/csharp-mode.el | 21 ++++++++++++++++++++- lisp/progmodes/java-ts-mode.el | 21 ++++++++++++++++++++- lisp/progmodes/js.el | 21 ++++++++++++++++++++- lisp/progmodes/json-ts-mode.el | 21 ++++++++++++++++++++- lisp/progmodes/typescript-ts-mode.el | 21 ++++++++++++++++++++- lisp/textmodes/css-mode.el | 21 ++++++++++++++++++++- 7 files changed, 121 insertions(+), 7 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index fc35d9aedd..c384182772 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -52,7 +52,7 @@ c-ts-mode-indent-style The selected style could be one of GNU, K&R, LINUX or BSD. If one of the supplied styles doesn't suffice a function could be -set instead. This function is expected return a list that +set instead. This function is expected to return a list that follows the form of `treesit-simple-indent-rules'." :version "29.1" :type '(choice (symbol :tag "Gnu" 'gnu) diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el index a544a4b5cb..a6373b5826 100644 --- a/lisp/progmodes/csharp-mode.el +++ b/lisp/progmodes/csharp-mode.el @@ -696,6 +696,24 @@ csharp-ts-mode-indent-offset :safe 'integerp :group 'csharp) +(defcustom csharp-ts-mode-indent-style 'csharp-ts-mode--default-style + "Style used for indentation. + +The selected style could either be the default style, or a +function could be set instead. This function is expected to +return a list that follows the form of +`treesit-simple-indent-rules'." + :version "29.1" + :type '(choice (symbol :tag "Default" 'csharp-ts-mode--default-style) + (function :tag "A function for user customized style" ignore)) + :group 'csharp) + +(defun csharp-ts-mode--set-indent-style () + "Helper function to set indentation style." + (if (functionp csharp-ts-mode-indent-style) + (funcall csharp-ts-mode-indent-style) + csharp-ts-mode--indent-rules)) + (defvar csharp-ts-mode--indent-rules `((c-sharp ((parent-is "compilation_unit") parent-bol 0) @@ -962,7 +980,8 @@ csharp-ts-mode (setq-local comment-end "") ;; Indent. - (setq-local treesit-simple-indent-rules csharp-ts-mode--indent-rules) + (setq-local treesit-simple-indent-rules + (csharp-ts-mode--set-indent-style)) ;; Electric (setq-local electric-indent-chars diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index ee2934f53c..29de2ccb28 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -42,6 +42,24 @@ java-ts-mode-indent-offset :safe 'integerp :group 'java) +(defcustom java-ts-mode-indent-style 'java-ts-mode--default-style + "Style used for indentation. + +The selected style could either be the default style, or a +function could be set instead. This function is expected to +return a list that follows the form of +`treesit-simple-indent-rules'." + :version "29.1" + :type '(choice (symbol :tag "Default" 'java-ts-mode--default-style) + (function :tag "A function for user customized style" ignore)) + :group 'java) + +(defun java-ts-mode--set-indent-style () + "Helper function to set indentation style." + (if (functionp java-ts-mode-indent-style) + (funcall java-ts-mode-indent-style) + java-ts-mode--indent-rules)) + (defvar java-ts-mode--syntax-table (let ((table (make-syntax-table))) ;; Taken from the cc-langs version @@ -305,7 +323,8 @@ java-ts-mode (setq-local treesit-comment-end (rx (+ (or "*")) "/")) ;; Indent. - (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules) + (setq-local treesit-simple-indent-rules + (java-ts-mode--set-indent-style)) ;; Electric (setq-local electric-indent-chars diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index da05b7b364..9535d4986f 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3457,6 +3457,24 @@ js--treesit-indent-rules ((node-is "/") parent 0) ((parent-is "jsx_self_closing_element") parent js-indent-level))))) +(defcustom js-ts-mode-indent-style 'js--treesit-default-style + "Style used for indentation. + +The selected style could either be the default style, or a +function could be set instead. This function is expected to +return a list that follows the form of +`treesit-simple-indent-rules'." + :version "29.1" + :type '(choice (symbol :tag "Default" 'js--treesit-default-style) + (function :tag "A function for user customized style" ignore)) + :group 'javascript) + +(defun js--treesit-set-indent-style () + "Helper function to set indentation style." + (if (functionp js-ts-mode-indent-style) + (funcall js-ts-mode-indent-style) + js--treesit-indent-rules)) + (defvar js--treesit-keywords '("as" "async" "await" "break" "case" "catch" "class" "const" "continue" "debugger" "default" "delete" "do" "else" "export" "extends" "finally" @@ -3862,7 +3880,8 @@ js-ts-mode ;; Tree-sitter setup. (treesit-parser-create 'javascript) ;; Indent. - (setq-local treesit-simple-indent-rules js--treesit-indent-rules) + (setq-local treesit-simple-indent-rules + (js--treesit-set-indent-style)) ;; Navigation. (setq-local treesit-defun-type-regexp (rx (or "class_declaration" diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el index 101e873cf6..4ee2cad9da 100644 --- a/lisp/progmodes/json-ts-mode.el +++ b/lisp/progmodes/json-ts-mode.el @@ -44,6 +44,24 @@ json-ts-mode-indent-offset :safe 'integerp :group 'json) +(defcustom json-ts-mode-indent-style 'json-ts-mode--default-style + "Style used for indentation. + +The selected style could either be the default style, or a +function could be set instead. This function is expected to +return a list that follows the form of +`treesit-simple-indent-rules'." + :version "29.1" + :type '(choice (symbol :tag "Default" 'json-ts-mode--default-style) + (function :tag "A function for user customized style" ignore)) + :group 'json) + +(defun json-ts-mode--set-indent-style () + "Helper function to set indentation style." + (if (functionp json-ts-mode-indent-style) + (funcall json-ts-mode-indent-style) + json-ts-mode--indent-rules)) + (defvar json-ts-mode--syntax-table (let ((table (make-syntax-table))) ;; Taken from the cc-langs version @@ -147,7 +165,8 @@ json-ts-mode (append "{}():;," electric-indent-chars)) ;; Indent. - (setq-local treesit-simple-indent-rules json-ts--indent-rules) + (setq-local treesit-simple-indent-rules + (json-ts-mode--set-indent-style)) ;; Navigation. (setq-local treesit-defun-type-regexp diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 763686bf66..106b8363f0 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -37,6 +37,24 @@ typescript-ts-mode-indent-offset :safe 'integerp :group 'typescript) +(defcustom typescript-ts-mode-indent-style 'typescript-ts-mode--default-style + "Style used for indentation. + +The selected style could either be the default style, or a +function could be set instead. This function is expected to +return a list that follows the form of +`treesit-simple-indent-rules'." + :version "29.1" + :type '(choice (symbol :tag "Default" 'typescript-ts-mode--default-style) + (function :tag "A function for user customized style" ignore)) + :group 'typescript) + +(defun typescript-ts-mode--set-indent-style () + "Helper function to set indentation style." + (if (functionp typescript-ts-mode-indent-style) + (funcall typescript-ts-mode-indent-style) + typescript-ts-mode--indent-rules)) + (defvar typescript-ts-mode--syntax-table (let ((table (make-syntax-table))) ;; Taken from the cc-langs version @@ -308,7 +326,8 @@ typescript-ts-mode (append "{}():;," electric-indent-chars)) ;; Indent. - (setq-local treesit-simple-indent-rules typescript-ts-mode--indent-rules) + (setq-local treesit-simple-indent-rules + (typescript-ts-mode--set-indent-style)) ;; Navigation. (setq-local treesit-defun-type-regexp diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el index 734252ee66..eaf3fec939 100644 --- a/lisp/textmodes/css-mode.el +++ b/lisp/textmodes/css-mode.el @@ -1343,6 +1343,24 @@ css--treesit-indent-rules ((match nil "declaration" nil 3) (nth-sibling 2) 0))) "Tree-sitter indentation rules for `css-ts-mode'.") +(defcustom css-ts-mode-indent-style 'css--treesit-default-style + "Style used for indentation. + +The selected style could either be the default style, or a +function could be set instead. This function is expected to +return a list that follows the form of +`treesit-simple-indent-rules'." + :version "29.1" + :type '(choice (symbol :tag "Default" 'css--treesit-default-style) + (function :tag "A function for user customized style" ignore)) + :group 'css) + +(defun css--treesit-set-indent-style () + "Helper function to set indentation style." + (if (functionp css-ts-mode-indent-style) + (funcall css-ts-mode-indent-style) + css--treesit-indent-rules)) + (defvar css--treesit-settings (treesit-font-lock-rules :feature 'comment @@ -1804,7 +1822,8 @@ css-ts-mode ;; Tree-sitter specific setup. (treesit-parser-create 'css) - (setq-local treesit-simple-indent-rules css--treesit-indent-rules) + (setq-local treesit-simple-indent-rules + (css--treesit-set-indent-style)) (setq-local treesit-defun-type-regexp "rule_set") (setq-local treesit-font-lock-settings css--treesit-settings) (setq-local treesit-font-lock-feature-list -- 2.34.1 --=-=-=--