]> git.eshelyaron.com Git - emacs.git/commitdiff
Correctly handled the local parser for jsdoc (bug#75456)
authorVincenzo Pupillo <v.pupillo@gmail.com>
Mon, 3 Feb 2025 21:14:44 +0000 (22:14 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sun, 9 Feb 2025 08:40:58 +0000 (09:40 +0100)
As a result of recent patches to treesitter, local parsers must
now be recognized by 'treesit-language-at-point' and have their
own indentation rules.

* lisp/progmodes/js.el
(js--treesit-indent-rules): New rule for jsdoc.
(js--treesit-language-at-point): New function.
(js-ts-mode): Use the new function.

(cherry picked from commit 4396f6414c2fb82cb8cee68420b658e376bb4e01)

lisp/progmodes/js.el

index 4e2e32132efa2d87825a7f69c01bc436f84f6203..81cf02664c646098fe5a22a14889fdc6ca7bef03 100644 (file)
@@ -3490,7 +3490,10 @@ Check if a node type is available, then return the right indent rules."
        ((match "/" "jsx_self_closing_element") parent 0)
        ((parent-is "jsx_self_closing_element") parent js-indent-level)
        ;; FIXME(Theo): This no-node catch-all should be removed.  When is it needed?
-       (no-node parent-bol 0)))))
+       (no-node parent-bol 0))
+      (jsdoc
+       ((and (parent-is "document") c-ts-common-looking-at-star)
+        c-ts-common-comment-start-after-first-star -1)))))
 
 (defvar js--treesit-keywords
   '("as" "async" "await" "break" "case" "catch" "class" "const" "continue"
@@ -3718,6 +3721,22 @@ Return nil if there is no name or if NODE is not a defun node."
     ("lexical_declaration" (treesit-node-top-level node))
     (_ t)))
 
+(defun js--treesit-language-at-point (point)
+  "Return the language at POINT."
+  (let* ((node (treesit-node-at point 'javascript))
+         (node-type (treesit-node-type node))
+         (node-start (treesit-node-start node))
+         (node-end (treesit-node-end node)))
+    (if (not (treesit-ready-p 'jsdoc t))
+        'javascript
+      (if (equal node-type "comment")
+          (save-excursion
+            (goto-char node-start)
+            (if (search-forward "/**" node-end t)
+                'jsdoc
+              'javascript))
+        'javascript))))
+
 ;;; Main Function
 
 ;;;###autoload
@@ -3922,6 +3941,7 @@ See `treesit-thing-settings' for more information.")
 
     ;; Tree-sitter setup.
     (setq-local treesit-primary-parser (treesit-parser-create 'javascript))
+    (setq-local treesit-language-at-point-function #'js--treesit-language-at-point)
 
     ;; Indent.
     (setq-local treesit-simple-indent-rules js--treesit-indent-rules)