diff --git a/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_assert_10.json b/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_assert_10.json index c68a2c7d374b..987ebde619d8 100644 --- a/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_assert_10.json +++ b/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_assert_10.json @@ -145,7 +145,7 @@ "leadingMinutiae": [ { "kind": "WHITESPACE_MINUTIAE", - "value": " " + "value": " " } ], "trailingMinutiae": [ @@ -203,7 +203,7 @@ "leadingMinutiae": [ { "kind": "WHITESPACE_MINUTIAE", - "value": " " + "value": " " } ] } diff --git a/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_source_10.bal b/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_source_10.bal index f5f93c0c3adc..139b64d604cd 100644 --- a/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_source_10.bal +++ b/compiler/ballerina-parser/src/test/resources/expressions/object-constructor/object_constructor_source_10.bal @@ -1,5 +1,5 @@ public function main() { foo(object { - int i = 1; - }); + int i = 1; + }); } diff --git a/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingOptions.java b/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingOptions.java index 214b8eb4405e..938ebeb76881 100644 --- a/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingOptions.java +++ b/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingOptions.java @@ -30,17 +30,20 @@ public class FormattingOptions { private boolean lineWrapping; + private int continuationIndent; + private ForceFormattingOptions forceFormattingOptions; private ImportFormattingOptions importFormattingOptions; private FormattingOptions(int tabSize, String wsCharacter, int columnLimit, boolean lineWrapping, - ForceFormattingOptions forceFormattingOptions, + int continuationIndent, ForceFormattingOptions forceFormattingOptions, ImportFormattingOptions importFormattingOptions) { this.tabSize = tabSize; this.wsCharacter = wsCharacter; this.columnLimit = columnLimit; this.lineWrapping = lineWrapping; + this.continuationIndent = continuationIndent; this.forceFormattingOptions = forceFormattingOptions; this.importFormattingOptions = importFormattingOptions; } @@ -140,6 +143,10 @@ public void setLineWrapping(boolean lineWrapping) { this.lineWrapping = lineWrapping; } + public int getContinuationIndent() { + return continuationIndent; + } + public ForceFormattingOptions getForceFormattingOptions() { return forceFormattingOptions; } @@ -162,6 +169,7 @@ public static class FormattingOptionsBuilder { private String wsCharacter = " "; private int columnLimit = 120; private boolean lineWrapping = false; + private int continuationIndent = 2; private ForceFormattingOptions forceFormattingOptions = ForceFormattingOptions.builder().build(); private ImportFormattingOptions importFormattingOptions = ImportFormattingOptions.builder().build(); @@ -198,8 +206,8 @@ public FormattingOptions.FormattingOptionsBuilder setImportFormattingOptions( } public FormattingOptions build() { - return new FormattingOptions(tabSize, wsCharacter, columnLimit, lineWrapping, forceFormattingOptions, - importFormattingOptions); + return new FormattingOptions(tabSize, wsCharacter, columnLimit, lineWrapping, continuationIndent, + forceFormattingOptions, importFormattingOptions); } } } diff --git a/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingTreeModifier.java b/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingTreeModifier.java index 6526e00fadbe..185d1094f875 100644 --- a/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingTreeModifier.java +++ b/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormattingTreeModifier.java @@ -365,10 +365,10 @@ public FunctionSignatureNode transform(FunctionSignatureNode functionSignatureNo Token openPara = formatToken(functionSignatureNode.openParenToken(), 0, parenTrailingNL); // Start a new indentation of two tabs for the parameters. - indent(2); + indent(options.getContinuationIndent()); SeparatedNodeList parameters = formatSeparatedNodeList(functionSignatureNode.parameters(), 0, 0, 0, 0, 0, 0, true); - unindent(2); + unindent(options.getContinuationIndent()); Token closePara; ReturnTypeDescriptorNode returnTypeDesc = null; @@ -1062,8 +1062,15 @@ public ReturnStatementNode transform(ReturnStatementNode returnStatementNode) { public FunctionCallExpressionNode transform(FunctionCallExpressionNode functionCallExpressionNode) { NameReferenceNode functionName = formatNode(functionCallExpressionNode.functionName(), 0, 0); Token functionCallOpenPara = formatToken(functionCallExpressionNode.openParenToken(), 0, 0); + int prevIndentation = env.currentIndentation; + if (functionCallExpressionNode.arguments().size() > 0) { + if (!isScopedFunctionArgument(functionCallExpressionNode.arguments().get(0))) { + indent(options.getContinuationIndent()); + } + } SeparatedNodeList arguments = formatSeparatedNodeList(functionCallExpressionNode - .arguments(), 0, 0, 0, 0); + .arguments(), 0, 0, 0, 0, true); + env.currentIndentation = prevIndentation; Token functionCallClosePara = formatToken(functionCallExpressionNode.closeParenToken(), env.trailingWS, env.trailingNL); @@ -1836,6 +1843,9 @@ public MarkdownCodeLineNode transform(MarkdownCodeLineNode markdownCodeLineNode) @Override public PositionalArgumentNode transform(PositionalArgumentNode positionalArgumentNode) { + if (env.lineLength != 0 && isScopedFunctionArgument(positionalArgumentNode)) { + env.currentIndentation = env.lineLength; + } ExpressionNode expression = formatNode(positionalArgumentNode.expression(), env.trailingWS, env.trailingNL); return positionalArgumentNode.modify() .withExpression(expression) @@ -4472,10 +4482,14 @@ private void preserveIndentation(boolean value) { */ private int getPreservedIndentation(Token token) { int position = token.lineRange().startLine().offset(); - int offset = position % 4; + int tabSize = options.getTabSize(); + int offset = position % tabSize; + if (env.currentIndentation % tabSize == 0 && env.currentIndentation > position) { + return env.currentIndentation; + } if (offset != 0) { if (offset > 2) { - position = position + 4 - offset; + position = position + tabSize - offset; } else { position = position - offset; } @@ -4713,4 +4727,15 @@ private NodeList sortAndGroupImportDeclarationNodes( imports.addAll(thirdPartyImportNodes.stream().collect(Collectors.toList())); return NodeFactory.createNodeList(imports); } + + private boolean isScopedFunctionArgument(FunctionArgumentNode functionArgumentNode) { + if (functionArgumentNode.parent().kind() == SyntaxKind.FUNCTION_CALL && + functionArgumentNode.children().size() > 0) { + SyntaxKind kind = functionArgumentNode.children().get(0).kind(); + if (kind == SyntaxKind.OBJECT_CONSTRUCTOR || kind == SyntaxKind.MAPPING_CONSTRUCTOR) { + return true; + } + } + return false; + } } diff --git a/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_3.bal b/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_3.bal index 2ae7f56b3534..6ce4f5736fdd 100644 --- a/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_3.bal +++ b/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_3.bal @@ -1,7 +1,7 @@ public function foo() { ( ) y = () -; + ; var x = (); int diff --git a/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_4.bal b/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_4.bal index c314dd697088..cbf973139ef2 100644 --- a/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_4.bal +++ b/misc/formatter/modules/formatter-core/src/test/resources/misc/linebreaks/assert/line_breaks_4.bal @@ -1,7 +1,7 @@ public function foo() { foreach string animal in animals { match - animal + animal { "Mouse" => { diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_2.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_2.bal new file mode 100644 index 000000000000..d4b192fb7f58 --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_2.bal @@ -0,0 +1,13 @@ +import ballerina/io; + +function calculateTotalInvoiceAmount(int customerId, string invoiceDate, + decimal[] itemPrices, boolean isTaxable) returns decimal { + decimal totalAmount = 0.0; + return totalAmount; +} + +public function main() { + decimal invoiceAmount = calculateTotalInvoiceAmount(12345, "2023-09-13", + [25.99, 19.95, 12.49, 7.99, 34.50], true); + io:println(invoiceAmount); +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_3.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_3.bal new file mode 100644 index 000000000000..b4570806ec28 --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_3.bal @@ -0,0 +1,21 @@ +function calculate() returns int { + int result = subtract(add([21, 45, 6, 12, 67, 89], [1, 5, 6, 9]), + add([11, 12, 13, 14, 15, 16, 17, 18, 19, 20], + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); + return result; +} + +function add(int[] a, int[] b) returns int { + int sum = 0; + foreach int n in a { + sum += n; + } + foreach int n in b { + sum += n; + } + return sum; +} + +function subtract(int x, int y) returns int { + return x - y; +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_4.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_4.bal new file mode 100644 index 000000000000..b7be24b37726 --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_4.bal @@ -0,0 +1,13 @@ +type SourcePatient record { + string message; + string detail; + string cause; +}; + +function foo(string m, int c, int t, string d, string cause) { +} + +function bar(SourcePatient sourcePatient, int errorCode, int errorType) { + return foo(sourcePatient.message, errorCode, errorType, sourcePatient.detail, + sourcePatient.cause); +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_5.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_5.bal new file mode 100644 index 000000000000..061faf40c6f2 --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_5.bal @@ -0,0 +1,32 @@ +public function main() { + foo(object { + int i = 1; + }, a, + b); +} + +public function bar() { + bar(t1, object { + int i = 1; + }, t2, t3); +} + +public function baz() { + baz(t1, t2, object { + int i = 1; + int y = 2; + }, + b, + c, + d); +} + +public function fox() { + foz(t1, + object { + int i = 1; + }, + b, + c, + d); +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_6.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_6.bal new file mode 100644 index 000000000000..5f3cb33a835a --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/assert/call_statement_6.bal @@ -0,0 +1,21 @@ +function processEmployeeInfo(string firstName, string lastName, string department, string jobTitle, + int employeeId, float salary) { +} + +function foo() { + string firstName = "John"; + string lastName = "Doe"; + string department = "Engineering"; + string jobTitle = "Software Engineer"; + int employeeId = 1001; + float salary = 75000.0; + + processEmployeeInfo( + firstName, + lastName, + department, + jobTitle, + employeeId, + salary + ); +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_2.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_2.bal new file mode 100644 index 000000000000..fe29d6cb3bdd --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_2.bal @@ -0,0 +1,13 @@ +import ballerina/io; + +function calculateTotalInvoiceAmount(int customerId, string invoiceDate, + decimal[] itemPrices, boolean isTaxable) returns decimal { + decimal totalAmount = 0.0; + return totalAmount; +} + +public function main() { + decimal invoiceAmount = calculateTotalInvoiceAmount ( 12345, "2023-09-13", + [25.99, 19.95, 12.49, 7.99, 34.50], true ) ; + io:println ( invoiceAmount ) ; +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_3.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_3.bal new file mode 100644 index 000000000000..a64ab1eecd1b --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_3.bal @@ -0,0 +1,21 @@ +function calculate() returns int { + int result = subtract(add([21, 45, 6, 12, 67, 89], [1,5,6,9]), + add([11,12,13,14,15,16,17,18,19,20], + [1,2,3,4,5,6,7,8,9,10])); + return result; +} + +function add(int[] a, int[] b) returns int { + int sum = 0; + foreach int n in a { + sum += n; + } + foreach int n in b { + sum += n; + } + return sum; +} + +function subtract(int x, int y) returns int { + return x - y; +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_4.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_4.bal new file mode 100644 index 000000000000..46a52510845b --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_4.bal @@ -0,0 +1,13 @@ +type SourcePatient record { + string message; + string detail; + string cause; +}; + +function foo(string m, int c, int t, string d, string cause) { +} + +function bar(SourcePatient sourcePatient, int errorCode, int errorType) { + return foo(sourcePatient.message, errorCode, errorType, sourcePatient.detail, + sourcePatient.cause); +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_5.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_5.bal new file mode 100644 index 000000000000..4b94f5bbf6cd --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_5.bal @@ -0,0 +1,32 @@ +public function main() { + foo(object { + int i = 1; + }, a, + b); +} + +public function bar() { + bar(t1, object { + int i = 1; + }, t2, t3); +} + +public function baz() { + baz(t1, t2, object { + int i = 1; + int y = 2; + }, + b, + c, + d); +} + +public function fox() { + foz(t1, + object { + int i = 1; + }, + b, + c, + d); +} diff --git a/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_6.bal b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_6.bal new file mode 100644 index 000000000000..1601f40175a2 --- /dev/null +++ b/misc/formatter/modules/formatter-core/src/test/resources/statements/call/source/call_statement_6.bal @@ -0,0 +1,21 @@ +function processEmployeeInfo(string firstName, string lastName, string department, string jobTitle, + int employeeId, float salary) { +} + +function foo() { + string firstName = "John"; + string lastName = "Doe"; + string department = "Engineering"; + string jobTitle = "Software Engineer"; + int employeeId = 1001; + float salary = 75000.0; + + processEmployeeInfo( + firstName, + lastName, + department, + jobTitle, + employeeId, + salary + ); +}