Skip to content

Commit

Permalink
Update function call formatting with scoped args
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Oct 13, 2023
1 parent 4f921a4 commit 1108daa
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 8 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 @@ -1062,10 +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);
indent(options.getContinuationIndent());
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, true);
unindent(options.getContinuationIndent());
env.currentIndentation = prevIndentation;
Token functionCallClosePara = formatToken(functionCallExpressionNode.closeParenToken(),
env.trailingWS, env.trailingNL);

Expand Down Expand Up @@ -1838,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 @@ -4475,6 +4483,9 @@ private void preserveIndentation(boolean value) {
private int getPreservedIndentation(Token token) {
int position = token.lineRange().startLine().offset();
int offset = position % 4;
if (env.currentIndentation % 4 == 0 && env.currentIndentation > position) {
return env.currentIndentation;
}
if (offset != 0) {
if (offset > 2) {
position = position + 4 - offset;
Expand Down Expand Up @@ -4715,4 +4726,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 @@
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 @@
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 1108daa

Please sign in to comment.