use of org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter in project ballerina by ballerina-lang.
the class ParserRuleTypeNameContextResolver method resolveItems.
@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
ParserRuleContext parserRuleContext = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
CompletionItemSorter itemSorter = ItemSorters.getSorterByClass(DefaultItemSorter.class);
if (parserRuleContext.getParent() instanceof BallerinaParser.CatchClauseContext && CommonUtil.isWithinBrackets(completionContext, Collections.singletonList(CATCH_KEY_WORD))) {
this.populateCompletionItemList(filterCatchConditionSymbolInfo(completionContext), completionItems);
} else if (tokenStream.get(completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY)).getText().equals(":")) {
/*
TODO: ATM, this particular condition becomes true only when try to access packages' items in the
endpoint definition context
*/
this.populateCompletionItemList(filterEndpointContextSymbolInfo(completionContext), completionItems);
} else {
StatementTemplateFilter statementTemplateFilter = new StatementTemplateFilter();
// Add the statement templates
completionItems.addAll(statementTemplateFilter.filterItems(completionContext));
this.populateBasicTypes(completionItems, completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY));
itemSorter = ItemSorters.getSorterByClass(completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY).getClass());
}
itemSorter.sortItems(completionContext, completionItems);
return completionItems;
}
use of org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter in project ballerina by ballerina-lang.
the class ServiceContextResolver method resolveItems.
@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
// TODO: Add annotations
ParserRuleContext parserRuleContext = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
if (parserRuleContext != null) {
AbstractItemResolver resolver = CompletionItemResolver.getResolverByClass(parserRuleContext.getClass());
if (resolver != null) {
completionItems.addAll(resolver.resolveItems(completionContext));
}
} else {
this.populateBasicTypes(completionItems, completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY));
CompletionItemSorter itemSorter = ItemSorters.getSorterByClass(completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY).getClass());
itemSorter.sortItems(completionContext, completionItems);
}
return completionItems;
}
use of org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter in project ballerina by ballerina-lang.
the class ParserRuleVariableDefinitionStatementContextResolver method resolveItems.
@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
PackageActionFunctionAndTypesFilter actionFunctionTypeFilter = new PackageActionFunctionAndTypesFilter();
ConnectorInitExpressionItemFilter connectorInitItemFilter = new ConnectorInitExpressionItemFilter();
// action invocation or worker invocation
if (isInvocationOrFieldAccess(completionContext)) {
ArrayList<SymbolInfo> actionAndFunctions = new ArrayList<>();
actionAndFunctions.addAll(actionFunctionTypeFilter.filterItems(completionContext));
this.populateCompletionItemList(actionAndFunctions, completionItems);
} else {
// Fill completions if user is writing a connector init
List<SymbolInfo> filteredConnectorInitSuggestions = connectorInitItemFilter.filterItems(completionContext);
if (!filteredConnectorInitSuggestions.isEmpty()) {
populateCompletionItemList(filteredConnectorInitSuggestions, completionItems);
}
// Add the create keyword
CompletionItem createKeyword = new CompletionItem();
createKeyword.setInsertText(Snippet.CREATE_KEYWORD_SNIPPET.toString());
createKeyword.setLabel(ItemResolverConstants.CREATE_KEYWORD);
createKeyword.setDetail(ItemResolverConstants.KEYWORD_TYPE);
List<SymbolInfo> filteredList = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY).stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
SymbolKind symbolKind = bSymbol.kind;
// Here we return false if the BType is not either a package symbol or ENUM
return !((bSymbol instanceof BTypeSymbol) && !(bSymbol instanceof BPackageSymbol || SymbolKind.ENUM.equals(symbolKind)));
}).collect(Collectors.toList());
// Remove the functions without a receiver symbol
filteredList.removeIf(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BInvokableSymbol && ((BInvokableSymbol) bSymbol).receiverSymbol != null;
});
populateCompletionItemList(filteredList, completionItems);
completionItems.add(createKeyword);
}
Class sorterKey = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY).getClass();
CompletionItemSorter itemSorter = ItemSorters.getSorterByClass(sorterKey);
itemSorter.sortItems(completionContext, completionItems);
return completionItems;
}
use of org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter in project ballerina by ballerina-lang.
the class ConnectorDefinitionContextResolver method resolveItems.
@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
if (!this.isAnnotationContext(completionContext)) {
ParserRuleContext parserRuleContext = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
if (parserRuleContext != null) {
AbstractItemResolver resolver = CompletionItemResolver.getResolverByClass(parserRuleContext.getClass());
if (resolver != null) {
completionItems.addAll(resolver.resolveItems(completionContext));
}
} else {
CompletionItemSorter itemSorter = ItemSorters.getSorterByClass(completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY).getClass());
this.populateBasicTypes(completionItems, completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY));
itemSorter.sortItems(completionContext, completionItems);
}
}
return completionItems;
}
Aggregations