summaryrefslogtreecommitdiff
path: root/lisp/progmodes/typescript-ts-mode.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes/typescript-ts-mode.el')
-rw-r--r--lisp/progmodes/typescript-ts-mode.el69
1 files changed, 69 insertions, 0 deletions
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 7021f012dcd..ea4f6417c5a 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -391,6 +391,52 @@ Argument LANGUAGE is either `typescript' or `tsx'."
:override t
'((escape_sequence) @font-lock-escape-face))))
+(defvar typescript-ts-mode--sentence-nodes
+ '("import_statement"
+ "debugger_statement"
+ "expression_statement"
+ "if_statement"
+ "switch_statement"
+ "for_statement"
+ "for_in_statement"
+ "while_statement"
+ "do_statement"
+ "try_statement"
+ "with_statement"
+ "break_statement"
+ "continue_statement"
+ "return_statement"
+ "throw_statement"
+ "empty_statement"
+ "labeled_statement"
+ "variable_declaration"
+ "lexical_declaration"
+ "property_signature")
+ "Nodes that designate sentences in TypeScript.
+See `treesit-thing-settings' for more information.")
+
+(defvar typescript-ts-mode--sexp-nodes
+ '("expression"
+ "pattern"
+ "array"
+ "function"
+ "string"
+ "escape"
+ "template"
+ "regex"
+ "number"
+ "identifier"
+ "this"
+ "super"
+ "true"
+ "false"
+ "null"
+ "undefined"
+ "arguments"
+ "pair")
+ "Nodes that designate sexps in TypeScript.
+See `treesit-thing-settings' for more information.")
+
;;;###autoload
(define-derived-mode typescript-ts-base-mode prog-mode "TypeScript"
"Generic major mode for editing TypeScript.
@@ -401,6 +447,7 @@ This mode is intended to be inherited by concrete major modes."
;; Comments.
(c-ts-common-comment-setup)
+ (setq-local treesit-defun-prefer-top-level t)
;; Electric
(setq-local electric-indent-chars
@@ -415,6 +462,14 @@ This mode is intended to be inherited by concrete major modes."
"lexical_declaration")))
(setq-local treesit-defun-name-function #'js--treesit-defun-name)
+ (setq-local treesit-thing-settings
+ `((typescript
+ (sexp ,(regexp-opt typescript-ts-mode--sexp-nodes))
+ (sentence ,(regexp-opt
+ typescript-ts-mode--sentence-nodes))
+ (text ,(regexp-opt '("comment"
+ "template_string"))))))
+
;; Imenu (same as in `js-ts-mode').
(setq-local treesit-simple-imenu-settings
`(("Function" "\\`function_declaration\\'" nil nil)
@@ -450,6 +505,8 @@ This mode is intended to be inherited by concrete major modes."
(treesit-major-mode-setup)))
+(derived-mode-add-parents 'typescript-ts-mode '(typescript-mode))
+
(if (treesit-ready-p 'typescript)
(add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode)))
@@ -485,6 +542,16 @@ at least 3 (which is the default value)."
(setq-local treesit-simple-indent-rules
(typescript-ts-mode--indent-rules 'tsx))
+ (setq-local treesit-thing-settings
+ `((tsx
+ (sexp ,(regexp-opt
+ (append typescript-ts-mode--sexp-nodes
+ '("jsx"))))
+ (sentence ,(regexp-opt
+ (append typescript-ts-mode--sentence-nodes
+ '("jsx_element"
+ "jsx_self_closing_element")))))))
+
;; Font-lock.
(setq-local treesit-font-lock-settings
(typescript-ts-mode--font-lock-settings 'tsx))
@@ -497,6 +564,8 @@ at least 3 (which is the default value)."
(treesit-major-mode-setup)))
+(derived-mode-add-parents 'tsx-ts-mode '(tsx-mode))
+
(defvar typescript-ts--s-p-query
(when (treesit-available-p)
(treesit-query-compile 'typescript