Code actions

The Ballerina VS Code extension provides code actions to simplify the developing process of your code.

Code actions is a feature in the VS Code Editor that enables quick fixes and code refactoring suggestions.

The Ballerina VS Code extension enables the following code actions.

Quick fixes

Quick fixes are suggested based on the diagnostics displayed at a given cursor position. The Ballerina VS Code extension provides a wide range of quick fixes for such diagnostics.

Variable assignment

The following code actions are available as quick fixes when the Variable assignment required diagnostic is identified in your code.

Code actionDescription
Create variableCreate a variable for an expression.
Create variable and type guardCreate a type guard to handle the error gracefully.
Create variable and check errorAdd a check expression.
Ignore return valueIgnore the return value of a function call or a method call with _.

Type inference

The following quick fixes are available for type inference.

Code actionDescription
Create variable with typeCreate a variable for a remote method expression in which the Cannot infer the 'typedesc' argument for parameter diagnostic is present.

Missing check expressions

The following quick fixes are available for missing check expressions.

Code actionDescription
Add 'check' errorAdd the check keyword when the selected expression's type is a subtype of error, or a union with an error member while the expected type doesn't contain an error.

Check expressions

The following quick fixes are available for check expressions.

Code actionDescription
Surround with do/on-failSurround a check expression with a do/on-fail clause.
Change return type to 'error?'Change the return type of a function/method to error?.

Imports

The following quick fixes are available for imports.

Code actionDescription
Import moduleAdd the import statement for a module that has a reference without an import statement.
Pull unresolved modulePull one or more Ballerina packages (which are imported in the source and are unavailable locally) from Ballerina Central.
Remove importRemove an unused or re-declared module import.
Remove all unused importsRemove all unused imports except when there is a re-declared import statement.

Documentation

The following quick fixes are available for documentation.

Code actionDescription
Document thisAdd the documentation to the closest node that can be the documented clause.
Update documentationUpdate the existing documentation when parameters are missing or not documented. This depends on the warning diagnostics issued by the compiler.

Incompatible types

The following quick fixes are available for incompatible types.

Code actionDescription
Change variable typeChange the type of variable.
Add type castAdd a type cast for the incompatible type.
Change return typeChange the incompatible return type of a function/method.
Change parameter typeAdd a conditional default value for the incompatible type.
Add conditional default valueIgnore the return value of a function call or a method call with _.
Convert to 'xml'/'string' templateConvert a raw template into a subtype (xml or string).
Add type cast for numeric expressionsAdd a type cast for an unsupported numeric expression.

The Change variable type, Change parameter type, Add type cast, and Change return type quick fixes are demonstrated below.

The Add conditional default value and Convert to 'xml'/'string' template quick fixes are demonstrated below.

Functions

The following quick fixes are available for creating functions.

Code actionDescription
Create functionCreate a function when there's an undefined function invoked by a function call expression.
Implement methodImplement an unimplemented method declared by an included object type.
Implement allImplement all the methods declared by an included object type.
Add explicit return statementAdd an explicit return statement to a function.
Convert to public functionAdd a public access modifier to a function definition.
Add isolated qualifierAdd an isolated qualifier to a function.

The Create function, Implement method, and Implement all quick fixes are demonstrated below.

The Add explicit return statement, Convert to public function, and Add isolated qualifier quick fixes are demonstrated below.

Records

The following quick fixes are available for records.

Code actionDescription
Create recordCreate a record for an unknown type clause.
Fill record fieldsAdd missing record fields to a mapping constructor expression.

Annotations

The following quick fixes are available for annotations.

Code actionDescription
Convert to constantConvert an annotation declaration to a constant annotation declaration clause.

Other quick fixes

Code actionDescription
Convert to readonly cloneConvert an e expression to e.cloneReadOnly().
Ignore unused variableIgnore an unused variable.
Make construct pubicMake a construct public where the attempt to expose non-public symbol warning is present.
Convert module variable to listener declarationConvert a module var declaration statement to a listener declaration.
Remove unreachable codeRemove unreachable statements.

The Convert to readonly clone, Ignore unused variable, and Remove unreachable code quick fixes are demonstrated below.

The Make construct public quick fix is demonstrated below.

The Convert module variable to listener declaration quick fix is demonstrated below.

Refactoring actions

Refactoring actions are suggested when the existing code can be rearranged or restructured to improve the quality and maintainability of your code while ensuring that the overall behavior of the code remains unaffected.

Extract to code actions

The following refactoring actions are available for extracting expressions.

Code actionDescription
Extract to constantExtract a constant expression to a constant.
Extract to functionExtract an expression or a statement to a function.
Extract to local variableExtract an expression to a local variable.
Extract typeExtract anonymous records to record types.

Rewrite code actions

The following refactoring actions are available as rewrite code actions.

Code actionDescription
Map with a query expressionWhen both the right-hand side (RHS) and left-hand side (LHS) types are array types, convert the RHS to a query expression. You can modify the query expression with additional clauses and mappings later.

Source actions

Source actions are used to generate common code structures and recurring elements.

Generate getters and setters

The following source actions are available for generating getters and setters.

Code actionDescription
Create a getterGenerate a getter for class-level variables.
Create a setterGenerate a setter for class-level variables.
Create getter and setterGenerate both getter and setter for class-level variables.

Variables

The following source actions are available for variables.

Code actionDescription
Type guard variableType guard a variable if the variable type is a union type.

Imports

The following source actions are available for imports.

Code actionDescription
Optimize importsOptimize the import statements to remove unused imports and arrange imports in alphabetical order.

Documentation

The following source actions are available for documentation.

Code actionDescription
Document allDocument all the top-level constructs.