Search in sources :

Example 26 with BLangNode

use of org.wso2.ballerinalang.compiler.tree.BLangNode in project ballerina by ballerina-lang.

the class CallableUnitBodyItemSorter method sortItems.

@Override
public void sortItems(TextDocumentServiceContext ctx, List<CompletionItem> completionItems) {
    BLangNode previousNode = ctx.get(CompletionKeys.PREVIOUS_NODE_KEY);
    TokenStream tokenStream = ctx.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    if (ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY) != null) {
        int currentTokenStart = ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY).getStart().getTokenIndex();
        Token nextToken = tokenStream.get(currentTokenStart + 1);
        int cursorLine = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
        int cursorChar = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();
        if (nextToken.getChannel() != Token.DEFAULT_CHANNEL && (cursorLine > nextToken.getLine() - 1 || (cursorLine == nextToken.getLine() - 1 && cursorChar > nextToken.getCharPositionInLine()))) {
            completionItems.clear();
            return;
        }
    }
    this.clearItemsIfWorkerExists(ctx, completionItems);
    if (previousNode == null) {
        this.populateWhenCursorBeforeOrAfterEp(completionItems);
    } else if (previousNode instanceof BLangVariableDef) {
        // } else
        if (ctx.get(CompletionKeys.INVOCATION_STATEMENT_KEY) == null || !ctx.get(CompletionKeys.INVOCATION_STATEMENT_KEY)) {
            CompletionItem workerItem = this.getWorkerSnippet();
            workerItem.setSortText(Priority.PRIORITY160.toString());
            completionItems.add(workerItem);
        }
    } else if (previousNode instanceof BLangWorker) {
        completionItems.add(this.getWorkerSnippet());
    }
    this.setPriorities(completionItems);
}
Also used : BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) TokenStream(org.antlr.v4.runtime.TokenStream) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) CompletionItem(org.eclipse.lsp4j.CompletionItem) Token(org.antlr.v4.runtime.Token) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)

Example 27 with BLangNode

use of org.wso2.ballerinalang.compiler.tree.BLangNode in project ballerina by ballerina-lang.

the class ConnectorContextItemSorter method sortItems.

@Override
public void sortItems(TextDocumentServiceContext ctx, List<CompletionItem> completionItems) {
    BLangNode previousNode = ctx.get(CompletionKeys.PREVIOUS_NODE_KEY);
    /*
        Remove the statement type completion type. When the going through the parser
        rule contexts such as typeNameContext, we add the statements as well.
        Sorters are responsible for to the next level of such filtering.
         */
    this.removeCompletionsByType(new ArrayList<>(Collections.singletonList(ItemResolverConstants.STATEMENT_TYPE)), completionItems);
    if (previousNode == null) {
        this.populateWhenCursorBeforeOrAfterEp(completionItems);
    } else if (previousNode instanceof BLangVariableDef) {
        // BType bLangType = ((BLangVariableDef) previousNode).var.type;
        // if (bLangType instanceof BEndpointType) {
        // this.populateWhenCursorBeforeOrAfterEp(completionItems);
        // } else {
        this.setPriorities(completionItems);
        CompletionItem resItem = this.getActionSnippet();
        resItem.setSortText(Priority.PRIORITY160.toString());
        completionItems.add(resItem);
    // }
    } else if (previousNode instanceof BLangAction) {
        completionItems.clear();
        completionItems.add(this.getActionSnippet());
    }
}
Also used : BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionItem(org.eclipse.lsp4j.CompletionItem) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction)

Example 28 with BLangNode

use of org.wso2.ballerinalang.compiler.tree.BLangNode in project ballerina by ballerina-lang.

the class SignatureTreeVisitor method acceptNode.

// Private Methods
private void acceptNode(BLangNode node, SymbolEnv env) {
    if (this.terminateVisitor) {
        return;
    }
    SymbolEnv prevEnv = this.symbolEnv;
    this.symbolEnv = env;
    node.accept(this);
    this.symbolEnv = prevEnv;
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 29 with BLangNode

use of org.wso2.ballerinalang.compiler.tree.BLangNode in project ballerina by ballerina-lang.

the class SignatureTreeVisitor method visit.

@Override
public void visit(BLangPackage pkgNode) {
    SymbolEnv pkgEnv = symTable.pkgEnvMap.get(pkgNode.symbol);
    // Then visit each top-level element sorted using the compilation unit
    String fileName = documentServiceContext.get(DocumentServiceKeys.FILE_NAME_KEY);
    BLangCompilationUnit compilationUnit = pkgNode.getCompilationUnits().stream().filter(bLangCompilationUnit -> bLangCompilationUnit.getName().equals(fileName)).findFirst().orElse(null);
    List<TopLevelNode> topLevelNodes = compilationUnit.getTopLevelNodes();
    if (!topLevelNodes.isEmpty()) {
        topLevelNodes.forEach(topLevelNode -> acceptNode((BLangNode) topLevelNode, pkgEnv));
    }
}
Also used : BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 30 with BLangNode

use of org.wso2.ballerinalang.compiler.tree.BLangNode in project ballerina by ballerina-lang.

the class CompletionTestUtil method getCompletions.

/**
 * Get the completions list.
 *
 * @param documentManager Document manager instance
 * @param pos             {@link TextDocumentPositionParams} position params
 */
public static List<CompletionItem> getCompletions(WorkspaceDocumentManagerImpl documentManager, TextDocumentPositionParams pos) {
    List<CompletionItem> completions;
    TextDocumentServiceContext completionContext = new TextDocumentServiceContext();
    completionContext.put(DocumentServiceKeys.POSITION_KEY, pos);
    completionContext.put(DocumentServiceKeys.FILE_URI_KEY, pos.getTextDocument().getUri());
    BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(completionContext, documentManager, false, CompletionCustomErrorStrategy.class, false).get(0);
    completionContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
    // Visit the package to resolve the symbols
    TreeVisitor treeVisitor = new TreeVisitor(completionContext);
    bLangPackage.accept(treeVisitor);
    BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
    if (symbolEnvNode == null) {
        completions = CompletionItemResolver.getResolverByClass(TopLevelResolver.class).resolveItems(completionContext);
    } else {
        completions = CompletionItemResolver.getResolverByClass(symbolEnvNode.getClass()).resolveItems(completionContext);
    }
    return completions;
}
Also used : TreeVisitor(org.ballerinalang.langserver.completions.TreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionCustomErrorStrategy(org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Aggregations

BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)17 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)13 ArrayList (java.util.ArrayList)7 CompletionItem (org.eclipse.lsp4j.CompletionItem)7 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)7 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)5 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)5 List (java.util.List)4 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)4 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)3 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)3 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)3 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Stack (java.util.Stack)2 Collectors (java.util.stream.Collectors)2 Token (org.antlr.v4.runtime.Token)2 TokenStream (org.antlr.v4.runtime.TokenStream)2