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

Add hover support for resource paths #41773

Merged
merged 10 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ContextConstants {
public static final String TYPE_DEF = "TYPE_DEF";
public static final String DESCRIPTION = "Description";
public static final String PARAM_TITLE = "Parameters";
public static final String PATH_PARAM_TITLE = "Path Parameters";
public static final String QUERY_PARAM_TITLE = "Query Parameters";
public static final String RETURN_TITLE = "Return";
public static final String FIELD_TITLE = "Fields";
public static final String METHOD_TITLE = "Methods";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
import io.ballerina.compiler.api.symbols.ObjectTypeSymbol;
import io.ballerina.compiler.api.symbols.ParameterKind;
import io.ballerina.compiler.api.symbols.ParameterSymbol;
import io.ballerina.compiler.api.symbols.PathParameterSymbol;
import io.ballerina.compiler.api.symbols.RecordTypeSymbol;
import io.ballerina.compiler.api.symbols.ResourceMethodSymbol;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.api.symbols.SymbolKind;
import io.ballerina.compiler.api.symbols.TypeDefinitionSymbol;
import io.ballerina.compiler.api.symbols.TypeDescKind;
import io.ballerina.compiler.api.symbols.TypeReferenceTypeSymbol;
import io.ballerina.compiler.api.symbols.TypeSymbol;
import io.ballerina.compiler.api.symbols.UnionTypeSymbol;
import io.ballerina.compiler.api.symbols.VariableSymbol;
import io.ballerina.compiler.api.symbols.resourcepath.PathRestParam;
import io.ballerina.compiler.api.symbols.resourcepath.PathSegmentList;
import io.ballerina.compiler.api.symbols.resourcepath.ResourcePath;
import io.ballerina.compiler.syntax.tree.DefaultableParameterNode;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
Expand Down Expand Up @@ -159,11 +164,42 @@
}
List<String> hoverContent = new ArrayList<>();
documentation.get().description().ifPresent(hoverContent::add);

List<PathParameterSymbol> parameterSymbols = new ArrayList<>();
boolean isResourceMethod = functionSymbol.kind() == SymbolKind.RESOURCE_METHOD;

if (isResourceMethod) {
ResourcePath resourcePath = ((ResourceMethodSymbol) functionSymbol).resourcePath();
if (resourcePath.kind() == ResourcePath.Kind.PATH_SEGMENT_LIST) {
PathSegmentList pathSegmentList = (PathSegmentList) resourcePath;
List<PathParameterSymbol> pathParameterSymbols = pathSegmentList.pathParameters();
parameterSymbols.addAll(pathParameterSymbols);
if (pathSegmentList.pathRestParameter().isPresent()) {
parameterSymbols.add(pathSegmentList.pathRestParameter().get());
}
} else if (resourcePath.kind() == ResourcePath.Kind.PATH_REST_PARAM) {
mindula marked this conversation as resolved.
Show resolved Hide resolved
parameterSymbols.add(((PathRestParam) resourcePath).parameter());

Check warning on line 180 in language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/hover/HoverObjectResolver.java

View check run for this annotation

Codecov / codecov/patch

language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/hover/HoverObjectResolver.java#L180

Added line #L180 was not covered by tests
mindula marked this conversation as resolved.
Show resolved Hide resolved
}
}
Map<String, String> paramsMap = documentation.get().parameterMap();
List<String> params = new ArrayList<>();

if (!parameterSymbols.isEmpty()) {
params.add(MarkupUtils.header(3, ContextConstants.PATH_PARAM_TITLE) + CommonUtil.MD_LINE_SEPARATOR);
params.addAll(parameterSymbols.stream().map(param -> {
if (param.getName().isEmpty()) {
return MarkupUtils.quotedString(NameUtil
.getModifiedTypeName(context, param.typeDescriptor()));

Check warning on line 191 in language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/hover/HoverObjectResolver.java

View check run for this annotation

Codecov / codecov/patch

language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/hover/HoverObjectResolver.java#L190-L191

Added lines #L190 - L191 were not covered by tests
}
String paramName = param.getName().get();
String desc = paramsMap.getOrDefault(paramName, "");
return MarkupUtils.quotedString(NameUtil.getModifiedTypeName(context, param.typeDescriptor())) + " "
+ MarkupUtils.italicString(MarkupUtils.boldString(paramName)) + " : " + desc;
}).collect(Collectors.toList()));
}

