Skip to content

Commit

Permalink
Merge pull request #41773 from mindula/fix-41646
Browse files Browse the repository at this point in the history
Add hover support for resource paths
  • Loading branch information
KavinduZoysa authored Jan 17, 2024
2 parents 8a53bed + 74474f6 commit b5a4134
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 49 deletions.
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 @@ private Hover getHoverObjectForSymbol(FunctionSymbol functionSymbol) {
}
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()));
}
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 @@ private Hover getHoverObjectForSymbol(FunctionSymbol functionSymbol) {

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];
}

0 comments on commit b5a4134

Please sign in to comment.