use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol 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.BPackageSymbol in project ballerina by ballerina-lang.
the class PackageActionFunctionAndTypesFilter method getActionsFunctionsAndTypes.
/**
* Get the actions, functions and types.
* @param completionContext Text Document Service context (Completion Context)
* @param delimiterIndex delimiter index (index of either . or :)
* @return {@link ArrayList} List of filtered symbol info
*/
private ArrayList<SymbolInfo> getActionsFunctionsAndTypes(TextDocumentServiceContext completionContext, int delimiterIndex) {
ArrayList<SymbolInfo> actionFunctionList = new ArrayList<>();
TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
List<SymbolInfo> symbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
String packageName = tokenStream.get(delimiterIndex - 1).getText();
// Extract the package symbol
SymbolInfo packageSymbolInfo = symbols.stream().filter(item -> {
Scope.ScopeEntry scopeEntry = item.getScopeEntry();
return item.getSymbolName().equals(packageName) && scopeEntry.symbol instanceof BPackageSymbol;
}).findFirst().orElse(null);
if (packageSymbolInfo != null) {
Scope.ScopeEntry packageEntry = packageSymbolInfo.getScopeEntry();
SymbolInfo symbolInfo = new SymbolInfo(packageSymbolInfo.getSymbolName(), packageEntry);
symbolInfo.getScopeEntry().symbol.scope.entries.forEach((name, value) -> {
if ((value.symbol instanceof BInvokableSymbol && ((BInvokableSymbol) value.symbol).receiverSymbol == null) || (value.symbol instanceof BTypeSymbol && !(value.symbol instanceof BPackageSymbol))) {
actionFunctionList.add(new SymbolInfo(name.toString(), value));
}
});
}
return actionFunctionList;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol in project ballerina by ballerina-lang.
the class PackageActionFunctionAndTypesFilter method invocationsAndFieldsOnIdentifier.
/**
* Get the invocations and fields against an identifier (functions, struct fields and types including the enums).
* @param context Text Document Service context (Completion Context)
* @param delimiterIndex delimiter index (index of either . or :)
* @return {@link ArrayList} List of filtered symbol info
*/
private ArrayList<SymbolInfo> invocationsAndFieldsOnIdentifier(TextDocumentServiceContext context, int delimiterIndex) {
ArrayList<SymbolInfo> actionFunctionList = new ArrayList<>();
TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
List<SymbolInfo> symbols = context.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
SymbolTable symbolTable = context.get(DocumentServiceKeys.SYMBOL_TABLE_KEY);
String variableName = CommonUtil.getPreviousDefaultToken(tokenStream, delimiterIndex).getText();
SymbolInfo variable = this.getVariableByName(variableName, symbols);
String builtinPkgName = symbolTable.builtInPackageSymbol.name.getValue();
Map<Name, Scope.ScopeEntry> entries = new HashMap<>();
String currentPkgName = context.get(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY);
if (variable == null) {
return actionFunctionList;
}
String packageID;
BType bType = variable.getScopeEntry().symbol.getType();
String bTypeValue;
if (variable.getScopeEntry().symbol instanceof BEndpointVarSymbol) {
BType getClientFuncType = ((BEndpointVarSymbol) variable.getScopeEntry().symbol).getClientFunction.type;
if (!UtilSymbolKeys.ACTION_INVOCATION_SYMBOL_KEY.equals(tokenStream.get(delimiterIndex).getText()) || !(getClientFuncType instanceof BInvokableType)) {
return actionFunctionList;
}
BType boundType = ((BInvokableType) getClientFuncType).retTypes.get(0);
packageID = boundType.tsymbol.pkgID.toString();
bTypeValue = boundType.toString();
} else if (bType instanceof BArrayType) {
packageID = ((BArrayType) bType).eType.tsymbol.pkgID.toString();
bTypeValue = bType.toString();
} else {
packageID = bType.tsymbol.pkgID.toString();
bTypeValue = bType.toString();
}
// Extract the package symbol. This is used to extract the entries of the particular package
SymbolInfo packageSymbolInfo = symbols.stream().filter(item -> {
Scope.ScopeEntry scopeEntry = item.getScopeEntry();
return (scopeEntry.symbol instanceof BPackageSymbol) && scopeEntry.symbol.pkgID.toString().equals(packageID);
}).findFirst().orElse(null);
if (packageSymbolInfo == null && packageID.equals(builtinPkgName)) {
// If the packageID is ballerina.builtin, we extract entries of builtin package
entries = symbolTable.builtInPackageSymbol.scope.entries;
} else if (packageSymbolInfo == null && packageID.equals(currentPkgName)) {
entries = this.getScopeEntries(bType, context);
} else if (packageSymbolInfo != null) {
// If the package exist, we extract particular entries from package
entries = packageSymbolInfo.getScopeEntry().symbol.scope.entries;
}
entries.forEach((name, scopeEntry) -> {
if (scopeEntry.symbol instanceof BInvokableSymbol && ((BInvokableSymbol) scopeEntry.symbol).receiverSymbol != null) {
String symbolBoundedName = ((BInvokableSymbol) scopeEntry.symbol).receiverSymbol.getType().toString();
if (symbolBoundedName.equals(bTypeValue)) {
// TODO: Need to handle the name in a proper manner
String[] nameComponents = name.toString().split("\\.");
SymbolInfo actionFunctionSymbol = new SymbolInfo(nameComponents[nameComponents.length - 1], scopeEntry);
actionFunctionList.add(actionFunctionSymbol);
}
} else if ((scopeEntry.symbol instanceof BTypeSymbol) && bTypeValue.equals(scopeEntry.symbol.type.toString())) {
// Get the struct fields
Map<Name, Scope.ScopeEntry> fields = scopeEntry.symbol.scope.entries;
fields.forEach((fieldName, fieldScopeEntry) -> {
actionFunctionList.add(new SymbolInfo(fieldName.getValue(), fieldScopeEntry));
});
}
});
// Populate possible iterable operators over the variable
populateIterableOperations(variable, actionFunctionList);
return actionFunctionList;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol in project ballerina by ballerina-lang.
the class ParserRuleTypeNameContextResolver method filterEndpointContextSymbolInfo.
private static List<SymbolInfo> filterEndpointContextSymbolInfo(TextDocumentServiceContext context) {
List<SymbolInfo> symbolInfos = context.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
int currentTokenIndex = context.get(DocumentServiceKeys.TOKEN_INDEX_KEY) - 1;
TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
Token packageAlias = CommonUtil.getPreviousDefaultToken(tokenStream, currentTokenIndex);
Token constraintStart = CommonUtil.getPreviousDefaultToken(tokenStream, packageAlias.getTokenIndex() - 1);
List<SymbolInfo> returnList = new ArrayList<>();
if (!(constraintStart.getText().equals("<") || constraintStart.getText().equals("create"))) {
PackageActionFunctionAndTypesFilter filter = new PackageActionFunctionAndTypesFilter();
returnList.addAll(filter.filterItems(context));
} else {
SymbolInfo packageSymbolInfo = symbolInfos.stream().filter(item -> {
Scope.ScopeEntry scopeEntry = item.getScopeEntry();
return item.getSymbolName().equals(packageAlias.getText()) && scopeEntry.symbol instanceof BPackageSymbol;
}).findFirst().orElse(null);
if (packageSymbolInfo != null) {
packageSymbolInfo.getScopeEntry().symbol.scope.entries.forEach((name, value) -> {
if (value.symbol.kind != null && value.symbol.kind.toString().equals(CONNECTOR_KIND)) {
returnList.add(new SymbolInfo(name.toString(), value));
}
});
}
}
return returnList;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol in project ballerina by ballerina-lang.
the class SignatureTreeVisitor method populateSymbols.
/**
* Populate the symbols.
* @param symbolEntries symbol entries
*/
private void populateSymbols(Map<Name, Scope.ScopeEntry> symbolEntries) {
// TODO: Populate only the visible functions
this.terminateVisitor = true;
String identifierAgainst = documentServiceContext.get(SignatureKeys.IDENTIFIER_AGAINST);
List<SymbolInfo> visibleSymbols = new ArrayList<>();
/*
During the first iteration we filter out the functions and if there is, the variable reference against which
the function is called.
*/
symbolEntries.forEach((k, v) -> {
if (v.symbol instanceof BInvokableSymbol && !(v.symbol instanceof BOperatorSymbol) && !v.symbol.getName().getValue().contains("<init>")) {
SymbolInfo symbolInfo = new SymbolInfo(k.getValue(), v);
visibleSymbols.add(symbolInfo);
} else if (v.symbol instanceof BVarSymbol && k.getValue().equals(identifierAgainst)) {
documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
} else if (v.symbol instanceof BPackageSymbol && k.getValue().equals(identifierAgainst)) {
documentServiceContext.put(SignatureKeys.IDENTIFIER_PKGID, v.symbol.pkgID.toString());
documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
visibleSymbols.addAll(this.getInvokableSymbolsInPackage((BPackageSymbol) v.symbol));
}
});
/*
In this iteration we filter out the functions either having a receiver or otherwise.
If the identifier against value is a valid value, then check whether the receiver type equals to identifier
type. If there is no identifier, filter out functions without the receiver
*/
List<SymbolInfo> filteredSymbols = new ArrayList<>();
visibleSymbols.forEach(symbolInfo -> {
BVarSymbol receiver = ((BInvokableSymbol) symbolInfo.getScopeEntry().symbol).receiverSymbol;
String[] nameTokens = symbolInfo.getSymbolName().split("\\.");
String funcNameFromSymbol = nameTokens[nameTokens.length - 1];
String functionName = documentServiceContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
String identifierPkgName = documentServiceContext.get(SignatureKeys.IDENTIFIER_PKGID);
boolean onIdentifierTypePkg = "package".equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE)) && symbolInfo.getScopeEntry().symbol.pkgID.toString().equals(identifierPkgName);
boolean onReceiverTypeMatchIdentifier = receiver != null && receiver.type.toString().equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE));
boolean onIdentifierAgainstNull = (receiver == null && (identifierAgainst == null || identifierAgainst.equals("")));
if ((onIdentifierTypePkg || onReceiverTypeMatchIdentifier || onIdentifierAgainstNull) && funcNameFromSymbol.equals(functionName)) {
filteredSymbols.add(symbolInfo);
}
});
documentServiceContext.put(SignatureKeys.FILTERED_FUNCTIONS, filteredSymbols);
}
Aggregations