summaryrefslogtreecommitdiff
path: root/lisp/progmodes
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes')
-rw-r--r--lisp/progmodes/c-ts-mode.el230
-rw-r--r--lisp/progmodes/csharp-mode.el73
-rw-r--r--lisp/progmodes/java-ts-mode.el68
-rw-r--r--lisp/progmodes/js.el98
-rw-r--r--lisp/progmodes/json-ts-mode.el40
-rw-r--r--lisp/progmodes/python.el43
-rw-r--r--lisp/progmodes/rust-ts-mode.el73
-rw-r--r--lisp/progmodes/scheme.el20
-rw-r--r--lisp/progmodes/sh-script.el2
-rw-r--r--lisp/progmodes/typescript-ts-mode.el32
10 files changed, 221 insertions, 458 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 161e01d4411..73e488a8058 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -487,92 +487,44 @@ For NODE, OVERRIDE, START, and END, see
(defun c-ts-mode--defun-name (node)
"Return the name of the defun NODE.
-Return nil if NODE is not a defun node, return an empty string if
-NODE doesn't have a name."
+Return nil if NODE is not a defun node or doesn't have a name."
(treesit-node-text
(pcase (treesit-node-type node)
((or "function_definition" "declaration")
(c-ts-mode--declarator-identifier
(treesit-node-child-by-field-name node "declarator")))
- ("struct_specifier"
+ ((or "struct_specifier" "enum_specifier"
+ "union_specifier" "class_specifier")
(treesit-node-child-by-field-name node "name")))
t))
-(defun c-ts-mode--imenu-1 (node)
- "Helper for `c-ts-mode--imenu'.
-Find string representation for NODE and set marker, then recurse
-the subtrees."
- (let* ((ts-node (car node))
- (subtrees (mapcan #'c-ts-mode--imenu-1 (cdr node)))
- (name (when ts-node
- (treesit-defun-name ts-node)))
- (marker (when ts-node
- (set-marker (make-marker)
- (treesit-node-start ts-node)))))
- (cond
- ;; A struct_specifier could be inside a parameter list, another
- ;; struct definition, a variable declaration, a function
- ;; declaration. In those cases we don't include it.
- ((string-match-p
- (rx (or "parameter_declaration" "field_declaration"
- "declaration" "function_definition"))
- (or (treesit-node-type (treesit-node-parent ts-node))
- ""))
- nil)
- ;; Ignore function local variable declarations.
- ((and (equal (treesit-node-type ts-node) "declaration")
- (not (equal (treesit-node-type (treesit-node-parent ts-node))
- "translation_unit")))
- nil)
- ((or (null ts-node) (null name)) subtrees)
- (subtrees
- `((,name ,(cons name marker) ,@subtrees)))
- (t
- `((,name . ,marker))))))
-
-(defun c-ts-mode--imenu ()
- "Return Imenu alist for the current buffer."
- (let* ((node (treesit-buffer-root-node))
- (func-tree (treesit-induce-sparse-tree
- node "^function_definition$" nil 1000))
- (var-tree (treesit-induce-sparse-tree
- node "^declaration$" nil 1000))
- (struct-tree (treesit-induce-sparse-tree
- node "^struct_specifier$" nil 1000))
- (func-index (c-ts-mode--imenu-1 func-tree))
- (var-index (c-ts-mode--imenu-1 var-tree))
- (struct-index (c-ts-mode--imenu-1 struct-tree)))
- (append
- (when struct-index `(("Struct" . ,struct-index)))
- (when var-index `(("Variable" . ,var-index)))
- (when func-index `(("Function" . ,func-index))))))
-
;;; Defun navigation
-(defun c-ts-mode--end-of-defun ()
- "`end-of-defun-function' of `c-ts-mode'."
- ;; A struct/enum/union_specifier node doesn't include the ; at the
- ;; end, so we manually skip it.
- (treesit-end-of-defun)
- (when (looking-at (rx (* " ") ";"))
- (goto-char (match-end 0))
- ;; This part is copied from `end-of-defun'.
- (unless (bolp)
- (skip-chars-forward " \t")
- (if (looking-at "\\s<\\|\n")
- (forward-line 1)))))
-
(defun c-ts-mode--defun-valid-p (node)
- (if (string-match-p
- (rx (or "struct_specifier"
- "enum_specifier"
- "union_specifier"))
- (treesit-node-type node))
- (null
- (treesit-node-top-level
- node (rx (or "function_definition"
- "type_definition"))))
- t))
+ "Return non-nil if NODE is a valid defun node.
+Ie, NODE is not nested."
+ (not (or (and (member (treesit-node-type node)
+ '("struct_specifier"
+ "enum_specifier"
+ "union_specifier"
+ "declaration"))
+ ;; If NODE's type is one of the above, make sure it is
+ ;; top-level.
+ (treesit-node-top-level
+ node (rx (or "function_definition"
+ "type_definition"
+ "struct_specifier"
+ "enum_specifier"
+ "union_specifier"
+ "declaration"))))
+
+ (and (equal (treesit-node-type node) "declaration")
+ ;; If NODE is a declaration, make sure it is not a
+ ;; function declaration.
+ (equal (treesit-node-type
+ (treesit-node-child-by-field-name
+ node "declarator"))
+ "function_declarator")))))
(defun c-ts-mode--defun-skipper ()
"Custom defun skipper for `c-ts-mode' and friends.
@@ -660,6 +612,59 @@ ARG is passed to `fill-paragraph'."
;; itself.
t)))
+(defun c-ts-mode-comment-setup ()
+ "Set up local variables for C-like comment.
+
+Set up:
+ - `comment-start'
+ - `comment-end'
+ - `comment-start-skip'
+ - `comment-end-skip'
+ - `adaptive-fill-mode'
+ - `adaptive-fill-first-line-regexp'
+ - `paragraph-start'
+ - `paragraph-separate'
+ - `fill-paragraph-function'"
+ (setq-local comment-start "// ")
+ (setq-local comment-end "")
+ (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
+ (seq "/" (+ "*")))
+ (* (syntax whitespace))))
+ (setq-local comment-end-skip
+ (rx (* (syntax whitespace))
+ (group (or (syntax comment-end)
+ (seq (+ "*") "/")))))
+ (setq-local adaptive-fill-mode t)
+ ;; This matches (1) empty spaces (the default), (2) "//", (3) "*",
+ ;; but do not match "/*", because we don't want to use "/*" as
+ ;; prefix when filling. (Actually, it doesn't matter, because
+ ;; `comment-start-skip' matches "/*" which will cause
+ ;; `fill-context-prefix' to use "/*" as a prefix for filling, that's
+ ;; why we mask the "/*" in `c-ts-mode--fill-paragraph'.)
+ (setq-local adaptive-fill-regexp
+ (concat (rx (* (syntax whitespace))
+ (group (or (seq "/" (+ "/")) (* "*"))))
+ adaptive-fill-regexp))
+ ;; Same as `adaptive-fill-regexp'.
+ (setq-local adaptive-fill-first-line-regexp
+ (rx bos
+ (seq (* (syntax whitespace))
+ (group (or (seq "/" (+ "/")) (* "*")))
+ (* (syntax whitespace)))
+ eos))
+ ;; Same as `adaptive-fill-regexp'.
+ (setq-local paragraph-start
+ (rx (or (seq (* (syntax whitespace))
+ (group (or (seq "/" (+ "/")) (* "*")))
+ (* (syntax whitespace))
+ ;; Add this eol so that in
+ ;; `fill-context-prefix', `paragraph-start'
+ ;; doesn't match the prefix.
+ eol)
+ "\f")))
+ (setq-local paragraph-separate paragraph-start)
+ (setq-local fill-paragraph-function #'c-ts-mode--fill-paragraph))
+
;;; Modes
(defvar-keymap c-ts-mode-map
@@ -694,44 +699,25 @@ ARG is passed to `fill-paragraph'."
(when (eq c-ts-mode-indent-style 'linux)
(setq-local indent-tabs-mode t))
- (setq-local adaptive-fill-mode t)
- ;; This matches (1) empty spaces (the default), (2) "//", (3) "*",
- ;; but do not match "/*", because we don't want to use "/*" as
- ;; prefix when filling. (Actually, it doesn't matter, because
- ;; `comment-start-skip' matches "/*" which will cause
- ;; `fill-context-prefix' to use "/*" as a prefix for filling, that's
- ;; why we mask the "/*" in `c-ts-mode--fill-paragraph'.)
- (setq-local adaptive-fill-regexp
- (concat (rx (* (syntax whitespace))
- (group (or (seq "/" (+ "/")) (* "*"))))
- adaptive-fill-regexp))
- ;; Same as `adaptive-fill-regexp'.
- (setq-local adaptive-fill-first-line-regexp
- (rx bos
- (seq (* (syntax whitespace))
- (group (or (seq "/" (+ "/")) (* "*")))
- (* (syntax whitespace)))
- eos))
- ;; Same as `adaptive-fill-regexp'.
- (setq-local paragraph-start
- (rx (or (seq (* (syntax whitespace))
- (group (or (seq "/" (+ "/")) (* "*")))
- (* (syntax whitespace))
- ;; Add this eol so that in
- ;; `fill-context-prefix', `paragraph-start'
- ;; doesn't match the prefix.
- eol)
- "\f")))
- (setq-local paragraph-separate paragraph-start)
- (setq-local fill-paragraph-function #'c-ts-mode--fill-paragraph)
+ ;; Comment
+ (c-ts-mode-comment-setup)
;; Electric
(setq-local 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-simple-imenu-settings
+ (let ((pred #'c-ts-mode--defun-valid-p))
+ `(("Struct" ,(rx bos (or "struct" "enum" "union")
+ "_specifier" eos)
+ ,pred nil)
+ ("Variable" ,(rx bos "declaration" eos) ,pred nil)
+ ("Function" "\\`function_definition\\'" ,pred nil)
+ ("Class" ,(rx bos (or "class_specifier"
+ "function_definition")
+ eos)
+ ,pred nil))))
(setq-local treesit-font-lock-feature-list
'(( comment definition)
@@ -752,13 +738,6 @@ ARG is passed to `fill-paragraph'."
;; Comments.
(setq-local comment-start "/* ")
(setq-local comment-end " */")
- (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
- (seq "/" (+ "*")))
- (* (syntax whitespace))))
- (setq-local comment-end-skip
- (rx (* (syntax whitespace))
- (group (or (syntax comment-end)
- (seq (+ "*") "/")))))
(setq-local treesit-simple-indent-rules
(c-ts-mode--set-indent-style 'c))
@@ -766,11 +745,7 @@ ARG is passed to `fill-paragraph'."
;; Font-lock.
(setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'c))
- (treesit-major-mode-setup)
-
- ;; Override default value of end-of-defun-function set by
- ;; `treesit-major-mode-setup'.
- (setq-local end-of-defun-function #'c-ts-mode--end-of-defun))
+ (treesit-major-mode-setup))
;;;###autoload
(define-derived-mode c++-ts-mode c-ts-base-mode "C++"
@@ -781,17 +756,6 @@ ARG is passed to `fill-paragraph'."
(unless (treesit-ready-p 'cpp)
(error "Tree-sitter for C++ isn't available"))
- ;; Comments.
- (setq-local comment-start "// ")
- (setq-local comment-end "")
- (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
- (seq "/" (+ "*")))
- (* (syntax whitespace))))
- (setq-local comment-end-skip
- (rx (* (syntax whitespace))
- (group (or (syntax comment-end)
- (seq (+ "*") "/")))))
-
(setq-local treesit-text-type-regexp
(regexp-opt '("comment"
"raw_string_literal")))
@@ -804,11 +768,7 @@ ARG is passed to `fill-paragraph'."
;; Font-lock.
(setq-local treesit-font-lock-settings (c-ts-mode--font-lock-settings 'cpp))
- (treesit-major-mode-setup)
-
- ;; Override default value of end-of-defun-function set by
- ;; `treesit-major-mode-setup'.
- (setq-local end-of-defun-function #'c-ts-mode--end-of-defun))
+ (treesit-major-mode-setup))
(provide 'c-ts-mode)
diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el
index dd2d877c969..33a5f7046f1 100644
--- a/lisp/progmodes/csharp-mode.el
+++ b/lisp/progmodes/csharp-mode.el
@@ -34,6 +34,7 @@
(require 'cc-mode)
(require 'cc-langs)
(require 'treesit)
+(require 'c-ts-mode) ; For comment indenting and filling.
(eval-when-compile
(require 'cc-fonts)
@@ -42,6 +43,7 @@
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-start "treesit.c")
+(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(defgroup csharp nil
@@ -632,6 +634,9 @@ compilation and evaluation time conflicts."
((node-is "}") parent-bol 0)
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
+ ((and (parent-is "comment") c-ts-mode--looking-at-star)
+ c-ts-mode--comment-start-after-first-star -1)
+ ((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "namespace_declaration") parent-bol 0)
((parent-is "class_declaration") parent-bol 0)
((parent-is "constructor_declaration") parent-bol 0)
@@ -853,54 +858,6 @@ Return nil if there is no name or if NODE is not a defun node."
node "name")
t))))
-(defun csharp-ts-mode--imenu-1 (node)
- "Helper for `csharp-ts-mode--imenu'.
-Find string representation for NODE and set marker, then recurse
-the subtrees."
- (let* ((ts-node (car node))
- (subtrees (mapcan #'csharp-ts-mode--imenu-1 (cdr node)))
- (name (when ts-node
- (or (treesit-defun-name ts-node)
- "Unnamed node")))
- (marker (when ts-node
- (set-marker (make-marker)
- (treesit-node-start ts-node)))))
- (cond
- ((null ts-node) subtrees)
- (subtrees
- `((,name ,(cons name marker) ,@subtrees)))
- (t
- `((,name . ,marker))))))
-
-(defun csharp-ts-mode--imenu ()
- "Return Imenu alist for the current buffer."
- (let* ((node (treesit-buffer-root-node))
- (class-tree (treesit-induce-sparse-tree
- node "^class_declaration$" nil 1000))
- (interface-tree (treesit-induce-sparse-tree
- node "^interface_declaration$" nil 1000))
- (enum-tree (treesit-induce-sparse-tree
- node "^enum_declaration$" nil 1000))
- (struct-tree (treesit-induce-sparse-tree
- node "^struct_declaration$" nil 1000))
- (record-tree (treesit-induce-sparse-tree
- node "^record_declaration$" nil 1000))
- (method-tree (treesit-induce-sparse-tree
- node "^method_declaration$" nil 1000))
- (class-index (csharp-ts-mode--imenu-1 class-tree))
- (interface-index (csharp-ts-mode--imenu-1 interface-tree))
- (enum-index (csharp-ts-mode--imenu-1 enum-tree))
- (record-index (csharp-ts-mode--imenu-1 record-tree))
- (struct-index (csharp-ts-mode--imenu-1 struct-tree))
- (method-index (csharp-ts-mode--imenu-1 method-tree)))
- (append
- (when class-index `(("Class" . ,class-index)))
- (when interface-index `(("Interface" . ,interface-index)))
- (when enum-index `(("Enum" . ,enum-index)))
- (when record-index `(("Record" . ,record-index)))
- (when struct-index `(("Struct" . ,struct-index)))
- (when method-index `(("Method" . ,method-index))))))
-
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.cs\\'" . csharp-mode))
@@ -929,15 +886,7 @@ Key bindings:
(treesit-parser-create 'c-sharp)
;; Comments.
- (setq-local comment-start "// ")
- (setq-local comment-end "")
- (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
- (seq "/" (+ "*")))
- (* (syntax whitespace))))
- (setq-local comment-end-skip
- (rx (* (syntax whitespace))
- (group (or (syntax comment-end)
- (seq (+ "*") "/")))))
+ (c-ts-mode-comment-setup)
(setq-local treesit-text-type-regexp
(regexp-opt '("comment"
@@ -964,8 +913,14 @@ Key bindings:
( bracket delimiter)))
;; Imenu.
- (setq-local imenu-create-index-function #'csharp-ts-mode--imenu)
- (setq-local which-func-functions nil) ;; Piggyback on imenu
+ (setq-local treesit-simple-imenu-settings
+ '(("Class" "\\`class_declaration\\'" nil nil)
+ ("Interface" "\\`interface_declaration\\'" nil nil)
+ ("Enum" "\\`enum_declaration\\'" nil nil)
+ ("Record" "\\`record_declaration\\'" nil nil)
+ ("Struct" "\\`struct_declaration\\'" nil nil)
+ ("Method" "\\`method_declaration\\'" nil nil)))
+
(treesit-major-mode-setup))
(provide 'csharp-mode)
diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index b6f747cf1cc..215b5c16388 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -29,10 +29,12 @@
(require 'treesit)
(eval-when-compile (require 'rx))
+(require 'c-ts-mode) ; For comment indent and filling.
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-start "treesit.c")
+(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(defcustom java-ts-mode-indent-offset 4
@@ -71,8 +73,9 @@
((node-is "}") (and parent parent-bol) 0)
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
- ((and (parent-is "comment") comment-end) comment-start -1)
- ((parent-is "comment") comment-start-skip 0)
+ ((and (parent-is "comment") c-ts-mode--looking-at-star)
+ c-ts-mode--comment-start-after-first-star -1)
+ ((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "text_block") no-indent)
((parent-is "class_body") parent-bol java-ts-mode-indent-offset)
((parent-is "interface_body") parent-bol java-ts-mode-indent-offset)
@@ -264,50 +267,6 @@ Return nil if there is no name or if NODE is not a defun node."
(treesit-node-child-by-field-name node "name")
t))))
-(defun java-ts-mode--imenu-1 (node)
- "Helper for `java-ts-mode--imenu'.
-Find string representation for NODE and set marker, then recurse
-the subtrees."
- (let* ((ts-node (car node))
- (subtrees (mapcan #'java-ts-mode--imenu-1 (cdr node)))
- (name (when ts-node
- (or (treesit-defun-name ts-node)
- "Unnamed node")))
- (marker (when ts-node
- (set-marker (make-marker)
- (treesit-node-start ts-node)))))
- (cond
- ((null ts-node) subtrees)
- (subtrees
- `((,name ,(cons name marker) ,@subtrees)))
- (t
- `((,name . ,marker))))))
-
-(defun java-ts-mode--imenu ()
- "Return Imenu alist for the current buffer."
- (let* ((node (treesit-buffer-root-node))
- (class-tree (treesit-induce-sparse-tree
- node "^class_declaration$" nil 1000))
- (interface-tree (treesit-induce-sparse-tree
- node "^interface_declaration$" nil 1000))
- (enum-tree (treesit-induce-sparse-tree
- node "^enum_declaration$" nil 1000))
- (record-tree (treesit-induce-sparse-tree
- node "^record_declaration$" nil 1000))
- (method-tree (treesit-induce-sparse-tree
- node "^method_declaration$" nil 1000))
- (class-index (java-ts-mode--imenu-1 class-tree))
- (interface-index (java-ts-mode--imenu-1 interface-tree))
- (enum-index (java-ts-mode--imenu-1 enum-tree))
- (record-index (java-ts-mode--imenu-1 record-tree))
- (method-index (java-ts-mode--imenu-1 method-tree)))
- (append
- (when class-index `(("Class" . ,class-index)))
- (when interface-index `(("Interface" . ,interface-index)))
- (when enum-index `(("Enum" . ,enum-index)))
- (when record-index `(("Record" . ,record-index)))
- (when method-index `(("Method" . ,method-index))))))
-
;;;###autoload
(define-derived-mode java-ts-mode prog-mode "Java"
"Major mode for editing Java, powered by tree-sitter."
@@ -320,15 +279,7 @@ the subtrees."
(treesit-parser-create 'java)
;; Comments.
- (setq-local comment-start "// ")
- (setq-local comment-end "")
- (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
- (seq "/" (+ "*")))
- (* (syntax whitespace))))
- (setq-local comment-end-skip
- (rx (* (syntax whitespace))
- (group (or (syntax comment-end)
- (seq (+ "*") "/")))))
+ (c-ts-mode-comment-setup)
(setq-local treesit-text-type-regexp
(regexp-opt '("line_comment"
@@ -363,8 +314,11 @@ the subtrees."
( bracket delimiter operator)))
;; Imenu.
- (setq-local imenu-create-index-function #'java-ts-mode--imenu)
- (setq-local which-func-functions nil) ;; Piggyback on imenu
+ (setq-local treesit-simple-imenu-settings
+ '(("Class" "\\`class_declaration\\'" nil nil)
+ ("Interface" "\\`interface_declaration\\'" nil nil)
+ ("Enum" "\\`record_declaration\\'" nil nil)
+ ("Method" "\\`method_declaration\\'" nil nil)))
(treesit-major-mode-setup))
(provide 'java-ts-mode)
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index 229bd53e1ed..653038a09e3 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -54,6 +54,7 @@
(require 'json)
(require 'prog-mode)
(require 'treesit)
+(require 'c-ts-mode) ; For comment indent and filling.
(eval-when-compile
(require 'cl-lib)
@@ -3425,9 +3426,9 @@ This function is intended for use in `after-change-functions'."
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
((node-is ">") parent-bol 0)
- ((parent-is "comment") comment-start 0)
- ((and (parent-is "comment") comment-end) comment-start -1)
- ((parent-is "comment") comment-start-skip 0)
+ ((and (parent-is "comment") c-ts-mode--looking-at-star)
+ c-ts-mode--comment-start-after-first-star -1)
+ ((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "ternary_expression") parent-bol js-indent-level)
((parent-is "member_expression") parent-bol js-indent-level)
((node-is ,switch-case) parent-bol 0)
@@ -3669,70 +3670,11 @@ Return nil if there is no name or if NODE is not a defun node."
"name")
t))
-(defun js--treesit-imenu-1 (node)
- "Given a sparse tree, create an imenu alist.
-
-NODE is the root node of the tree returned by
-`treesit-induce-sparse-tree' (not a tree-sitter node, its car is
-a tree-sitter node). Walk that tree and return an imenu alist.
-
-Return a list of ENTRY where
-
-ENTRY := (NAME . MARKER)
- | (NAME . ((JUMP-LABEL . MARKER)
- ENTRY
- ...)
-
-NAME is the function/class's name, JUMP-LABEL is like \"*function
-definition*\"."
- (let* ((ts-node (car node))
- (children (cdr node))
- (subtrees (mapcan #'js--treesit-imenu-1
- children))
- (type (pcase (treesit-node-type ts-node)
- ("lexical_declaration" 'variable)
- ("class_declaration" 'class)
- ("method_definition" 'method)
- ("function_declaration" 'function)))
- ;; The root of the tree could have a nil ts-node.
- (name (when ts-node
- (or (treesit-defun-name ts-node)
- "Anonymous")))
- (marker (when ts-node
- (set-marker (make-marker)
- (treesit-node-start ts-node)))))
- (cond
- ((null ts-node)
- subtrees)
- ;; Don't included non-top-level variable declarations.
- ((and (eq type 'variable)
- (treesit-node-top-level ts-node))
- nil)
- (subtrees
- `((,name
- ,(cons "" marker)
- ,@subtrees)))
- (t (list (cons name marker))))))
-
-(defun js--treesit-imenu ()
- "Return Imenu alist for the current buffer."
- (let* ((node (treesit-buffer-root-node))
- (class-tree (treesit-induce-sparse-tree
- node (rx (or "class_declaration"
- "method_definition"))
- nil 1000))
- (func-tree (treesit-induce-sparse-tree
- node "function_declaration" nil 1000))
- (var-tree (treesit-induce-sparse-tree
- node "lexical_declaration" nil 1000)))
- ;; When a sub-tree is empty, we should not return that pair at all.
- (append
- (and func-tree
- `(("Function" . ,(js--treesit-imenu-1 func-tree))))
- (and var-tree
- `(("Variable" . ,(js--treesit-imenu-1 var-tree))))
- (and class-tree
- `(("Class" . ,(js--treesit-imenu-1 class-tree)))))))
+(defun js--treesit-valid-imenu-entry (node)
+ "Return nil if NODE is a non-top-level \"lexical_declaration\"."
+ (pcase (treesit-node-type node)
+ ("lexical_declaration" (treesit-node-top-level node))
+ (_ t)))
;;; Main Function
@@ -3845,15 +3787,7 @@ Currently there are `js-mode' and `js-ts-mode'."
;; Which-func.
(setq-local which-func-imenu-joiner-function #'js--which-func-joiner)
;; Comment.
- (setq-local comment-start "// ")
- (setq-local comment-end "")
- (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
- (seq "/" (+ "*")))
- (* (syntax whitespace))))
- (setq-local comment-end-skip
- (rx (* (syntax whitespace))
- (group (or (syntax comment-end)
- (seq (+ "*") "/")))))
+ (c-ts-mode-comment-setup)
(setq-local comment-multi-line t)
(setq-local treesit-text-type-regexp
@@ -3887,10 +3821,14 @@ Currently there are `js-mode' and `js-ts-mode'."
identifier jsx number pattern property)
( bracket delimiter operator)))
;; Imenu
- (setq-local imenu-create-index-function
- #'js--treesit-imenu)
- ;; Which-func (use imenu).
- (setq-local which-func-functions nil)
+ (setq-local treesit-simple-imenu-settings
+ `(("Function" "\\`function_declaration\\'" nil nil)
+ ("Variable" "\\`lexical_declaration\\'"
+ js--treesit-valid-imenu-entry nil)
+ ("Class" ,(rx bos (or "class_declaration"
+ "method_definition")
+ eos)
+ nil nil)))
(treesit-major-mode-setup)))
;;;###autoload
diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el
index 6725c5f2270..adba2f820fa 100644
--- a/lisp/progmodes/json-ts-mode.el
+++ b/lisp/progmodes/json-ts-mode.el
@@ -33,6 +33,7 @@
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-start "treesit.c")
+(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
@@ -112,36 +113,11 @@
Return nil if there is no name or if NODE is not a defun node."
(pcase (treesit-node-type node)
((or "pair" "object")
- (treesit-node-text
- (treesit-node-child-by-field-name
- node "key")
- t))))
-
-(defun json-ts-mode--imenu-1 (node)
- "Helper for `json-ts-mode--imenu'.
-Find string representation for NODE and set marker, then recurse
-the subtrees."
- (let* ((ts-node (car node))
- (subtrees (mapcan #'json-ts-mode--imenu-1 (cdr node)))
- (name (when ts-node
- (or (treesit-defun-name ts-node)
- "Anonymous")))
- (marker (when ts-node
- (set-marker (make-marker)
- (treesit-node-start ts-node)))))
- (cond
- ((null ts-node) subtrees)
- (subtrees
- `((,name ,(cons name marker) ,@subtrees)))
- (t
- `((,name . ,marker))))))
-
-(defun json-ts-mode--imenu ()
- "Return Imenu alist for the current buffer."
- (let* ((node (treesit-buffer-root-node))
- (tree (treesit-induce-sparse-tree
- node "pair" nil 1000)))
- (json-ts-mode--imenu-1 tree)))
+ (string-trim (treesit-node-text
+ (treesit-node-child-by-field-name
+ node "key")
+ t)
+ "\"" "\""))))
;;;###autoload
(define-derived-mode json-ts-mode prog-mode "JSON"
@@ -179,8 +155,8 @@ the subtrees."
(bracket delimiter error)))
;; Imenu.
- (setq-local imenu-create-index-function #'json-ts-mode--imenu)
- (setq-local which-func-functions nil) ;; Piggyback on imenu
+ (setq-local treesit-simple-imenu-settings
+ '((nil "\\`pair\\'" nil nil)))
(treesit-major-mode-setup))
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 0cd0c6c225a..07f86d31551 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1080,7 +1080,6 @@ fontified."
:feature 'string
:language 'python
- :override t
'((string) @python--treesit-fontify-string)
:feature 'string-interpolation
@@ -1097,9 +1096,7 @@ fontified."
:feature 'function
:language 'python
- '((function_definition
- name: (identifier) @font-lock-function-name-face)
- (call function: (identifier) @font-lock-function-name-face)
+ '((call function: (identifier) @font-lock-function-name-face)
(call function: (attribute
attribute: (identifier) @font-lock-function-name-face)))
@@ -1130,7 +1127,7 @@ fontified."
@font-lock-variable-name-face)
(assignment left: (attribute
attribute: (identifier)
- @font-lock-variable-name-face))
+ @font-lock-property-face))
(pattern_list (identifier)
@font-lock-variable-name-face)
(tuple_pattern (identifier)
@@ -1162,12 +1159,10 @@ fontified."
:feature 'number
:language 'python
- :override t
'([(integer) (float)] @font-lock-number-face)
:feature 'property
:language 'python
- :override t
'((attribute
attribute: (identifier) @font-lock-property-face)
(class_definition
@@ -1178,20 +1173,44 @@ fontified."
:feature 'operator
:language 'python
- :override t
`([,@python--treesit-operators] @font-lock-operator-face)
:feature 'bracket
:language 'python
- :override t
'(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face)
:feature 'delimiter
:language 'python
- :override t
- '(["," "." ":" ";" (ellipsis)] @font-lock-delimiter-face))
+ '(["," "." ":" ";" (ellipsis)] @font-lock-delimiter-face)
+
+ :feature 'variable
+ :language 'python
+ '((identifier) @python--treesit-fontify-variable))
"Tree-sitter font-lock settings.")
+(defun python--treesit-variable-p (node)
+ "Check whether NODE is a variable.
+NODE's type should be \"identifier\"."
+ ;; An identifier can be a function/class name, a property, or a
+ ;; variables. This funtion filters out function/class names and
+ ;; properties.
+ (pcase (treesit-node-type (treesit-node-parent node))
+ ((or "function_definition" "class_definition") nil)
+ ("attribute"
+ (pcase (treesit-node-field-name node)
+ ("object" t)
+ (_ nil)))
+ (_ t)))
+
+(defun python--treesit-fontify-variable (node override start end &rest _)
+ "Fontify an identifier node if it is a variable.
+For NODE, OVERRIDE, START, END, and ARGS, see
+`treesit-font-lock-rules'."
+ (when (python--treesit-variable-p node)
+ (treesit-fontify-with-override
+ (treesit-node-start node) (treesit-node-end node)
+ 'font-lock-variable-name-face override start end)))
+
;;; Indentation
@@ -6646,7 +6665,7 @@ implementations: `python-mode' and `python-ts-mode'."
( keyword string type)
( assignment builtin constant decorator
escape-sequence number property string-interpolation )
- ( function bracket delimiter operator)))
+ ( bracket delimiter function operator variable)))
(setq-local treesit-font-lock-settings python--treesit-settings)
(setq-local imenu-create-index-function
#'python-imenu-treesit-create-index)
diff --git a/lisp/progmodes/rust-ts-mode.el b/lisp/progmodes/rust-ts-mode.el
index 81f5b8765f1..d03dffe628e 100644
--- a/lisp/progmodes/rust-ts-mode.el
+++ b/lisp/progmodes/rust-ts-mode.el
@@ -29,6 +29,7 @@
(require 'treesit)
(eval-when-compile (require 'rx))
+(require 'c-ts-mode) ; For comment indent and filling.
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
@@ -70,6 +71,9 @@
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
((node-is "}") (and parent parent-bol) 0)
+ ((and (parent-is "comment") c-ts-mode--looking-at-star)
+ c-ts-mode--comment-start-after-first-star -1)
+ ((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "arguments") parent-bol rust-ts-mode-indent-offset)
((parent-is "await_expression") parent-bol rust-ts-mode-indent-offset)
((parent-is "array_expression") parent-bol rust-ts-mode-indent-offset)
@@ -244,35 +248,6 @@
'((ERROR) @font-lock-warning-face))
"Tree-sitter font-lock settings for `rust-ts-mode'.")
-(defun rust-ts-mode--imenu ()
- "Return Imenu alist for the current buffer."
- (let* ((node (treesit-buffer-root-node))
- (enum-tree (treesit-induce-sparse-tree
- node "enum_item" nil))
- (enum-index (rust-ts-mode--imenu-1 enum-tree))
- (func-tree (treesit-induce-sparse-tree
- node "function_item" nil))
- (func-index (rust-ts-mode--imenu-1 func-tree))
- (impl-tree (treesit-induce-sparse-tree
- node "impl_item" nil))
- (impl-index (rust-ts-mode--imenu-1 impl-tree))
- (mod-tree (treesit-induce-sparse-tree
- node "mod_item" nil))
- (mod-index (rust-ts-mode--imenu-1 mod-tree))
- (struct-tree (treesit-induce-sparse-tree
- node "struct_item" nil))
- (struct-index (rust-ts-mode--imenu-1 struct-tree))
- (type-tree (treesit-induce-sparse-tree
- node "type_item" nil))
- (type-index (rust-ts-mode--imenu-1 type-tree)))
- (append
- (when mod-index `(("Module" . ,mod-index)))
- (when enum-index `(("Enum" . ,enum-index)))
- (when impl-index `(("Impl" . ,impl-index)))
- (when type-index `(("Type" . ,type-index)))
- (when struct-index `(("Struct" . ,struct-index)))
- (when func-index `(("Fn" . ,func-index))))))
-
(defun rust-ts-mode--defun-name (node)
"Return the defun name of NODE.
Return nil if there is no name or if NODE is not a defun node."
@@ -300,27 +275,6 @@ Return nil if there is no name or if NODE is not a defun node."
(treesit-node-text
(treesit-node-child-by-field-name node "name") t))))
-(defun rust-ts-mode--imenu-1 (node)
- "Helper for `rust-ts-mode--imenu'.
-Find string representation for NODE and set marker, then recurse
-the subtrees."
- (let* ((ts-node (car node))
- (children (cdr node))
- (subtrees (mapcan #'rust-ts-mode--imenu-1
- children))
- (name (when ts-node
- (or (treesit-defun-name ts-node)
- "Anonymous")))
- (marker (when ts-node
- (set-marker (make-marker)
- (treesit-node-start ts-node)))))
- (cond
- ((or (null ts-node) (null name)) subtrees)
- (subtrees
- `((,name ,(cons name marker) ,@subtrees)))
- (t
- `((,name . ,marker))))))
-
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-ts-mode))
@@ -334,15 +288,7 @@ the subtrees."
(treesit-parser-create 'rust)
;; Comments.
- (setq-local comment-start "// ")
- (setq-local comment-end "")
- (setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
- (seq "/" (+ "*")))
- (* (syntax whitespace))))
- (setq-local comment-end-skip
- (rx (* (syntax whitespace))
- (group (or (syntax comment-end)
- (seq (+ "*") "/")))))
+ (c-ts-mode-comment-setup)
;; Font-lock.
(setq-local treesit-font-lock-settings rust-ts-mode--font-lock-settings)
@@ -354,8 +300,13 @@ the subtrees."
( bracket delimiter error operator)))
;; Imenu.
- (setq-local imenu-create-index-function #'rust-ts-mode--imenu)
- (setq-local which-func-functions nil)
+ (setq-local treesit-simple-imenu-settings
+ `(("Module" "\\`mod_item\\'" nil nil)
+ ("Enum" "\\`enum_item\\'" nil nil)
+ ("Impl" "\\`impl_item\\'" nil nil)
+ ("Type" "\\`type_item\\'" nil nil)
+ ("Struct" "\\`struct_item\\'" nil nil)
+ ("Fn" "\\`function_item\\'" nil nil)))
;; Indent.
(setq-local indent-tabs-mode nil
diff --git a/lisp/progmodes/scheme.el b/lisp/progmodes/scheme.el
index 8454f24356a..f45d7992524 100644
--- a/lisp/progmodes/scheme.el
+++ b/lisp/progmodes/scheme.el
@@ -115,7 +115,8 @@
(defvar scheme-imenu-generic-expression
`((nil
- ,(rx bol "(define"
+ ,(rx bol (zero-or-more space)
+ "(define"
(zero-or-one "*")
(zero-or-one "-public")
(one-or-more space)
@@ -123,36 +124,41 @@
(group (one-or-more (or word (syntax symbol)))))
1)
("Methods"
- ,(rx bol "(define-"
+ ,(rx bol (zero-or-more space)
+ "(define-"
(or "generic" "method" "accessor")
(one-or-more space)
(zero-or-one "(")
(group (one-or-more (or word (syntax symbol)))))
1)
("Classes"
- ,(rx bol "(define-class"
+ ,(rx bol (zero-or-more space)
+ "(define-class"
(one-or-more space)
(zero-or-one "(")
(group (one-or-more (or word (syntax symbol)))))
1)
("Records"
- ,(rx bol "(define-record-type"
+ ,(rx bol (zero-or-more space)
+ "(define-record-type"
(zero-or-one "*")
(one-or-more space)
(group (one-or-more (or word (syntax symbol)))))
1)
("Conditions"
- ,(rx bol "(define-condition-type"
+ ,(rx bol (zero-or-more space)
+ "(define-condition-type"
(one-or-more space)
(group (one-or-more (or word (syntax symbol)))))
1)
("Modules"
- ,(rx bol "(define-module"
+ ,(rx bol (zero-or-more space)
+ "(define-module"
(one-or-more space)
(group "(" (one-or-more any) ")"))
1)
("Macros"
- ,(rx bol "("
+ ,(rx bol (zero-or-more space) "("
(or (and "defmacro"
(zero-or-one "*")
(zero-or-one "-public"))
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index 3f995d17b5a..d12ade36af3 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -150,6 +150,8 @@
(require 'executable)
(require 'treesit)
+(declare-function treesit-parser-create "treesit.c")
+
(autoload 'comint-completion-at-point "comint")
(autoload 'comint-filename-completion "comint")
(autoload 'comint-send-string "comint")
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 6ba1b9b12c0..05ddc0e7a94 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -30,6 +30,7 @@
(require 'treesit)
(require 'js)
(eval-when-compile (require 'rx))
+(require 'c-ts-mode) ; For comment indent and filling.
(declare-function treesit-parser-create "treesit.c")
@@ -73,8 +74,9 @@ Argument LANGUAGE is either `typescript' or `tsx'."
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
((node-is ">") parent-bol 0)
- ((and (parent-is "comment") comment-end) comment-start -1)
- ((parent-is "comment") comment-start-skip 0)
+ ((and (parent-is "comment") c-ts-mode--looking-at-star)
+ c-ts-mode--comment-start-after-first-star -1)
+ ((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "ternary_expression") parent-bol typescript-ts-mode-indent-offset)
((parent-is "member_expression") parent-bol typescript-ts-mode-indent-offset)
((parent-is "named_imports") parent-bol typescript-ts-mode-indent-offset)
@@ -331,18 +333,12 @@ Argument LANGUAGE is either `typescript' or `tsx'."
:syntax-table typescript-ts-mode--syntax-table
;; Comments.
- (setq-local comment-start "// ")
- (setq-local comment-end "")
- (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
- (setq-local comment-end-skip
- (rx (* (syntax whitespace))
- (group (or (syntax comment-end)
- (seq (+ "*") "/")))))
+ (c-ts-mode-comment-setup)
+ (setq-local treesit-defun-prefer-top-level t)
(setq-local treesit-text-type-regexp
(regexp-opt '("comment"
"template_string")))
- (setq-local treesit-defun-prefer-top-level t)
;; Electric
(setq-local electric-indent-chars
@@ -354,11 +350,17 @@ Argument LANGUAGE is either `typescript' or `tsx'."
"method_definition"
"function_declaration"
"lexical_declaration")))
- ;; Imenu.
- (setq-local imenu-create-index-function #'js--treesit-imenu)
-
- ;; Which-func (use imenu).
- (setq-local which-func-functions nil))
+ (setq-local treesit-defun-name-function #'js--treesit-defun-name)
+
+ ;; Imenu (same as in `js-ts-mode').
+ (setq-local treesit-simple-imenu-settings
+ `(("Function" "\\`function_declaration\\'" nil nil)
+ ("Variable" "\\`lexical_declaration\\'"
+ js--treesit-valid-imenu-entry nil)
+ ("Class" ,(rx bos (or "class_declaration"
+ "method_definition")
+ eos)
+ nil nil))))
;;;###autoload
(define-derived-mode typescript-ts-mode typescript-ts-base-mode "TypeScript"