JuliaEditorSupport / julia-ts-mode

Experimental Emacs Julia major mode using tree-sitter
MIT License
37 stars 7 forks source link

PR 135 Recent change in grammar causes issues in font locking (syntax highlighting) #25

Open dhanak opened 2 weeks ago

dhanak commented 2 weeks ago

Hi,

I've been using Emacs 29.4 with julia-ts-mode for a while with great statisfaction. However, a recent update to the libtree-sitter-julia.so grammar causes the following error in font locking, and as a result, I don't get any syntax highlighting at all:

Error during redisplay: (jit-lock-function 1501) signaled (treesit-query-error "Node type error at" 177 "(assignment (identifier) @font-lock-variable-name-face (_)) (assignment (field_expression (identifier) \".\" (identifier) @font-lock-variable-name-face) (operator)) (assignment (bare_tuple (identifier) @font-lock-variable-name-face)) (assignment (bare_tuple (field_expression (identifier) \".\" (identifier) @font-lock-variable-name-face)) (operator)) (local_statement (identifier) @font-lock-variable-name-face) (let_binding (identifier) @font-lock-variable-name-face) (global_statement (identifier) @font-lock-variable-name-face)" "Debug the query with `treesit-query-validate'")

I assume something changed in the grammar that the ts-mode implementation didn't follow, but I can't figure out how to debug this. I see in the issue history that there have been similar issues in the past, so I'm hoping that there should be a fix for this.

I observed this issue first when I tried to update the grammar on April 12 this year, but I don't know exactly what version I had before that worked. The issue persists with the most recent grammar version.

Any help would be appreciated!

dhanak commented 2 weeks ago

Aha, I think I figured it out after all! bare_tuple has been renamed to open_tuple in this change, so the query for assignment in julia-ts-mode.el#L165 and two lines below bust be adjusted.

dhanak commented 2 weeks ago

Unfortunately, there are other changes to the grammar as well, e.g., no more short_function_definition, the name: tags are no longer valid, etc.

dhanak commented 2 weeks ago

This PR is responsible for these changes: https://github.com/tree-sitter/tree-sitter-julia/pull/135

Could this be the solution? Is it possible that these are all the necessary changes? Did I miss something? I get no errors now, but I can't be certain...

--- julia-ts-mode.el.orig   2024-08-29 12:57:10.007777566 +0200
+++ julia-ts-mode.el    2024-08-29 13:26:48.256695303 +0200
@@ -160,12 +160,14 @@
    :feature 'assignment
    `((assignment (identifier) @font-lock-variable-name-face (_))
      (assignment
-      (field_expression (identifier) "." (identifier) @font-lock-variable-name-face)
+      (field_expression
+       value: (identifier) "." (identifier) @font-lock-variable-name-face)
       (operator))
-     (assignment (bare_tuple (identifier) @font-lock-variable-name-face))
+     (assignment (open_tuple (identifier) @font-lock-variable-name-face))
      (assignment
-      (bare_tuple
-       (field_expression (identifier) "." (identifier) @font-lock-variable-name-face))
+      (open_tuple
+       (field_expression
+        value: (identifier) "." (identifier) @font-lock-variable-name-face))
       (operator))
      (local_statement (identifier) @font-lock-variable-name-face)
      (let_binding (identifier) @font-lock-variable-name-face)
@@ -187,15 +191,31 @@
    :language 'julia
    :feature 'definition
    `((function_definition
-      name: (identifier) @font-lock-function-name-face)
+      (signature (identifier) @font-lock-function-name-face))
      (function_definition
-      name: (field_expression (identifier) "." (identifier) @font-lock-function-name-face))
+      (signature
+       (call_expression (identifier) @font-lock-function-name-face)))
+     (function_definition
+      (signature
+       (call_expression
+        (field_expression
+         value: (identifier) "." (identifier) @font-lock-function-name-face))))
+     (macro_definition
+      (signature
+       (call_expression (identifier) @font-lock-function-name-face)))
      (macro_definition
-      name: (identifier) @font-lock-function-name-face)
-     (short_function_definition
-      name: (identifier) @font-lock-function-name-face)
-     (short_function_definition
-      name: (field_expression (identifier) "." (identifier) @font-lock-function-name-face)))
+      (signature
+       (call_expression
+        (field_expression
+         value: (identifier) "." (identifier) @font-lock-function-name-face))))
+     (assignment
+      (call_expression (identifier) @font-lock-function-name-face)
+      (operator))
+     (assignment
+      (call_expression
+       (field_expression
+        value: (identifier) "." (identifier) @font-lock-function-name-face))
+      (operator)))

    :language 'julia
    :feature 'error
@@ -261,8 +281,7 @@
    :override t
    `((type_clause (operator) (_) @font-lock-type-face)
      (typed_expression (_) "::" (_) @font-lock-type-face)
-     (typed_parameter
-      type: (_) @font-lock-type-face)
+     (unary_typed_expression (_) @font-lock-type-face)
      (where_clause "where"
                    (curly_expression "{"
                                      (binary_expression (identifier)
@@ -361,7 +380,6 @@
   (pcase (treesit-node-type node)
     ((or "abstract_definition"
          "function_definition"
-         "short_function_definition"
          "struct_definition")
      (treesit-node-text
       (treesit-node-child-by-field-name node "name")
@@ -401,7 +419,7 @@

   ;; Imenu.
   (setq-local treesit-simple-imenu-settings
-              `(("Function" "\\`function_definition\\|short_function_definition\\'" nil nil)
+              `(("Function" "\\`function_definition\\'" nil nil)
                 ("Struct" "\\`struct_definition\\'" nil nil)))

   ;; Fontification