Skip to content

Commit

Permalink
Merge pull request #41397 from poorna2152/function_call_formatting
Browse files Browse the repository at this point in the history
Fix multline function call formatting
  • Loading branch information
rdulmina authored Oct 19, 2023
2 parents ac3d86c + 5a4dbaf commit 74c138e
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
"value": " "
}
],
"trailingMinutiae": [
Expand Down Expand Up @@ -203,7 +203,7 @@
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
"value": " "
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public function main() {
foo(object {
int i = 1;
});
int i = 1;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -140,6 +143,10 @@ public void setLineWrapping(boolean lineWrapping) {
this.lineWrapping = lineWrapping;
}

public int getContinuationIndent() {
return continuationIndent;
}

public ForceFormattingOptions getForceFormattingOptions() {
return forceFormattingOptions;
}
Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParameterNode> parameters =
formatSeparatedNodeList(functionSignatureNode.parameters(), 0, 0, 0, 0, 0, 0, true);
unindent(2);
unindent(options.getContinuationIndent());

Token closePara;
ReturnTypeDescriptorNode returnTypeDesc = null;
Expand Down Expand Up @@ -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<FunctionArgumentNode> 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);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -4713,4 +4727,15 @@ private NodeList<ImportDeclarationNode> 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;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public function foo() {
(
) y = ()
;
;
var x = ();

int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public function foo() {
foreach string animal in animals {
match
animal
animal
{
"Mouse" =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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
);
}
Original file line number Diff line number Diff line change
@@ -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 ) ;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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
);
}

0 comments on commit 74c138e

Please sign in to comment.