Search in sources :

Example 1 with PsiInvalidElementAccessException

use of com.intellij.psi.PsiInvalidElementAccessException in project intellij-community by JetBrains.

the class DomInvocationHandler method getFixedChild.

@NotNull
final IndexedElementInvocationHandler getFixedChild(final Pair<FixedChildDescriptionImpl, Integer> info) {
    final FixedChildDescriptionImpl description = info.first;
    XmlName xmlName = description.getXmlName();
    final EvaluatedXmlName evaluatedXmlName = createEvaluatedXmlName(xmlName);
    if (myStub != null && description.isStubbed()) {
        List<DomStub> stubs = myStub.getChildrenByName(xmlName.getLocalName(), xmlName.getNamespaceKey());
        DomStub stub = stubs.isEmpty() ? null : stubs.get(0);
        DomParentStrategy strategy = stub == null ? new StubParentStrategy.Empty(myStub) : new StubParentStrategy(stub);
        return new IndexedElementInvocationHandler(evaluatedXmlName, description, 0, strategy, myManager, (ElementStub) stub);
    }
    final XmlTag tag = getXmlTag();
    final int index = info.second;
    if (tag != null) {
        if (!tag.isValid()) {
            throw new PsiInvalidElementAccessException(tag);
        }
        final XmlTag[] subTags = tag.getSubTags();
        for (int i = 0, subTagsLength = subTags.length; i < subTagsLength; i++) {
            XmlTag xmlTag = subTags[i];
            if (!xmlTag.isValid()) {
                throw new PsiInvalidElementAccessException(xmlTag, "invalid children of valid tag: " + tag.getText() + "; subtag=" + xmlTag + "; index=" + i);
            }
        }
        final List<XmlTag> tags = DomImplUtil.findSubTags(subTags, evaluatedXmlName, getFile());
        if (tags.size() > index) {
            final XmlTag child = tags.get(index);
            final IndexedElementInvocationHandler semElement = myManager.getSemService().getSemElement(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, child);
            if (semElement == null) {
                final IndexedElementInvocationHandler take2 = myManager.getSemService().getSemElement(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, child);
                throw new AssertionError("No DOM at XML. Parent=" + tag + "; child=" + child + "; index=" + index + "; second attempt=" + take2);
            }
            return semElement;
        }
    }
    return new IndexedElementInvocationHandler(evaluatedXmlName, description, index, new VirtualDomParentStrategy(this), myManager, null);
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) DomStub(com.intellij.util.xml.stubs.DomStub) StubParentStrategy(com.intellij.util.xml.stubs.StubParentStrategy) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiInvalidElementAccessException

use of com.intellij.psi.PsiInvalidElementAccessException in project intellij-community by JetBrains.

the class VariantsProcessor method execute.

@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState substitutor) {
    // skip whatever the filter rejects
    if (myNodeFilter != null && !myNodeFilter.value(element))
        return true;
    // TODO: refactor to look saner; much code duplication
    if (element instanceof PsiNamedElement) {
        final PsiNamedElement psiNamedElement = (PsiNamedElement) element;
        final String name = PyUtil.getElementNameWithoutExtension(psiNamedElement);
        if (name != null && nameIsAcceptable(name)) {
            addElement(name, psiNamedElement);
        }
    } else if (element instanceof PyReferenceExpression) {
        PyReferenceExpression expr = (PyReferenceExpression) element;
        String referencedName = expr.getReferencedName();
        if (nameIsAcceptable(referencedName)) {
            addElement(referencedName, expr);
        }
    } else if (element instanceof PyImportedNameDefiner) {
        boolean handledAsImported = false;
        if (element instanceof PyImportElement) {
            final PyImportElement importElement = (PyImportElement) element;
            handledAsImported = handleImportElement(importElement);
        }
        if (!handledAsImported) {
            final PyImportedNameDefiner definer = (PyImportedNameDefiner) element;
            for (PyElement expr : definer.iterateNames()) {
                if (expr != null && expr != myContext) {
                    // NOTE: maybe rather have SingleIterables skip nulls outright?
                    if (!expr.isValid()) {
                        throw new PsiInvalidElementAccessException(expr, "Definer: " + definer);
                    }
                    String referencedName = expr instanceof PyFile ? FileUtil.getNameWithoutExtension(((PyFile) expr).getName()) : expr.getName();
                    if (referencedName != null && nameIsAcceptable(referencedName)) {
                        addImportedElement(referencedName, expr);
                    }
                }
            }
        }
    }
    return true;
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) PsiNamedElement(com.intellij.psi.PsiNamedElement)

Example 3 with PsiInvalidElementAccessException

