dgutov / mmm-mode

New official home for mmm-mode, fixed for Emacs >= 23
http://mmm-mode.sourceforge.net/
GNU General Public License v2.0
334 stars 32 forks source link

Activating `mmm-mode` without any setup leads to error in `typescript-ts-mode` #138

Open dschrempf opened 1 year ago

dschrempf commented 1 year ago

I just wanted to try out mmm-mode before setting anything up (I need to edit SQL statements within TypeScript code).

I open a TypeScript file (*.ts), activate typescript-ts-mode, and activate mmm-mode.

I get an error with the following trace:

Debugger entered--Lisp error: (treesit-query-error "Node type error at" 2 "(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)" "Debug the query with `treesit-query-validate'")
  treesit-font-lock-fontify-region(1 7380 nil)
  font-lock-fontify-syntactically-region(1 7380 nil)
  font-lock-default-fontify-region(1 7380 nil)
  font-lock-fontify-region(1 7380 nil)
  font-lock-default-fontify-buffer()
  font-lock-fontify-buffer()
  font-lock-initial-fontify()
  font-lock-mode(1)
  treesit-major-mode-setup()
  typescript-ts-mode()
  mmm-update-mode-info(typescript-ts-mode)
  mmm-mode-on()
  mmm-mode(nil)
  funcall-interactively(mmm-mode nil)
  command-execute(mmm-mode record)
  execute-extended-command(nil "mmm-mode" nil)
  funcall-interactively(execute-extended-command nil "mmm-mode" nil)
  command-execute(execute-extended-command)
dgutov commented 1 year ago

Hi!

Are you sure you don't see any such errors in the Messages without enabling mmm-mode?

dschrempf commented 1 year ago

I am not completely sure, but the few tests I made just now didn't show any warnings nor errors with TypeScript files.

dgutov commented 1 year ago

Okay, I can repro.

This is odd: the font-lock query with jsx_opening_element should only be used with tsx-ts-mode, not with this one.

dgutov commented 1 year ago

Unfortunately, the best fix I found so far is patch against Emacs:

diff --git a/lisp/treesit.el b/lisp/treesit.el
index f0d6f32b421..c1834bbfb9a 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2725,7 +2725,7 @@ treesit-major-mode-setup
                 '( nil nil nil nil
                    (font-lock-fontify-syntactically-function
                     . treesit-font-lock-fontify-region)))
-    (font-lock-mode 1)
+    ;; (font-lock-mode 1)
     (treesit-font-lock-recompute-features)
     (dolist (parser (treesit-parser-list))
       (treesit-parser-add-notifier

I've filed a report to Debbugs (https://debbugs.gnu.org/66223), maybe we'll be able to have this in Emacs 29.2. Or you could patch locally, or build from source.

The problem is mostly specific to typescript-ts-mode (in the context of mmm-mode), so it should be better with most or all other TS modes. Still, the way tree-sitter works is both more likely to conflict with this package, so I'm curious to hear about your and others' experience with this combination.

In any case, treesit.el also has its own way of defining mixed-language modes, but it requires writing more Elisp.

dschrempf commented 1 year ago

Great work, thanks for finding the cause and reporting it uptream!

The following is definitely unrelated to mmm-mode, but I thought it may be relevant for the bug you reported:

I thought I could circumvent the bug by just using tsx-ts-mode (the TypeScript mode equivalent for tsx files, which, I thought, should also work for ts files). But when activating tsx-ts-mode (no mmm-mode activated), I also get

Debugger entered--Lisp error: (treesit-query-error "Structure error at" 22 "(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)" "Debug the query with `treesit-query-validate'")
  treesit-font-lock-fontify-region(1 307 nil)
  font-lock-fontify-syntactically-region(1 307 nil)
  font-lock-default-fontify-region(1 307 nil)
  font-lock-fontify-region(1 307)
  #f(compiled-function (fun) #<bytecode -0x1552e40ec3721b83>)(font-lock-fontify-region)
  jit-lock--run-functions(1 307)
  jit-lock-fontify-now(1 307)
  font-lock-ensure()
  orglink-mode()
  run-hooks(change-major-mode-after-body-hook prog-mode-hook typescript-ts-base-mode-hook tsx-ts-mode-hook)
  apply(run-hooks (change-major-mode-after-body-hook prog-mode-hook typescript-ts-base-mode-hook tsx-ts-mode-hook))
  run-mode-hooks(tsx-ts-mode-hook)
  tsx-ts-mode()
  funcall-interactively(tsx-ts-mode)
  command-execute(tsx-ts-mode record)
  execute-extended-command(nil "tsx-ts-mode" nil)
  funcall-interactively(execute-extended-command nil "tsx-ts-mode" nil)
  command-execute(execute-extended-command)
dgutov commented 1 year ago

The following is definitely unrelated to mmm-mode, but I thought it may be relevant for the bug you reported:

Thank you. This is a known one, actually :grimacing: It's fixed in the emacs-29 branch (https://github.com/emacs-mirror/emacs/commit/65f4810003b617ea), to become Emacs 29.2 is some not too-distant future.

To have it working right now, you can either build from source (or patch locally), or install an older version of the typescript grammar. Anything slightly older than https://github.com/tree-sitter/tree-sitter-typescript/commit/b893426 should work (e.g. its parent).