emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master ec3f9434c7d 10/10: Make typescript-ts-mode work with latest gramm


From: Yuan Fu
Subject: master ec3f9434c7d 10/10: Make typescript-ts-mode work with latest grammar
Date: Mon, 30 Dec 2024 03:24:10 -0500 (EST)

branch: master
commit ec3f9434c7d615dc37ba444a2200f13c84984299
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Make typescript-ts-mode work with latest grammar
    
    * lisp/progmodes/typescript-ts-mode.el:
    (tsx-ts-mode--font-lock-compatibility-bb1f97b): Return dummy
    query when neither query works.  In latest grammar, neither will
    work, because typescript grammar doesn't include jsx grammar
    anymore.
---
 lisp/progmodes/typescript-ts-mode.el | 77 +++++++++++++++++++++++-------------
 1 file changed, 50 insertions(+), 27 deletions(-)

diff --git a/lisp/progmodes/typescript-ts-mode.el 
b/lisp/progmodes/typescript-ts-mode.el
index df41c179e30..06635378dfd 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -22,6 +22,15 @@
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
 
+;;; Tree-sitter language versions
+;;
+;; typescript-ts-mode is known to work with the following languages and 
version:
+;; - tree-sitter-typescript: v0.23.2-2-g8e13e1d
+;;
+;; We try our best to make builtin modes work with latest grammar
+;; versions, so a more recent grammar version has a good chance to work.
+;; Send us a bug report if it doesn't.
+
 ;;; Commentary:
 ;;
 
@@ -192,32 +201,47 @@ Argument LANGUAGE is either `typescript' or `tsx'."
   ;; Warning: treesitter-query-capture says both node types are valid,
   ;; but then raises an error if the wrong node type is used. So it is
   ;; important to check with the new node type (member_expression)
+  ;;
+  ;; Later typescript grammar removed support for jsx, so the later
+  ;; grammar versions this function just return nil.
   (typescript-ts-mode--check-dialect language)
-  (condition-case nil
-      (progn (treesit-query-capture language '((jsx_opening_element 
(member_expression) @capture)))
-            '((jsx_opening_element
-               [(member_expression (identifier)) (identifier)]
-               @typescript-ts-jsx-tag-face)
-
-              (jsx_closing_element
-               [(member_expression (identifier)) (identifier)]
-               @typescript-ts-jsx-tag-face)
-
-              (jsx_self_closing_element
-               [(member_expression (identifier)) (identifier)]
-               @typescript-ts-jsx-tag-face)))
-    (treesit-query-error
-           '((jsx_opening_element
-             [(nested_identifier (identifier)) (identifier)]
-             @typescript-ts-jsx-tag-face)
-
-            (jsx_closing_element
-             [(nested_identifier (identifier)) (identifier)]
-             @typescript-ts-jsx-tag-face)
-
-            (jsx_self_closing_element
-             [(nested_identifier (identifier)) (identifier)]
-             @typescript-ts-jsx-tag-face)))))
+  (let ((queries-a '((jsx_opening_element
+                     [(member_expression (identifier)) (identifier)]
+                     @typescript-ts-jsx-tag-face)
+
+                    (jsx_closing_element
+                     [(member_expression (identifier)) (identifier)]
+                     @typescript-ts-jsx-tag-face)
+
+                    (jsx_self_closing_element
+                     [(member_expression (identifier)) (identifier)]
+                     @typescript-ts-jsx-tag-face)
+
+                     (jsx_attribute (property_identifier)
+                                    @typescript-ts-jsx-attribute-face)))
+        (queries-b '((jsx_opening_element
+                     [(nested_identifier (identifier)) (identifier)]
+                     @typescript-ts-jsx-tag-face)
+
+                     (jsx_closing_element
+                     [(nested_identifier (identifier)) (identifier)]
+                     @typescript-ts-jsx-tag-face)
+
+                     (jsx_self_closing_element
+                     [(nested_identifier (identifier)) (identifier)]
+                     @typescript-ts-jsx-tag-face)
+
+                     (jsx_attribute (property_identifier)
+                                    @typescript-ts-jsx-attribute-face))))
+    (or (ignore-errors
+          (treesit-query-compile language queries-a t)
+          queries-a)
+        (ignore-errors
+          (treesit-query-compile language queries-b t)
+          queries-b)
+        ;; Return a dummy query that doens't do anything, if neither
+        ;; query works.
+        '("," @_ignore))))
 
 (defun tsx-ts-mode--font-lock-compatibility-function-expression (language)
   "Handle tree-sitter grammar breaking change for `function' expression.
@@ -386,8 +410,7 @@ Argument LANGUAGE is either `typescript' or `tsx'."
 
      :language language
      :feature 'jsx
-     (append (tsx-ts-mode--font-lock-compatibility-bb1f97b language)
-             `((jsx_attribute (property_identifier) 
@typescript-ts-jsx-attribute-face)))
+     (tsx-ts-mode--font-lock-compatibility-bb1f97b language)
 
      :language language
      :feature 'number



reply via email to

[Prev in Thread] Current Thread [Next in Thread]