Search in sources :

Example 6 with SymbolInfo

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;
}
Also used : SignatureInformation(org.eclipse.lsp4j.SignatureInformation) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Example 7 with SymbolInfo

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;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) ArrayList(java.util.ArrayList) StatementTemplateFilter(org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) PackageActionFunctionAndTypesFilter(org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Example 8 with SymbolInfo

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 "";
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Example 9 with SymbolInfo

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;
}
Also used : ConnectorInitExpressionItemFilter(org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter) CompletionItemSorter(org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) ItemResolverConstants(org.ballerinalang.langserver.completions.util.ItemResolverConstants) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ItemSorters(org.ballerinalang.langserver.completions.util.sorters.ItemSorters) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CompletionItem(org.eclipse.lsp4j.CompletionItem) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) List(java.util.List) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) Snippet(org.ballerinalang.langserver.completions.util.Snippet) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) AbstractItemResolver(org.ballerinalang.langserver.completions.resolvers.AbstractItemResolver) PackageActionFunctionAndTypesFilter(org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) ArrayList(java.util.ArrayList) CompletionItemSorter(org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ConnectorInitExpressionItemFilter(org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter) PackageActionFunctionAndTypesFilter(org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Example 10 with SymbolInfo

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;
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) 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