if (!paramsMap.isEmpty()) {
List<String> params = new ArrayList<>();
params.add(MarkupUtils.header(3, ContextConstants.PARAM_TITLE) + CommonUtil.MD_LINE_SEPARATOR);
params.add(MarkupUtils.header(3, isResourceMethod ? ContextConstants.QUERY_PARAM_TITLE
: ContextConstants.PARAM_TITLE) + CommonUtil.MD_LINE_SEPARATOR);
params.addAll(functionSymbol.typeDescriptor().params().get().stream().map(param -> {
if (param.getName().isEmpty()) {
return MarkupUtils.quotedString(NameUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.ballerina.compiler.syntax.tree.ExpressionNode;
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.projects.Document;
import io.ballerina.projects.ModuleId;
Expand Down Expand Up @@ -70,7 +71,7 @@ public static Hover getHover(HoverContext context) {
LinePosition linePosition = LinePosition.from(cursorPosition.getLine(), cursorPosition.getCharacter());
// Check for the cancellation before the time-consuming operation
context.checkCancelled();
Optional<? extends Symbol> symbolAtCursor = semanticModel.get().symbol(srcFile.get(), linePosition);
Optional<Symbol> symbolAtCursor = getSymbolAtCursor(context, semanticModel.get(), srcFile.get(), linePosition);
// Check for the cancellation after the time-consuming operation
context.checkCancelled();

Expand Down Expand Up @@ -117,6 +118,18 @@ public static Hover getHover(HoverContext context) {
return hoverObj;
}

private static Optional<Symbol> getSymbolAtCursor(HoverContext context, SemanticModel semanticModel,
Document srcFile, LinePosition linePosition) {
NonTerminalNode cursor = context.getNodeAtCursor();
if (cursor.kind() == SyntaxKind.LIST || cursor.kind() == SyntaxKind.PARENTHESIZED_ARG_LIST
mindula marked this conversation as resolved.
Show resolved Hide resolved
|| cursor.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE
&& cursor.parent().kind() == SyntaxKind.CLIENT_RESOURCE_ACCESS_ACTION) {
return semanticModel.symbol(cursor.parent());
} else {
return semanticModel.symbol(srcFile, linePosition);
}
mindula marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* returns the default hover object.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"position": {
"line": 15,
"character": 34
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description.\n \n \n--- \n \n### Path Parameters \n \n`string` ***param1*** : parameter description \n`string` ***param2*** : parameter description \n### Query Parameters \n \n`string` ***param3*** : parameter description \n`int[]...` ***param4*** : parameter description \n \n--- \n \n### Return \n`string` : return value description"
}
},
"id": {
"left": "324"
},
"jsonrpc": "2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"position": {
"line": 15,
"character": 39
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description.\n \n \n--- \n \n### Path Parameters \n \n`string` ***param1*** : parameter description \n`string` ***param2*** : parameter description \n### Query Parameters \n \n`string` ***param3*** : parameter description \n`int[]...` ***param4*** : parameter description \n \n--- \n \n### Return \n`string` : return value description"
}
},
"id": {
"left": "324"
},
"jsonrpc": "2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"position": {
"line": 15,
"character": 55
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description.\n \n \n--- \n \n### Path Parameters \n \n`string` ***param1*** : parameter description \n`string` ***param2*** : parameter description \n### Query Parameters \n \n`string` ***param3*** : parameter description \n`int[]...` ***param4*** : parameter description \n \n--- \n \n### Return \n`string` : return value description"
}
},
"id": {
"left": "324"
},
"jsonrpc": "2.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description2 for testResource1\n \n \n--- \n \n### Parameters \n \n`int` ***param1*** : a Parameter Description2 \n \n--- \n \n### Return \n`int` : Return Value Description2"
"value": "Test Description2 for testResource1\n \n \n--- \n \n### Query Parameters \n \n`int` ***param1*** : a Parameter Description2 \n \n--- \n \n### Return \n`int` : Return Value Description2"
}
},
"id": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
client class Client {
# Test Description.
#
# + param1 - parameter description
# + param2 - parameter description
# + param3 - parameter description
# + param4 - parameter description
# + return - return value description
resource function post path/[string param1]/path2/[string ...param2](string param3, int ...param4) returns string {
return "post";
}
}

public function test() {
Client cli = new Client();
string stringResult = cli->/path/[""]/path2/[""].post("", 1);
}
Loading