use of org.ballerinalang.langserver.completions.SymbolInfo in project ballerina by ballerina-lang.
the class SignatureHelpUtil method getFunctionSignatureHelp.
/**
* Get the functionSignatureHelp instance.
*
* @param context Signature help context
* @return {@link SignatureHelp} Signature help for the completion
*/
public static SignatureHelp getFunctionSignatureHelp(TextDocumentServiceContext context) {
// Get the functions List
List<SymbolInfo> functions = context.get(SignatureKeys.FILTERED_FUNCTIONS);
List<SignatureInformation> signatureInformationList = functions.stream().map(symbolInfo -> getSignatureInformation((BInvokableSymbol) symbolInfo.getScopeEntry().symbol, context)).filter(Objects::nonNull).collect(Collectors.toList());
SignatureHelp signatureHelp = new SignatureHelp();
signatureHelp.setSignatures(signatureInformationList);
signatureHelp.setActiveParameter(context.get(SignatureKeys.PARAMETER_COUNT));
signatureHelp.setActiveSignature(0);
return signatureHelp;
}
use of org.ballerinalang.langserver.completions.SymbolInfo 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;
}
use of org.ballerinalang.langserver.completions.SymbolInfo in project ballerina by ballerina-lang.
the class AssignmentStmtContextSorter method getVariableType.
/**
* Get the variable type.
*
* @param ctx Document Service context (Completion context)
* @return {@link String} type of the variable
*/
@Override
String getVariableType(TextDocumentServiceContext ctx) {
String variableName = ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY).getStart().getText();
List<SymbolInfo> visibleSymbols = ctx.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
SymbolInfo filteredSymbol = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
String symbolName = symbolInfo.getSymbolName();
return bSymbol instanceof BVarSymbol && symbolName.equals(variableName);
}).findFirst().orElse(null);
if (filteredSymbol != null) {
return filteredSymbol.getScopeEntry().symbol.type.toString();
}
return "";
}
use of org.ballerinalang.langserver.completions.SymbolInfo 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.SymbolInfo in project ballerina by ballerina-lang.
the class PackageActionFunctionAndTypesFilter method filterItems.
@Override
public List<SymbolInfo> filterItems(TextDocumentServiceContext completionContext) {
TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
int delimiterIndex = this.getPackageDelimiterTokenIndex(completionContext);
String delimiter = tokenStream.get(delimiterIndex).getText();
ArrayList<SymbolInfo> returnSymbolsInfoList = new ArrayList<>();
if (UtilSymbolKeys.DOT_SYMBOL_KEY.equals(delimiter) || UtilSymbolKeys.ACTION_INVOCATION_SYMBOL_KEY.equals(delimiter)) {
// If the delimiter is "." then we are filtering the bound functions for the structs
returnSymbolsInfoList.addAll(this.invocationsAndFieldsOnIdentifier(completionContext, delimiterIndex));
} else if (UtilSymbolKeys.PKG_DELIMITER_KEYWORD.equals(delimiter)) {
// We are filtering the package functions, actions and the types
ArrayList<SymbolInfo> filteredList = this.getActionsFunctionsAndTypes(completionContext, delimiterIndex);
// If this is a connector init
if (isConnectorInit(delimiterIndex, completionContext)) {
List<SymbolInfo> connectorKindList = filteredList.stream().filter(item -> item.getScopeEntry().symbol.kind.toString().equals(SymbolKind.CONNECTOR.toString())).collect(Collectors.toList());
returnSymbolsInfoList.addAll(connectorKindList);
} else {
returnSymbolsInfoList.addAll(filteredList);
}
}
return returnSymbolsInfoList;
}
Aggregations