Skip to content

Commit

Permalink
Fix formatting of class member functions
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Oct 16, 2023
1 parent c79ac50 commit 1c937ed
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@
{
"kind": "RESOURCE_KEYWORD",
"leadingMinutiae": [
{
"kind": "END_OF_LINE_MINUTIAE",
"value": "\n"
},
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
service / on new http:Listener(8080) {
resource function get /hello(string name) {
}

resource function get hello/(string name) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.ballerina.compiler.syntax.tree.ByteArrayLiteralNode;
import io.ballerina.compiler.syntax.tree.CaptureBindingPatternNode;
import io.ballerina.compiler.syntax.tree.CheckExpressionNode;
import io.ballerina.compiler.syntax.tree.ChildNodeList;
import io.ballerina.compiler.syntax.tree.ClassDefinitionNode;
import io.ballerina.compiler.syntax.tree.ClauseNode;
import io.ballerina.compiler.syntax.tree.ClientResourceAccessActionNode;
Expand Down Expand Up @@ -311,8 +312,25 @@ public FunctionDefinitionNode transform(FunctionDefinitionNode functionDefinitio
}
NodeList<Node> relativeResourcePath = formatNodeList(functionDefinitionNode.relativeResourcePath(), 0, 0, 0, 0);
FunctionSignatureNode functionSignatureNode = formatNode(functionDefinitionNode.functionSignature(), 1, 0);
int trailingNL = env.trailingNL;
if (isMemberOfScope(functionDefinitionNode)) {
ChildNodeList parentChildren = functionDefinitionNode.parent().children();
int nChildren = parentChildren.size();
boolean lastNode = false;

for (int i = nChildren - 1; i > -1; i--) {
Node child = parentChildren.get(i);
if (isMemberOfScope(child)) {
if (child.equals(functionDefinitionNode)) {
lastNode = true;
}
break;
}
}
trailingNL = (env.trailingNL > 2 || lastNode) ? env.trailingNL : 2;
}
FunctionBodyNode functionBodyNode =
formatNode(functionDefinitionNode.functionBody(), env.trailingWS, env.trailingNL);
formatNode(functionDefinitionNode.functionBody(), env.trailingWS, trailingNL);

return functionDefinitionNode.modify()
.withMetadata(metadata)
Expand Down Expand Up @@ -3886,6 +3904,17 @@ private <T extends Node> boolean isMultilineModuleMember(T node) {
}
}

private <T extends Node> boolean isMemberOfScope(T node) {
switch (node.kind()) {
case OBJECT_METHOD_DEFINITION:
case RESOURCE_ACCESSOR_DEFINITION:
case OBJECT_FIELD:
return true;
default:
return false;
}
}

/**
* Format a list of nodes.
*
Expand Down Expand Up @@ -4220,7 +4249,8 @@ private MinutiaeList getLeadingMinutiae(Token token) {

int consecutiveNewlines = 0;
Minutiae prevMinutiae = null;
if (env.hasNewline) {
boolean missingTokenWithNL = token.isMissing() && env.hasNewline;
if (!token.isMissing() && env.hasNewline) {
// 'hasNewlines == true' means a newline has already been added.
// Therefore, increase the 'consecutiveNewlines' count
consecutiveNewlines++;
Expand Down Expand Up @@ -4275,6 +4305,9 @@ private MinutiaeList getLeadingMinutiae(Token token) {
case INVALID_TOKEN_MINUTIAE_NODE:
case INVALID_NODE_MINUTIAE:
default:
if (missingTokenWithNL) {
missingTokenWithNL = false;
}
consecutiveNewlines = 0;
leadingMinutiae.add(minutiae);
break;
Expand All @@ -4284,7 +4317,7 @@ private MinutiaeList getLeadingMinutiae(Token token) {
}

// token.isMission() issue has to be discussed.
if (consecutiveNewlines > 0 && !env.preserveIndentation) {
if ((consecutiveNewlines > 0 || missingTokenWithNL) && !env.preserveIndentation) {
addWhitespace(env.currentIndentation, leadingMinutiae);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Foo {
function getName() {

}

remote function get() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Student {
string name;
int age;

public function init(string name, int age) {
self.name = name;
self.age = age;
}

public function getName() returns string {
return self.name;
}

public function getAge() returns int {
return self.age;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ class Person {
string b = "bar";
return b;
}

string month = "february";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Student {
string name;
int age;

public function init(string name, int age) {
self.name = name;
self.age = age;
}
public function getName() returns string {
return self.name;
}
public function getAge() returns int {
return self.age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ballerina/http;

public type Student readonly & record {
string name;
int age;
};

http:Listener httpListener = check new (9090);

public service class StudentService {
*http:Service;

Student[] students = [];

resource function get students() returns Student[] {
return self.students;
}

resource function post student(Student student) {
self.students.push(student);
}

resource function post students(Student[] students) {
self.students.push(...students);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ballerina/http;

public type Student readonly & record {
string name;
int age;
};

http:Listener httpListener = check new (9090);

public service class StudentService {
*http:Service;

Student[] students = [];

resource function get students() returns Student[] {
return self.students;
}
resource function post student(Student student) {
self.students.push(student);
}
resource function post students(Student[] students) {
self.students.push(...students);
}
}

0 comments on commit 1c937ed

Please sign in to comment.