~casouri/public-inbox

2 2

Why should tree-sitter modes be separate modes?

Jim Blandy <jimb@red-bean.com>
Details
Message ID
<CAHgb+0kk9H74JRJUpmFocUh7TLHzC7L+c95t9u3-ygzMMeEZpw@mail.gmail.com>
DKIM signature
missing
Download raw message
The starter guide says:

> Tree-sitter modes should be separate major modes, usually named xxx-ts-mode.

I'm a maintainer of rust-mode, and it seems to me that rust-mode
should simply use tree-sitter when it's available, and fall back to
the old techniques otherwise. But I'm new to treesit in Emacs, so I
don't have the full picture.

Why do you recommend keeping the tree-sitter and traditional modes separate?
Details
Message ID
<0F690646-3067-4513-8604-31C6CFF468EE@gmail.com>
In-Reply-To
<CAHgb+0kk9H74JRJUpmFocUh7TLHzC7L+c95t9u3-ygzMMeEZpw@mail.gmail.com> (view parent)
DKIM signature
missing
Download raw message

> On Jan 22, 2023, at 5:13 PM, Jim Blandy <jimb@red-bean.com> wrote:
> 
> The starter guide says:
> 
>> Tree-sitter modes should be separate major modes, usually named xxx-ts-mode.
> 
> I'm a maintainer of rust-mode, and it seems to me that rust-mode
> should simply use tree-sitter when it's available, and fall back to
> the old techniques otherwise. But I'm new to treesit in Emacs, so I
> don't have the full picture.
> 
> Why do you recommend keeping the tree-sitter and traditional modes separate?

We didn’t use separate modes initially and ran into some issues. 

For an example, js2-mode and js-json-mode derives from js-mode and relies on js-mode for setting up many things. If the user enables tree-sitter for js-mode, it will setup the tree-sitter stuff, and will not setup the native stuff, which js-json-mode and js2-mode relies on. So now js-json-mode and js2-mode are broken, whenever the user enabled tree-sitter in js-mode.

Also, in some cases many of the configuration variables for the native mode doesn’t apply to the tree-sitter mode, so keeping them in separate modes reduces confusion. (Obviously this varies from mode to mode.)

In the future we’ll have some fall-back mechanism for auto-mode, so it can decide which mode to use.

If you want automatic fallback, you can try defining a wrapper major mode that chooses between rust-mode and rust-ts-mode depending on tree-sitter’s availability. Or you can make rust-ts-mode fallback to rust-mode, we didn’t do that because it’s a tiny bit unclean: even if it falls back to rust-mode, rust-ts-mode-hook still runs. Ideally we don’t want rust-ts-mode-hook to run if rust-mode end up being chosen. That’s why we are planning to improve the auto-mode facility, rather than making tree-sitter modes fall back to native modes by themselves.

Yuan
Jim Blandy <jimb@red-bean.com>
Details
Message ID
<CAHgb+0kFsG0Ua3MuRVPnd-4Wkq1Ke4D4bQDfFsNL22GO79fKdQ@mail.gmail.com>
In-Reply-To
<0F690646-3067-4513-8604-31C6CFF468EE@gmail.com> (view parent)
DKIM signature
missing
Download raw message
That's a great explanation. Thanks very much.

On Mon, Jan 23, 2023 at 4:24 PM Yuan Fu <casouri@gmail.com> wrote:
>
>
>
> > On Jan 22, 2023, at 5:13 PM, Jim Blandy <jimb@red-bean.com> wrote:
> >
> > The starter guide says:
> >
> >> Tree-sitter modes should be separate major modes, usually named xxx-ts-mode.
> >
> > I'm a maintainer of rust-mode, and it seems to me that rust-mode
> > should simply use tree-sitter when it's available, and fall back to
> > the old techniques otherwise. But I'm new to treesit in Emacs, so I
> > don't have the full picture.
> >
> > Why do you recommend keeping the tree-sitter and traditional modes separate?
>
> We didn’t use separate modes initially and ran into some issues.
>
> For an example, js2-mode and js-json-mode derives from js-mode and relies on js-mode for setting up many things. If the user enables tree-sitter for js-mode, it will setup the tree-sitter stuff, and will not setup the native stuff, which js-json-mode and js2-mode relies on. So now js-json-mode and js2-mode are broken, whenever the user enabled tree-sitter in js-mode.
>
> Also, in some cases many of the configuration variables for the native mode doesn’t apply to the tree-sitter mode, so keeping them in separate modes reduces confusion. (Obviously this varies from mode to mode.)
>
> In the future we’ll have some fall-back mechanism for auto-mode, so it can decide which mode to use.
>
> If you want automatic fallback, you can try defining a wrapper major mode that chooses between rust-mode and rust-ts-mode depending on tree-sitter’s availability. Or you can make rust-ts-mode fallback to rust-mode, we didn’t do that because it’s a tiny bit unclean: even if it falls back to rust-mode, rust-ts-mode-hook still runs. Ideally we don’t want rust-ts-mode-hook to run if rust-mode end up being chosen. That’s why we are planning to improve the auto-mode facility, rather than making tree-sitter modes fall back to native modes by themselves.
>
> Yuan
>
Reply to thread Export thread (mbox)