]> git.eshelyaron.com Git - emacs.git/commitdiff
Add treesit-sentence-type-regexp
authorTheodor Thornhill <theo@thornhill.no>
Sun, 15 Jan 2023 21:14:51 +0000 (22:14 +0100)
committerTheodor Thornhill <theo@thornhill.no>
Sun, 15 Jan 2023 21:18:02 +0000 (22:18 +0100)
* 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
lisp/progmodes/typescript-ts-mode.el

index 902d4fa7ab3206b2c266629eaa86684826d6b0b6..f6103d43eea41c3ba2bda206a156d65745877b2b 100644 (file)
@@ -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
index 0786150d9066c1dc4b2dffc5a141f9625d4a652e..be84caa7b26219a9f76cfd55f40eb689af8ee2c4 100644 (file)
@@ -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))