private static boolean isValid(JSObjectLiteralExpression expression) { if (expression == null) { return false; } try { PsiFile file = expression.getContainingFile(); if (file == null) { return false; } } catch (PsiInvalidElementAccessException e) { return false; } JSProperty data = expression.findProperty("data"); if (data == null || data.getValue() == null) { return false; } return true; }
public static int getExportsEndOffset(PsiElement anyElementOnWeexScript, String name) { JSObjectLiteralExpression exports = getExportsStatement(anyElementOnWeexScript); if (exports != null) { try { PsiFile file = exports.getContainingFile(); if (file == null) { return -1; } } catch (PsiInvalidElementAccessException e) { return -1; } JSProperty data = exports.findProperty(name); if (data == null || data.getValue() == null) { return -1; } return data.getValue().getTextRange().getEndOffset() - 1; } return -1; }
/** * 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() { ASTNode node = myNode; if (node == null) { ApplicationManager.getApplication().assertReadAccessAllowed(); PsiFileImpl file = (PsiFileImpl)getContainingFile(); if (!file.isValid()) throw new PsiInvalidElementAccessException(this); FileElement treeElement = file.getTreeElement(); if (treeElement != null && myNode == null) { return notBoundInExistingAst(file, treeElement); } treeElement = file.calcTreeElement(); node = myNode; if (node == null) { return failedToBindStubToAst(file, treeElement); } } return node; }
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<PsiElement>(); 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); } PsiElement result = resolveChild(candidate, name, file, false, true); if (result != null) { if (!result.isValid()) { throw new PsiInvalidElementAccessException(result, "Got an invalid candidate from resolveChild(): " + result.getClass()); } resultList.add(result); } } if (!resultList.isEmpty()) { return rateResults(resultList); } return Collections.emptyList(); }
@NotNull public static String getNamespaceString(@NotNull PsiElement element, boolean inParens) { Qn namespace; if (element instanceof SchemaTypeDef) { SchemaTypeDef typeDef = (SchemaTypeDef) element; namespace = typeDef.getNamespace(); } else { namespace = NamespaceManager.getNamespace(element); } if (namespace != null) return inParens ? "(" + namespace.toString() + ')' : namespace.toString(); try { PsiFile containingFile = element.getContainingFile(); if (containingFile == null) return "[invalid]"; return inParens ? "(" + containingFile.getName() + ')' : containingFile.getName(); } catch (PsiInvalidElementAccessException e) { return "[invalid]"; // file not available } }
@NotNull private Object[] variantsViaQuery(@NotNull final Query<PsiClass> query) { final List<LookupElement> variants = new LinkedList<LookupElement>(); final Project project = getElement().getProject(); for(final PsiClass klass : query.findAll()) { if(klass == null) { continue; } try { variants.add( LookupElementBuilder.create(klass). withInsertHandler(QualifiedClassNameInsertHandler.INSTANCE). withIcon(IconUtil.getIcon(klass.getContainingFile().getVirtualFile(), 0, project)). withTypeText(klass.getContainingFile().getName())); } catch(final PsiInvalidElementAccessException invalidElementAccess) { LOG.error(invalidElementAccess); } } return variants.toArray(new Object[variants.size()]); }
@Override @NotNull public ASTNode getNode() { ASTNode node = myNode; if (node == null) { ApplicationManager.getApplication().assertReadAccessAllowed(); PsiFileImpl file = (PsiFileImpl)getContainingFile(); if (!file.isValid()) throw new PsiInvalidElementAccessException(this); FileElement treeElement = file.getTreeElement(); StubTree stubTree = file.getStubTree(); if (treeElement != null && myNode == null) { return notBoundInExistingAst(file, treeElement, stubTree); } final FileElement fileElement = file.calcTreeElement(); node = myNode; if (node == null) { return failedToBindStubToAst(file, stubTree, fileElement); } } return node; }
/** * 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 @Nonnull 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(); }
@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(); }
@Nonnull static SubstrateRef createInvalidRef(@Nonnull final StubBasedPsiElementBase<?> psi) { return new SubstrateRef() { @Nonnull @Override public ASTNode getNode() { throw new PsiInvalidElementAccessException(psi); } @Override public boolean isValid() { return false; } @Nonnull @Override public PsiFile getContainingFile() { throw new PsiInvalidElementAccessException(psi); } }; }
private static Locale safeGetLocale(final @NotNull IProperty property) { try { PropertiesFile file = property.getPropertiesFile(); return file == null ? null : file.getLocale(); } catch (PsiInvalidElementAccessException e) { return null; } }
public static JSProperty getVarDeclaration(PsiElement anyElementOnWeexScript, String valueName) { valueName = CodeUtil.getVarNameFromMustache(valueName); JSObjectLiteralExpression exports = getExportsStatement(anyElementOnWeexScript); vars.clear(); JSProperty ret = null; if (exports != null) { try { PsiFile file = exports.getContainingFile(); if (file == null) { return null; } } catch (PsiInvalidElementAccessException e) { return null; } JSProperty data = exports.findProperty("data"); if (data == null || data.getValue() == null) { return null; } for (PsiElement pe : data.getValue().getChildren()) { if (pe instanceof JSProperty) { String varName = ((JSProperty) pe).getName(); String varValue = getJSPropertyType((JSProperty) pe); if (varName != null && varValue != null) { vars.put(varName, varValue); } if (valueName.equals(varName)) { ret = (JSProperty) pe; } } } } return ret; }
@Nullable public static PsiElement resolveExposed(PsiElement element, Function<ElmFile, PsiElement> resolveInFile) { PsiFile file; try { file = element.getContainingFile(); } catch (PsiInvalidElementAccessException ex) { return null; } return resolveExposed(file, resolveInFile); }
@Override @NotNull public PsiFile getContainingFile() { StubElement stub = myStub; if (stub != null) { while (!(stub instanceof PsiFileStub)) { stub = stub.getParentStub(); } PsiFile psi = (PsiFile)stub.getPsi(); if (psi != null) { return psi; } ApplicationManager.getApplication().assertReadAccessAllowed(); synchronized (PsiLock.LOCK) { if (myStub != null) { String reason = ((PsiFileStubImpl<?>)stub).getInvalidationReason(); PsiInvalidElementAccessException exception = new PsiInvalidElementAccessException(this, "no psi for file stub " + stub + ", invalidation reason=" + reason, null); if (PsiFileImpl.STUB_PSI_MISMATCH.equals(reason)) { // we're between finding stub-psi mismatch and the next EDT spot where the file is reparsed and stub rebuilt // see com.intellij.psi.impl.source.PsiFileImpl.rebuildStub() // most likely it's just another highlighting thread accessing the same PSI concurrently and not yet canceled, so cancel it throw new ProcessCanceledException(exception); } throw exception; } } } PsiFile file = super.getContainingFile(); if (file == null) { throw new PsiInvalidElementAccessException(this); } return file; }
@Override public PsiManagerEx getManager() { PsiElement parent = this; while (parent instanceof ASTDelegatePsiElement) { parent = parent.getParent(); } if (parent == null) { throw new PsiInvalidElementAccessException(this); } return (PsiManagerEx)parent.getManager(); }
public boolean poke(final PsiElement what, final int rate) { if (what == null) return false; if (!(what instanceof LightElement) && !what.isValid()) { throw new PsiInvalidElementAccessException(what, "Trying to resolve a reference to an invalid element"); } super.add(new RatedResolveResult(rate, what)); return true; }
@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); }
public static List<XmlTag> findSubTags(@NotNull final XmlTag tag, final EvaluatedXmlName name, final XmlFile file) { if (!tag.isValid()) { throw new AssertionError("Invalid tag"); } final XmlTag[] tags = tag.getSubTags(); if (tags.length == 0) { return Collections.emptyList(); } return ContainerUtil.findAll(tags, new Condition<XmlTag>() { @Override public boolean value(XmlTag childTag) { try { return isNameSuitable(name, childTag.getLocalName(), childTag.getName(), childTag.getNamespace(), file); } catch (PsiInvalidElementAccessException e) { if (!childTag.isValid()) { LOG.error("tag.getSubTags() returned invalid, " + "tag=" + tag + ", " + "containing file: " + tag.getContainingFile() + "subTag.parent=" + childTag.getNode().getTreeParent()); return false; } throw e; } } }); }
public T getOriginal() { T element = myRef.get(); if (element != null) { return element; } element = (T)myOriginal.retrieve(); if (element == null) { throw new PsiInvalidElementAccessException(this); } myRef = new SoftReference<T>(element); return element; }
public static <T extends GrStatement> T replaceWithStatement(GroovyPsiElement element, @NotNull T newStmt) { PsiElement parent = element.getParent(); if (parent == null) { throw new PsiInvalidElementAccessException(element); } return (T)element.replace(newStmt); }
/** Get the project for the hierarchy. */ public Project getProject(PsiElement element) { Project project; try { project = element.getContainingFile().getProject(); if (project == null) { return null; } } catch (PsiInvalidElementAccessException ex) { LOG.error("Error getting project with annotation " + element.getText(), ex); return null; } return project; }
/** * Returns the project associated with {@code method} or null if it cannot retrieve the project. */ @Nullable private static Project getProject(PsiMethod method) { Project project; try { project = method.getContainingFile().getProject(); } catch (PsiInvalidElementAccessException ex) { LOG.error("Error getting project with parameter " + method.getText(), ex); return null; } return project; }
private void doTest(@NotNull final String actualText, final int referencePosition, @Nullable final TextRange resolvedReferenceRange, @NotNull final String errorText) throws Exception { final ASTNode root = ParserTestUtil.buildTree(actualText, ourLanguage.getParserDefinition()); final PsiReference reference = findReferenceAt(root.getPsi(), referencePosition); assertNotNull(reference, errorText); PsiElement element = null; boolean containingFileRequested = false; try { element = reference.resolve(); } catch (final PsiInvalidElementAccessException e) { containingFileRequested = true; } if (resolvedReferenceRange == null) { assertNull(element, errorText); } else { assertNotNull(element, errorText); assertTrue(element instanceof OCamlResolvedReference, errorText); assertEquals(element.getTextRange(), resolvedReferenceRange, errorText); assertFalse(containingFileRequested, errorText); } }
@Override @NotNull public PsiFile getContainingFile() { StubElement stub = myStub; if (stub != null) { while (!(stub instanceof PsiFileStub)) { stub = stub.getParentStub(); } PsiFile psi = (PsiFile)stub.getPsi(); if (psi != null) { return psi; } ApplicationManager.getApplication().assertReadAccessAllowed(); synchronized (PsiLock.LOCK) { if (myStub != null) { throw new PsiInvalidElementAccessException(this, "no psi for file stub " + stub, null); } } } PsiFile file = super.getContainingFile(); if (file == null) { throw new PsiInvalidElementAccessException(this); } return file; }
public static List<XmlTag> findSubTags(@NotNull final XmlTag tag, final EvaluatedXmlName name, final XmlFile file) { if (!tag.isValid()) { throw new AssertionError("Invalid tag"); } final XmlTag[] tags = tag.getSubTags(); if (tags.length == 0) { return Collections.emptyList(); } return ContainerUtil.findAll(tags, new Condition<XmlTag>() { public boolean value(XmlTag childTag) { try { return isNameSuitable(name, childTag.getLocalName(), childTag.getName(), childTag.getNamespace(), file); } catch (PsiInvalidElementAccessException e) { if (!childTag.isValid()) { LOG.error("tag.getSubTags() returned invalid, " + "tag=" + tag + ", " + "containing file: " + tag.getContainingFile() + "subTag.parent=" + childTag.getNode().getTreeParent()); return false; } throw e; } } }); }
private void attachEntityClass(@NotNull Collection<LineMarkerInfo> lineMarkerInfos, @NotNull PsiElement psiElement) { if(psiElement.getNode().getElementType() != YAMLTokenTypes.SCALAR_KEY) { return; } PsiElement yamlKeyValue = psiElement.getParent(); if(!(yamlKeyValue instanceof YAMLKeyValue)) { return; } if(yamlKeyValue.getParent() instanceof YAMLMapping && yamlKeyValue.getParent().getParent() instanceof YAMLDocument) { PsiFile containingFile; try { containingFile = yamlKeyValue.getContainingFile(); } catch (PsiInvalidElementAccessException e) { return; } String fileName = containingFile.getName(); if(isMetadataFile(fileName)) { String keyText = ((YAMLKeyValue) yamlKeyValue).getKeyText(); if(StringUtils.isNotBlank(keyText)) { Collection<PhpClass> phpClasses = PhpElementsUtil.getClassesInterface(psiElement.getProject(), keyText); if(phpClasses.size() > 0) { NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.DOCTRINE_LINE_MARKER). setTargets(phpClasses). setTooltipText("Navigate to class"); lineMarkerInfos.add(builder.createLineMarkerInfo(psiElement)); } } } } }
public static long getLastUpdate(PsiElement element) { try { final PsiClass topLevelClass = PsiUtil.getTopLevelClass(element); if (topLevelClass == null) { return -1; } final PsiFile containingFile = topLevelClass.getContainingFile(); return containingFile.getOriginalFile().getModificationStamp(); } catch (PsiInvalidElementAccessException e) { return -1; } }
public boolean canPerformReRun() { try { return myUsageSearcherFactory != null && allTargetsAreValid() && myUsageSearcherFactory.create() != null; } catch (PsiInvalidElementAccessException e) { return false; } }
@Override @Nonnull public PsiFile getContainingFile() { try { return mySubstrateRef.getContainingFile(); } catch (PsiInvalidElementAccessException e) { if (PsiInvalidElementAccessException.getInvalidationTrace(this) != null) { throw new PsiInvalidElementAccessException(this, e); } else { throw e; } } }
public static boolean treeWalkUp(@Nonnull final PsiScopeProcessor processor, @Nonnull final PsiElement entrance, @Nullable final PsiElement maxScope, @Nonnull final ResolveState state) { if (!entrance.isValid()) { LOGGER.error(new PsiInvalidElementAccessException(entrance)); } PsiElement prevParent = entrance; PsiElement scope = entrance; while (scope != null) { ProgressIndicatorProvider.checkCanceled(); if (!scope.processDeclarations(processor, state, prevParent, entrance)) { return false; // resolved } if (scope == maxScope) break; prevParent = scope; scope = prevParent.getContext(); if (scope != null && scope != prevParent.getParent() && !scope.isValid()) { break; } } return true; }
private PsiFile reportError(StubElement stub) { ApplicationManager.getApplication().assertReadAccessAllowed(); String reason = ((PsiFileStubImpl<?>)stub).getInvalidationReason(); PsiInvalidElementAccessException exception = new PsiInvalidElementAccessException(myStub.getPsi(), "no psi for file stub " + stub + ", invalidation reason=" + reason, null); if (PsiFileImpl.STUB_PSI_MISMATCH.equals(reason)) { // we're between finding stub-psi mismatch and the next EDT spot where the file is reparsed and stub rebuilt // see com.intellij.psi.impl.source.PsiFileImpl.rebuildStub() // most likely it's just another highlighting thread accessing the same PSI concurrently and not yet canceled, so cancel it throw new ProcessCanceledException(exception); } throw exception; }
@Override public String toString() { String namespace; try { namespace = getNamespace(); } catch(PsiInvalidElementAccessException e) { namespace = "!!!Invalid!!!"; } return getName() + " (" + namespace + ")"; }
@Override public PsiFile getContainingFile() { PsiElement parent = getParent(); if(parent == null) { throw new PsiInvalidElementAccessException(this); } return parent.getContainingFile(); }