Search in sources :

Example 26 with BLangVariableDef

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

the class ServiceScopeResolver method isWithinScopeAfterLastChildNode.

/**
 * Check whether the given node is within the scope and located after the last child node.
 * @param node          Current Node to evaluate
 * @param treeVisitor   Operation Tree Visitor
 * @param curLine       line of the cursor
 * @param curCol        column of the cursor
 * @return              {@link Boolean} whether the last child node or not
 */
protected boolean isWithinScopeAfterLastChildNode(Node node, TreeVisitor treeVisitor, int curLine, int curCol) {
    BLangService bLangService = (BLangService) treeVisitor.getBlockOwnerStack().peek();
    List<BLangResource> resources = bLangService.resources;
    List<BLangVariableDef> variableDefs = bLangService.vars;
    int serviceEndLine = bLangService.pos.getEndLine();
    int serviceEndCol = bLangService.pos.getEndColumn();
    int nodeEndLine = node.getPosition().getEndLine();
    int nodeEndCol = node.getPosition().getEndColumn();
    boolean isLastChildNode;
    if (resources.isEmpty()) {
        isLastChildNode = variableDefs.indexOf(node) == (variableDefs.size() - 1);
    } else {
        isLastChildNode = resources.indexOf(node) == (resources.size() - 1);
    }
    boolean isWithinScope = (isLastChildNode && (curLine < serviceEndLine || (curLine == serviceEndLine && curCol < serviceEndCol)) && (nodeEndLine < curLine || (nodeEndLine == curLine && nodeEndCol < curCol)));
    if (isWithinScope) {
        treeVisitor.setPreviousNode((BLangNode) node);
    }
    return isWithinScope;
}
Also used : BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)

Example 27 with BLangVariableDef

use of org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef 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 28 with BLangVariableDef

use of org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef 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)

Aggregations

BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)27 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)14 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)10 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)5 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)5 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)4 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)4 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)3 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)3 InvokableNode (org.ballerinalang.model.tree.InvokableNode)2 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)2 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)2 BLangMatch (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch)2 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)2 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)2