/** * @return children of specified type, taken from stubs (if this element is currently stub-based) or AST (otherwise). */ public <S extends StubElement, Psi extends PsiElement> Psi[] getStubOrPsiChildren(final IStubElementType<S, Psi> elementType, Psi[] array) { T stub = myStub; if (stub != null) { //noinspection unchecked return (Psi[])stub.getChildrenByType(elementType, array); } else { final ASTNode[] nodes = SharedImplUtil.getChildrenOfType(getNode(), elementType); //noinspection unchecked Psi[] psiElements = (Psi[])Array.newInstance(array.getClass().getComponentType(), nodes.length); for (int i = 0; i < nodes.length; i++) { //noinspection unchecked psiElements[i] = (Psi)nodes[i].getPsi(); } return psiElements; } }
/** * @return children of specified type, taken from stubs (if this element is currently stub-based) or AST (otherwise). */ public <S extends StubElement, Psi extends PsiElement> Psi[] getStubOrPsiChildren(final IStubElementType<S, Psi> elementType, ArrayFactory<Psi> f) { T stub = myStub; if (stub != null) { //noinspection unchecked return (Psi[])stub.getChildrenByType(elementType, f); } else { final ASTNode[] nodes = SharedImplUtil.getChildrenOfType(getNode(), elementType); Psi[] psiElements = f.create(nodes.length); for (int i = 0; i < nodes.length; i++) { //noinspection unchecked psiElements[i] = (Psi)nodes[i].getPsi(); } return psiElements; } }
/** * @return children of specified type, taken from stubs (if this element is currently stub-based) or AST (otherwise). */ @Nonnull public <S extends StubElement, Psi extends PsiElement> Psi[] getStubOrPsiChildren(@Nonnull IStubElementType<S, ? extends Psi> elementType, @Nonnull Psi[] array) { T stub = getGreenStub(); if (stub != null) { //noinspection unchecked return (Psi[])stub.getChildrenByType(elementType, array); } else { final ASTNode[] nodes = SharedImplUtil.getChildrenOfType(getNode(), elementType); //noinspection unchecked Psi[] psiElements = (Psi[])Array.newInstance(array.getClass().getComponentType(), nodes.length); for (int i = 0; i < nodes.length; i++) { //noinspection unchecked psiElements[i] = (Psi)nodes[i].getPsi(); } return psiElements; } }
/** * @return children of specified type, taken from stubs (if this element is currently stub-based) or AST (otherwise). */ @Nonnull public <S extends StubElement, Psi extends PsiElement> Psi[] getStubOrPsiChildren(@Nonnull IStubElementType<S, ? extends Psi> elementType, @Nonnull ArrayFactory<Psi> f) { T stub = getGreenStub(); if (stub != null) { //noinspection unchecked return (Psi[])stub.getChildrenByType(elementType, f); } else { final ASTNode[] nodes = SharedImplUtil.getChildrenOfType(getNode(), elementType); Psi[] psiElements = f.create(nodes.length); for (int i = 0; i < nodes.length; i++) { //noinspection unchecked psiElements[i] = (Psi)nodes[i].getPsi(); } return psiElements; } }
@Override public PsiImportStaticStatement bindToTargetClass(final PsiClass aClass) throws IncorrectOperationException { final String qualifiedName = aClass.getQualifiedName(); if (qualifiedName == null) throw new IncorrectOperationException(); final PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(getProject()).getParserFacade(); final CompositeElement newRef = (CompositeElement)parserFacade.createReferenceFromText(qualifiedName, null).getNode(); if (getQualifier() != null) { replaceChildInternal(findChildByRole(ChildRole.QUALIFIER), newRef); return (PsiImportStaticStatement)getParent(); } else { final LeafElement dot = Factory.createSingleLeafElement(JavaTokenType.DOT, ".", 0, 1, SharedImplUtil.findCharTableByTree(newRef), getManager()); newRef.rawInsertAfterMe(dot); final CompositeElement errorElement = Factory.createErrorElement(JavaErrorMessages.message("import.statement.identifier.or.asterisk.expected.")); dot.rawInsertAfterMe(errorElement); final CompositeElement parentComposite = (CompositeElement)SourceTreeToPsiMap.psiElementToTree(getParent()); parentComposite.addInternal(newRef, errorElement, this, Boolean.TRUE); parentComposite.deleteChildInternal(this); return (PsiImportStaticStatement)SourceTreeToPsiMap.treeElementToPsi(parentComposite); } }
protected PsiElement getDefinitionParent() { final PsiElement candidate = getParentByStub(); if (candidate instanceof LuaPsiFile) { return candidate; } return SharedImplUtil.getParent(getNode()); }
@Override public PsiElement getParent() { final PsiClassStub stub = getStub(); if (stub != null) { final StubElement parentStub = stub.getParentStub(); if (parentStub instanceof PsiFileStub || parentStub instanceof PsiClassStub ) { return parentStub.getPsi(); } } return SharedImplUtil.getParent(getNode()); }
private static PsiBuilderImpl createBuilder(final String text, ASTNode originalTree) { final Language lang = StdFileTypes.JAVA.getLanguage(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang); assertNotNull(parserDefinition); PsiFile psiFile = createFile("x.java", text); return new PsiBuilderImpl(getProject(), psiFile, parserDefinition, JavaParserDefinition.createLexer(LanguageLevel.JDK_1_5), SharedImplUtil.findCharTableByTree(psiFile.getNode()), text, originalTree, null); }
protected <T extends PsiElement> T[] findChildrenByType(IElementType elementType, Class<T> arrayClass) { return ContainerUtil.map2Array(SharedImplUtil.getChildrenOfType(getNode(), elementType), arrayClass, new Function<ASTNode, T>() { @Override public T fun(final ASTNode s) { return (T)s.getPsi(); } }); }
public static <T extends PsiElement> T handleContentChange(T element, TextRange range, String newContent, final IElementType tokenType) { CheckUtil.checkWritable(element); final CompositeElement attrNode = (CompositeElement)element.getNode(); final ASTNode valueNode = attrNode.findLeafElementAt(range.getStartOffset()); LOG.assertTrue(valueNode != null, "Leaf not found in " + attrNode + " at offset " + range.getStartOffset() + " in element " + element); final PsiElement elementToReplace = valueNode.getPsi(); String text; try { text = elementToReplace.getText(); final int offsetInParent = elementToReplace.getStartOffsetInParent(); String textBeforeRange = text.substring(0, range.getStartOffset() - offsetInParent); String textAfterRange = text.substring(range.getEndOffset()- offsetInParent, text.length()); newContent = element.getText().startsWith("'") || element.getText().endsWith("'") ? newContent.replace("'", "'") : newContent.replace("\"", """); text = textBeforeRange + newContent + textAfterRange; } catch(StringIndexOutOfBoundsException e) { LOG.error("Range: " + range + " in text: '" + element.getText() + "'", e); throw e; } final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(attrNode); final LeafElement newValueElement = Factory.createSingleLeafElement(tokenType, text, charTableByTree, element.getManager()); attrNode.replaceChildInternal(valueNode, newValueElement); return element; }
@Override public ASTNode encodeXmlTextContents(String displayText, PsiElement text) { final ASTNode firstChild = text.getNode().getFirstChildNode(); boolean textAlreadyHasCDATA = firstChild != null && firstChild.getElementType() == XmlElementType.XML_CDATA; if ((textAlreadyHasCDATA || XmlUtil.toCode(displayText)) && !displayText.isEmpty()) { final FileElement dummyParent = createCDATAElement(text.getManager(), SharedImplUtil.findCharTableByTree(text.getNode()), displayText); return dummyParent.getFirstChildNode(); } else { return super.encodeXmlTextContents(displayText, text); } }
@NotNull @Override public JavaResolveResult[] multiResolve(final boolean incompleteCode) { FileElement fileElement = SharedImplUtil.findFileElement(this); if (fileElement == null) { LOG.error("fileElement == null!"); return JavaResolveResult.EMPTY_ARRAY; } final PsiManagerEx manager = fileElement.getManager(); if (manager == null) { LOG.error("getManager() == null!"); return JavaResolveResult.EMPTY_ARRAY; } PsiFile file = SharedImplUtil.getContainingFile(fileElement); boolean valid = file != null && file.isValid(); if (!valid) { LOG.error("invalid!"); return JavaResolveResult.EMPTY_ARRAY; } final MethodReferenceResolver resolver = new MethodReferenceResolver(); final Map<PsiMethodReferenceExpression, PsiType> map = PsiMethodReferenceUtil.ourRefs.get(); if (map != null && map.containsKey(this)) { return (JavaResolveResult[])resolver.resolve(this, incompleteCode); } ResolveResult[] results = ResolveCache.getInstance(getProject()).resolveWithCaching(this, resolver, true, incompleteCode,file); return results.length == 0 ? JavaResolveResult.EMPTY_ARRAY : (JavaResolveResult[])results; }
protected final PsiElement getParentByStub() { final StubElement<?> stub = getStub(); if (stub != null) { return stub.getParentStub().getPsi(); } return SharedImplUtil.getParent(getNode()); }
public <Stub extends StubElement, Psi extends PsiElement> Psi[] getStubOrPsiChildren(final IStubElementType<Stub, Psi> elementType, Psi[] array) { T stub = myStub; if (stub != null) { //noinspection unchecked return (Psi[])stub.getChildrenByType(elementType, array); } else { final ASTNode[] nodes = SharedImplUtil.getChildrenOfType(getNode(), elementType); Psi[] psiElements = (Psi[])Array.newInstance(array.getClass().getComponentType(), nodes.length); for (int i = 0; i < nodes.length; i++) { psiElements[i] = (Psi)nodes[i].getPsi(); } return psiElements; } }
public <Stub extends StubElement, Psi extends PsiElement> Psi[] getStubOrPsiChildren(final IStubElementType<Stub, Psi> elementType, ArrayFactory<Psi> f) { T stub = myStub; if (stub != null) { //noinspection unchecked return (Psi[])stub.getChildrenByType(elementType, f); } else { final ASTNode[] nodes = SharedImplUtil.getChildrenOfType(getNode(), elementType); Psi[] psiElements = f.create(nodes.length); for (int i = 0; i < nodes.length; i++) { psiElements[i] = (Psi)nodes[i].getPsi(); } return psiElements; } }
public ASTNode encodeXmlTextContents(String displayText, PsiElement text) { final ASTNode firstChild = text.getNode().getFirstChildNode(); boolean textAlreadyHasCDATA = firstChild != null && firstChild.getElementType() == XmlElementType.XML_CDATA; if ((textAlreadyHasCDATA || XmlUtil.toCode(displayText)) && displayText.length() > 0) { final FileElement dummyParent = createCDATAElement(text.getManager(), SharedImplUtil.findCharTableByTree(text.getNode()), displayText); return dummyParent.getFirstChildNode(); } else { return super.encodeXmlTextContents(displayText, text); } }
public static <T extends PsiElement> T handleContentChange(T element, TextRange range, String newContent, final IElementType tokenType) { CheckUtil.checkWritable(element); final CompositeElement attrNode = (CompositeElement)element.getNode(); final ASTNode valueNode = attrNode.findLeafElementAt(range.getStartOffset()); LOG.assertTrue(valueNode != null, "Leaf not found in " + attrNode + " at offset " + range.getStartOffset() + " in element " + element); final PsiElement elementToReplace = valueNode.getPsi(); String text; try { text = elementToReplace.getText(); final int offsetInParent = elementToReplace.getStartOffsetInParent(); String textBeforeRange = text.substring(0, range.getStartOffset() - offsetInParent); String textAfterRange = text.substring(range.getEndOffset()- offsetInParent, text.length()); text = textBeforeRange + newContent + textAfterRange; } catch(StringIndexOutOfBoundsException e) { LOG.error("Range: " + range + " in text: '" + element.getText() + "'", e); throw e; } final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(attrNode); final LeafElement newValueElement = Factory.createSingleLeafElement(tokenType, text, charTableByTree, element.getManager()); attrNode.replaceChildInternal(valueNode, newValueElement); return element; }
public HaxePsiBuilder(@NotNull Project project, @NotNull ParserDefinition parserDefinition, @NotNull Lexer lexer, @NotNull ASTNode chameleon, @NotNull CharSequence text) { super(project, parserDefinition, lexer, chameleon, text); psiFile = SharedImplUtil.getContainingFile(chameleon); setupDebugTraces(); }
/** * Please consider using {@link #getParent()} instead, because this method can return different results before and after AST is loaded. * @return a PSI element taken from parent stub (if present) or parent AST node. */ protected final PsiElement getParentByStub() { final StubElement<?> stub = getStub(); if (stub != null) { return stub.getParentStub().getPsi(); } return SharedImplUtil.getParent(getNode()); }
/** * @return the parent of this element. Uses stub hierarchy if possible, but might cause an expensive switch to AST * if the parent stub doesn't correspond to the parent AST node. */ @Override public PsiElement getParent() { T stub = getGreenStub(); if (stub != null && !((ObjectStubBase)stub).isDangling()) { return stub.getParentStub().getPsi(); } return SharedImplUtil.getParent(getNode()); }
@Override public PsiElement setName(@NotNull final String name) throws IncorrectOperationException { final PomModel model = PomManager.getModel(getProject()); final XmlAspect aspect = model.getModelAspect(XmlAspect.class); model.runTransaction(new PomTransactionBase(this, aspect) { @Override public PomModelEvent runInner() throws IncorrectOperationException { final String oldName = getName(); final XmlTagImpl dummyTag = (XmlTagImpl) XmlElementFactory.getInstance(getProject()).createTagFromText(XmlTagUtil.composeTagText(name, "aa")); final XmlTagImpl tag = XmlTagImpl.this; final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(tag); ASTNode child = XmlChildRole.START_TAG_NAME_FINDER.findChild(tag); LOG.assertTrue(child != null, "It seems '" + name + "' is not a valid tag name"); TreeElement tagElement = (TreeElement) XmlChildRole.START_TAG_NAME_FINDER.findChild(dummyTag); LOG.assertTrue(tagElement != null, "What's wrong with it? '" + name + "'"); tag.replaceChild(child, ChangeUtil.copyElement(tagElement, charTableByTree)); final ASTNode childByRole = XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(tag); if(childByRole != null) { final TreeElement treeElement = (TreeElement) XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(dummyTag); if(treeElement != null) { tag.replaceChild(childByRole, ChangeUtil.copyElement(treeElement, charTableByTree)); } } return XmlTagNameChangedImpl.createXmlTagNameChanged(model, tag, oldName); } }); return this; }
@Override public void setQualifierExpression(@Nullable PsiExpression newQualifier) throws IncorrectOperationException { final PsiExpression oldQualifier = getQualifierExpression(); if(newQualifier == null) { if(oldQualifier != null) { deleteChildInternal(oldQualifier.getNode()); } } else { if(oldQualifier != null) { oldQualifier.replace(newQualifier); } else { final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this); TreeElement dot = (TreeElement) findChildByRole(ChildRole.DOT); if(dot == null) { dot = Factory.createSingleLeafElement(JavaTokenType.DOT, ".", 0, 1, treeCharTab, getManager()); dot = addInternal(dot, dot, getFirstChildNode(), Boolean.TRUE); } addBefore(newQualifier, dot.getPsi()); } } }
@NotNull public static <T extends PsiJavaCodeReferenceElement> JavaResolveResult[] multiResolveImpl(@NotNull T element, boolean incompleteCode, @NotNull ResolveCache.PolyVariantContextResolver<? super T> resolver) { FileASTNode fileElement = SharedImplUtil.findFileElement(element.getNode()); if(fileElement == null) { PsiUtilCore.ensureValid(element); LOG.error("fileElement == null!"); return JavaResolveResult.EMPTY_ARRAY; } PsiFile psiFile = SharedImplUtil.getContainingFile(fileElement); PsiManager manager = psiFile == null ? null : psiFile.getManager(); if(manager == null) { PsiUtilCore.ensureValid(element); LOG.error("getManager() == null!"); return JavaResolveResult.EMPTY_ARRAY; } boolean valid = psiFile.isValid(); if(!valid) { PsiUtilCore.ensureValid(element); LOG.error("psiFile.isValid() == false!"); return JavaResolveResult.EMPTY_ARRAY; } if(element instanceof PsiMethodReferenceExpression) { // method refs: do not cache results during parent conflict resolving, acceptable checks, etc final Map<PsiElement, PsiType> map = LambdaUtil.ourFunctionTypes.get(); if(map != null && map.containsKey(element)) { return (JavaResolveResult[]) resolver.resolve(element, psiFile, incompleteCode); } } return multiResolveImpl(manager.getProject(), psiFile, element, incompleteCode, resolver); }
@Override public PsiElement getParent() { return SharedImplUtil.getParent(getNode()); }
@Override public PsiElement addRange(PsiElement first, PsiElement last) throws IncorrectOperationException { return SharedImplUtil.addRange(this, first, last, null, null); }
@Override public PsiElement addRangeBefore(@NotNull PsiElement first, @NotNull PsiElement last, PsiElement anchor) throws IncorrectOperationException { return SharedImplUtil.addRange(this, first, last, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); }
@Override public PsiElement addRangeAfter(PsiElement first, PsiElement last, PsiElement anchor) throws IncorrectOperationException { return SharedImplUtil.addRange(this, first, last, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE); }
@Override public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException { CompositeElement treeElement = calcTreeElement(); return SharedImplUtil.doReplace(this, treeElement, newElement); }
@Override public void acceptChildren(@NotNull PsiElementVisitor visitor) { SharedImplUtil.acceptChildren(visitor, calcTreeElement()); }
public void setReplaced(@NotNull ASTNode replaced) { CharTable charTableByTree = myNewNode.getTreeParent() == null ? null : SharedImplUtil.findCharTableByTree(myNewNode); setOldLength(((TreeElement)replaced).getNotCachedLength()); myOldNode = replaced; myOldNode.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree); }
@Override public PsiFile getContainingFile() { return SharedImplUtil.getContainingFile(getNode()); }
@Override public PsiElement getFirstChild() { return SharedImplUtil.getFirstChild(getNode()); }
@Override public PsiElement getLastChild() { return SharedImplUtil.getLastChild(getNode()); }
@Override public PsiElement getNextSibling() { return SharedImplUtil.getNextSibling(getNode()); }