From 7690dbbd4359e4bc7dad3272cc4190af0692961a 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. (json-ts-mode--indent-rules): Rename from 'json-ts--indent-rules' * 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. (Bug#59617) --- 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 | 23 +++++++++++++++++++++-- lisp/progmodes/typescript-ts-mode.el | 21 ++++++++++++++++++++- lisp/textmodes/css-mode.el | 21 ++++++++++++++++++++- 7 files changed, 122 insertions(+), 8 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index a79dabcd31..b72c17a8bd 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 3da3079f08..426ac141a0 100644 --- a/lisp/progmodes/csharp-mode.el +++ b/lisp/progmodes/csharp-mode.el @@ -626,6 +626,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 takes no arguments +and 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) @@ -908,7 +926,8 @@ csharp-ts-mode (seq (+ "*") "/"))))) ;; 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 cf2482bb6e..9806b23d34 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -43,6 +43,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 takes no arguments +and 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 @@ -309,7 +327,8 @@ java-ts-mode (seq (+ "*") "/"))))) ;; 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 ad1fe62d42..1f25412796 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 takes no arguments +and 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" @@ -3865,7 +3883,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..0a6debf788 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 takes no arguments +and 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 @@ -64,7 +82,7 @@ json-ts-mode--syntax-table "Syntax table for `json-ts-mode'.") -(defvar json-ts--indent-rules +(defvar json-ts-mode--indent-rules `((json ((node-is "}") parent-bol 0) ((node-is ")") parent-bol 0) @@ -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 6c926a4e3e..cf4e8512c0 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 takes no arguments +and 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 @@ -313,7 +331,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 b82886e397..aaf1c91443 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 takes no arguments +and 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 @@ -1832,7 +1850,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