renzmann / treesit-auto

Automatic installation, usage, and fallback for tree-sitter major modes in Emacs 29
GNU General Public License v3.0
369 stars 28 forks source link

Bug preventing mode switching when grammer is already installed #20

Closed mohkale closed 1 year ago

mohkale commented 1 year ago

There looks to be a bug in the current implementation where if a grammer is already ready you never try switching to its tree-sitter mode variant. Please confirm if this is correct behaviour. I just installed this package for the first time and I'm not sure if this is a regression or intentional.

renzmann commented 1 year ago

Could you describe the behavior of the bug you're facing? To diagnose we'd probably need your Emacs build/version plus the configuration you're using for treesit-auto.

The part you linked to is operating as it's supposed to: "if a grammar is not ready, continue to the next step." If the grammar is already ready, then we don't need to install it, which is what treesit-auto--maybe-install-grammar is responsible for

mohkale commented 1 year ago

The part you linked to is operating as it's supposed to: "if a grammar is not ready, continue to the next step." If the grammar is already ready, then we don't need to install it, which is what treesit-auto--maybe-install-grammar is responsible for

Doesn't it also switch to that ts-mode after installing the grammer? That's what the (funcall (treesit-auto--lang-to-ts-mode lang))) looks to be doing to me. Because the entire expression is wrapped in a when-let when not-ready is false and I already have a grammer installed that mode-switch isn't called so I don't end up in the ts-mode.

The behaviour I'm seeing is: I open a python file but my major-mode is python-mode instead of python-ts-mode even though I have the grammer installed.

My Emacs version is GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.36, cairo version 1.17.6) of 2023-02-04. I encounter this behaviour even with just a simple:

(use-package treesit-auto
  :straight t
  :demand t
  :config
  (global-treesit-auto-mode))
renzmann commented 1 year ago

Ah yeah, that's definitely not the intended behavior - it should be switching to the tree-sitter mode. However that's handled by treesit-auto--remap-language-source, which hacks into the major-mode-remap-alist, ultimately prompting Emacs to make the switch from python-mode to python-ts-mode when you visit a python buffer. If you do C-h v major-mode-remap-alist and don't see an entry like (python-mode . python-ts-mode), then that would be the crux of the issue and we'd have to figure out why that's not getting remapped even with the grammar installed.

renzmann commented 1 year ago

To be explicit, this is what's handling the automatic switch to python-ts-mode: https://github.com/renzmann/treesit-auto/blob/603de37a7ce2e7ad1698d3f26924b1ea518c6fa1/treesit-auto.el#L193-L195

The part you're looking at is just in the case where we've freshly installed a grammar and need to call python-ts-mode by name in order to enable it.

mohkale commented 1 year ago

@renzmann

Turns out to have been because the part of my config that installs the tree-sitter modules was after I tried to install treesit-auto. My bad. I wasn't installing grammars from this repo and was using casouri/tree-sitter-module instead. After switching the order I'm no longer getting any issues. Thanks for helping investigate :-).