Skip to content

Commit

Permalink
Merge pull request ballerina-platform#41663 from nipunayf/fix-path-re…
Browse files Browse the repository at this point in the history
…source

Improved the order of completions provided for resource access paths
  • Loading branch information
KavinduZoysa authored Nov 24, 2023
2 parents f59419a + 09e995d commit 75f1758
Show file tree
Hide file tree
Showing 27 changed files with 192 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ private static ResourceAccessPathPart getResourceAccessPartForSegment(PathSegmen
new ResourceAccessPathPart(computedInsertText, computedSignature);
if (context.currentSemanticModel().isPresent() &&
context.currentSemanticModel().get().types().STRING.subtypeOf(typeSymbol)) {
resourceAccessPathPart.namedPathSignature = "<path>";
resourceAccessPathPart.namedPathSignature = "<" +
(pathParameterSymbol.getName().isPresent() ? pathParameterSymbol.getName().get() : "path") + ">";
resourceAccessPathPart.namedPathInsertText = "${" + placeHolderIndex + ":" + "path" + "}";
resourceAccessPathPart.computedPathInsertText = "[${" + placeHolderIndex + ":" + "\"path\"" + "}]";
resourceAccessPathPart.isStringPathPram = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -543,6 +544,7 @@ protected List<LSCompletionItem> expressionCompletions(BallerinaCompletionContex
Predicate<Symbol> symbolFilter = getExpressionContextSymbolFilter();
List<Symbol> filteredList = visibleSymbols.stream()
.filter(symbolFilter)
.sorted(Comparator.comparing(symbol -> symbol.getName().get()))
.collect(Collectors.toList());
completionItems.addAll(this.getCompletionItemList(filteredList, context));
completionItems.addAll(this.getBasicAndOtherTypeCompletions(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public List<LSCompletionItem> getCompletions(BallerinaCompletionContext context,
@Override
public void sort(BallerinaCompletionContext context, AsyncSendActionNode node,
List<LSCompletionItem> completionItems) {
for (LSCompletionItem completionItem : completionItems) {
sortByAssignability(context, completionItem, SortingUtil.toRank(context, completionItem));
for (int i = 0; i < completionItems.size(); i++) {
LSCompletionItem completionItem = completionItems.get(i);
sortByAssignability(context, completionItem, SortingUtil.toRank(context, completionItem, i + 1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,26 @@ public void sort(BallerinaCompletionContext context, ClientResourceAccessActionN
return;
}

super.sort(context, node, completionItems);
Optional<TypeSymbol> parameterSymbol = getParameterTypeSymbol(context);
for (int i = 0; i < completionItems.size(); i++) {
LSCompletionItem completionItem = completionItems.get(i);
if (completionItem.getType() == LSCompletionItem.CompletionItemType.NAMED_ARG) {
sortNamedArgCompletionItem(context, completionItem);
} else if (parameterSymbol.isEmpty()) {
sortParameterlessCompletionItem(context, completionItem);
} else if (completionItem.getType() == LSCompletionItem.CompletionItemType.SYMBOL) {
SymbolCompletionItem symbolCompletionItem = (SymbolCompletionItem) completionItem;
if (symbolCompletionItem.getSymbol().isPresent() &&
symbolCompletionItem.getSymbol().get().kind() == SymbolKind.RESOURCE_METHOD) {
completionItem.getCompletionItem().setSortText(
SortingUtil.genSortTextByAssignability(context, completionItem, parameterSymbol.get()) +
SortingUtil.genSortText(i + 1));
}
sortDefaultCompletionItem(context, parameterSymbol.get(), completionItem);
} else {
sortDefaultCompletionItem(context, parameterSymbol.get(), completionItem);
}
}
}

private List<LSCompletionItem> getPathSegmentCompletionItems(ClientResourceAccessActionNode node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,51 +72,69 @@ public List<LSCompletionItem> getCompletions(BallerinaCompletionContext context,

@Override
public void sort(BallerinaCompletionContext context, T node, List<LSCompletionItem> completionItems) {
if ((node.kind() == SyntaxKind.EXPLICIT_NEW_EXPRESSION &&
!TypeResolverUtil.isInNewExpressionParameterContext((ExplicitNewExpressionNode) node,
context.getCursorPositionInTree()))
if ((node.kind() == SyntaxKind.EXPLICIT_NEW_EXPRESSION &&
!TypeResolverUtil.isInNewExpressionParameterContext((ExplicitNewExpressionNode) node,
context.getCursorPositionInTree()))
|| (node.kind() == SyntaxKind.IMPLICIT_NEW_EXPRESSION &&
!TypeResolverUtil.isInNewExpressionParameterContext(
(ImplicitNewExpressionNode) node, context.getCursorPositionInTree()))) {
!TypeResolverUtil.isInNewExpressionParameterContext(
(ImplicitNewExpressionNode) node, context.getCursorPositionInTree()))) {
super.sort(context, node, completionItems);
return;
}

Optional<TypeSymbol> parameterSymbol = getParameterTypeSymbol(context);
for (LSCompletionItem completionItem : completionItems) {
if (completionItem.getType() == LSCompletionItem.CompletionItemType.NAMED_ARG) {
sortNamedArgCompletionItem(context, completionItem);
} else if (parameterSymbol.isEmpty()) {
sortParameterlessCompletionItem(context, completionItem);
} else {
sortDefaultCompletionItem(context, parameterSymbol.get(), completionItem);
}
}
}

protected static Optional<TypeSymbol> getParameterTypeSymbol(BallerinaCompletionContext context) {
Optional<TypeSymbol> parameterSymbol = Optional.empty();
if (context.currentSemanticModel().isPresent() && context.currentDocument().isPresent()) {
parameterSymbol = context.currentSemanticModel().get().expectedType(context.currentDocument().get(),
LinePosition.from(context.getCursorPosition().getLine(),
context.getCursorPosition().getCharacter()));
}
return parameterSymbol;
}

for (LSCompletionItem completionItem : completionItems) {
if (completionItem.getType() == LSCompletionItem.CompletionItemType.NAMED_ARG) {
NamedArgCompletionItem argCompletionItem = (NamedArgCompletionItem) completionItem;
Either<ParameterSymbol, RecordFieldSymbol> symbol = argCompletionItem.getParameterSymbol();
String sortText;
if (symbol.isRight()) {
RecordFieldSymbol right = symbol.getRight();
if (right.isOptional()) {
sortText = SortingUtil.genSortText(1) + SortingUtil.genSortText(3);
} else if (right.hasDefaultValue()) {
sortText = SortingUtil.genSortText(1) + SortingUtil.genSortText(2);
} else {
sortText = SortingUtil.genSortText(1) + SortingUtil.genSortText(1);
}
} else {
sortText = SortingUtil.genSortText(1) +
SortingUtil.genSortText(SortingUtil.toRank(context, completionItem));
}
completionItem.getCompletionItem().setSortText(sortText);
} else if (parameterSymbol.isEmpty()) {
completionItem.getCompletionItem().setSortText(SortingUtil.genSortText(
SortingUtil.toRank(context, completionItem)));
protected static void sortNamedArgCompletionItem(BallerinaCompletionContext context,
LSCompletionItem completionItem) {
NamedArgCompletionItem argCompletionItem = (NamedArgCompletionItem) completionItem;
Either<ParameterSymbol, RecordFieldSymbol> symbol = argCompletionItem.getParameterSymbol();
String sortText;
if (symbol.isRight()) {
RecordFieldSymbol right = symbol.getRight();
if (right.isOptional()) {
sortText = SortingUtil.genSortText(1) + SortingUtil.genSortText(3);
} else if (right.hasDefaultValue()) {
sortText = SortingUtil.genSortText(1) + SortingUtil.genSortText(2);
} else {
completionItem.getCompletionItem().setSortText(
SortingUtil.genSortTextByAssignability(context, completionItem, parameterSymbol.get()));
sortText = SortingUtil.genSortText(1) + SortingUtil.genSortText(1);
}
} else {
sortText = SortingUtil.genSortText(1) +
SortingUtil.genSortText(SortingUtil.toRank(context, completionItem));
}
completionItem.getCompletionItem().setSortText(sortText);
}

protected static void sortParameterlessCompletionItem(BallerinaCompletionContext context,
LSCompletionItem completionItem) {
completionItem.getCompletionItem().setSortText(SortingUtil.genSortText(
SortingUtil.toRank(context, completionItem)));
}

protected static void sortDefaultCompletionItem(BallerinaCompletionContext context, TypeSymbol parameterSymbol,
LSCompletionItem completionItem) {
completionItem.getCompletionItem().setSortText(
SortingUtil.genSortTextByAssignability(context, completionItem, parameterSymbol));
}

protected List<LSCompletionItem> getNamedArgCompletionItems(BallerinaCompletionContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"value": "**Package:** _._ \n \n \n"
}
},
"sortText": "CC",
"sortText": "CD",
"filterText": "remote1",
"insertText": "remote1()",
"insertTextFormat": "Snippet"
Expand All @@ -31,7 +31,7 @@
"value": "**Package:** _._ \n \n \n \n \n**Return** `string` \n \n"
}
},
"sortText": "CC",
"sortText": "CE",
"filterText": "path1|get",
"insertText": "/path1;",
"insertTextFormat": "Snippet"
Expand All @@ -46,7 +46,7 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string` a \n- `int` b"
}
},
"sortText": "CC",
"sortText": "CF",
"filterText": "post",
"insertText": "/.post(${1});",
"insertTextFormat": "Snippet",
Expand All @@ -65,13 +65,13 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string[]` ids"
}
},
"sortText": "CC",
"sortText": "CG",
"filterText": "get",
"insertText": "/[${1:\"path\"}];",
"insertTextFormat": "Snippet"
},
{
"label": "/<path>",
"label": "/<ids>",
"kind": "Function",
"detail": "()",
"documentation": {
Expand All @@ -80,7 +80,7 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string[]` ids"
}
},
"sortText": "CC",
"sortText": "CH",
"filterText": "get",
"insertText": "/${1:path};",
"insertTextFormat": "Snippet"
Expand All @@ -95,13 +95,13 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string` id1"
}
},
"sortText": "CC",
"sortText": "CI",
"filterText": "path1|get",
"insertText": "/path1/[${1:\"path\"}];",
"insertTextFormat": "Snippet"
},
{
"label": "/path1/<path>",
"label": "/path1/<id1>",
"kind": "Function",
"detail": "()",
"documentation": {
Expand All @@ -110,7 +110,7 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string` id1"
}
},
"sortText": "CC",
"sortText": "CJ",
"filterText": "path1|get",
"insertText": "/path1/${1:path};",
"insertTextFormat": "Snippet"
Expand All @@ -125,13 +125,13 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string` id1 \n \n**Return** `int` \n \n"
}
},
"sortText": "CC",
"sortText": "CK",
"filterText": "path1|path2|get",
"insertText": "/path1/[${1:\"path\"}]/path2;",
"insertTextFormat": "Snippet"
},
{
"label": "/path1/<path>/path2",
"label": "/path1/<id1>/path2",
"kind": "Function",
"detail": "int",
"documentation": {
Expand All @@ -140,7 +140,7 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string` id1 \n \n**Return** `int` \n \n"
}
},
"sortText": "CC",
"sortText": "CL",
"filterText": "path1|path2|get",
"insertText": "/path1/${1:path}/path2;",
"insertTextFormat": "Snippet"
Expand All @@ -155,7 +155,7 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string` id1 \n- `string[]` ids \n- `string` b \n- `string[]` ids2"
}
},
"sortText": "CC",
"sortText": "CM",
"filterText": "path1|path2|post",
"insertText": "/path1/[${1:\"path\"}]/path2/[${2:\"path\"}].post(${3});",
"insertTextFormat": "Snippet",
Expand All @@ -165,7 +165,7 @@
}
},
{
"label": "/path1/<path>/path2/<path>.post(string b, string... ids2)",
"label": "/path1/<id1>/path2/<ids>.post(string b, string... ids2)",
"kind": "Function",
"detail": "()",
"documentation": {
Expand All @@ -174,7 +174,7 @@
"value": "**Package:** _._ \n \n \n**Params** \n- `string` id1 \n- `string[]` ids \n- `string` b \n- `string[]` ids2"
}
},
"sortText": "CC",
"sortText": "CN",
"filterText": "path1|path2|post",
"insertText": "/path1/${1:path}/path2/${2:path}.post(${3});",
"insertTextFormat": "Snippet",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
]
},
{
"label": "/<path>",
"label": "/<ids>",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down Expand Up @@ -131,7 +131,7 @@
}
},
{
"label": "/path2/<path>.post(string b, string... ids2)",
"label": "/path2/<ids>.post(string b, string... ids2)",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
]
},
{
"label": "/<path>",
"label": "/<ids>",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down Expand Up @@ -131,7 +131,7 @@
}
},
{
"label": "/path2/<path>.post(string b, string... ids2)",
"label": "/path2/<ids>.post(string b, string... ids2)",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
]
},
{
"label": "/<path>",
"label": "/<ids>",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
]
},
{
"label": "/<path>",
"label": "/<ids>",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down Expand Up @@ -161,7 +161,7 @@
]
},
{
"label": "/path1/<path>",
"label": "/path1/<id1>",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down Expand Up @@ -221,7 +221,7 @@
]
},
{
"label": "/path1/<path>/path2",
"label": "/path1/<id1>/path2",
"kind": "Function",
"detail": "int",
"documentation": {
Expand Down Expand Up @@ -285,7 +285,7 @@
}
},
{
"label": "/path1/<path>/path2/<path>.post(string b, string... ids2)",
"label": "/path1/<id1>/path2/<ids>.post(string b, string... ids2)",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down Expand Up @@ -353,7 +353,7 @@
}
},
{
"label": "/path1/<path>/path3.post(string b, string... ids2)",
"label": "/path1/<id1>/path3.post(string b, string... ids2)",
"kind": "Function",
"detail": "()",
"documentation": {
Expand Down
Loading

0 comments on commit 75f1758

Please sign in to comment.