Skip to content

Option to indent wrapped lines by 2 * indent (e.g. +4) #5865

Closed

Description

@mprobst

prettier wraps lines and indents by the regular indentation level, i.e. +2 whitespace by default:

if (this.line.is.very.long()
  && moreCodeHere) {
  bodyOfBlock();
}
const similarilyAssumingLongVariableNames =
  someValue;

Many style guides (see citations below) request a 2 * indent for wrapped lines (also known as continuation indent), to make them stand out more from the body of the block:

if (this.line.is.very.long()
    && moreCodeHere) { // note: +4 indent here, not +2
  bodyOfBlock();
}
const similarilyAssumingLongVariableNames =
    someValue;

The reasoning is that it's otherwise easy to mix up statements of the body with expressions from the wrapped line, and indent from control structures/blocks (such as if statements) is more easily distinguished from wrapped lines.

This isn't universal, e.g. AirBnB's style guide is not explicit on the issue, but examples use a +2 continuation indent.

Would this be a valid option for prettier?

[0] https://contribute.jquery.org/style-guide/js/#multi-line-statements
[1] https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md#indent-style
[2] https://google.github.io/styleguide/javaguide.html#s4.5.2-line-wrapping-indent
[3] https://google.github.io/styleguide/jsguide.html#formatting-indent
[4] https://www.oracle.com/technetwork/java/codeconventions-150003.pdf
[5] counter example: https://github.com/airbnb/javascript

Activity

  1. lydell commented on Feb 14, 2019

    @lydell
    Member

    It's already clearly separated:

    Prettier 1.16.4
    Playground link

    --parser babylon

    Input:

    if (this.line.is.very.long()
      && more.code.here(running + out, ofIdeas, wrapAlready)) {
      bodyOfBlock();
    }
    const similarilyAssumingLongVariableNames =
      someValue;

    Output:

    if (
      this.line.is.very.long() &&
      more.code.here(running + out, ofIdeas, wrapAlready)
    ) {
      bodyOfBlock();
    }
    const similarilyAssumingLongVariableNames = someValue;
  2. added
    status:awaiting response
    on Feb 14, 2019
  3. voithos commented on Feb 14, 2019

    @voithos
    Contributor

    Prettier seems to do something analogous to this with template languages, as well (at least, I've seen this on Angular template formatting). If the opening tag is wrapped (e.g. because it has many attributes), the indent is +2 by default.

    <really-long-tag
      withLotsOf="attributes"
      [and]="bindings"
      ...
    >
      <inner-code>...</inner-code>
    </really-long-tag>
    <other-tag
      *ngIf="long.condition"
      ...
    >
      ...
    </other-tag>
    

    This seems to create a bit of a visual imbalance (perhaps more so than the JS examples), since the opening tag name is attached to the first <, instead of going on the next line. Compare to the +4 continuation indent example, which seems to more clearly differentiate between inner HTML vs continuation lines:

    <really-long-tag
        withLotsOf="attributes"
        [and]="bindings"
        ...
    >
      <inner-code>...</inner-code>
    </really-long-tag>
    <other-tag
        *ngIf="long.condition"
        ...
    >
      ...
    </other-tag>
    
  4. added
    type:option request
    lang:javascript
    on Feb 14, 2019
  5. no-response commented on Feb 28, 2019

    @no-response

    This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

  6. vincent-olivert-riera commented on Mar 10, 2019

    @vincent-olivert-riera

    @lydell can this issue be reopened? I'm interested on that feature (double-indent for wrapped lines).

  7. lydell commented on Mar 10, 2019

    @lydell
    Member

    @vincent-olivert-riera Feel free to open a new issue! It is not clear what this one is about anymore.

  8. mprobst commented on Mar 10, 2019

    @mprobst
    Author
  9. removed
    status:awaiting response
    on Mar 10, 2019
  10. added
    locked-due-to-inactivity
    on Jun 8, 2019
  11. locked as resolved and limited conversation to collaborators on Jun 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:javascriptlocked-due-to-inactivitytype:option request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions