Search in sources :

Example 6 with AssignmentStatementNode

use of org.ballerinalang.plugins.idea.psi.AssignmentStatementNode in project ballerina by ballerina-lang.

the class UnusedVariableInspection method checkFile.

@Override
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    List<ProblemDescriptor> problemDescriptors = new LinkedList<>();
    List<LocalQuickFix> availableFixes = new ArrayList<>();
    Collection<VariableDefinitionNode> variables = PsiTreeUtil.findChildrenOfType(file, VariableDefinitionNode.class);
    for (VariableDefinitionNode variable : variables) {
        ProgressManager.checkCanceled();
        PsiElement identifier = variable.getNameIdentifier();
        if (identifier == null) {
            continue;
        }
        Query<PsiReference> psiReferences = ReferencesSearch.search(identifier);
        PsiReference firstReference = psiReferences.findFirst();
        if (firstReference == null) {
            ProblemDescriptor problemDescriptor = getProblemDescriptor(manager, isOnTheFly, identifier, availableFixes);
            problemDescriptors.add(problemDescriptor);
        }
    }
    Collection<AssignmentStatementNode> assignmentStatementNodes = PsiTreeUtil.findChildrenOfType(file, AssignmentStatementNode.class);
    for (AssignmentStatementNode assignmentStatementNode : assignmentStatementNodes) {
        ProgressManager.checkCanceled();
        boolean isVarAssignment = BallerinaPsiImplUtil.isVarAssignmentStatement(assignmentStatementNode);
        if (isVarAssignment) {
            List<IdentifierPSINode> identifiers = BallerinaPsiImplUtil.getVariablesFromVarAssignment(assignmentStatementNode);
            for (IdentifierPSINode identifier : identifiers) {
                if (BallerinaPsiImplUtil.isRedeclaredVar(identifier)) {
                    continue;
                }
                Query<PsiReference> psiReferences = ReferencesSearch.search(identifier);
                PsiReference firstReference = psiReferences.findFirst();
                if (firstReference == null && !"_".equals(identifier.getText())) {
                    ProblemDescriptor problemDescriptor = getProblemDescriptor(manager, isOnTheFly, identifier, availableFixes);
                    problemDescriptors.add(problemDescriptor);
                }
            }
        }
    }
    return problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
Also used : AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiReference(com.intellij.psi.PsiReference) LinkedList(java.util.LinkedList) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with AssignmentStatementNode

use of org.ballerinalang.plugins.idea.psi.AssignmentStatementNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method isValidVarAssignment.

private static boolean isValidVarAssignment(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull ScopeNode scope, int caretOffset) {
    PsiElement elementAtCaret = scope.getContainingFile().findElementAt(caretOffset);
    if (elementAtCaret == null) {
        return false;
    }
    // Todo - temp fix. Might need to add {, }, etc.
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(elementAtCaret);
    if (prevVisibleLeaf != null && !";".equals(prevVisibleLeaf.getText())) {
        AssignmentStatementNode parent = PsiTreeUtil.getParentOfType(prevVisibleLeaf, AssignmentStatementNode.class);
        if (parent != null && assignmentStatementNode.equals(parent)) {
            return false;
        }
    }
    if (!isVarAssignmentStatement(assignmentStatementNode)) {
        return false;
    }
    return assignmentStatementNode.getTextRange().getEndOffset() < caretOffset;
}
Also used : AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 8 with AssignmentStatementNode

use of org.ballerinalang.plugins.idea.psi.AssignmentStatementNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getAllLocalVariablesInScope.

