Skip to content

Commit

Permalink
[JavaScript] Fix loop statement indentation (sublimehq#3222)
Browse files Browse the repository at this point in the history
This commit restricts `bracketIndentNextLinePattern` to not match loops
if the loop condition expression spans multiple lines as it breaks
indentation of the following code block if the block's opening
brace directly follows the closing parenthesis on the same line.

The result was only the first line of the block being indented.

Fixes https://forum.sublimetext.com/t/the-indenting-of-a-c-for-loop-is-incorrect-under-certain-conditions/52634/4
  • Loading branch information
deathaxe authored and mitranim committed Mar 20, 2022
1 parent 2ac883c commit 59cb3eb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
11 changes: 5 additions & 6 deletions JavaScript/Indentation Rules.tmPreferences
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
# followed by whitespace or block comments [optional]
\g<comment_or_whitespace>
# top-level balanced parentheses
\(
(?<group_body> (?:
(?<group>
\( (?:
# nested balanced parentheses
\( \g<group_body> \)
\g<group>
# double quoted string with ignored escaped quotation marks
| \".*(?<![^\\]\\)(?<![\\]{3})\"
# single quoted character with ignored escaped quotation marks
Expand All @@ -63,9 +63,8 @@
| \g<block_comment>
# anything but closing parenthesis
| [^)]
)* )
# maybe missing, balanced or stray closing parenthesis
\)*
)* \)
)
)
# followed by whitespace or block comments [optional]
\g<comment_or_whitespace>
Expand Down
81 changes: 67 additions & 14 deletions JavaScript/tests/syntax_test_js_indent_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,33 +668,50 @@ function testSwitchCaseIndentationWithLineComments(v) {
} // comments
} // comments

function testForIndentation(int {
function testForIndentation(v) {

for (int i = 0; i < 10; i++)
for (let i = 0; i < 10; i++)
System.out.println("Row " + i);

for (int i = 0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
System.out.println("Row " + i);
}

for (int i = 0; i < 10; i++)
for (let i = 0; i < 10; i++)
{
System.out.println("Row " + i);
}

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++)
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++)
System.out.println("Row " + i + " Col " + j);
}

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
System.out.println("Row " + i + " Col " + j);
}
}

for (
let i = 0;
i < 10;
i++)
{
let j = 0;
let k = 0;
}

for (
let i = 0;
i < 10;
i++) {
let j = 0;
let k = 0;
}
}

function testWhileIndentationNoBraces(int {
function testWhileIndentationNoBraces(v) {
while () v++
while () v++;
while (()) v++
Expand Down Expand Up @@ -722,7 +739,7 @@ function testWhileIndentationNoBraces(int {
v++
}

function testWhileIndentationNoBracesButComments(int {
function testWhileIndentationNoBracesButComments(v) {
while () v++ // ; "comment" ()
while () v++; // ; "comment" ()
while (()) v++ // ; "comment" ()
Expand Down Expand Up @@ -751,7 +768,7 @@ function testWhileIndentationNoBracesButComments(int {
while () { } // a hack to make tests succeed
}

function testWhileIndentationNoBracesButBlockCommentsPart1(int {
function testWhileIndentationNoBracesButBlockCommentsPart1(v) {
while /*(*/ () v++ /*(*/ // ; "comment" ()
while /*(*/ () v++; /*(*/ // ; "comment" ()
while /*(*/ (()) v++ /*(*/ // ; "comment" ()
Expand Down Expand Up @@ -780,7 +797,7 @@ function testWhileIndentationNoBracesButBlockCommentsPart1(int {
while /*(*/ () { } // a hack to make tests succeed
}

function testWhileIndentationNoBracesButBlockCommentsPart2(int {
function testWhileIndentationNoBracesButBlockCommentsPart2(v) {
while /*)*/ () v++ /*)*/ // ; "comment" ()
while /*)*/ () v++; /*)*/ // ; "comment" ()
while /*)*/ (()) v++ /*)*/ // ; "comment" ()
Expand Down Expand Up @@ -809,7 +826,7 @@ function testWhileIndentationNoBracesButBlockCommentsPart2(int {
while /*)*/ () { } // a hack to make tests succeed
}

function testWhileIndentationWithBraces(int {
function testWhileIndentationWithBraces(v) {

while () { v++ }
while () { v++; }
Expand Down Expand Up @@ -886,9 +903,27 @@ function testWhileIndentationWithBraces(int {
{
v++
}
while (
v == foo( bar("") + "" )
)
{
v++
v++
}
while (
v == foo( bar("") + "" ) ) {
v++
v++
}
while (
v == foo( bar("") + "" )
) {
v++
v++
}
}

function testWhileIndentationWithBracesAndComments(int {
function testWhileIndentationWithBracesAndComments(v) {

while () { v++ } // ; "comments" ()
while () { v++; } // ; "comments" ()
Expand Down Expand Up @@ -965,4 +1000,22 @@ function testWhileIndentationWithBracesAndComments(int {
{ // ; "comments" ()
v++ // ; "comments" ()
} // ; "comments" ()
while ( // ; "comments" ()
v == foo( bar("") + "" ) // ; "comments" ()
) // ; "comments" ()
{ // ; "comments" ()
v++ // ; "comments" ()
v++ // ; "comments" ()
} // ; "comments" ()
while ( // ; "comments" ()
v == foo( bar("") + "" ) ) { // ; "comments" ()
v++ // ; "comments" ()
v++ // ; "comments" ()
} // ; "comments" ()
while ( // ; "comments" ()
v == foo( bar("") + "" ) // ; "comments" ()
) { // ; "comments" ()
v++ // ; "comments" ()
v++ // ; "comments" ()
} // ; "comments" ()
}

0 comments on commit 59cb3eb

Please sign in to comment.