From 59cb3eb1ebe31ffb83cf59c3a5ccc900a553f20e Mon Sep 17 00:00:00 2001 From: deathaxe Date: Sun, 30 Jan 2022 20:49:24 +0100 Subject: [PATCH] [JavaScript] Fix loop statement indentation (#3222) 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 --- JavaScript/Indentation Rules.tmPreferences | 11 ++- .../tests/syntax_test_js_indent_common.js | 81 +++++++++++++++---- 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/JavaScript/Indentation Rules.tmPreferences b/JavaScript/Indentation Rules.tmPreferences index e3d227d1a05..164335011ac 100644 --- a/JavaScript/Indentation Rules.tmPreferences +++ b/JavaScript/Indentation Rules.tmPreferences @@ -51,10 +51,10 @@ # followed by whitespace or block comments [optional] \g # top-level balanced parentheses - \( - (? (?: + (? + \( (?: # nested balanced parentheses - \( \g \) + \g # double quoted string with ignored escaped quotation marks | \".*(? # anything but closing parenthesis | [^)] - )* ) - # maybe missing, balanced or stray closing parenthesis - \)* + )* \) + ) ) # followed by whitespace or block comments [optional] \g diff --git a/JavaScript/tests/syntax_test_js_indent_common.js b/JavaScript/tests/syntax_test_js_indent_common.js index f5b193640c4..a0e3dc8b5d4 100644 --- a/JavaScript/tests/syntax_test_js_indent_common.js +++ b/JavaScript/tests/syntax_test_js_indent_common.js @@ -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++ @@ -722,7 +739,7 @@ function testWhileIndentationNoBraces(int { v++ } -function testWhileIndentationNoBracesButComments(int { +function testWhileIndentationNoBracesButComments(v) { while () v++ // ; "comment" () while () v++; // ; "comment" () while (()) v++ // ; "comment" () @@ -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" () @@ -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" () @@ -809,7 +826,7 @@ function testWhileIndentationNoBracesButBlockCommentsPart2(int { while /*)*/ () { } // a hack to make tests succeed } -function testWhileIndentationWithBraces(int { +function testWhileIndentationWithBraces(v) { while () { v++ } while () { v++; } @@ -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" () @@ -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" () }