Skip to content

Commit

Permalink
Merge pull request #41806 from poorna2152/adding_spaces
Browse files Browse the repository at this point in the history
Fix formatter adding of spaces in consecutive use of format command
  • Loading branch information
lochana-chathura authored Feb 21, 2024
2 parents 8c00f39 + 300adf5 commit 838820f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
import io.ballerina.compiler.syntax.tree.XMLStartTagNode;
import io.ballerina.compiler.syntax.tree.XMLStepExpressionNode;
import io.ballerina.compiler.syntax.tree.XMLTextNode;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -4234,10 +4235,12 @@ private MinutiaeList getLeadingMinutiae(Token token) {
// Therefore, increase the 'consecutiveNewlines' count
consecutiveNewlines++;

for (int i = 0; i < env.leadingNL; i++) {
prevMinutiae = getNewline();
leadingMinutiae.add(prevMinutiae);
consecutiveNewlines++;
if (!token.isMissing()) {
for (int i = 0; i < env.leadingNL; i++) {
prevMinutiae = getNewline();
leadingMinutiae.add(prevMinutiae);
consecutiveNewlines++;
}
}
}

Expand All @@ -4257,7 +4260,8 @@ private MinutiaeList getLeadingMinutiae(Token token) {
// Shouldn't update the prevMinutiae
continue;
}
if (env.preserveIndentation) {
if (env.preserveIndentation &&
(prevMinutiae == null || prevMinutiae.kind() == SyntaxKind.END_OF_LINE_MINUTIAE)) {
addWhitespace(getPreservedIndentation(token), leadingMinutiae);
} else {
addWhitespace(1, leadingMinutiae);
Expand Down Expand Up @@ -4492,18 +4496,23 @@ private void preserveIndentation(boolean value) {
* @param token token of which the indentation is required.
*/
private int getPreservedIndentation(Token token) {
int position = token.lineRange().startLine().offset();
LinePosition startLinePos = token.lineRange().startLine();
int position = startLinePos.offset();
int startLine = startLinePos.line();
for (Token invalidToken : token.leadingInvalidTokens()) {
LinePosition invalidTokenStartLinePos = invalidToken.lineRange().startLine();
if (invalidTokenStartLinePos.line() == startLine) {
position = invalidTokenStartLinePos.offset();
break;
}
}
int tabSize = options.getTabSize();
int offset = position % tabSize;
if (env.currentIndentation % tabSize == 0 && env.currentIndentation > position) {
return env.currentIndentation;
}
int offset = position % tabSize;
if (offset != 0) {
if (offset > 2) {
position = position + tabSize - offset;
} else {
position = position - offset;
}
return offset > 2 ? position + tabSize - offset : position - offset;
}
return position;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function f {
if
worker A {

}
}

function g {
if
worker
B {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function f {
if
worker A{

}
}

function g {
if
worker
B {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function baz() {
int x = 1;
do {
int b = 2;
} on f

if
while x < 5 {
x += 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function baz() {
int x = 1;
do {
int b = 2;
} on f

if
while x < 5 {
x += 1;
}
}

0 comments on commit 838820f

Please sign in to comment.