Skip to content

Commit

Permalink
Merge pull request #42370 from ballerina-platform/poorna_formatter
Browse files Browse the repository at this point in the history
[Master] Maintain minimum number of new lines when formatting methods in a class/service
  • Loading branch information
lochana-chathura authored Mar 22, 2024
2 parents c7f447d + 5b03581 commit 972ae7d
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 5 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 @@ -253,6 +253,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.ballerinalang.formatter.core.FormatterUtils.isInlineRange;
Expand Down Expand Up @@ -288,7 +289,8 @@ public FormattingTreeModifier(FormattingOptions options, LineRange lineRange) {
@Override
public ModulePartNode transform(ModulePartNode modulePartNode) {
NodeList<ImportDeclarationNode> imports = sortAndGroupImportDeclarationNodes(modulePartNode.imports());
NodeList<ModuleMemberDeclarationNode> members = formatModuleMembers(modulePartNode.members());
NodeList<ModuleMemberDeclarationNode> members =
formatMemberDeclarations(modulePartNode.members(), n -> isMultilineModuleMember(n));
Token eofToken = formatToken(modulePartNode.eofToken(), 0, 0);
return modulePartNode.modify(imports, members, eofToken);
}
Expand Down Expand Up @@ -700,7 +702,8 @@ public ServiceDeclarationNode transform(ServiceDeclarationNode serviceDeclaratio
formatSeparatedNodeList(serviceDeclarationNode.expressions(), 0, 0, 1, 0);
Token openBrace = formatToken(serviceDeclarationNode.openBraceToken(), 0, 1);
indent(); // increase the indentation of the following statements.
NodeList<Node> members = formatNodeList(serviceDeclarationNode.members(), 0, 1, 0, 1);
NodeList<Node> members =
formatMemberDeclarations(serviceDeclarationNode.members(), n -> isClassOrServiceMultiLineMember(n));
unindent(); // reset the indentation.
Optional<Token> optSemicolon = serviceDeclarationNode.semicolonToken();
Token closeBrace = optSemicolon.isPresent() ?
Expand Down Expand Up @@ -3462,7 +3465,8 @@ public ClassDefinitionNode transform(ClassDefinitionNode classDefinitionNode) {
Token openBrace = formatToken(classDefinitionNode.openBrace(), 0, 1);

indent();
NodeList<Node> members = formatNodeList(classDefinitionNode.members(), 0, 1, 0, 1);
NodeList<Node> members =
formatMemberDeclarations(classDefinitionNode.members(), n -> isClassOrServiceMultiLineMember(n));
unindent();
Optional<Token> optSemicolon = classDefinitionNode.semicolonToken();
Token closeBrace = optSemicolon.isPresent() ?
Expand Down Expand Up @@ -3837,7 +3841,14 @@ private <T extends Token> T formatToken(T token, int trailingWS, int trailingNL)
return formatToken(token, trailingWS, trailingNL, null);
}

protected <T extends Node> NodeList<T> formatModuleMembers(NodeList<T> members) {
/**
* Format members in module-level, class and service level.
*
* @param members Members of the scope
* @param filter filter to identify multiline members
* @return Formatted list of members
*/
protected <T extends Node> NodeList<T> formatMemberDeclarations(NodeList<T> members, Predicate<Node> filter) {
if (members.isEmpty()) {
return members;
}
Expand All @@ -3855,7 +3866,7 @@ protected <T extends Node> NodeList<T> formatModuleMembers(NodeList<T> members)
// We need to do this check, because different kinds of children needs
// different number of newlines in-between.
int itemTrailingNL = 1;
if (isMultilineModuleMember(currentMember) || isMultilineModuleMember(nextMember)) {
if (filter.test(currentMember) || filter.test(nextMember)) {
itemTrailingNL++;
}

Expand Down Expand Up @@ -3896,6 +3907,20 @@ private <T extends Node> boolean isMultilineModuleMember(T node) {
}
}

private boolean isClassOrServiceMultiLineMember(Node node) {
if (node == null) {
return false;
}

switch (node.kind()) {
case OBJECT_METHOD_DEFINITION:
case RESOURCE_ACCESSOR_DEFINITION:
return true;
default:
return false;
}
}

/**
* Format a list of nodes.
*
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 @@ -10,9 +10,11 @@ class Person {

public string name = "sample name";
int year = 50;

function bar() returns string {
string b = "bar";
return b;
}

string month = "february";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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,35 @@
import ballerina/grpc;

configurable int port = 9090;

type Book record {
string id;
string title;
string author;
float price;
};

Book[] books = [
{id: "1", title: "Book One", author: "Author One", price: 29.99},
{id: "2", title: "Book Two", author: "Author Two", price: 19.99},
{id: "3", title: "Book Three", author: "Author Three", price: 39.99}
];

service "Books" on new grpc:Listener(port) {
remote function addBook(Book book) returns Book|error {
books.push(book);
return book;
}

remote function getBook(string id) returns Book|error {
Book[] book = books.filter(b => b.id == id);
if (book.length() == 0) {
return error grpc:NotFoundError(string `Cannot find the album for ID ${id}`);
}
return book[0];
}

remote function listBooks() returns stream<Book, error?>|error {
return books.toStream();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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,37 @@
import ballerina/grpc;


configurable int port = 9090;

type Book record {
string id;
string title;
string author;
float price;
};

Book[] books = [
{id: "1", title: "Book One", author: "Author One", price: 29.99},
{id: "2", title: "Book Two", author: "Author Two", price: 19.99},
{id: "3", title: "Book Three", author: "Author Three", price: 39.99}
];

service "Books" on new grpc:Listener(port) {
remote function addBook(Book book) returns Book | error {
books.push(book);
return book;
}
remote function getBook(string id) returns Book | error {
Book[] book = books.filter(b => b.id == id);
if (book.length() == 0) {
return error grpc:NotFoundError(string `Cannot find the album for ID ${id}`);
}
return book[0];
}



remote function listBooks( ) returns stream<Book, error?> | error {
return books.toStream();
}
}

0 comments on commit 972ae7d

Please sign in to comment.