Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ballerina Formatting CLI tool #16003

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ All notable changes to this project will be documented in this file. When sendin

### Tooling

-
- [Add Formatting CLI tool](https://github.com/ballerina-platform/ballerina-lang/pull/15664)
-
-
-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
NAME
Ballerina Format - Formats the Ballerina source files according to
the default Ballerina style guide.
Style guide: https://github.com/ballerina-platform/ballerina-lang/tree/master/docs/style_guide

SYNOPSIS
ballerina format [ballerinaFile | moduleName] [-d | --dry-run]

DESCRIPTION
Formats Ballerina source files according to the Ballerina style guide.

Formatting can be performed on a Ballerina project, on a Ballerina Module, or
on a Ballerina file.

The formatted content will be written to the original files. By using the dry run
option, you will be able to check which files will be formatted after the execution.

If Ballerina sources contain syntax errors, they will be notified and
formatting will not be proceed until they are fixed.

SUB COMMANDS
ballerina format
Formats all the Ballerina source files that are available in the given Ballerina project.
This command should be executed from the root of the Ballerina project.

ballerina format <moduleName>
Formats all the Ballerina source files that are available in the given module. This command
should be executed from the root of the Ballerina project.

ballerina format <ballerinaFile>
Formats the given Ballerina file to match the default Ballerina source formatting style as per the style guide.
The Formatted source will replace the content of the given file.

OPTIONS
-d
--dry-run
By providing this option, you can dry run the formatter and see which files will
be formatted after the execution.

-h
--help
Prints the help guide for the Ballerina format tool.

EXAMPLES
Formats a Ballerina project.
$ ballerina format

Formats a Ballerina module.
$ ballerina format module1

Formats a Ballerina file.
$ ballerina format hello.bal

Performs a dry run to see which files will be formatted.
$ ballerina format -d
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ DESCRIPTION
ballerina [COMMAND] [options]

COMMAND is one of the available commands listed below:
run Run Ballerina program
build Compile Ballerina program
install Install modules to the home repository
pull Download modules from Ballerina Central
push Upload modules to Ballerina Central
init Initialize Ballerina project
search Search for modules within Ballerina Central
list List dependencies of modules
doc Generate Ballerina API documentation
run Run Ballerina program.
build Compile Ballerina program.
install Install modules to the home repository.
pull Download modules from Ballerina Central.
push Upload modules to Ballerina Central.
init Initialize Ballerina project.
search Search for modules within Ballerina Central.
list List dependencies of modules.
doc Generate Ballerina API documentation.
grpc Generate connector/service using protobuf
definition
definition.
openapi Generate client/service using openapi definition
or export openapi file for a Ballerina service
test Test Ballerina program
version Print Ballerina version
encrypt Encrypt sensitive data
or export openapi file for a Ballerina service.
test Tests the Ballerina program.
version Prints the Ballerina version.
encrypt Encrypts sensitive data.
format Formats Ballerina source code.

OPTIONS
Ballerina help takes no command line options
Ballerina help does not take command line options.

EXAMPLES
Look for help on how to use the ballerina command
Expand Down
1 change: 1 addition & 0 deletions distribution/zip/ballerina-tools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
dist project(':openapi-ballerina:openapi-to-ballerina-generator')
dist project(':ballerina-backend-jvm')
dist project(':language-server:language-server-compiler')
dist project(':ballerina-formatter')
dist project(':ballerina-packerina')
dist project(':protobuf-ballerina')
dist project(':openapi-ballerina:openapi-to-ballerina-generator')
Expand Down
4 changes: 4 additions & 0 deletions distribution/zip/ballerina-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@
<groupId>org.ballerinalang</groupId>
<artifactId>openapi-to-ballerina-generator</artifactId>
</dependency>
<dependency>
<groupId>org.ballerinalang</groupId>
<artifactId>ballerina-formatter</artifactId>
</dependency>
<dependency>
<groupId>org.ballerinalang</groupId>
<artifactId>ballerina-client-generator</artifactId>
Expand Down
1 change: 1 addition & 0 deletions distribution/zip/ballerina-tools/src/assembly/bin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
in ballerina OpenAPI CLI tools
-->
<include>org.ballerinalang:language-server-compiler:jar</include>
<include>org.ballerinalang:ballerina-formatter:jar</include>
<include>org.ballerinalang:ballerina-packerina:jar</include>
<include>org.ballerinalang:protobuf-ballerina:jar</include>
<include>org.ballerinalang:ballerina-client-generator:jar</include>
Expand Down
2 changes: 1 addition & 1 deletion distribution/zip/jballerina-tools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ dependencies {
dist project(':ballerina-runtime')

dist project(':testerina:testerina-core')

dist project(':ballerina-formatter')
dist project(':openapi-ballerina:openapi-to-ballerina-generator')
dist project(':ballerina-backend-jvm')
dist project(':language-server:language-server-compiler')
Expand Down
4 changes: 4 additions & 0 deletions distribution/zip/jballerina-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@
<groupId>org.ballerinalang</groupId>
<artifactId>ballerina-client-generator</artifactId>
</dependency>
<dependency>
<groupId>org.ballerinalang</groupId>
<artifactId>ballerina-formatter</artifactId>
</dependency>
<dependency>
<groupId>org.ballerinalang</groupId>
<artifactId>protobuf-ballerina</artifactId>
Expand Down
1 change: 1 addition & 0 deletions distribution/zip/jballerina-tools/src/assembly/bin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
in ballerina OpenAPI CLI tools
-->
<include>org.ballerinalang:language-server-compiler:jar</include>
<include>org.ballerinalang:ballerina-formatter:jar</include>
<include>org.ballerinalang:ballerina-packerina:jar</include>
<include>org.ballerinalang:protobuf-ballerina:jar</include>
<include>org.ballerinalang:ballerina-client-generator:jar</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ballerinalang.langserver.formatting;
package org.ballerinalang.langserver.compiler.format;

/**
* Formatting constants for keywords used in the formatting util.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ballerinalang.langserver.formatting;
package org.ballerinalang.langserver.compiler.format;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.ballerinalang.langserver.compiler.sourcegen.FormattingSourceGen;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -3025,8 +3026,8 @@ public void formatPanicNode(JsonObject node) {
}

// Handle the expression formatting.
if (node.has("expressions")) {
node.getAsJsonObject("expressions").add(FormattingConstants.FORMATTING_CONFIG,
if (node.has(FormattingConstants.EXPRESSIONS)) {
node.getAsJsonObject(FormattingConstants.EXPRESSIONS).add(FormattingConstants.FORMATTING_CONFIG,
this.getFormattingConfig(0, 1, this.getWhiteSpaceCount(indentation),
false, this.getWhiteSpaceCount(indentationOfParent),
formatConfig.get(FormattingConstants.USE_PARENT_INDENTATION).getAsBoolean()));
Expand Down Expand Up @@ -3118,20 +3119,10 @@ public void formatRecordLiteralExprNode(JsonObject node) {
if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);
JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);
String indentation = this.getIndentation(formatConfig, false);
String indentWithParentIndentation = this.getParentIndentation(formatConfig);
String parentKind = node.getAsJsonObject(FormattingConstants.PARENT).get(FormattingConstants.KIND)
.getAsString();
boolean isTable = parentKind.equals("Table");
boolean isExpression = parentKind.equals("Endpoint") || parentKind.equals("AnnotationAttachment")
|| parentKind.equals("Service") || parentKind.equals("Variable") || parentKind.equals("Invocation")
|| parentKind.equals("ErrorConstructor") || parentKind.equals("RecordLiteralExpr")
|| parentKind.equals("RecordLiteralKeyValue") || parentKind.equals("Return");

if (isExpression) {
node.getAsJsonObject(FormattingConstants.POSITION).addProperty(FormattingConstants.START_COLUMN,
indentation);
}

// Preserve line separation that already available.
this.preserveHeight(ws, indentWithParentIndentation);
Expand All @@ -3149,12 +3140,12 @@ public void formatRecordLiteralExprNode(JsonObject node) {
// Update whitespace for opening brace.
if (text.equals(Tokens.OPENING_BRACE)
&& this.noHeightAvailable(currentWS.get(FormattingConstants.WS).getAsString())) {
if (isExpression) {
currentWS.addProperty(FormattingConstants.WS,
this.getWhiteSpaces(formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt()));
} else if (isTable) {
if (isTable) {
currentWS.addProperty(FormattingConstants.WS, this.getNewLines(formatConfig
.get(FormattingConstants.NEW_LINE_COUNT).getAsInt()) + indentWithParentIndentation);
} else {
currentWS.addProperty(FormattingConstants.WS,
this.getWhiteSpaces(formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt()));
}
}

Expand Down Expand Up @@ -4334,8 +4325,8 @@ public void formatStringTemplateLiteralNode(JsonObject node) {
}

// Handle expressions format
if (node.has("expressions")) {
JsonArray expressions = node.getAsJsonArray("expressions");
if (node.has(FormattingConstants.EXPRESSIONS)) {
JsonArray expressions = node.getAsJsonArray(FormattingConstants.EXPRESSIONS);
// Every three expression is related to
// Start of the template expression
// expression of the template expression
Expand Down Expand Up @@ -5221,8 +5212,51 @@ public void formatTypedescExpressionNode(JsonObject node) {
* @param node {JsonObject} node as json object
*/
public void formatTypeInitExprNode(JsonObject node) {
// TODO: fix formatting for type init expression
this.skipFormatting(node, true);
if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);
JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);

String indentation = this.getIndentation(formatConfig, false);
String indentationOfParent = this.getParentIndentation(formatConfig);
boolean useParentIndentation = formatConfig.get(FormattingConstants.USE_PARENT_INDENTATION).getAsBoolean();

// Preserve available new lines in node.
this.preserveHeight(ws, useParentIndentation ? indentationOfParent : indentation);

// Iterate and update whitespaces for node.
for (JsonElement wsItem : ws) {
JsonObject currentWS = wsItem.getAsJsonObject();
if (this.noHeightAvailable(currentWS.get(FormattingConstants.WS).getAsString())) {
String text = currentWS.get(FormattingConstants.TEXT).getAsString();
if (text.equals(Tokens.NEW)) {
currentWS.addProperty(FormattingConstants.WS,
this.getWhiteSpaces(formatConfig.get(FormattingConstants.SPACE_COUNT).getAsInt()));
} else if (text.equals(Tokens.OPENING_PARENTHESES)) {
if (node.has(FormattingConstants.TYPE)) {
currentWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
} else {
currentWS.addProperty(FormattingConstants.WS, FormattingConstants.SINGLE_SPACE);
}
} else if (text.equals(Tokens.COMMA) || text.equals(Tokens.CLOSING_PARENTHESES)) {
currentWS.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
}
}
}

// Iterate and format expressions.
if (node.has(FormattingConstants.EXPRESSIONS)) {
JsonArray expressions = node.getAsJsonArray(FormattingConstants.EXPRESSIONS);
iterateAndFormatMembers(indentation.isEmpty() ? indentationOfParent : indentation,
expressions);
}

// Handle type name formatting.
if (node.has(FormattingConstants.TYPE)) {
node.getAsJsonObject(FormattingConstants.TYPE).add(FormattingConstants.FORMATTING_CONFIG,
this.getFormattingConfig(0, 1, 0, false,
this.getWhiteSpaceCount(indentationOfParent), true));
}
}
}

