Skip to content

Instantly share code, notes, and snippets.

@hschlichter
Last active July 6, 2018 14:16
Show Gist options
  • Save hschlichter/f685804da0faa24109fc617d06b23c9c to your computer and use it in GitHub Desktop.
Save hschlichter/f685804da0faa24109fc617d06b23c9c to your computer and use it in GitHub Desktop.

New Parens Indentation Option

So we have some issues with how we handle indentation and closing parens under certain weird cases. We shouldn't fix the current set of options to make it "work" with what we want. Instead we should create a new option that indents and closes parens with the wanted behavior. Then we enable the option in our configs separately.

The rules are as follows

Rule 1

While having open parens, doing line break will only result in 1 indentation per line break.

Rule 2

Closing parens that are not on the same line, should be at the original indentation level of the line with the open parens.

Rule 3

The line with the closing parens, should have same amount of closing parens as the original line with the open parens had.

Let us demostrate this with some example cases.

Case 1

Input

bool success(GenerateSecondaryUVSet(test(
    &mesh.vertices[0].x, mesh.vertices.size(),
    &triUV[0].x, &triList[0], triSrcPoly.size() ? &triSrcPoly[0] : 0, triCount,
    &outUV[0].x, param, errorBuffer, bufferSize
    )
));

Output

bool success(GenerateSecondaryUVSet(test(
    &mesh.vertices[0].x, mesh.vertices.size(),
    &triUV[0].x, &triList[0], triSrcPoly.size() ? &triSrcPoly[0] : 0, triCount,
    &outUV[0].x, param, errorBuffer, bufferSize
)));

In this case the first line bool success(GenerateSecondaryUVSet(test( contains 3 open parens. This means that the line that contains the closing parens should also have 3 closing parens. As shown in the output block in the last line containing )));.

Case 2.1

Input

int m = aaaaaaaaaaabaaaaaaaaaa(caaaaaaaaaa(
    eaaaaaaaaaa(gaaaaaaaaaaa(haaaaaaaaaaaaa(
        iaaaaaaaaaaaaaaaaaaaaaa(kaaaaaaaaaaaaaaaaaaaa)
        )
        )
    )
    )
);

Output

int m = aaaaaaaaaaabaaaaaaaaaa(caaaaaaaaaa(
    eaaaaaaaaaa(gaaaaaaaaaaa(haaaaaaaaaaaaa(
        iaaaaaaaaaaaaaaaaaaaaaa(kaaaaaaaaaaaaaaaaaaaa)
    )))
));

Here we have a case where there is multiple levels of indentation, and the rules of having closing parens aligned is still preserved. So the first line has 2 open parens int m = aaaaaaaaaaabaaaaaaaaaa(caaaaaaaaaa( which means that the ending line should have 2 closing parens ));.

Case 2.2

Input

int m = aaaaaaaaaaabaaaaaaaaaa(caaaaaaaaaa(
    eaaaaaaaaaa(gaaaaaaaaaaa(haaaaaaaaaaaaa(
        iaaaaaaaaaaaaaaaaaaaaaa(kaaaaaaaaaaaaaaaaaaaa)
        ))
    ))
);

Output

int m = aaaaaaaaaaabaaaaaaaaaa(caaaaaaaaaa(
    eaaaaaaaaaa(gaaaaaaaaaaa(haaaaaaaaaaaaa(
        iaaaaaaaaaaaaaaaaaaaaaa(kaaaaaaaaaaaaaaaaaaaa)
    )))
));

Another case like 2.1 just with slightly different input. But the result is the same.

Case 3

Input

int m = aaaaaaaaaaabaaaaaaaaaa(
    caaaaaaaaaa(
        eaaaaaaaaaa(
            gaaaaaaaaaaa(
                haaaaaaaaaaaaa(
                    iaaaaaaaaaaaaaaaaaaaaaa(kaaaaaaaaaaaaaaaaaaaa)
                )
            )
        )
    )
);

Output

int m = aaaaaaaaaaabaaaaaaaaaa(
    caaaaaaaaaa(
        eaaaaaaaaaa(
            gaaaaaaaaaaa(
                haaaaaaaaaaaaa(
                    iaaaaaaaaaaaaaaaaaaaaaa(kaaaaaaaaaaaaaaaaaaaa)
                )
            )
        )
    )
);

This is a case where the input already follows the mentioned rules. But it is actually formated differently. It is still very much legal code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment