[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Can this indentation be achieved with treesit-simple-indent-rules?
From: |
Nicolás Ojeda Bär |
Subject: |
Re: Can this indentation be achieved with treesit-simple-indent-rules? |
Date: |
Tue, 20 Jun 2023 12:16:46 +0200 |
Dear Yuan,
Thanks for your response. Let me give you a bit of context. I am
trying writing a major mode for OCaml. For indentation, I am trying to
replicate (as much as possible) the popular tool `ocp-indent`
(https://github.com/OCamlPro/ocp-indent). As far as I can see, this
tool tries to produce indentations which are "diff-friendly", that is,
such that breaking a line does not cause indentation to change in the
following lines. Concretely, consider the (indented) example
```
let x =
match e with
| Foo -> 0
```
If we move the `match` construct to the same line as `let`, I would
like the result to be indented as follows:
```
let x = match e with
| Foo -> 0
```
and _not_
```
let x = match e with
| Foo -> 0
```
(in the context of my original message, "a" was the "let" expression,
"b", the "match" expression, and "c" the match clause "| Foo -> 0").
I hope that clarifies a bit my motivation. Do let me know if anything
is unclear.
Cheers,
Nicolas
On Tue, Jun 20, 2023 at 8:12 AM Yuan Fu <casouri@gmail.com> wrote:
>
>
>
> > On Jun 19, 2023, at 12:11 PM, Nicolás Ojeda Bär <n.oje.bar@gmail.com> wrote:
> >
> > Hello,
> >
> > I am trying my hand at writing a tree-sitter major mode. I am using
> > "treesit-simple-indent-rules" for indentation, but have not managed to
> > get the indentation style that I am looking for.
> >
> > Suppose that the AST that you are trying to indent is (a (b c)) and
> > that you have
> > the indentation rules
> >
> > ((parent-is "a") parent-bol 2)
> > ((parent-is "b") parent-bol 0)
> >
> > Then the following is well-indented:
> >
> > a
> > b
> > c
> >
> > However, if "b" is in the same line as "a", then the result of indentation
> > is:
> >
> > a b
> > c
> >
> > But I would like to have instead
> >
> > a b
> > c
> >
> > That is, I would like "c" to be indented _as if_ "b" was on a separate
> > line. Is there a way to achieve this?
>
> There are different approaches to this. Could you maybe explain why you want
> to indent it like this, so that I know what’s your intent? Maybe also give a
> concert example? I’m guessing something like a is special and you only want
> one level of indentation for the whole statement, even if it occupies
> multiple lines; or it could be that b and c are special and should align?
>
> Yuan