use of com.intellij.psi.PsiInvalidElementAccessException in project intellij-community by JetBrains.

the class ResolveImportUtil method resolveNameInFromImport.

@NotNull
public static List<RatedResolveResult> resolveNameInFromImport(PyFromImportStatement importStatement, @NotNull QualifiedName qName) {
    PsiFile file = importStatement.getContainingFile().getOriginalFile();
    String name = qName.getComponents().get(0);
    final List<PsiElement> candidates = importStatement.resolveImportSourceCandidates();
    List<PsiElement> resultList = new ArrayList<>();
    for (PsiElement candidate : candidates) {
        if (!candidate.isValid()) {
            throw new PsiInvalidElementAccessException(candidate, "Got an invalid candidate from resolveImportSourceCandidates(): " + candidate.getClass());
        }
        if (candidate instanceof PsiDirectory) {
            candidate = PyUtil.getPackageElement((PsiDirectory) candidate, importStatement);
        }
        List<RatedResolveResult> results = resolveChildren(candidate, name, file, false, true, false);
        if (!results.isEmpty()) {
            for (RatedResolveResult result : results) {
                final PsiElement element = result.getElement();
                if (element != null) {
                    if (!element.isValid()) {
                        throw new PsiInvalidElementAccessException(element, "Got an invalid candidate from resolveChild(): " + element.getClass());
                    }
                    resultList.add(element);
                }
            }
        }
    }
    if (!resultList.isEmpty()) {
        return rateResults(resultList);
    }
    return Collections.emptyList();
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PsiInvalidElementAccessException

use of com.intellij.psi.PsiInvalidElementAccessException in project intellij-community by JetBrains.

the class ASTDelegatePsiElement method getManager.

@Override
public PsiManagerEx getManager() {
    Project project = ProjectCoreUtil.theOnlyOpenProject();
    if (project != null) {
        return PsiManagerEx.getInstanceEx(project);
    }
    PsiElement parent = this;
    while (parent instanceof ASTDelegatePsiElement) {
        parent = parent.getParent();
    }
    if (parent == null) {
        throw new PsiInvalidElementAccessException(this);
    }
    return (PsiManagerEx) parent.getManager();
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) Project(com.intellij.openapi.project.Project) PsiElement(com.intellij.psi.PsiElement) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 5 with PsiInvalidElementAccessException

use of com.intellij.psi.PsiInvalidElementAccessException in project intellij-community by JetBrains.

the class StubBasedPsiElementBase method getNode.

/**
   * Ensures this element is AST-based. This is an expensive operation that might take significant time and allocate lots of objects,
   * so it should be to be avoided if possible.
   *
   * @return an AST node corresponding to this element. If the element is currently operating via stubs,
   * this causes AST to be loaded for the whole file and all stub-based PSI elements in this file (including the current one)
   * to be switched from stub to AST. So, after this call {@link #getStub()} will return null.
   */
@Override
@NotNull
public ASTNode getNode() {
    if (mySubstrateRef instanceof SubstrateRef.StubRef) {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        PsiFileImpl file = (PsiFileImpl) getContainingFile();
        if (!file.isValid())
            throw new PsiInvalidElementAccessException(this);
        FileElement treeElement = file.getTreeElement();
        if (treeElement != null && mySubstrateRef instanceof SubstrateRef.StubRef) {
            return notBoundInExistingAst(file, treeElement);
        }
        treeElement = file.calcTreeElement();
        if (mySubstrateRef instanceof SubstrateRef.StubRef) {
            return failedToBindStubToAst(file, treeElement);
        }
    }
    return mySubstrateRef.getNode();
}
Also used : PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) SubstrateRef(com.intellij.psi.impl.source.SubstrateRef) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileElement(com.intellij.psi.impl.source.tree.FileElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiInvalidElementAccessException (com.intellij.psi.PsiInvalidElementAccessException)11 NotNull (org.jetbrains.annotations.NotNull)8 Project (com.intellij.openapi.project.Project)6 PsiElement (com.intellij.psi.PsiElement)4 PsiMethod (com.intellij.psi.PsiMethod)4 PsiParameter (com.intellij.psi.PsiParameter)3 PsiType (com.intellij.psi.PsiType)3 PsiFile (com.intellij.psi.PsiFile)2 TestStateStorage (com.intellij.execution.TestStateStorage)1 TestStateInfo (com.intellij.execution.testframework.sm.runner.states.TestStateInfo)1 Document (com.intellij.openapi.editor.Document)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiArrayType (com.intellij.psi.PsiArrayType)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiModifierList (com.intellij.psi.PsiModifierList)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)1 LightElement (com.intellij.psi.impl.light.LightElement)1 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)1