Search in sources :

Example 1 with ConnectorInitExpressionItemFilter

use of org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter 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)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)1 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)1 CompletionKeys (org.ballerinalang.langserver.completions.CompletionKeys)1 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)1 AbstractItemResolver (org.ballerinalang.langserver.completions.resolvers.AbstractItemResolver)1 ItemResolverConstants (org.ballerinalang.langserver.completions.util.ItemResolverConstants)1 Snippet (org.ballerinalang.langserver.completions.util.Snippet)1 ConnectorInitExpressionItemFilter (org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter)1 PackageActionFunctionAndTypesFilter (org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter)1 CompletionItemSorter (org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter)1 ItemSorters (org.ballerinalang.langserver.completions.util.sorters.ItemSorters)1 SymbolKind (org.ballerinalang.model.symbols.SymbolKind)1 CompletionItem (org.eclipse.lsp4j.CompletionItem)1 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)1 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)1