From 70477be3e38d7bd5243e5d2f66577d8fbe8d1010 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sun, 15 Jan 2023 22:14:51 +0100 Subject: [PATCH] Add treesit-sentence-type-regexp * lisp/progmodes/js.el (js--treesit-sentence-type-regexp): New defvar with sentence nodes. (js-ts-mode): Use the new defvar for treesit-sentence-type-regexp. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--sentence-type-regexp): New defvar with sentence nodes. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-base-mode): Use the new defvar for treesit-sentence-type-regexp. (tsx-ts-mode): Extend treesit-sentence-type-regexp with jsx nodes. --- lisp/progmodes/js.el | 29 ++++++++++++++++++++++++ lisp/progmodes/typescript-ts-mode.el | 34 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 902d4fa7ab3..f6103d43eea 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3789,6 +3789,31 @@ Currently there are `js-mode' and `js-ts-mode'." ;;(syntax-propertize (point-max)) ) +(defvar js--treesit-sentence-type-regexp + '("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" + "jsx_element" + "jsx_self_closing_element") + "Nodes that designate sentences in JavaScript. +See `treesit-sentence-type-regexp' for more information.") + ;;;###autoload (define-derived-mode js-ts-mode js-base-mode "JavaScript" "Major mode for editing JavaScript. @@ -3828,6 +3853,10 @@ Currently there are `js-mode' and `js-ts-mode'." "function_declaration" "lexical_declaration"))) (setq-local treesit-defun-name-function #'js--treesit-defun-name) + + (setq-local treesit-sentence-type-regexp + (regexp-opt js--treesit-sentence-type-regexp)) + ;; Fontification. (setq-local treesit-font-lock-settings js--treesit-font-lock-settings) (setq-local treesit-font-lock-feature-list diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 0786150d906..be84caa7b26 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -312,6 +312,30 @@ Argument LANGUAGE is either `typescript' or `tsx'." :override t '((escape_sequence) @font-lock-escape-face))) +(defvar typescript-ts-mode--sentence-type-regexp + '("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-sentence-type-regexp' for more information.") + ;;;###autoload (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode)) @@ -344,6 +368,9 @@ Argument LANGUAGE is either `typescript' or `tsx'." "lexical_declaration"))) (setq-local treesit-defun-name-function #'js--treesit-defun-name) + (setq-local treesit-sentence-type-regexp + (regexp-opt typescript-ts-mode--sentence-type-regexp)) + ;; Imenu (same as in `js-ts-mode'). (setq-local treesit-simple-imenu-settings `(("Function" "\\`function_declaration\\'" nil nil) @@ -402,6 +429,13 @@ Argument LANGUAGE is either `typescript' or `tsx'." (setq-local treesit-simple-indent-rules (typescript-ts-mode--indent-rules 'tsx)) + ;; Navigation + (setq-local treesit-sentence-type-regexp + (regexp-opt (append + typescript-ts-mode--sentence-type-regexp + '("jsx_element" + "jsx_self_closing_element")))) + ;; Font-lock. (setq-local treesit-font-lock-settings (typescript-ts-mode--font-lock-settings 'tsx)) -- 2.39.5