Fix some inconsistencies in *-ts-modes

(c-ts-mode--base-mode): Extract comment-* so that we can separate
between C and C++.
(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--imenu): Add categories.  Only display categories that
exist in the file.
(java-ts-mode): Add electric-indent.

* lisp/progmodes/json-ts-mode.el
(json-ts-mode): Add electric-indent.

* lisp/progmodes/ts-mode.el
(ts-mode--font-lock-settings): Whitespace cleanup.
(ts-mode): Add electric-indent.
This commit is contained in:
Theodor Thornhill 2022-11-14 08:03:11 +01:00 committed by Yuan Fu
parent 7e9c2e2952
commit 829c7e3566
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
5 changed files with 89 additions and 33 deletions

View file

@ -33,6 +33,7 @@
(defcustom c-ts-mode-indent-offset 2
"Number of spaces for each indentation step in `c-ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'c)
@ -44,6 +45,7 @@ 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
follows the form of `treesit-simple-indent-rules'."
:version "29.1"
:type '(choice (symbol :tag "Gnu" 'gnu)
(symbol :tag "K&R" 'k&r)
(symbol :tag "Linux" 'linux)
@ -396,14 +398,8 @@ the subtrees."
;;;###autoload
(define-derived-mode c-ts-mode--base-mode prog-mode "C"
"Major mode for editing C, powered by tree-sitter."
: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,7 +411,7 @@ the subtrees."
;; 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)
@ -436,6 +432,11 @@ the subtrees."
(treesit-parser-create 'c)
;; Comments.
(setq-local comment-start "/* ")
(setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
(setq-local comment-end " */")
(setq-local treesit-simple-indent-rules
(c-ts-mode--set-indent-style 'c))
@ -452,6 +453,11 @@ the subtrees."
(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 "")
(treesit-parser-create 'cpp)
(setq-local treesit-simple-indent-rules

View file

@ -34,6 +34,7 @@
(defcustom css-ts-mode-indent-offset 2
"Number of spaces for each indentation step in `ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'css)
@ -114,6 +115,10 @@ the subtrees."
;; 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.

View file

@ -29,10 +29,10 @@
;;; Code:
(require 'treesit)
(require 'rx)
(defcustom java-ts-mode-indent-offset 4
"Number of spaces for each indentation step in `java-ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'java)
@ -241,18 +241,38 @@ the subtrees."
(defun java-ts-mode--imenu ()
"Return Imenu alist for the current buffer."
(let* ((node (treesit-buffer-root-node))
(tree (treesit-induce-sparse-tree
node (rx (or "class_declaration"
"interface_declaration"
"enum_declaration"
"record_declaration"
"method_declaration")))))
(java-ts-mode--imenu-1 tree)))
(class-tree
`("Class" . ,(java-ts-mode--imenu-1
(treesit-induce-sparse-tree
node "^class_declaration$"))))
(interface-tree
`("Interface" . ,(java-ts-mode--imenu-1
(treesit-induce-sparse-tree
node "^interface_declaration$"))))
(enum-tree
`("Enum" . ,(java-ts-mode--imenu-1
(treesit-induce-sparse-tree
node "^enum_declaration$"))))
(record-tree
`("Record" . ,(java-ts-mode--imenu-1
(treesit-induce-sparse-tree
node "^record_declaration$"))))
(method-tree
`("Method" . ,(java-ts-mode--imenu-1
(treesit-induce-sparse-tree
node "^method_declaration$")))))
(cl-remove-if
#'null
`(,(when (cdr class-tree) class-tree)
,(when (cdr interface-tree) interface-tree)
,(when (cdr enum-tree) enum-tree)
,(when (cdr record-tree) record-tree)
,(when (cdr method-tree) method-tree)))))
;;;###autoload
(define-derived-mode java-ts-mode prog-mode "Java"
"Major mode for editing Java, powered by tree-sitter."
:group 'c
:group 'java
:syntax-table java-ts-mode--syntax-table
(unless (treesit-ready-p nil 'java)
@ -268,9 +288,12 @@ the subtrees."
;; 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")))
(setq-local treesit-defun-type-regexp "declaration")
;; Font-lock.
(setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings)

View file

@ -33,6 +33,7 @@
(defcustom json-ts-mode-indent-offset 2
"Number of spaces for each indentation step in `json-ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'json)
@ -67,20 +68,26 @@
(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 +134,10 @@ the subtrees."
(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 +148,7 @@ the subtrees."
;; Font-lock.
(setq-local treesit-font-lock-settings json-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list
'((minimal) () ()))
'((comment string number) (constant pair) ()))
;; Imenu.
(setq-local imenu-create-index-function #'json-ts-mode--imenu)

View file

@ -30,6 +30,7 @@
(defcustom ts-mode-indent-offset 2
"Number of spaces for each indentation step in `ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'typescript)
@ -100,7 +101,6 @@
(defvar ts-mode--font-lock-settings
(treesit-font-lock-rules
:language 'tsx
:override t
:feature 'comment
@ -271,18 +271,26 @@
((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
@ -291,9 +299,12 @@
(property pattern jsx)))
;; 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)