/**
Expand Down Expand Up @@ -6245,8 +6279,8 @@ private void modifyReturnTypeAnnotations(JsonObject node, String indentation) {
}

private void modifyExpressions(JsonObject node, String indentWithParentIndentation) {
if (node.has("expressions")) {
JsonArray expressions = node.getAsJsonArray("expressions");
if (node.has(FormattingConstants.EXPRESSIONS)) {
JsonArray expressions = node.getAsJsonArray(FormattingConstants.EXPRESSIONS);
iterateAndFormatMembers(indentWithParentIndentation, expressions);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ballerinalang.langserver.formatting;
package org.ballerinalang.langserver.compiler.format;

import com.google.gson.JsonObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ballerinalang.langserver.formatting;
package org.ballerinalang.langserver.compiler.format;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ballerinalang.langserver.formatting;
package org.ballerinalang.langserver.compiler.format;

/**
* Constants values to be used when comparing tokens in formatting.
Expand Down Expand Up @@ -110,5 +110,6 @@ public class Tokens {
public static final String TRAP = "trap";
public static final String TYPEOF = "typeof";
public static final String UNTAINT = "untaint";
public static final String NEW = "new";
public static final String FLUSH = "flush";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ballerinalang.langserver.formatting;
package org.ballerinalang.langserver.compiler.sourcegen;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.ballerinalang.langserver.compiler.format.FormattingConstants;

import java.util.ArrayList;
import java.util.Comparator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
import org.ballerinalang.langserver.compiler.LSServiceOperationContext;
import org.ballerinalang.langserver.compiler.common.LSCustomErrorStrategy;
import org.ballerinalang.langserver.compiler.common.LSDocument;
import org.ballerinalang.langserver.compiler.format.FormattingVisitorEntry;
import org.ballerinalang.langserver.compiler.format.TextDocumentFormatUtil;
import org.ballerinalang.langserver.compiler.sourcegen.FormattingSourceGen;
import org.ballerinalang.langserver.compiler.workspace.WorkspaceDocumentException;
import org.ballerinalang.langserver.compiler.workspace.WorkspaceDocumentManager;
import org.ballerinalang.langserver.completions.CompletionKeys;
import org.ballerinalang.langserver.completions.SymbolInfo;
import org.ballerinalang.langserver.completions.util.CompletionUtil;
import org.ballerinalang.langserver.definition.LSReferencesException;
import org.ballerinalang.langserver.diagnostic.DiagnosticsHelper;
import org.ballerinalang.langserver.formatting.FormattingSourceGen;
import org.ballerinalang.langserver.formatting.FormattingVisitorEntry;
import org.ballerinalang.langserver.implementation.GotoImplementationCustomErrorStrategy;
import org.ballerinalang.langserver.implementation.GotoImplementationUtil;
import org.ballerinalang.langserver.index.LSIndexImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
import org.ballerinalang.langserver.compiler.common.modal.SymbolMetaInfo;
import org.ballerinalang.langserver.compiler.format.JSONGenerationException;
import org.ballerinalang.langserver.compiler.format.TextDocumentFormatUtil;
import org.ballerinalang.langserver.compiler.sourcegen.FormattingSourceGen;
import org.ballerinalang.langserver.compiler.workspace.WorkspaceDocumentException;
import org.ballerinalang.langserver.compiler.workspace.WorkspaceDocumentManager;
import org.ballerinalang.langserver.extensions.OASGenerationException;
import org.ballerinalang.langserver.formatting.FormattingSourceGen;
import org.ballerinalang.model.tree.ServiceNode;
import org.ballerinalang.model.tree.TopLevelNode;
import org.ballerinalang.openapi.CodeGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.ballerinalang.langserver.compiler.common.modal.BallerinaFile;
import org.ballerinalang.langserver.compiler.format.JSONGenerationException;
import org.ballerinalang.langserver.compiler.format.TextDocumentFormatUtil;
import org.ballerinalang.langserver.formatting.FormattingSourceGen;
import org.ballerinalang.langserver.compiler.sourcegen.FormattingSourceGen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit;
Expand Down
Loading