- Write a RESTful API with Ballerina
- Write a gRPC service with Ballerina
- Write a GraphQL API with Ballerina
- Work with data using queries in Ballerina
- Build a data service in Ballerina
- Build a Change Data Capture (CDC) service in Ballerina
- Work with Large Language Models (LLMs) using natural expressions
- Deploy Ballerina on Kubernetes
- Manage data persistence with bal persist
- Create your first connector with Ballerina
This feature allows developers to customize how a Ballerina project is formatted via a local or remote configuration file. This allows for consistency in code style across projects in an organization and simplifies the process of enforcing formatting standards. This is introduced as an experimental feature in Ballerina 2201.9.0.
The formatting configuration file
Users can customize formatting configuration via a configuration file in the toml format. Users can provide an arbitrary name for the file.
The formatting configuration file can be specified in two ways:
-
Ballerina.toml: Define the path to the configuration file in theBallerina.tomlfile. The path can be absolute, relative to the package root, or a URL (HTTP/HTTPS). This path has to be specified under theformatheader in theBallerina.tomlfile asconfigPath.[package] org = "format-org" name = "format_project" version = "0.1.0" distribution = "2201.9.0" [format] configPath = "Format.toml" -
Format.toml: If no configuration path is specified in theBallerina.tomlfile, the formatter will look for a file namedFormat.tomlin the package's root directory.
If the configPath value is not provided in the Ballerina.toml file or the Format.toml file does not exist in the project root, default formatting options will be used.
If the format configPath value is an HTTP/HTTPS URL, the configuration file will be cached in the target directory. If the remote configuration file is modified, then make sure to delete the cached Format.toml file inside the target/format/ directory.
Formatting Options
Indent
indentSize
-
Specifies the number of spaces for each level of indentation.
-
Value: Integer (e.g.
2,4) -
Default:
4public function main() { boolean condition = true; if condition { io:println("Indented code"); } }
continuationIndentSize
-
Determines the indentation size for continuation lines.
-
Value: Integer (e.g.
4,8) -
Default:
8function longFunctionName(int param1, int param2, int param3, int param4, int param5) { }
Wrapping
maxLineLength
- Specifies the maximum line length before code is wrapped.
- Value: Integer (e.g.
80,120) - Default: No line wrapping if
maxLineLengthis not specified.
simpleBlocksInOneLine
-
Preserve single-line blocks in user code.
-
Value: Boolean
-
Default:
falseif x == 1 { return true; } while i < 5 { i += 1; }
simpleFunctionsInOneLine
-
Preserve single-line methods on one line.
-
Value: Boolean
-
Default:
falsefunction getName() returns string { return self.name; }
Braces
classBraceStyle:
-
Placement of the opening brace of the class definition.
-
Values:
NewLine,EndOfLine -
Default:
EndOfLine-
NewLineclass MyClass { } -
EndOfLineclass MyClass { }
-
functionBraceStyle
-
Placement of the opening brace of the function definition.
-
Values:
NewLine,EndOfLine -
Default:
EndOfLine-
NewLinefunction foo() { } -
EndOfLinefunction foo(){ }
-
Function Definition
parametersWrap
-
Formatting of the function parameters when the
maxLineLengthis exceeded. -
Values:
Wrap,NoWrap,ChopDown -
Default:
WrapifmaxLineLengthis specified, elseNoWrap-
Wrapfunction foo(int argumentNumberOne, int argumentTwo, int argumentThree, int argumentFour) { } -
NoWrapfunction foo(int argumentOne, int argumentTwo, int argumentThree) { } -
ChopDownfunction foo(int argumentNumberOne, int argumentTwo, int argumentThree, int argumentFour) { }
-
alignMultilineParameters
-
Align multiline parameters with the opening brace.
-
Values: Boolean
-
Default:
falsefunction foo(int argumentNumberOne, int argumentTwo, int argumentThree, int argumentFour) { }
newLineAfterLeftParen
-
Newline after the left parenthesis of the function declaration.
-
Values: Boolean
-
Default:
falsefunction foo( int argumentNumberOne, int argumentTwo, int argumentThree, int argumentFour) { }
rightParenOnNewLine
-
Right parenthesis of the function declaration on a newline.
-
Values: Boolean
-
Default:
falsefunction foo(int argumentNumberOne, int argumentTwo, int argumentThree, int argumentFour ) { }
Function Call
argumentsWrap
-
Values:
Wrap,NoWrap,ChopDown -
Default:
WrapifmaxLineLengthspecified, elseNoWrap-
WrapmyMethod(param1, param2, param3, param4, param5, param6); -
NoWrapmyMethod(param1, param2, param3, param4, param5, param6); -
ChopDownmyMethod(param1, param2, param3, param4, param5, param6);
-
alignMultilineArguments
-
Align multiline arguments with the opening brace.
-
Values: Boolean
-
Default:
falsemyMethod(param1, param2, param3, param4 param5, param6);
newLineAfterLeftParen
-
Newline after the left parenthesis of the function declaration.
-
Values: Boolean
-
Default:
falsemyMethod( param1, param2, param3, param4, param5, param6);
rightParenOnNewLine
-
Right parenthesis of the function declaration on a newline.
-
Values: Boolean
-
Default:
falsemyMethod(param1, param2, param3, param4, param5, param6 );
If Statement
elseOnNewLine
-
Specify the start of the
elseandelse ifclause on a newline following the}of the previousiforelse ifclause. -
Values: Boolean
-
Default:
falseboolean condition = true; if condition { // if block } else { // else block }
Spacing
afterTypeCast
-
Add a space after the
type-cast-param. -
Values: Boolean
-
Default:
trueint x = <int> someValue;
aroundRecordBraces
-
Space after the opening brace and before the closing brace of a mapping constructor.
-
Values: Boolean
-
Default:
falseStudent x = { name: “Alex”, age: 15 };
alignConsecutiveDefinitions
-
Align the equals signs in consecutive definitions.
-
Values: Boolean
-
Default:
falseconst A = "a"; const AB = "ab"; const ABC = "ab";
Import
groupImport
- Group imports in the order of,
- Imports from the same package.
- Imports from the
ballerina/andballerinax/organizations. - Imports from third-party organizations.
- Values: Boolean
- Default:
true
sortImports
- Sort imports based on alphabetical order.
- Values: Boolean
- Default:
true
Query
alignMultiLineQueries
-
Align multiline queries with the equals on the first line.
-
Values: Boolean
-
Default:
false-
Default behavior
int[] numsReversed = from int i in nums order by i descending select i; -
When
alignMultiLineQueries = trueint[] numsReversed = from int j in nums order by j descending select ;
-
Example Configuration
[indent] indentSize = 4 continuationIndentSize = 8 [wrapping] maxLineLength = 120 simpleBlocksInOneLine = true simpleFunctionsInOneLine = true [braces] classBraceStyle = "NewLine" functionBraceStyle = "NewLine" [functionDefinition] parametersWrap = "ChopDown" alignMultilineParameters = true newLineAfterLeftParen = false rightParenOnNewLine = false [functionCall] argumentsWrap = "Wrap" alignMultilineArguments = false newLineAfterLeftParen = false rightParenOnNewLine = false [ifStatement] elseOnNewLine = false [spacing] afterTypeCast = true aroundRecordBraces = true [import] groupImports = true sortImports = true [query] alignMultiLineQueries = false