Subject: Re: bug#74386: Tree-sitter javascript indentation

From: Yuan Fu <[email-masked]>
Date: Wed, 11 Dec 2024 21:28:58 -0800

> On Dec 11, 2024, at 7:20 PM, Dmitry Gutov <[email-masked]> wrote:
> 
> On 11/12/2024 08:18, Yuan Fu wrote:
>>>> Maybe not by hardcoding this in inside the 'standalone-parent' matcher, but writing this in the indentation rules? Different languages might have differing ASTs for such construct.
>>>> 
>>>> Or if you meant to do a text search, a period might start a method call, but it could also continue a "range" literal in some other language, or some struct initializer (I think?) in C/C++. Also, some languages allow (and style guides suggest) to have the previous at the end of the line, then followed by newline and then the method name.
>>> Not hard-coded, but customizable, like this:
>>> 
>>> Yuan
>>> 
>>> <standalone-predicate-poc.patch>
>> Circling back on this. WDYT? I think this would benefit all “modern” languages with chaining method calls.
> 
> It's an interesting suggestion - and the docstring is very readable.
> 
> I'd be great to see how it works with some existing modes' indentation code - e.g. to try to rewrite any of the rules in ruby-ts-mode (do we have any other ts mode with as many options affecting indentation?).
> 
> OT2H the use of 'standalone-parent' is optional, and it's combined with a matcher anyway, so it's good even if it covers like 80% of the cases.
> 
> What would be our next step in this? Replacing all 'parent-bol' anchors with 'standalone-parent' across most ts modes?

Speaking of next step, I recently added another handy tool for languages with C-like syntax: c-ts-common-baseline-indent-rule. I figured out an indent logic that can work on all C-like languages and covers a wide range of cases. This one rule can give you all theses indentation:

1. Statements align to their previous sibling:

    int main() {
      int a = 1;
      int b = 2; <-- Align to prev line’s sibling.
    }

2. Indents one level for blocks: function, if, for, struct, etc.

    int main() {
      return 0;   <-- Indent one level.
      {           <-- Align to prev line’s sibling.
        return 1; <-- Indent one level.
      }
    }

3. Elements in parenthesis and brackets:

    return [1, 2, 3,
            4, 5, 6]; <-- Align to first sibling.

    return [
      1, 2, 3,  <-- Indent one level (option 1).
      4, 5, 6,  <-- Align to prev line’s sibling.
    ];

    return [
            1, 2, 3,  <-- Align to opening bracket (option 2).
            4, 5, 6,  <-- Align to prev line’s sibling.
           ];         <-- Align to opening bracket.

    for (int i = 0;
         i < 10; <-- Align to first sibling.
         i++) {  <-- Align to prev line’s sibling.
      continue;
    }

4. Statement expressions indent one level when it’s broken into two
   lines:

    int main() {
      int var
        = 1287;  <-- Indent one level.
      int var =
        1287;    <-- Indent one level.
    }

Then a C-like language’s major mode only need to add special cases over the baseline indent rule. And if we add the configurable heuristic for standalone-parent, the baseline indent rules would make use of it.

I brought it up because if we’re going to do some renovations to indent rules, might as well make use of c-ts-common-baseline-indent-rule, and we probably don’t even need to replace parent-box with standalone-parent, because the baseline indent rule would cover most cases. 

I’ve already used it to rewrite c-ts-mode indent rules and it’s been a success; this baseline + override approach has been very helpful. c-ts-mode still has a lot of indent rules because of things like preproc directive, etc, but it’s much more manageable than before.

I don’t know how much it would help modes that has simpler indent rules. Go-ts-mode and rust-ts-mode only has a handful of indent rules, maybe they don’t really need this baseline rule. OTOH Lua and Ruby has more involved indent rules, maybe they can benefit and reduce the number of rules they need to define.

Yuan