Search in sources :

Example 11 with ResolveResult

use of com.intellij.psi.ResolveResult in project intellij-plugins by JetBrains.

the class AngularIndexUtil method multiResolveAngularNamedDefinitionIndex.

public static ResolveResult[] multiResolveAngularNamedDefinitionIndex(@NotNull final Project project, @NotNull final ID<String, AngularNamedItemDefinition> INDEX, @NotNull final String id, @NotNull final Condition<VirtualFile> filter, boolean dirtyResolve) {
    final FileBasedIndex instance = FileBasedIndex.getInstance();
    Collection<VirtualFile> files = instance.getContainingFiles(INDEX, id, GlobalSearchScope.allScope(project));
    if (files.isEmpty())
        return ResolveResult.EMPTY_ARRAY;
    final List<VirtualFile> filtered = ContainerUtil.filter(files, filter);
    if (filtered.isEmpty()) {
        if (!dirtyResolve)
            return ResolveResult.EMPTY_ARRAY;
    } else {
        files = filtered;
    }
    final List<JSImplicitElement> elements = new ArrayList<>();
    for (VirtualFile file : files) {
        final List<AngularNamedItemDefinition> values = instance.getValues(INDEX, id, GlobalSearchScope.fileScope(project, file));
        for (AngularNamedItemDefinition value : values) {
            JSQualifiedNameImpl qName = JSQualifiedNameImpl.fromQualifiedName(id);
            JSImplicitElementImpl.Builder elementBuilder = new JSImplicitElementImpl.Builder(qName, null);
            final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
            if (psiFile != null) {
                elements.add(new JSOffsetBasedImplicitElement(elementBuilder, (int) value.getStartOffset(), psiFile));
            }
        }
    }
    final List<ResolveResult> list = ContainerUtil.map(elements, JS_IMPLICIT_TO_RESOLVE_RESULT);
    return list.toArray(new ResolveResult[list.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JSQualifiedNameImpl(com.intellij.lang.javascript.psi.JSQualifiedNameImpl) JSImplicitElementImpl(com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) JSOffsetBasedImplicitElement(com.intellij.lang.javascript.psi.impl.JSOffsetBasedImplicitElement) JSImplicitElement(com.intellij.lang.javascript.psi.stubs.JSImplicitElement) JSResolveResult(com.intellij.lang.javascript.psi.resolve.JSResolveResult) ResolveResult(com.intellij.psi.ResolveResult) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Example 12 with ResolveResult

use of com.intellij.psi.ResolveResult in project intellij-elixir by KronicDeth.

the class ImportTest method testImportModule.

/*
     * Tests
     */
public void testImportModule() {
    myFixture.configureByFiles("import_module.ex", "imported.ex");
    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    assertNotNull(elementAtCaret);
    PsiElement maybeCall = elementAtCaret.getParent().getParent();
    assertInstanceOf(maybeCall, Call.class);
    Call call = (Call) maybeCall;
    assertEquals("imported", call.functionName());
    assertEquals(0, call.resolvedFinalArity());
    PsiReference reference = call.getReference();
    assertNotNull(reference);
    assertInstanceOf(reference, PsiPolyVariantReference.class);
    PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) reference;
    ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
    assertEquals(2, resolveResults.length);
}
Also used : Call(org.elixir_lang.psi.call.Call) PsiReference(com.intellij.psi.PsiReference) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference)

Example 13 with ResolveResult

use of com.intellij.psi.ResolveResult in project intellij-elixir by KronicDeth.

the class ImportTest method testImportModuleOnlyNameArity.

public void testImportModuleOnlyNameArity() {
    myFixture.configureByFiles("import_module_only_name_arity.ex", "imported.ex");
    PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    assertNotNull(elementAtCaret);
    PsiElement maybeCall = elementAtCaret.getParent().getParent();
    assertInstanceOf(maybeCall, Call.class);
    Call call = (Call) maybeCall;
    assertEquals("imported", call.functionName());
    assertEquals(0, call.resolvedFinalArity());
    PsiReference reference = call.getReference();
    assertNotNull(reference);
    assertInstanceOf(reference, PsiPolyVariantReference.class);
    PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) reference;
    ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
    assertEquals(2, resolveResults.length);
}
Also used : Call(org.elixir_lang.psi.call.Call) PsiReference(com.intellij.psi.PsiReference) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference)

Example 14 with ResolveResult

use of com.intellij.psi.ResolveResult in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoUnresolvedReferenceInspection method buildGoVisitor.

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
    return new GoVisitor() {

        @Override
        public void visitFieldName(@NotNull GoFieldName o) {
            super.visitFieldName(o);
            PsiElement resolve = o.resolve();
            if (resolve == null) {
                PsiElement id = o.getIdentifier();
                holder.registerProblem(id, "Unknown field <code>#ref</code> in struct literal #loc", LIKE_UNKNOWN_SYMBOL);
            }
        }

        @Override
        public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
            super.visitReferenceExpression(o);
            GoReference reference = o.getReference();
            GoReferenceExpression qualifier = o.getQualifier();
            GoReference qualifierRef = qualifier != null ? qualifier.getReference() : null;
            PsiElement qualifierResolve = qualifierRef != null ? qualifierRef.resolve() : null;
            if (qualifier != null && qualifierResolve == null)
                return;
            ResolveResult[] results = reference.multiResolve(false);
            PsiElement id = o.getIdentifier();
            String name = id.getText();
            if (results.length > 1) {
                holder.registerProblem(id, "Ambiguous reference " + "'" + name + "'", GENERIC_ERROR_OR_WARNING);
            } else if (reference.resolve() == null) {
                LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
                if (isProhibited(o, qualifier)) {
                    fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
                } else if (holder.isOnTheFly()) {
                    boolean canBeLocal = PsiTreeUtil.getParentOfType(o, GoBlock.class) != null;
                    List<LocalQuickFix> fixesList = ContainerUtil.newArrayList(new GoIntroduceGlobalVariableFix(id, name));
                    if (canBeLocal) {
                        fixesList.add(new GoIntroduceLocalVariableFix(id, name));
                    }
                    PsiElement parent = o.getParent();
                    if (o.getReadWriteAccess() == ReadWriteAccessDetector.Access.Read) {
                        fixesList.add(new GoIntroduceGlobalConstantFix(id, name));
                        if (canBeLocal) {
                            fixesList.add(new GoIntroduceLocalConstantFix(id, name));
                        }
                    } else if (canBeLocal) {
                        PsiElement grandParent = parent.getParent();
                        if (grandParent instanceof GoAssignmentStatement) {
                            fixesList.add(new GoReplaceAssignmentWithDeclarationQuickFix(grandParent));
                        } else if (parent instanceof GoRangeClause || parent instanceof GoRecvStatement) {
                            fixesList.add(new GoReplaceAssignmentWithDeclarationQuickFix(parent));
                        }
                    }
                    if (parent instanceof GoCallExpr && PsiTreeUtil.getParentOfType(o, GoConstDeclaration.class) == null) {
                        fixesList.add(new GoIntroduceFunctionFix(parent, name));
                    }
                    fixes = fixesList.toArray(new LocalQuickFix[fixesList.size()]);
                }
                holder.registerProblem(id, "Unresolved reference " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL, fixes);
            }
        }

        @Override
        public void visitImportSpec(@NotNull GoImportSpec o) {
            if (o.isCImport())
                return;
            GoImportString string = o.getImportString();
            if (string.getTextLength() < 2)
                return;
            PsiReference[] references = string.getReferences();
            for (PsiReference reference : references) {
                if (reference instanceof FileReference) {
                    ResolveResult[] resolveResults = ((FileReference) reference).multiResolve(false);
                    if (resolveResults.length == 0) {
                        ProblemHighlightType type = reference.getRangeInElement().isEmpty() ? GENERIC_ERROR_OR_WARNING : LIKE_UNKNOWN_SYMBOL;
                        holder.registerProblem(reference, ProblemsHolder.unresolvedReferenceMessage(reference), type);
                    }
                }
            }
        }

        @Override
        public void visitLabelRef(@NotNull GoLabelRef o) {
            PsiReference reference = o.getReference();
            String name = o.getText();
            if (reference.resolve() == null) {
                holder.registerProblem(o, "Unresolved label " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL);
            }
        }

        @Override
        public void visitTypeReferenceExpression(@NotNull GoTypeReferenceExpression o) {
            super.visitTypeReferenceExpression(o);
            PsiReference reference = o.getReference();
            GoTypeReferenceExpression qualifier = o.getQualifier();
            PsiElement qualifierResolve = qualifier != null ? qualifier.resolve() : null;
            if (qualifier != null && qualifierResolve == null)
                return;
            if (reference.resolve() == null) {
                PsiElement id = o.getIdentifier();
                String name = id.getText();
                boolean isProhibited = isProhibited(o, qualifier);
                LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
                if (isProhibited) {
                    fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
                } else if (holder.isOnTheFly()) {
                    fixes = new LocalQuickFix[] { new GoIntroduceTypeFix(id, name) };
                }
                holder.registerProblem(id, "Unresolved type " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL, fixes);
            }
        }
    };
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) PsiReference(com.intellij.psi.PsiReference) GoReference(com.goide.psi.impl.GoReference) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with ResolveResult

use of com.intellij.psi.ResolveResult in project intellij-plugins by JetBrains.

the class CucumberStepReference method multiResolve.

@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
    final List<ResolveResult> result = new ArrayList<>();
    final List<PsiElement> resolvedElements = new ArrayList<>();
    final CucumberJvmExtensionPoint[] extensionList = Extensions.getExtensions(CucumberJvmExtensionPoint.EP_NAME);
    for (CucumberJvmExtensionPoint e : extensionList) {
        final List<PsiElement> extensionResult = e.resolveStep(myStep);
        for (final PsiElement element : extensionResult) {
            if (element != null && !resolvedElements.contains(element)) {
                resolvedElements.add(element);
                result.add(new ResolveResult() {

                    @Override
                    public PsiElement getElement() {
                        return element;
                    }

                    @Override
                    public boolean isValidResult() {
                        return true;
                    }
                });
            }
        }
    }
    return result.toArray(new ResolveResult[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) CucumberJvmExtensionPoint(org.jetbrains.plugins.cucumber.CucumberJvmExtensionPoint) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ResolveResult (com.intellij.psi.ResolveResult)30 PsiElement (com.intellij.psi.PsiElement)25 PsiPolyVariantReference (com.intellij.psi.PsiPolyVariantReference)10 NotNull (org.jetbrains.annotations.NotNull)8 PsiReference (com.intellij.psi.PsiReference)5 PsiElementResolveResult (com.intellij.psi.PsiElementResolveResult)4 PsiFile (com.intellij.psi.PsiFile)4 ImportedResolveResult (com.jetbrains.python.psi.resolve.ImportedResolveResult)3 Call (org.elixir_lang.psi.call.Call)3 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)2 JSE4XNamespaceReference (com.intellij.lang.javascript.psi.e4x.JSE4XNamespaceReference)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Nullable (org.jetbrains.annotations.Nullable)2 GoReference (com.goide.psi.impl.GoReference)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 ASTNode (com.intellij.lang.ASTNode)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1