use of org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter in project ballerina by ballerina-lang.
the class DefaultResolver method resolveItems.
@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
CompletionItem workerItem = new CompletionItem();
workerItem.setLabel(ItemResolverConstants.WORKER);
workerItem.setInsertText(Snippet.WORKER.toString());
workerItem.setInsertTextFormat(InsertTextFormat.Snippet);
workerItem.setDetail(ItemResolverConstants.WORKER_TYPE);
completionItems.add(workerItem);
populateCompletionItemList(completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY), completionItems);
// Add the statement templates
StatementTemplateFilter statementTemplateFilter = new StatementTemplateFilter();
completionItems.addAll(statementTemplateFilter.filterItems(completionContext));
return completionItems;
}
use of org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter 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.filters.StatementTemplateFilter in project ballerina by ballerina-lang.
the class StatementContextResolver method resolveItems.
@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
// action invocation or worker invocation
if (isInvocationOrFieldAccess(completionContext)) {
ArrayList<SymbolInfo> actionAndFunctions = new ArrayList<>();
PackageActionFunctionAndTypesFilter actionFunctionTypeFilter = new PackageActionFunctionAndTypesFilter();
actionAndFunctions.addAll(actionFunctionTypeFilter.filterItems(completionContext));
this.populateCompletionItemList(actionAndFunctions, completionItems);
} else {
CompletionItem xmlns = new CompletionItem();
xmlns.setLabel(ItemResolverConstants.XMLNS);
xmlns.setInsertText(Snippet.NAMESPACE_DECLARATION.toString());
xmlns.setInsertTextFormat(InsertTextFormat.Snippet);
xmlns.setDetail(ItemResolverConstants.SNIPPET_TYPE);
completionItems.add(xmlns);
// Add the var keyword
CompletionItem varKeyword = new CompletionItem();
varKeyword.setInsertText("var ");
varKeyword.setLabel("var");
varKeyword.setDetail(ItemResolverConstants.KEYWORD_TYPE);
completionItems.add(varKeyword);
StatementTemplateFilter statementTemplateFilter = new StatementTemplateFilter();
// Add the statement templates
completionItems.addAll(statementTemplateFilter.filterItems(completionContext));
// We need to remove the functions having a receiver symbol and the bTypes of the following
// ballerina.coordinator, ballerina.runtime, and anonStructs
ArrayList<String> invalidPkgs = new ArrayList<>(Arrays.asList("ballerina.runtime", "ballerina.transactions.coordinator"));
completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY).removeIf(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
String symbolName = bSymbol.getName().getValue();
return (bSymbol instanceof BInvokableSymbol && ((BInvokableSymbol) bSymbol).receiverSymbol != null) || (bSymbol instanceof BPackageSymbol && invalidPkgs.contains(symbolName)) || (symbolName.startsWith("$anonStruct"));
});
populateCompletionItemList(completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY), completionItems);
// Now we need to sort the completion items and populate the completion items specific to the scope owner
// as an example, resource, action, function scopes are different from the if-else, while, and etc
Class itemSorter = completionContext.get(CompletionKeys.BLOCK_OWNER_KEY).getClass();
ItemSorters.getSorterByClass(itemSorter).sortItems(completionContext, completionItems);
}
return completionItems;
}
Aggregations