ocaml / tuareg

Emacs OCaml mode
GNU General Public License v3.0
360 stars 79 forks source link

Using `indent-line-function` in another major-mode? #318

Closed KaranAhlawat closed 3 weeks ago

KaranAhlawat commented 3 weeks ago

What would be needed to use the indentation engine tuareg provides in another major mode? The use case is using the indentation engine with ocaml-ts-mode.

A similar case is python-ts-mode, which uses python-indent-line-function for indentation in python-ts-mode instead of using treesitter for indentation.

monnier commented 3 weeks ago

What would be needed to use the indentation engine tuareg provides in another major mode?

Hard to know without trying but at least:

The use case is using the indentation engine with ocaml-ts-mode.

Count me baffled, since I thought the main benefit of TreeSitter is for indentation.

A similar case is python-ts-mode, which uses python-indent-line-function for indentation in python-ts-mode instead of using treesitter for indentation.

I haven't looked at python-ts-mode but at least Python's indentation-significant syntax gives me a good reason to think that it might be "unusual". Can't think of any reason why OCaml's indentation wouldn't benefit from an actual AST like the one Tree Sitter can provide. Is it that Tree Sitter's OCaml grammar sucks?

KaranAhlawat commented 3 weeks ago

- Set indent-line-function. For this, I wasn't really sure how to set the indent-line-function since tuareg-mode seems to be wrapping the existing function in an advice only. But where does the existing function come from in the first place?

Count me baffled, since I thought the main benefit of TreeSitter is for indentation. It's not that it can't be used for indentation, but quite a few users of Tuareg mode like it's indentation engine, and I'd like to save some duplicated effort if possible (read: I'm being a bit lazy).

Can't think of any reason why OCaml's indentation wouldn't benefit from an actual AST like the one Tree Sitter can provide. Is it that Tree Sitter's OCaml grammar sucks? I don't think the grammar sucks (from my experiments with it). But, isn't Ocaml a little leaning to indentation based syntax as well? For example, if you have multiple let bindings in a file, you can define them without a ;;. Anyways, I digress.

If you can help me figure out the indent-line-function I think we might be going somewhere

monnier commented 3 weeks ago

- Set indent-line-function. For this, I wasn't really sure how to set the indent-line-function since tuareg-mode seems to be wrapping the existing function in an advice only. But where does the existing function come from in the first place?

It's set by smie-setup.

Count me baffled, since I thought the main benefit of TreeSitter is for indentation. It's not that it can't be used for indentation, but quite a few users of Tuareg mode like it's indentation engine, and I'd like to save some duplicated effort if possible (read: I'm being a bit lazy).

As the "main" original author of that indentation code, I'm happy to hear it, but it would be nice to try and reproduce its behavior using Tree Sitter.

I don't think the grammar sucks (from my experiments with it). But, isn't Ocaml a little leaning to indentation based syntax as well? For example, if you have multiple let bindings in a file, you can define them without a ;;. Anyways, I digress.

It can feel a bit magical, indeed, but this is disambiguated without any recourse to indentation information. 🙂

KaranAhlawat commented 3 weeks ago

Oookay, so, I was able to set it up and it works pretty much the same as tuareg-mode, but the rest of the mode is enhanced by tree-sitter (syntax highlighting, imenu, usage with third party packages such as combobulate, textobjects etc etc).

If I find free time over the weekend, I'll try to add some indentation using tree-sitter itself, we can compare which one feels nicer overall.

monnier commented 3 weeks ago

Oookay, so, I was able to set it up and it works pretty much the same as tuareg-mode, but the rest of the mode is enhanced by tree-sitter (syntax highlighting, imenu, usage with third party packages such as combobulate, textobjects etc etc).

👍🏾

If I find free time over the weekend, I'll try to add some indentation using tree-sitter itself, we can compare which one feels nicer overall.

That will compare your Tree Sitter indentation rules to the SMIE indentation rules, rather than comparing Tree Sitter to SMIE. IOW, which is better will largely depend on which one got more effort put into it.

KaranAhlawat commented 3 weeks ago

We can close this now, thank you so much for your help :)