Search in sources :

Example 1 with StatementTemplateFilter

use of org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter in project ballerina by ballerina-lang.

the class DefaultResolver method resolveItems.

@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    CompletionItem workerItem = new CompletionItem();
    workerItem.setLabel(ItemResolverConstants.WORKER);
    workerItem.setInsertText(Snippet.WORKER.toString());
    workerItem.setInsertTextFormat(InsertTextFormat.Snippet);
    workerItem.setDetail(ItemResolverConstants.WORKER_TYPE);
    completionItems.add(workerItem);
    populateCompletionItemList(completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY), completionItems);
    // Add the statement templates
    StatementTemplateFilter statementTemplateFilter = new StatementTemplateFilter();
    completionItems.addAll(statementTemplateFilter.filterItems(completionContext));
    return completionItems;
}
Also used : CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) StatementTemplateFilter(org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter)

Example 2 with StatementTemplateFilter

use of org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter in project ballerina by ballerina-lang.

the class ParserRuleTypeNameContextResolver method resolveItems.

@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    ParserRuleContext parserRuleContext = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
    CompletionItemSorter itemSorter = ItemSorters.getSorterByClass(DefaultItemSorter.class);
    if (parserRuleContext.getParent() instanceof BallerinaParser.CatchClauseContext && CommonUtil.isWithinBrackets(completionContext, Collections.singletonList(CATCH_KEY_WORD))) {
        this.populateCompletionItemList(filterCatchConditionSymbolInfo(completionContext), completionItems);
    } else if (tokenStream.get(completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY)).getText().equals(":")) {
        /*
            TODO: ATM, this particular condition becomes true only when try to access packages' items in the 
            endpoint definition context
             */
        this.populateCompletionItemList(filterEndpointContextSymbolInfo(completionContext), completionItems);
    } else {
        StatementTemplateFilter statementTemplateFilter = new StatementTemplateFilter();
        // Add the statement templates
        completionItems.addAll(statementTemplateFilter.filterItems(completionContext));
        this.populateBasicTypes(completionItems, completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY));
        itemSorter = ItemSorters.getSorterByClass(completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY).getClass());
    }
    itemSorter.sortItems(completionContext, completionItems);
    return completionItems;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) TokenStream(org.antlr.v4.runtime.TokenStream) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) CompletionItemSorter(org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter) StatementTemplateFilter(org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser)

Example 3 with StatementTemplateFilter

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

Aggregations

ArrayList (java.util.ArrayList)3 StatementTemplateFilter (org.ballerinalang.langserver.completions.util.filters.StatementTemplateFilter)3 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 TokenStream (org.antlr.v4.runtime.TokenStream)1 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)1 PackageActionFunctionAndTypesFilter (org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter)1 CompletionItemSorter (org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter)1 BallerinaParser (org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser)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