Skip to content

Indentation is wrong inside function with multi-line parameter list #747

Closed
@gjm-lbo

Description

@gjm-lbo

Consider the following code:

function functionWithLongName(numberOfWidgets,
                              numberOfWadgets) {
  // ... code goes here ...
}

On hitting Enter after the {, the cursor is placed not one shift-width in from the left, but one shift-width in from the start of the numberOfWadgets line (i.e., the line on which the { occurs). Any new line created within the function body (but not within another {...} block) gets the same treatment.

This is not actually specific to function definitions; the same happens with the following code.

while (functionWithLongName(variableWithLongName)
       || a < b) {
  // ... body goes here ...
}

Of course this problem goes away if you use "Allman" rather than "1TBS" style and put the {s on their own lines (because the {s no longer occur on abnormally-indented lines), but it seems unfortunate for Vim to enforce that decision.

It seems like the "right" behaviour is something like:

  • Find the line with the enclosing {
  • Combine it with as many previous lines as are needed to avoid an excess of closing delimiters
  • Use the whitespace at the start of the resulting combined line as base for indentation
  • Use that combined line for identifying control structures that need special treatment, like switch

but maybe there's a better way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions