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 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@
*/
package org.ballerinalang.langserver.hover;

import io.ballerina.compiler.api.symbols.ArrayTypeSymbol;
import io.ballerina.compiler.api.symbols.ClassSymbol;
import io.ballerina.compiler.api.symbols.Documentation;
import io.ballerina.compiler.api.symbols.FunctionSymbol;
import io.ballerina.compiler.api.symbols.MethodSymbol;
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 +165,39 @@
}
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();
switch (resourcePath.kind()) {
case PATH_SEGMENT_LIST -> {
PathSegmentList pathSegmentList = (PathSegmentList) resourcePath;
List<PathParameterSymbol> pathParameterSymbols = pathSegmentList.pathParameters();
parameterSymbols.addAll(pathParameterSymbols);
pathSegmentList.pathRestParameter().ifPresent(parameterSymbols::add);
}
case PATH_REST_PARAM -> parameterSymbols.add(((PathRestParam) resourcePath).parameter());
default -> {
// ignore
}
}
}
Map<String, String> paramsMap = documentation.get().parameterMap();
if (!paramsMap.isEmpty()) {
List<String> params = new ArrayList<>();
List<String> params = new ArrayList<>();

if (!paramsMap.isEmpty() || !parameterSymbols.isEmpty()) {
params.add(MarkupUtils.header(3, ContextConstants.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 194 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#L193-L194

Added lines #L193 - L194 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()));
params.addAll(functionSymbol.typeDescriptor().params().get().stream().map(param -> {
if (param.getName().isEmpty()) {
return MarkupUtils.quotedString(NameUtil
Expand Down Expand Up @@ -196,12 +230,20 @@

Optional<ParameterSymbol> restParam = functionSymbol.typeDescriptor().restParam();
if (restParam.isPresent()) {
String modifiedTypeName = NameUtil.getModifiedTypeName(context, restParam.get().typeDescriptor());
TypeSymbol typeSymbol = restParam.get().typeDescriptor();
String modifiedTypeName = typeSymbol.typeKind() == TypeDescKind.ARRAY ? NameUtil
.getModifiedTypeName(context, ((ArrayTypeSymbol) typeSymbol).memberTypeDescriptor())
: NameUtil.getModifiedTypeName(context, typeSymbol);

StringBuilder restParamBuilder = new StringBuilder(MarkupUtils.quotedString(modifiedTypeName + "..."));
if (restParam.get().getName().isPresent()) {
String paramName = paramsMap.get(restParam.get().getName().get());
if (paramName == null) {
paramName = "";
}
restParamBuilder.append(" ")
.append(MarkupUtils.italicString(MarkupUtils.boldString(restParam.get().getName().get())))
.append(" : ").append(paramsMap.get(restParam.get().getName().get()));
.append(" : ").append(paramName);
}
params.add(restParamBuilder.toString());
}
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();
SyntaxKind kind = cursor.kind();
if (kind == SyntaxKind.LIST || kind == SyntaxKind.PARENTHESIZED_ARG_LIST
|| kind == SyntaxKind.SIMPLE_NAME_REFERENCE
&& cursor.parent().kind() == SyntaxKind.CLIENT_RESOURCE_ACCESS_ACTION) {
return semanticModel.symbol(cursor.parent());
}
return semanticModel.symbol(srcFile, linePosition);
}

/**
* returns the default hover object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"result": {
"contents": {
"kind": "markdown",
"value": "Returns the maximum of one or more int values.\n\n```ballerina\nint:max(50, 20, 30, 70, 65) ⇒ 70\n[int, int, int] scores = [52, 95, 76];\nint:max(...scores) ⇒ 95\nint n = 18;\nn.max(25, 30, 4, 15) ⇒ 30\n```\n \n \n--- \n \n### Parameters \n \n`int` ***n*** : first int value \n`int[]...` ***ns*** : other int values \n \n--- \n \n### Return \n`int` : maximum value of value of parameter `n` and all of parameter `ns` \n \n--- \n \n[View API Docs](https://lib.ballerina.io/ballerina/lang.int/0.0.0#max)"
"value": "Returns the maximum of one or more int values.\n\n```ballerina\nint:max(50, 20, 30, 70, 65) ⇒ 70\n[int, int, int] scores = [52, 95, 76];\nint:max(...scores) ⇒ 95\nint n = 18;\nn.max(25, 30, 4, 15) ⇒ 30\n```\n \n \n--- \n \n### Parameters \n \n`int` ***n*** : first int value \n`int...` ***ns*** : other int values \n \n--- \n \n### Return \n`int` : maximum value of value of parameter `n` and all of parameter `ns` \n \n--- \n \n[View API Docs](https://lib.ballerina.io/ballerina/lang.int/0.0.0#max)"
}
},
"id": {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"position": {
"line": 28,
"character": 34
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description.\n \n \n--- \n \n### Parameters \n \n`string` ***param1*** : parameter description \n`string` ***param2*** : parameter description \n`string` ***param3*** : parameter description \n`int...` ***param4*** : \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": 28,
"character": 39
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description.\n \n \n--- \n \n### Parameters \n \n`string` ***param1*** : parameter description \n`string` ***param2*** : parameter description \n`string` ***param3*** : parameter description \n`int...` ***param4*** : \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": 28,
"character": 55
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description.\n \n \n--- \n \n### Parameters \n \n`string` ***param1*** : parameter description \n`string` ***param2*** : parameter description \n`string` ***param3*** : parameter description \n`int...` ***param4*** : \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": 29,
"character": 34
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Description.\n \n \n--- \n \n### Parameters \n \n`int` ***ids*** : 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": 30,
"character": 35
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Description.\n \n \n--- \n \n### Parameters \n \n`int` ***ids*** : 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": 28,
"character": 34
},
"source": {
"file": "hover_for_resource_path.bal"
},
"expected": {
"result": {
"contents": {
"kind": "markdown",
"value": "Test Description.\n \n \n--- \n \n### Parameters \n \n`string` ***param1*** : parameter description \n`string` ***param2*** : parameter description \n`string` ***param3*** : parameter description \n`int...` ***param4*** : \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": "This is function4 with input parameters\n \n \n--- \n \n### Parameters \n \n`int` ***param1*** : param1 Parameter Description \n`int` ***param2*** : param2 Parameter Description \n`string` ***param3*** : param3 Parameter Description \n`float[]...` ***param4*** : param4 Parameter Description \n \n--- \n \n[View API Docs](https://lib.ballerina.io/ballerina/module1/0.1.0#function4)"
"value": "This is function4 with input parameters\n \n \n--- \n \n### Parameters \n \n`int` ***param1*** : param1 Parameter Description \n`int` ***param2*** : param2 Parameter Description \n`string` ***param3*** : param3 Parameter Description \n`float...` ***param4*** : param4 Parameter Description \n \n--- \n \n[View API Docs](https://lib.ballerina.io/ballerina/module1/0.1.0#function4)"
}
},
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"result": {
"contents": {
"kind": "markdown",
"value": "Description\n \n \n--- \n \n### Parameters \n \n`int` ***param1*** : param1 Parameter Description \n`int` ***param2*** : param2 Parameter Description \n`string` ***param3*** : param3 Parameter Description`(default: \"\")` \n`int[]...` ***restparam*** : restparam Parameter Description \n \n--- \n \n### Return \n`int` : Return Value Description"
"value": "Description\n \n \n--- \n \n### Parameters \n \n`int` ***param1*** : param1 Parameter Description \n`int` ***param2*** : param2 Parameter Description \n`string` ***param3*** : param3 Parameter Description`(default: \"\")` \n`int...` ***restparam*** : restparam Parameter Description \n \n--- \n \n### Return \n`int` : Return Value Description"
}
},
"id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"result": {
"contents": {
"kind": "markdown",
"value": "Description\n \n \n--- \n \n### Parameters \n \n`int` ***param1*** : param1 Parameter Description \n`int` ***param2*** : param2 Parameter Description \n`string` ***param3*** : param3 Parameter Description`(default: \"\")` \n`int[]...` ***restparam*** : restparam Parameter Description \n \n--- \n \n### Return \n`int` : Return Value Description"
"value": "Description\n \n \n--- \n \n### Parameters \n \n`int` ***param1*** : param1 Parameter Description \n`int` ***param2*** : param2 Parameter Description \n`string` ***param3*** : param3 Parameter Description`(default: \"\")` \n`int...` ***restparam*** : restparam Parameter Description \n \n--- \n \n### Return \n`int` : Return Value Description"
}
},
"id": {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
client class Client {
# Test Description.
#
# + param1 - parameter description
# + param2 - parameter description
# + param3 - parameter description
# + return - return value description
resource function post path/[string param1]/path2/[string ...param2](string param3, int ...param4) returns string {
return "post";
}

# Description.
#
# + ids - parameter description
# + return - return value description
resource function get [int... ids]() returns string {
return "get";
}

# Description.
# + return - return value description
resource function get path2/[int... ]() returns string {
return "get";
}
}

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