timakro / vim-yadi

Yet Another Detect Indent
MIT License
15 stars 3 forks source link

Continuation indent #1

Closed Frederick888 closed 3 years ago

Frederick888 commented 3 years ago

AFAIU, the algorithm currently doesn't distinguish regular indentation and continuation indentation.

For example, one may want the former to be 2 and the latter to be 4:

public class SomeClass {
  public void doSomething() {               // 2
    if (true                                // 2
        && false) {                         // 4
      someMethodWithALotOfArguments(arg1,   // 2
          arg2.formatWith("XYZ",            // 4
              true),                        // 4
          arg3.formatWith("ABC",            // 4
              false));                      // 4
    }
  }
}

In this case, it seems vim-yadi thinks the file is indented with 4 spaces but actually 2.

I wonder if it's possible to cater for this scenario?

timakro commented 3 years ago

I'd think files where this continuation indent is outweighing the standard indentation are pretty rare. By no means justifiable to complicate the algorithm for this in my opinion. You'll have to write :IndentSpaces 2 before you start editing the file I'm afraid. But chances are that your edit involves adding new lines and the balance shifts.

timakro commented 3 years ago

For something to happen here we'd need evidence that this is sufficiently common in real codebases as well as an implementation proposal.

For example I checked the https://github.com/AntennaPod/AntennaPod repository for misdetections and found only this and this file out of 489 which were misdetected. I can't see a language-agnostic way to solve this.

For reference, this is the command I used:

find -name '*.java' | xargs -d'\n' -n1 vim -c 'redir>>detectedindents | echo (&expandtab?shiftwidth()." sp ":"tabs ").@% | q'

Closing for now.

Frederick888 commented 3 years ago

Welp, Java programmers and their long method name fetish... And if you have a look at factory classes in projects that use lombok, it's not uncommon to have 10-20 lines with continuation indentations in each method.

While using something like a stack to keep track of various symbols may solve this issue, I guess that'll be a bit too expensive. So what about ignoring lines that begin with / come after some symbols? For example, when a line starts with ., &, |, ^, etc.; or when the last line ends with ,, (, ->, etc., it doesn't contribute to the end result of yadi. (And of course it'd be nice if these lists can be configurable.)