/**
 * Returns all local variables in the provided scope.
 *
 * @param scope
 * @param caretOffset
 * @return
 */
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInScope(@NotNull ScopeNode scope, int caretOffset) {
    List<IdentifierPSINode> results = new LinkedList<>();
    Collection<VariableDefinitionNode> variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, VariableDefinitionNode.class);
    for (VariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
        if (isValidVariable(variableDefinitionNode, scope, caretOffset)) {
            PsiElement identifier = variableDefinitionNode.getNameIdentifier();
            if (identifier != null && identifier instanceof IdentifierPSINode) {
                results.add(((IdentifierPSINode) identifier));
            }
        }
    }
    if (scope instanceof TransformerDefinitionNode) {
        Collection<ExpressionVariableDefinitionStatementNode> nodes = PsiTreeUtil.findChildrenOfType(scope, ExpressionVariableDefinitionStatementNode.class);
        for (ExpressionVariableDefinitionStatementNode node : nodes) {
            ScopeNode closestScope = PsiTreeUtil.getParentOfType(node, TransformerDefinitionNode.class);
            if (closestScope == null || !closestScope.equals(scope)) {
                continue;
            }
            PsiElement identifier = node.getNameIdentifier();
            if (identifier != null && identifier instanceof IdentifierPSINode) {
                results.add(((IdentifierPSINode) identifier));
            }
        }
    }
    if (scope instanceof ForEachStatementNode) {
        VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(scope, VariableReferenceListNode.class);
        if (variableReferenceListNode != null) {
            List<VariableReferenceNode> variableReferenceNodes = PsiTreeUtil.getChildrenOfTypeAsList(variableReferenceListNode, VariableReferenceNode.class);
            for (VariableReferenceNode variableReferenceNode : variableReferenceNodes) {
                if (variableReferenceNode != null) {
                    IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(variableReferenceNode, IdentifierPSINode.class);
                    if (identifier != null) {
                        results.add(identifier);
                    }
                }
            }
        }
    }
    Collection<AssignmentStatementNode> assignmentStatementNodes = PsiTreeUtil.findChildrenOfType(scope, AssignmentStatementNode.class);
    for (AssignmentStatementNode assignmentStatementNode : assignmentStatementNodes) {
        if (isValidVarAssignment(assignmentStatementNode, scope, caretOffset)) {
            results.addAll(getVariablesFromVarAssignment(assignmentStatementNode));
        }
    }
    return results;
}
Also used : TransformerDefinitionNode(org.ballerinalang.plugins.idea.psi.TransformerDefinitionNode) AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) ExpressionVariableDefinitionStatementNode(org.ballerinalang.plugins.idea.psi.ExpressionVariableDefinitionStatementNode) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) VariableReferenceListNode(org.ballerinalang.plugins.idea.psi.VariableReferenceListNode) LinkedList(java.util.LinkedList) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) ForEachStatementNode(org.ballerinalang.plugins.idea.psi.ForEachStatementNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with AssignmentStatementNode

use of org.ballerinalang.plugins.idea.psi.AssignmentStatementNode in project ballerina by ballerina-lang.

the class FieldReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    IdentifierPSINode identifier = getElement();
    PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
    // Todo - remove hard coded "."
    if (prevVisibleLeaf == null || !".".equals(prevVisibleLeaf.getText())) {
        return new LookupElement[0];
    }
    PsiElement previousField = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
    if (previousField == null) {
        return new LookupElement[0];
    }
    PsiReference reference = previousField.findReferenceAt(0);
    if (reference == null) {
        PsiElement prevSibling = identifier.getParent().getPrevSibling();
        if (prevSibling == null) {
            return new LookupElement[0];
        }
        if (prevSibling instanceof VariableReferenceNode) {
            PsiElement[] children = prevSibling.getChildren();
            if (children.length <= 0) {
                return new LookupElement[0];
            }
            PsiElement firstChild = children[0].getFirstChild();
            if (firstChild == null) {
                return new LookupElement[0];
            }
            PsiReference functionReference = firstChild.findReferenceAt(firstChild.getTextLength());
            if (functionReference == null) {
                return new LookupElement[0];
            }
            PsiElement resolvedElement = functionReference.resolve();
            if (resolvedElement == null) {
                return new LookupElement[0];
            }
            PsiElement parent = resolvedElement.getParent();
            if (parent instanceof FunctionDefinitionNode) {
                List<TypeNameNode> returnTypes = BallerinaPsiImplUtil.getReturnTypes(((FunctionDefinitionNode) parent));
                if (returnTypes.size() == 1) {
                    TypeNameNode typeNameNode = returnTypes.get(0);
                    List<LookupElement> functions = Arrays.asList((LookupElement[]) TypeReference.getVariants(typeNameNode));
                    return functions.toArray(new LookupElement[functions.size()]);
                }
            }
        }
    }
    if (reference == null) {
        return new LookupElement[0];
    }
    // Todo - use util method
    PsiElement resolvedElement = reference.resolve();
    if (resolvedElement == null || !(resolvedElement instanceof IdentifierPSINode)) {
        return new LookupElement[0];
    }
    PsiElement resolvedElementParent = resolvedElement.getParent();
    StructDefinitionNode structDefinitionNode = null;
    List<LookupElement> results = new LinkedList<>();
    // Resolve the corresponding resolvedElementParent to get the struct definition.
    if (resolvedElementParent instanceof VariableDefinitionNode || resolvedElementParent instanceof CodeBlockParameterNode || resolvedElementParent instanceof ParameterNode) {
        structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(resolvedElementParent);
    } else if (resolvedElementParent instanceof FieldDefinitionNode) {
        structDefinitionNode = BallerinaPsiImplUtil.resolveTypeNodeStruct((resolvedElementParent));
    } else if (resolvedElementParent instanceof NameReferenceNode) {
        AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(resolvedElement, AssignmentStatementNode.class);
        if (assignmentStatementNode != null) {
            structDefinitionNode = BallerinaPsiImplUtil.getStructDefinition(assignmentStatementNode, ((IdentifierPSINode) resolvedElement));
        } else {
            structDefinitionNode = BallerinaPsiImplUtil.findStructDefinition((IdentifierPSINode) resolvedElement);
        }
        if (structDefinitionNode != null) {
            IdentifierPSINode structName = PsiTreeUtil.findChildOfType(structDefinitionNode, IdentifierPSINode.class);
            if (structName != null) {
                resolvedElement = structName;
            }
        }
    } else if (resolvedElementParent instanceof EnumDefinitionNode) {
        Collection<EnumFieldNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(resolvedElementParent, EnumFieldNode.class);
        results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement));
        return results.toArray(new LookupElement[results.size()]);
    } else if (resolvedElementParent instanceof StructDefinitionNode) {
        structDefinitionNode = ((StructDefinitionNode) resolvedElementParent);
    }
    if (structDefinitionNode == null) {
        return new LookupElement[0];
    }
    Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
    results.addAll(BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, null));
    List<IdentifierPSINode> attachedFunctions = BallerinaPsiImplUtil.getAttachedFunctions(structDefinitionNode);
    results.addAll(BallerinaCompletionUtils.createAttachedFunctionsLookupElements(attachedFunctions));
    return results.toArray(new LookupElement[results.size()]);
}
Also used : AssignmentStatementNode(org.ballerinalang.plugins.idea.psi.AssignmentStatementNode) PsiReference(com.intellij.psi.PsiReference) VariableReferenceNode(org.ballerinalang.plugins.idea.psi.VariableReferenceNode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) LinkedList(java.util.LinkedList) VariableDefinitionNode(org.ballerinalang.plugins.idea.psi.VariableDefinitionNode) EnumFieldNode(org.ballerinalang.plugins.idea.psi.EnumFieldNode) ParameterNode(org.ballerinalang.plugins.idea.psi.ParameterNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) StructDefinitionNode(org.ballerinalang.plugins.idea.psi.StructDefinitionNode) TypeNameNode(org.ballerinalang.plugins.idea.psi.TypeNameNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) Collection(java.util.Collection) FieldDefinitionNode(org.ballerinalang.plugins.idea.psi.FieldDefinitionNode) EnumDefinitionNode(org.ballerinalang.plugins.idea.psi.EnumDefinitionNode) PsiElement(com.intellij.psi.PsiElement) NameReferenceNode(org.ballerinalang.plugins.idea.psi.NameReferenceNode) CodeBlockParameterNode(org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AssignmentStatementNode (org.ballerinalang.plugins.idea.psi.AssignmentStatementNode)9 PsiElement (com.intellij.psi.PsiElement)7 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)7 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)5 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)5 PsiReference (com.intellij.psi.PsiReference)4 LinkedList (java.util.LinkedList)4 CodeBlockParameterNode (org.ballerinalang.plugins.idea.psi.CodeBlockParameterNode)4 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)4 ParameterNode (org.ballerinalang.plugins.idea.psi.ParameterNode)4 Nullable (org.jetbrains.annotations.Nullable)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 AnonStructTypeNameNode (org.ballerinalang.plugins.idea.psi.AnonStructTypeNameNode)3 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)3 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)3 ReturnParameterNode (org.ballerinalang.plugins.idea.psi.ReturnParameterNode)3 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)3 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)3 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)2 BuiltInReferenceTypeNameNode (org.ballerinalang.plugins.idea.psi.BuiltInReferenceTypeNameNode)2