Search in sources :

Example 21 with BLangNode

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

the class DefinitionUtil method getDefinitionPosition.

/**
 * Get definition position for the given definition context.
 *
 * @param definitionContext   context of the definition.
 * @param lSPackageCache package context for language server.
 * @return position
 */
public static List<Location> getDefinitionPosition(TextDocumentServiceContext definitionContext, LSPackageCache lSPackageCache) {
    List<Location> contents = new ArrayList<>();
    if (definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY) == null) {
        return contents;
    }
    String nodeKind = definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY);
    BLangPackage bLangPackage = getPackageOfTheOwner(definitionContext.get(NodeContextKeys.NODE_OWNER_PACKAGE_KEY), definitionContext, lSPackageCache);
    BLangNode bLangNode = null;
    switch(nodeKind) {
        case ContextConstants.FUNCTION:
            bLangNode = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.STRUCT:
            bLangNode = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.OBJECT:
            bLangNode = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ENUM:
            bLangNode = bLangPackage.enums.stream().filter(enm -> enm.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            // Fixing the position issue with enum node.
            bLangNode.getPosition().eLine = bLangNode.getPosition().sLine;
            bLangNode.getPosition().eCol = bLangNode.getPosition().sCol;
            break;
        case ContextConstants.CONNECTOR:
            bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ACTION:
            bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) definitionContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.TRANSFORMER:
            bLangNode = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ENDPOINT:
            bLangNode = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangNode == null) {
                DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
                definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
                if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
                    bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
                }
            }
            break;
        case ContextConstants.VARIABLE:
            bLangNode = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(definitionContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
            // BLangNode is null only when node at the cursor position is a local variable.
            if (bLangNode == null) {
                DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
                definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
                if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
                    bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
                    break;
                }
            }
            break;
        default:
            break;
    }
    if (bLangNode == null) {
        return contents;
    }
    Location l = new Location();
    TextDocumentPositionParams position = definitionContext.get(DocumentServiceKeys.POSITION_KEY);
    Path parentPath = CommonUtil.getPath(new LSDocument(position.getTextDocument().getUri())).getParent();
    if (parentPath != null) {
        String fileName = bLangNode.getPosition().getSource().getCompilationUnitName();
        Path filePath = Paths.get(CommonUtil.getPackageURI(definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue(), parentPath.toString(), definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue()), fileName);
        l.setUri(filePath.toUri().toString());
        Range r = new Range();
        // Subtract 1 to convert the token lines and char positions to zero based indexing
        r.setStart(new Position(bLangNode.getPosition().getStartLine() - 1, bLangNode.getPosition().getStartColumn() - 1));
        r.setEnd(new Position(bLangNode.getPosition().getEndLine() - 1, bLangNode.getPosition().getEndColumn() - 1));
        l.setRange(r);
        contents.add(l);
    }
    return contents;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) PackageID(org.ballerinalang.model.elements.PackageID) Range(org.eclipse.lsp4j.Range) LSPackageCache(org.ballerinalang.langserver.LSPackageCache) NodeContextKeys(org.ballerinalang.langserver.common.constants.NodeContextKeys) ArrayList(java.util.ArrayList) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) DefinitionTreeVisitor(org.ballerinalang.langserver.definition.DefinitionTreeVisitor) LSDocument(org.ballerinalang.langserver.common.LSDocument) List(java.util.List) Paths(java.nio.file.Paths) ContextConstants(org.ballerinalang.langserver.common.constants.ContextConstants) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Location(org.eclipse.lsp4j.Location) Position(org.eclipse.lsp4j.Position) Path(java.nio.file.Path) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) Path(java.nio.file.Path) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) DefinitionTreeVisitor(org.ballerinalang.langserver.definition.DefinitionTreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) LSDocument(org.ballerinalang.langserver.common.LSDocument) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Location(org.eclipse.lsp4j.Location)

Example 22 with BLangNode

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

the class BlockStatementScopeResolver method isWithinScopeAfterLastChildNode.

private boolean isWithinScopeAfterLastChildNode(TreeVisitor treeVisitor, boolean lastChild, int nodeELine, int nodeECol, int line, int col, Node node) {
    if (!lastChild) {
        return false;
    } else {
        BLangBlockStmt bLangBlockStmt = treeVisitor.getBlockStmtStack().peek();
        Node blockOwner = treeVisitor.getBlockOwnerStack().peek();
        int blockOwnerELine = this.getBlockOwnerELine(blockOwner, bLangBlockStmt);
        int blockOwnerECol = this.getBlockOwnerECol(blockOwner, bLangBlockStmt);
        boolean isWithinScope = (line < blockOwnerELine || (line == blockOwnerELine && col <= blockOwnerECol)) && (line > nodeELine || (line == nodeELine && col > nodeECol));
        if (isWithinScope) {
            treeVisitor.setPreviousNode((BLangNode) node);
        }
        return isWithinScope;
    }
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) Node(org.ballerinalang.model.tree.Node)

Example 23 with BLangNode

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

the class ConnectorScopeResolver 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) {
    BLangConnector bLangConnector = (BLangConnector) treeVisitor.getBlockOwnerStack().peek();
    List<BLangAction> actions = bLangConnector.actions;
    List<BLangVariableDef> variableDefs = bLangConnector.varDefs;
    int connectorEndLine = bLangConnector.pos.getEndLine();
    int connectorEndCol = bLangConnector.pos.getEndColumn();
    int nodeEndLine = node.getPosition().getEndLine();
    int nodeEndCol = node.getPosition().getEndColumn();
    boolean isLastChildNode;
    if (actions.isEmpty()) {
        isLastChildNode = variableDefs.indexOf(node) == (variableDefs.size() - 1);
    } else {
        isLastChildNode = actions.indexOf(node) == (actions.size() - 1);
    }
    boolean isWithinScope = (isLastChildNode && (curLine < connectorEndLine || (curLine == connectorEndLine && curCol < connectorEndCol)) && (nodeEndLine < curLine || (nodeEndLine == curLine && nodeEndCol < curCol)));
    if (isWithinScope) {
        treeVisitor.setPreviousNode((BLangNode) node);
    }
    return isWithinScope;
}
Also used : BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction)

Example 24 with BLangNode

use of org.wso2.ballerinalang.compiler.tree.BLangNode 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 25 with BLangNode

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

the class BLangEndpointContextResolver method resolveItems.

@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    BLangNode bLangEndpoint = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    ArrayList<SymbolInfo> configurationFields = new ArrayList<>();
    List<BStructSymbol.BAttachedFunction> attachedFunctions = new ArrayList<>();
    if (((BLangEndpoint) bLangEndpoint).type.tsymbol instanceof BStructSymbol) {
        attachedFunctions.addAll(((BStructSymbol) ((BLangEndpoint) bLangEndpoint).type.tsymbol).attachedFuncs);
    }
    BStructSymbol.BAttachedFunction initFunction = attachedFunctions.stream().filter(bAttachedFunction -> bAttachedFunction.funcName.getValue().equals(INIT)).findFirst().orElseGet(null);
    BVarSymbol configSymbol = initFunction.symbol.getParameters().get(0);
    BType configSymbolType = configSymbol.getType();
    if (configSymbolType instanceof BStructType) {
        ((BStructType) configSymbolType).getFields().forEach(bStructField -> configurationFields.add(new SymbolInfo(bStructField.getName().getValue(), new Scope.ScopeEntry(bStructField.symbol, null))));
    }
    this.populateCompletionItemList(configurationFields, completionItems);
    return completionItems;
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) ArrayList(java.util.ArrayList) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) CompletionItem(org.eclipse.lsp4j.CompletionItem) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

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