Search in sources :

Example 21 with SymbolInfo

use of org.ballerinalang.langserver.completions.SymbolInfo in project ballerina by ballerina-lang.

the class GlobalScopeResolver method resolveItems.

@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    ParserRuleContext parserRuleContext = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
    if (parserRuleContext == null) {
        // If the parser rule context is null we don't have any errors. In this case we add the types
        List<SymbolInfo> bTypeSymbolInfo = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY).stream().filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.type != null).collect(Collectors.toList());
        this.populateCompletionItemList(bTypeSymbolInfo, completionItems);
    } else {
        return CompletionItemResolver.getResolverByClass(parserRuleContext.getClass()).resolveItems(completionContext);
    }
    return completionItems;
}
Also used : CompletionItem(org.eclipse.lsp4j.CompletionItem) List(java.util.List) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) Collectors(java.util.stream.Collectors) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) CompletionItemResolver(org.ballerinalang.langserver.completions.util.CompletionItemResolver) ArrayList(java.util.ArrayList) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Example 22 with SymbolInfo

use of org.ballerinalang.langserver.completions.SymbolInfo 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);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ArrayList(java.util.ArrayList) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Aggregations

SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)22 ArrayList (java.util.ArrayList)12 CompletionItem (org.eclipse.lsp4j.CompletionItem)7 List (java.util.List)6 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)6 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)6 TokenStream (org.antlr.v4.runtime.TokenStream)5 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)5 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)5 Collectors (java.util.stream.Collectors)4 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)4 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)4 CompletionKeys (org.ballerinalang.langserver.completions.CompletionKeys)4 Scope (org.wso2.ballerinalang.compiler.semantics.model.Scope)4 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)4 Token (org.antlr.v4.runtime.Token)3 ItemResolverConstants (org.ballerinalang.langserver.completions.util.ItemResolverConstants)3 PackageActionFunctionAndTypesFilter (org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter)3 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)3 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)3