use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol 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.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol in project ballerina by ballerina-lang.
the class ParserRuleMatchStatementContextResolver method resolveItems.
@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
int currentTokenIndex = completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY);
List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
while (true) {
if (currentTokenIndex < 0) {
// Ideally should not come to this point
return completionItems;
}
Token token = CommonUtil.getPreviousDefaultToken(tokenStream, currentTokenIndex);
if (token.getText().equals(UtilSymbolKeys.MATCH_KEYWORD_KEY)) {
currentTokenIndex = token.getTokenIndex();
break;
} else {
currentTokenIndex = token.getTokenIndex();
}
}
String identifierMatched = CommonUtil.getNextDefaultToken(tokenStream, currentTokenIndex).getText();
SymbolInfo identifierSymbol = visibleSymbols.stream().filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.getName().getValue().equals(identifierMatched)).findFirst().orElseGet(null);
if (identifierSymbol == null) {
return completionItems;
} else if (identifierSymbol.getScopeEntry().symbol.type instanceof BUnionType) {
Set<BType> memberTypes = ((BUnionType) identifierSymbol.getScopeEntry().symbol.type).getMemberTypes();
memberTypes.forEach(bType -> {
completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
});
} else if (identifierSymbol.getScopeEntry().symbol.type instanceof BJSONType) {
ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
}).collect(Collectors.toList());
this.populateCompletionItemList(filteredBasicTypes, completionItems);
} else if (identifierSymbol.getScopeEntry().symbol.type instanceof BStructType) {
List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
}).collect(Collectors.toList());
this.populateCompletionItemList(structSymbols, completionItems);
}
return completionItems;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol in project ballerina by ballerina-lang.
the class AbstractItemResolver method populateBallerinaFunctionCompletionItem.
/**
* Populate the Ballerina Function Completion Item.
* @param symbolInfo - symbol information
* @return completion item
*/
private CompletionItem populateBallerinaFunctionCompletionItem(SymbolInfo symbolInfo) {
String insertText;
String label;
CompletionItem completionItem = new CompletionItem();
if (symbolInfo.isIterableOperation()) {
insertText = symbolInfo.getIterableOperationSignature().getInsertText();
label = symbolInfo.getIterableOperationSignature().getLabel();
} else {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
if (!(bSymbol instanceof BInvokableSymbol)) {
return null;
}
BInvokableSymbol bInvokableSymbol = (BInvokableSymbol) bSymbol;
if (bInvokableSymbol.getName().getValue().contains("<") || bInvokableSymbol.getName().getValue().contains("<") || bInvokableSymbol.getName().getValue().equals("main")) {
return null;
}
FunctionSignature functionSignature = getFunctionSignature(bInvokableSymbol);
insertText = functionSignature.getInsertText();
label = functionSignature.getLabel();
}
completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
completionItem.setLabel(label);
completionItem.setInsertText(insertText);
completionItem.setDetail(ItemResolverConstants.FUNCTION_TYPE);
completionItem.setKind(CompletionItemKind.Function);
return completionItem;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol in project ballerina by ballerina-lang.
the class AbstractItemResolver method populateCompletionItemList.
/**
* Populate the completion item list by considering the.
* @param symbolInfoList - list of symbol information
* @param completionItems - completion item list to populate
*/
protected void populateCompletionItemList(List<SymbolInfo> symbolInfoList, List<CompletionItem> completionItems) {
symbolInfoList.forEach(symbolInfo -> {
CompletionItem completionItem = null;
BSymbol bSymbol = symbolInfo.getScopeEntry() != null ? symbolInfo.getScopeEntry().symbol : null;
if ((bSymbol instanceof BInvokableSymbol && ((BInvokableSymbol) bSymbol).kind != null && !((BInvokableSymbol) bSymbol).kind.equals(SymbolKind.WORKER)) || symbolInfo.isIterableOperation()) {
completionItem = this.populateBallerinaFunctionCompletionItem(symbolInfo);
} else if (!(bSymbol instanceof BInvokableSymbol) && bSymbol instanceof BVarSymbol) {
completionItem = this.populateVariableDefCompletionItem(symbolInfo);
} else if (bSymbol instanceof BTypeSymbol && !bSymbol.getName().getValue().equals(UtilSymbolKeys.NOT_FOUND_TYPE) && !(bSymbol instanceof BAnnotationSymbol)) {
completionItem = this.populateBTypeCompletionItem(symbolInfo);
}
if (completionItem != null) {
completionItems.add(completionItem);
}
});
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol in project ballerina by ballerina-lang.
the class PositionTreeVisitor method visit.
public void visit(BLangFunction funcNode) {
// Check for native functions
BSymbol funcSymbol = funcNode.symbol;
if (Symbols.isNative(funcSymbol)) {
return;
}
addTopLevelNodeToContext(funcNode, funcNode.name.getValue(), funcNode.symbol.pkgID, funcNode.symbol.kind.name(), funcNode.symbol.kind.name(), funcNode.symbol.owner.name.getValue(), funcNode.symbol.owner.pkgID);
setPreviousNode(funcNode);
this.addToNodeStack(funcNode);
if (funcNode.receiver != null) {
this.acceptNode(funcNode.receiver);
}
if (!funcNode.requiredParams.isEmpty()) {
funcNode.requiredParams.forEach(this::acceptNode);
}
if (!funcNode.retParams.isEmpty()) {
funcNode.retParams.forEach(this::acceptNode);
}
if (funcNode.body != null) {
this.acceptNode(funcNode.body);
}
if (funcNode.endpoints != null && !funcNode.endpoints.isEmpty()) {
funcNode.endpoints.forEach(this::acceptNode);
}
// Process workers
if (!funcNode.workers.isEmpty()) {
funcNode.workers.forEach(this::acceptNode);
}
if (!funcNode.defaultableParams.isEmpty()) {
funcNode.defaultableParams.forEach(this::acceptNode);
}
}
Aggregations