Skip to content

Commit

Permalink
Refactor sorting of completion items
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunayf committed May 10, 2024
1 parent 51dda1f commit 1f7f7e6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,25 @@ public void sort(BallerinaCompletionContext context, ClientResourceAccessActionN
}
}

private static void sortParameterlessCompletionItem(BallerinaCompletionContext context, int rank,
LSCompletionItem completionItem) {
completionItem.getCompletionItem().setSortText(SortingUtil.genSortText(
SortingUtil.toRank(context, completionItem)) + SortingUtil.genSortText(rank + 1));
}

private static void sortSymbolCompletionItem(BallerinaCompletionContext context, TypeSymbol parameterSymbol,
int rank, LSCompletionItem completionItem) {
SymbolCompletionItem symbolCompletionItem = (SymbolCompletionItem) completionItem;
Optional<Symbol> symbol = symbolCompletionItem.getSymbol();
if (symbol.isPresent() && symbol.get().kind() == SymbolKind.RESOURCE_METHOD) {
completionItem.getCompletionItem().setSortText(
SortingUtil.genSortTextByAssignability(context, completionItem, parameterSymbol) +
SortingUtil.genSortText(rank + 1));
return;
}
sortDefaultCompletionItem(context, parameterSymbol, completionItem);
}

private List<LSCompletionItem> getPathSegmentCompletionItems(ClientResourceAccessActionNode node,
BallerinaCompletionContext context,
List<ResourceMethodSymbol> resourceMethods,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.ballerina.compiler.api.symbols.ParameterSymbol;
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
import io.ballerina.compiler.api.symbols.RecordTypeSymbol;
import io.ballerina.compiler.api.symbols.SymbolKind;
import io.ballerina.compiler.api.symbols.TypeDescKind;
import io.ballerina.compiler.api.symbols.TypeSymbol;
import io.ballerina.compiler.syntax.tree.ExplicitNewExpressionNode;
Expand All @@ -41,7 +40,6 @@
import org.ballerinalang.langserver.commons.completion.LSCompletionException;
import org.ballerinalang.langserver.commons.completion.LSCompletionItem;
import org.ballerinalang.langserver.completions.NamedArgCompletionItem;
import org.ballerinalang.langserver.completions.SymbolCompletionItem;
import org.ballerinalang.langserver.completions.builder.NamedArgCompletionItemBuilder;
import org.ballerinalang.langserver.completions.providers.AbstractCompletionProvider;
import org.ballerinalang.langserver.completions.util.SortingUtil;
Expand Down Expand Up @@ -127,31 +125,12 @@ protected static void sortNamedArgCompletionItem(BallerinaCompletionContext cont
completionItem.getCompletionItem().setSortText(sortText);
}

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

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

protected static void sortSymbolCompletionItem(BallerinaCompletionContext context, TypeSymbol parameterSymbol, int i,
LSCompletionItem completionItem) {
SymbolCompletionItem symbolCompletionItem = (SymbolCompletionItem) completionItem;
if (symbolCompletionItem.getSymbol().isPresent() &&
symbolCompletionItem.getSymbol().get().kind() == SymbolKind.RESOURCE_METHOD) {
completionItem.getCompletionItem().setSortText(
SortingUtil.genSortTextByAssignability(context, completionItem, parameterSymbol) +
SortingUtil.genSortText(i + 1));
return;
}
sortDefaultCompletionItem(context, parameterSymbol, completionItem);
}

protected static void sortDefaultCompletionItem(BallerinaCompletionContext context, TypeSymbol parameterSymbol,
LSCompletionItem completionItem) {
completionItem.getCompletionItem().setSortText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample resource function to return a stream of objects.\n \n**Params** \n- `module1:TargetType2` targetType: Response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `stream<targetType, mod2:ClientError2?>` \n- A stream of targetType and/or ClientError \n \n"
}
},
"sortText": "C",
"sortText": "CI",
"filterText": "responses|delete",
"insertText": "/responses.delete(${1});",
"insertTextFormat": "Snippet",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample remote method with java interoperability\n \n**Params** \n- `string` path: Resource path \n- `handle` request: Request need to be forward \n- `mod:TargetType` targetType: Response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `targetType` \n- Response \n \n"
}
},
"sortText": "CC",
"sortText": "CF",
"filterText": "forward",
"insertText": "forward(${1})",
"insertTextFormat": "Snippet",
Expand All @@ -73,7 +73,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample remote method with java interoperability\n \n**Params** \n- `mod:TargetType2` targetType: `any`type which is expected to be returned after data binding(Defaultable) \n \n**Return** `targetType` \n- Type any \n \n"
}
},
"sortText": "CD",
"sortText": "CG",
"filterText": "delete",
"insertText": "delete(${1})",
"insertTextFormat": "Snippet",
Expand All @@ -92,7 +92,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample resource function with rest path praram with multiple target types with java interoperability\n \n**Params** \n- `string[]` path: Request path \n- `mod:RequestMessage` message: An HTTP outbound request or any allowed payload \n- `map<string|string[]>?` headers: The entity headers(Defaultable) \n- `string?` mediaType: The MIME type header of the request entity(Defaultable) \n- `mod:TargetType` targetType: HTTP response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `targetType|mod:ClientError` \n- The response or the payload (if the `targetType` is configured) or an `http:ClientError` if failed to \nestablish the communication with the upstream server or a data binding failure \n \n"
}
},
"sortText": "CE",
"sortText": "CH",
"filterText": "post",
"insertText": "/[${1:\"path\"}].post(${2});",
"insertTextFormat": "Snippet",
Expand All @@ -111,7 +111,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample resource function with rest path praram with multiple target types with java interoperability\n \n**Params** \n- `string[]` path: Request path \n- `mod:RequestMessage` message: An HTTP outbound request or any allowed payload \n- `map<string|string[]>?` headers: The entity headers(Defaultable) \n- `string?` mediaType: The MIME type header of the request entity(Defaultable) \n- `mod:TargetType` targetType: HTTP response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `targetType|mod:ClientError` \n- The response or the payload (if the `targetType` is configured) or an `http:ClientError` if failed to \nestablish the communication with the upstream server or a data binding failure \n \n"
}
},
"sortText": "CF",
"sortText": "CI",
"filterText": "post",
"insertText": "/${1:path}.post(${2});",
"insertTextFormat": "Snippet",
Expand All @@ -130,7 +130,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample remote method with java interoperability.\n \n**Params** \n- `string` path: Resource path \n- `mod:TargetType` targetType: Response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `targetType|mod2:ClientError2` \n- The Response or the ClientError \n \n"
}
},
"sortText": "C",
"sortText": "CC",
"filterText": "push",
"insertText": "push(${1})",
"insertTextFormat": "Snippet",
Expand All @@ -149,7 +149,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample remote method with java interoperability.\n \n**Params** \n- `string` path: Resource path \n- `typedesc<anydata>` targetType: Response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `targetType|mod2:ClientError2` \n- The Response or the ClientError \n \n"
}
},
"sortText": "C",
"sortText": "CD",
"filterText": "pull",
"insertText": "pull(${1})",
"insertTextFormat": "Snippet",
Expand All @@ -168,7 +168,7 @@
"value": "**Package:** _ballerina/module1:0.1.0_ \n \nSample remote method with java interoperability.\n \n**Params** \n- `string` path: Resource path \n- `typedesc<anydata>` targetType: Response or `anydata`, which is expected to be returned after data binding(Defaultable) \n \n**Return** `targetType|mod:ClientError` \n- The Response or the ClientError \n \n"
}
},
"sortText": "C",
"sortText": "CE",
"filterText": "clear",
"insertText": "clear(${1})",
"insertTextFormat": "Snippet",
Expand Down

0 comments on commit 1f7f7e6

Please sign in to comment.