Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] not enough indentation when using builder pattern #124

Closed
mtaran-google opened this issue Feb 28, 2018 · 4 comments
Closed

[java] not enough indentation when using builder pattern #124

mtaran-google opened this issue Feb 28, 2018 · 4 comments

Comments

@mtaran-google
Copy link
Contributor

class Clazz {
  void foo() {
    return new Foo.newBlah(
        new Bar().)
  }
}

if you have the cursor after the last . and press enter, the cursor goes underneath the start of new, whereas it should have a +4 indent

@marijnh
Copy link
Member

marijnh commented Mar 2, 2018

The builder pattern doesn't seem to a relevant factor here, this is how it indents any argument list. Do you want any continued expression to be indented extra? Or only specific types like .-access? I.e. should the third line be aligned with the start of the expression, or indented extra?

foo(
    1 + 3 +
    3 + 4)

@mtaran-google
Copy link
Contributor Author

I misspoke in my original example. The better example case is when you have the cursor before the last . and press enter. That's where the style guide permits a newline, not after the ..

https://google.github.io/styleguide/javaguide.html#s4.5.2-line-wrapping-indent says

two continuation lines use the same indentation level if and only if they begin with syntactically parallel elements.

I realize this is written a bit vaguely, but based on it I would say that things like (bracket-less) arithmetic, boolean, etc. expressions should be written with the same continuation level, but also sequential dot-accesses should too, e.g.:

T out = foo
    .bar()
    .baz()
    .quux();

is valid.

In my example (with the corrected position of the newline), there's a +4 indent after the newBlah(, because then you're in the context of the parameter list, and it's the first line break (wrap) there. Afterwards, after the new Bar(), you're in the context of a single element of the argument list, which has not yet had a line break (wrap) in it, so when you press enter you should get an additional +4 indent. If you continue typing .baz()\n.quux(), you'll continue to be in the context of the single element of the argument list, and since that element has already wrapped once, wrapping it more will not add additional indentation.

I hope that helps!

@marijnh
Copy link
Member

marijnh commented Mar 8, 2018

So a continued argument should always be intented more than the start of the argument? I.e. even in my example, you want the indentation shown below?

foo(
    1 + 3 +
        3 + 4)

@mtaran-google
Copy link
Contributor Author

I checked with our formatter, and yes, that is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants