Search in sources :

Example 11 with FileReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference 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 12 with FileReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.

the class ReferenceInjectionTest method testInjectIntoJava.

public void testInjectIntoJava() throws Exception {
    myFixture.configureByText("Foo.java", "class Foo {\n" + "    String bar() {\n" + "        return \"ba<caret>r.xml\";\n" + "    }    \n" + "}");
    assertNull(getInjectedReferences());
    InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile(), new FileReferenceInjector());
    PsiReference[] references = getInjectedReferences();
    PsiReference reference = assertOneElement(references);
    assertTrue(reference instanceof FileReference);
    UnInjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile());
    assertNull(getInjectedReferences());
}
Also used : FileReferenceInjector(org.intellij.plugins.intelliLang.references.FileReferenceInjector) PsiReference(com.intellij.psi.PsiReference) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)

Example 13 with FileReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.

the class ReferenceInjectionTest method testSurviveSerialization.

public void testSurviveSerialization() throws Exception {
    myFixture.configureByText("foo.xml", "<foo xmlns=\"http://foo.bar\" \n" + "     xxx=\"ba<caret>r\"/>");
    assertNull(myFixture.getReferenceAtCaretPosition());
    InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile(), new FileReferenceInjector());
    assertTrue(myFixture.getReferenceAtCaretPosition() instanceof FileReference);
    Configuration configuration = Configuration.getInstance();
    Element element = configuration.getState();
    configuration.loadState(element);
    ((PsiModificationTrackerImpl) PsiManager.getInstance(getProject()).getModificationTracker()).incCounter();
    assertTrue(myFixture.getReferenceAtCaretPosition() instanceof FileReference);
    UnInjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile());
    assertNull(myFixture.getReferenceAtCaretPosition());
}
Also used : FileReferenceInjector(org.intellij.plugins.intelliLang.references.FileReferenceInjector) PsiElement(com.intellij.psi.PsiElement) Element(org.jdom.Element) PsiModificationTrackerImpl(com.intellij.psi.impl.PsiModificationTrackerImpl) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)

Example 14 with FileReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.

the class ReferenceInjectionTest method testInjectIntoTagValue.

public void testInjectIntoTagValue() throws Exception {
    myFixture.configureByText("foo.xml", "<foo xmlns=\"http://foo.bar\" <bar>x<caret>xx</bar>/>");
    assertNull(myFixture.getReferenceAtCaretPosition());
    InjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile(), new FileReferenceInjector());
    assertTrue(myFixture.getReferenceAtCaretPosition() instanceof FileReference);
    UnInjectLanguageAction.invokeImpl(getProject(), myFixture.getEditor(), myFixture.getFile());
    assertNull(myFixture.getReferenceAtCaretPosition());
}
Also used : FileReferenceInjector(org.intellij.plugins.intelliLang.references.FileReferenceInjector) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)

Example 15 with FileReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference in project intellij-community by JetBrains.

the class AnchorPathReferenceProvider method createReferences.

@Override
public boolean createReferences(@NotNull final PsiElement psiElement, @NotNull final List<PsiReference> references, final boolean soft) {
    final TextRange range = ElementManipulators.getValueTextRange(psiElement);
    final String elementText = psiElement.getText();
    final int anchorOffset = elementText.indexOf('#');
    if (anchorOffset == -1) {
        return false;
    }
    final boolean dynamic = isDynamic(psiElement, anchorOffset + 1, elementText);
    if (dynamic) {
        return false;
    }
    FileReference fileReference = null;
    if (range.getStartOffset() != anchorOffset) {
        fileReference = findFileReference(references);
        if (fileReference == null || fileReference.resolve() == null) {
            return false;
        }
    }
    final int pos = elementText.indexOf('?', anchorOffset);
    final String anchor;
    try {
        int endIndex = pos != -1 ? pos : range.getEndOffset();
        if (endIndex <= anchorOffset) {
            endIndex = anchorOffset + 1;
        }
        anchor = elementText.substring(anchorOffset + 1, endIndex);
    } catch (StringIndexOutOfBoundsException e) {
        throw new RuntimeException(elementText, e);
    }
    final AnchorReferenceImpl anchorReference = new AnchorReferenceImpl(anchor, fileReference, psiElement, anchorOffset + 1, soft);
    references.add(anchorReference);
    return false;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)

Aggregations

FileReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)17 NotNull (org.jetbrains.annotations.NotNull)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 PsiElement (com.intellij.psi.PsiElement)5 PsiReference (com.intellij.psi.PsiReference)5 ArrayList (java.util.ArrayList)4 FileReferenceInjector (org.intellij.plugins.intelliLang.references.FileReferenceInjector)4 TextRange (com.intellij.openapi.util.TextRange)3 FileReferenceSet (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet)3 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 Nullable (org.jetbrains.annotations.Nullable)2 GoReference (com.goide.psi.impl.GoReference)1 CreateFileFix (com.intellij.codeInsight.daemon.quickFix.CreateFileFix)1 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 CfmlMappingsConfig (com.intellij.coldFusion.UI.config.CfmlMappingsConfig)1 CfmlProjectConfiguration (com.intellij.coldFusion.UI.config.CfmlProjectConfiguration)1 CfmlTag (com.intellij.coldFusion.model.psi.CfmlTag)1 ASTNode (com.intellij.lang.ASTNode)1 Module (com.intellij.openapi.module.Module)1 PsiDynaReference (com.intellij.openapi.paths.PsiDynaReference)1