diff options
| author | Theodor Thornhill | 2023-01-15 22:14:51 +0100 |
|---|---|---|
| committer | Theodor Thornhill | 2023-01-15 22:18:02 +0100 |
| commit | 70477be3e38d7bd5243e5d2f66577d8fbe8d1010 (patch) | |
| tree | 2b2ca9eed0706cb81de6a2a76f2042661e31da97 | |
| parent | 9ec60fde2e8b2b144a04d05031a83469abe7846f (diff) | |
| download | emacs-70477be3e38d7bd5243e5d2f66577d8fbe8d1010.tar.gz emacs-70477be3e38d7bd5243e5d2f66577d8fbe8d1010.zip | |
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.
| -rw-r--r-- | lisp/progmodes/js.el | 29 | ||||
| -rw-r--r-- | lisp/progmodes/typescript-ts-mode.el | 34 |
2 files changed, 63 insertions, 0 deletions
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'." | |||
| 3789 | ;;(syntax-propertize (point-max)) | 3789 | ;;(syntax-propertize (point-max)) |
| 3790 | ) | 3790 | ) |
| 3791 | 3791 | ||
| 3792 | (defvar js--treesit-sentence-type-regexp | ||
| 3793 | '("import_statement" | ||
| 3794 | "debugger_statement" | ||
| 3795 | "expression_statement" | ||
| 3796 | "if_statement" | ||
| 3797 | "switch_statement" | ||
| 3798 | "for_statement" | ||
| 3799 | "for_in_statement" | ||
| 3800 | "while_statement" | ||
| 3801 | "do_statement" | ||
| 3802 | "try_statement" | ||
| 3803 | "with_statement" | ||
| 3804 | "break_statement" | ||
| 3805 | "continue_statement" | ||
| 3806 | "return_statement" | ||
| 3807 | "throw_statement" | ||
| 3808 | "empty_statement" | ||
| 3809 | "labeled_statement" | ||
| 3810 | "variable_declaration" | ||
| 3811 | "lexical_declaration" | ||
| 3812 | "jsx_element" | ||
| 3813 | "jsx_self_closing_element") | ||
| 3814 | "Nodes that designate sentences in JavaScript. | ||
| 3815 | See `treesit-sentence-type-regexp' for more information.") | ||
| 3816 | |||
| 3792 | ;;;###autoload | 3817 | ;;;###autoload |
| 3793 | (define-derived-mode js-ts-mode js-base-mode "JavaScript" | 3818 | (define-derived-mode js-ts-mode js-base-mode "JavaScript" |
| 3794 | "Major mode for editing JavaScript. | 3819 | "Major mode for editing JavaScript. |
| @@ -3828,6 +3853,10 @@ Currently there are `js-mode' and `js-ts-mode'." | |||
| 3828 | "function_declaration" | 3853 | "function_declaration" |
| 3829 | "lexical_declaration"))) | 3854 | "lexical_declaration"))) |
| 3830 | (setq-local treesit-defun-name-function #'js--treesit-defun-name) | 3855 | (setq-local treesit-defun-name-function #'js--treesit-defun-name) |
| 3856 | |||
| 3857 | (setq-local treesit-sentence-type-regexp | ||
| 3858 | (regexp-opt js--treesit-sentence-type-regexp)) | ||
| 3859 | |||
| 3831 | ;; Fontification. | 3860 | ;; Fontification. |
| 3832 | (setq-local treesit-font-lock-settings js--treesit-font-lock-settings) | 3861 | (setq-local treesit-font-lock-settings js--treesit-font-lock-settings) |
| 3833 | (setq-local treesit-font-lock-feature-list | 3862 | (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'." | |||
| 312 | :override t | 312 | :override t |
| 313 | '((escape_sequence) @font-lock-escape-face))) | 313 | '((escape_sequence) @font-lock-escape-face))) |
| 314 | 314 | ||
| 315 | (defvar typescript-ts-mode--sentence-type-regexp | ||
| 316 | '("import_statement" | ||
| 317 | "debugger_statement" | ||
| 318 | "expression_statement" | ||
| 319 | "if_statement" | ||
| 320 | "switch_statement" | ||
| 321 | "for_statement" | ||
| 322 | "for_in_statement" | ||
| 323 | "while_statement" | ||
| 324 | "do_statement" | ||
| 325 | "try_statement" | ||
| 326 | "with_statement" | ||
| 327 | "break_statement" | ||
| 328 | "continue_statement" | ||
| 329 | "return_statement" | ||
| 330 | "throw_statement" | ||
| 331 | "empty_statement" | ||
| 332 | "labeled_statement" | ||
| 333 | "variable_declaration" | ||
| 334 | "lexical_declaration" | ||
| 335 | "property_signature") | ||
| 336 | "Nodes that designate sentences in TypeScript. | ||
| 337 | See `treesit-sentence-type-regexp' for more information.") | ||
| 338 | |||
| 315 | ;;;###autoload | 339 | ;;;###autoload |
| 316 | (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode)) | 340 | (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode)) |
| 317 | 341 | ||
| @@ -344,6 +368,9 @@ Argument LANGUAGE is either `typescript' or `tsx'." | |||
| 344 | "lexical_declaration"))) | 368 | "lexical_declaration"))) |
| 345 | (setq-local treesit-defun-name-function #'js--treesit-defun-name) | 369 | (setq-local treesit-defun-name-function #'js--treesit-defun-name) |
| 346 | 370 | ||
| 371 | (setq-local treesit-sentence-type-regexp | ||
| 372 | (regexp-opt typescript-ts-mode--sentence-type-regexp)) | ||
| 373 | |||
| 347 | ;; Imenu (same as in `js-ts-mode'). | 374 | ;; Imenu (same as in `js-ts-mode'). |
| 348 | (setq-local treesit-simple-imenu-settings | 375 | (setq-local treesit-simple-imenu-settings |
| 349 | `(("Function" "\\`function_declaration\\'" nil nil) | 376 | `(("Function" "\\`function_declaration\\'" nil nil) |
| @@ -402,6 +429,13 @@ Argument LANGUAGE is either `typescript' or `tsx'." | |||
| 402 | (setq-local treesit-simple-indent-rules | 429 | (setq-local treesit-simple-indent-rules |
| 403 | (typescript-ts-mode--indent-rules 'tsx)) | 430 | (typescript-ts-mode--indent-rules 'tsx)) |
| 404 | 431 | ||
| 432 | ;; Navigation | ||
| 433 | (setq-local treesit-sentence-type-regexp | ||
| 434 | (regexp-opt (append | ||
| 435 | typescript-ts-mode--sentence-type-regexp | ||
| 436 | '("jsx_element" | ||
| 437 | "jsx_self_closing_element")))) | ||
| 438 | |||
| 405 | ;; Font-lock. | 439 | ;; Font-lock. |
| 406 | (setq-local treesit-font-lock-settings | 440 | (setq-local treesit-font-lock-settings |
| 407 | (typescript-ts-mode--font-lock-settings 'tsx)) | 441 | (typescript-ts-mode--font-lock-settings 'tsx)) |