Skip to content

Commit

Permalink
Address sonar cloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jun 17, 2024
1 parent cd3d602 commit c57777a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -43,7 +42,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;

/**
* Abstract implementation of code action to add a parameter to the resource signature.
Expand Down Expand Up @@ -71,25 +71,19 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
cursorPosition.get())) {
return Optional.empty();
}
CodeActionArgument arg = CodeActionArgument.from(NODE_LOCATION_KEY, node.lineRange());
CodeActionInfo info = CodeActionInfo.from(String.format("Add %s parameter", paramKind()), List.of(arg));
return Optional.of(info);
return getCodeActionInfoWithLocation(node, String.format("Add %s parameter", paramKind()));
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument arg : context.arguments()) {
if (NODE_LOCATION_KEY.equals(arg.key())) {
lineRange = arg.valueAs(LineRange.class);
}
}
if (lineRange == null) {
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange);
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange.get());

if (!node.kind().equals(SyntaxKind.FUNCTION_SIGNATURE)) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -39,7 +38,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;

/**
* CodeAction to add response cache configuration.
Expand All @@ -58,24 +58,19 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Add response cache configuration", List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Add response cache configuration");
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument arg : context.arguments()) {
if (NODE_LOCATION_KEY.equals(arg.key())) {
lineRange = arg.valueAs(LineRange.class);
}
}
if (lineRange == null) {
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange);
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange.get());

String cacheConfig = "@http:Cache ";
int start = node.position();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -39,7 +38,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;

/**
* CodeAction to add response content-type.
Expand All @@ -58,24 +58,19 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Add response content-type", List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Add response content-type");
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument arg : context.arguments()) {
if (NODE_LOCATION_KEY.equals(arg.key())) {
lineRange = arg.valueAs(LineRange.class);
}
}
if (lineRange == null) {
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange);
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange.get());

String mediaTypedPayload = "@http:Payload{mediaType: \"\"} ";
int start = node.position();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.ballerina.compiler.syntax.tree.RestParameterNode;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -44,7 +43,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;

/**
* Abstract implementation of code action to change a resource header param's type.
Expand All @@ -66,38 +66,27 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {

DiagnosticProperty<?> diagnosticProperty = properties.get(0);
if (!(diagnosticProperty instanceof BSymbolicProperty) ||
!(diagnosticProperty.value() instanceof ParameterSymbol)) {
!(diagnosticProperty.value() instanceof ParameterSymbol parameterSymbol)) {
return Optional.empty();
}

ParameterSymbol parameterSymbol = (ParameterSymbol) diagnosticProperty.value();
Optional<NonTerminalNode> nonTerminalNode = parameterSymbol.getLocation()
.flatMap(location -> Optional.ofNullable(CodeActionUtil.findNode(syntaxTree, parameterSymbol)));
if (nonTerminalNode.isEmpty()) {
return Optional.empty();
}
return nonTerminalNode.flatMap(terminalNode -> getCodeActionInfoWithLocation(terminalNode,
String.format("Change header param to '%s'", headerParamType())));

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY,
nonTerminalNode.get().location().lineRange());
return Optional.of(CodeActionInfo.from(String.format("Change header param to '%s'", headerParamType()),
List.of(locationArg)));
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
lineRange = argument.valueAs(LineRange.class);
}
}
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange == null) {
if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange);
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange.get());

TextRange typeNodeTextRange;
switch (node.kind()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -37,7 +36,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;

/**
* Codeaction to change the return type to <pre>error?</pre> if the resource function has <pre>http:Caller</pre> as a
Expand All @@ -58,28 +58,22 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Change return type to 'error?'", List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Change return type to 'error?'");
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
lineRange = argument.valueAs(LineRange.class);
}
}
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange == null) {
if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
TextDocument textDocument = syntaxTree.textDocument();

int start = textDocument.textPositionFrom(lineRange.startLine());
int end = textDocument.textPositionFrom(lineRange.endLine());
int start = textDocument.textPositionFrom(lineRange.get().startLine());
int end = textDocument.textPositionFrom(lineRange.get().endLine());

List<TextEdit> textEdits = new ArrayList<>();
textEdits.add(TextEdit.from(TextRange.from(start, end - start), "error?"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import io.ballerina.tools.text.TextDocument;
import io.ballerina.tools.text.TextRange;

import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;

/**
* Utilities for code actions.
*/
Expand Down Expand Up @@ -90,4 +98,31 @@ public static boolean isWithinRange(LineRange lineRange, LinePosition pos) {
pos.line() == sLine && pos.offset() >= sCol
));
}

/**
* Get code action info with location.
*
* @param node Node
* @param title Title
* @return Code action info with location
*/
public static Optional<CodeActionInfo> getCodeActionInfoWithLocation(NonTerminalNode node, String title) {
CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from(title, List.of(locationArg)));
}

/**
* Get line range from location key.
*
* @param context Code action execution context
* @return Line range
*/
public static Optional<LineRange> getLineRangeFromLocationKey(CodeActionExecutionContext context) {
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
return Optional.of(argument.valueAs(LineRange.class));
}
}
return Optional.empty();

Check warning on line 126 in compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/CodeActionUtil.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/CodeActionUtil.java#L125-L126

Added lines #L125 - L126 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.compiler.syntax.tree.TypeDescriptorNode;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -55,8 +54,9 @@

import static io.ballerina.stdlib.http.compiler.HttpServiceContractResourceValidator.constructResourcePathName;
import static io.ballerina.stdlib.http.compiler.HttpServiceValidator.getServiceContractTypeDesc;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;
import static io.ballerina.stdlib.http.compiler.codeaction.Constants.LS;
import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;

/**
* Represents a code action to implement all the resource methods from the service contract type.
Expand All @@ -77,27 +77,20 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();

Check warning on line 77 in compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java#L77

Added line #L77 was not covered by tests
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Implement all the resource methods from service contract",
List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Implement all the resource methods from service contract");

Check warning on line 80 in compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java#L80

Added line #L80 was not covered by tests
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
lineRange = argument.valueAs(LineRange.class);
}
}
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

Check warning on line 85 in compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java#L85

Added line #L85 was not covered by tests

if (lineRange == null) {
if (lineRange.isEmpty()) {
return Collections.emptyList();

Check warning on line 88 in compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java#L88

Added line #L88 was not covered by tests
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
SemanticModel semanticModel = context.currentSemanticModel();
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange);
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange.get());

Check warning on line 93 in compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java#L91-L93

Added lines #L91 - L93 were not covered by tests
if (!node.kind().equals(SyntaxKind.SERVICE_DECLARATION)) {
return Collections.emptyList();

Check warning on line 95 in compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codeaction/ImplementServiceContract.java#L95

Added line #L95 was not covered by tests
}
Expand Down
Loading

0 comments on commit c57777a

Please sign in to comment.