@NotNull public static PsiElement findCodeFragment(@NotNull PsiElement element) { PsiElement codeFragment = element; PsiElement parent = codeFragment.getParent(); while (parent != null) { if (parent instanceof PsiDirectory || parent instanceof PsiMethod || parent instanceof PsiField || parent instanceof PsiClassInitializer || parent instanceof DummyHolder || parent instanceof PsiLambdaExpression) { break; } codeFragment = parent; parent = parent.getParent(); } return codeFragment; }
public void testMergeWhenEmptyElementAfterWhitespaceIsLastChild() throws Throwable { myBuilder = createBuilder(" foo bar"); parseWhenEmptyElementAfterWhitespaceIsLastChild(); final ASTNode tree = myBuilder.getTreeBuilt(); new DummyHolder(getPsiManager(), (TreeElement)tree, null); myBuilder = createBuilder(" bar", tree); parseWhenEmptyElementAfterWhitespaceIsLastChild(); DebugUtil.startPsiModification(null); try { myBuilder.getTreeBuilt(); fail(); } catch (BlockSupport.ReparsedSuccessfullyException e) { e.getDiffLog().performActualPsiChange(tree.getPsi().getContainingFile()); } finally { DebugUtil.finishPsiModification(); } assertEquals(" bar", tree.getText()); }
@Nullable public PsiElement createElementFromText(@Nullable final String text, @NotNull final Language language, @NotNull final IElementType type, @Nullable final PsiElement context) { if (text == null) return null; final DummyHolder result = DummyHolderFactory.createHolder(myManager, language, context); final FileElement holder = result.getTreeElement(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language); if (parserDefinition == null) { throw new AssertionError("No parser definition for " + language); } final Project project = myManager.getProject(); final Lexer lexer = parserDefinition.createLexer(project); final PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, holder, lexer, language, text); final ASTNode node = parserDefinition.createParser(project).parse(type, builder); holder.rawAddChildren((TreeElement)node); markGenerated(result); return node.getPsi(); }
@NotNull static PsiElement loadTree(@NotNull PsiElement host, @NotNull PsiFile containingFile) { if (containingFile instanceof DummyHolder) { PsiElement context = containingFile.getContext(); if (context != null) { PsiFile topFile = context.getContainingFile(); topFile.getNode(); //load tree TextRange textRange = host.getTextRange().shiftRight(context.getTextRange().getStartOffset()); PsiElement inLoadedTree = PsiTreeUtil.findElementOfClassAtRange(topFile, textRange.getStartOffset(), textRange.getEndOffset(), host.getClass()); if (inLoadedTree != null) { host = inLoadedTree; } } } return host; }
public static PsiElement findCodeFragment(PsiElement element) { PsiElement codeFragment = element; PsiElement parent = codeFragment.getParent(); while (parent != null) { if (parent instanceof PsiDirectory || parent instanceof PsiMethod || parent instanceof PsiField || parent instanceof PsiClassInitializer || parent instanceof DummyHolder || parent instanceof PsiLambdaExpression) { break; } codeFragment = parent; parent = parent.getParent(); } return codeFragment; }
public void testMergeWhenEmptyElementAfterWhitespaceIsLastChild() throws Throwable { myBuilder = createBuilder(" foo bar"); parseWhenEmptyElementAfterWhitespaceIsLastChild(); final ASTNode tree = myBuilder.getTreeBuilt(); new DummyHolder(getPsiManager(), (TreeElement)tree, null); myBuilder = createBuilder(" bar", tree); parseWhenEmptyElementAfterWhitespaceIsLastChild(); try { myBuilder.getTreeBuilt(); fail(); } catch (BlockSupport.ReparsedSuccessfullyException e) { e.getDiffLog().performActualPsiChange(tree.getPsi().getContainingFile()); } assertEquals(" bar", tree.getText()); }
@Nullable public PsiElement createElementFromText(@Nullable final String text, @Nonnull final Language language, @Nonnull final LanguageVersion languageVersion, @Nonnull final IElementType type, @Nullable final PsiElement context) { if (text == null) return null; final DummyHolder result = DummyHolderFactory.createHolder(myManager, language, context); final FileElement holder = result.getTreeElement(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language); if (parserDefinition == null) { throw new AssertionError("No parser definition for " + language); } final Project project = myManager.getProject(); final Lexer lexer = parserDefinition.createLexer(languageVersion); final PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, holder, lexer, language, languageVersion, text); final ASTNode node = parserDefinition.createParser(languageVersion).parse(type, builder, languageVersion); holder.rawAddChildren((TreeElement)node); markGenerated(result); return node.getPsi(); }
@Nonnull static PsiElement loadTree(@Nonnull PsiElement host, @Nonnull PsiFile containingFile) { if (containingFile instanceof DummyHolder) { PsiElement context = containingFile.getContext(); if (context != null) { PsiFile topFile = context.getContainingFile(); topFile.getNode(); //load tree TextRange textRange = host.getTextRange().shiftRight(context.getTextRange().getStartOffset()); PsiElement inLoadedTree = PsiTreeUtil.findElementOfClassAtRange(topFile, textRange.getStartOffset(), textRange.getEndOffset(), host.getClass()); if (inLoadedTree != null) { host = inLoadedTree; } } } return host; }
@NotNull @Override public PsiCatchSection createCatchSection(@NotNull final PsiType exceptionType, @NotNull final String exceptionName, @Nullable final PsiElement context) throws IncorrectOperationException { if(!(exceptionType instanceof PsiClassType || exceptionType instanceof PsiDisjunctionType)) { throw new IncorrectOperationException("Unexpected type:" + exceptionType); } @NonNls final String text = "catch (" + exceptionType.getCanonicalText(true) + " " + exceptionName + ") {}"; final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, CATCH_SECTION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if(!(element instanceof PsiCatchSection)) { throw new IncorrectOperationException("Incorrect catch section '" + text + "'. Parsed element: " + element); } final Project project = myManager.getProject(); final JavaPsiImplementationHelper helper = JavaPsiImplementationHelper.getInstance(project); helper.setupCatchBlock(exceptionName, context, (PsiCatchSection) element); final CodeStyleManager styleManager = CodeStyleManager.getInstance(project); final PsiCatchSection catchSection = (PsiCatchSection) styleManager.reformat(element); GeneratedMarkerVisitor.markGenerated(catchSection); return catchSection; }
protected PsiElement createEmptyPsiField() { final String text = "<undefined>"; final IElementType type = CsvTypes.FIELD; final Project project = this.element.getProject(); final PsiManager psiManager = PsiManager.getInstance(project); final DummyHolder dummyHolder = DummyHolderFactory.createHolder(psiManager, null); final FileElement fileElement = dummyHolder.getTreeElement(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(CsvLanguage.INSTANCE); final Lexer lexer = parserDefinition.createLexer(project); final PsiBuilder psiBuilder = PsiBuilderFactory.getInstance().createBuilder(project, fileElement, lexer, CsvLanguage.INSTANCE, text); final ASTNode node = parserDefinition.createParser(project).parse(type, psiBuilder); fileElement.rawAddChildren((com.intellij.psi.impl.source.tree.TreeElement)node); return node.getPsi(); }
@NotNull @Override public PsiAnnotation createAnnotationFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, ANNOTATION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiAnnotation)) { throw new IncorrectOperationException("Incorrect annotation '" + text + "'"); } return (PsiAnnotation)element; }
@NotNull @Override public PsiField createFieldFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, DECLARATION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiField)) { throw new IncorrectOperationException("Incorrect field '" + text + "'"); } return (PsiField)element; }
@NotNull @Override public PsiMethod createMethodFromText(@NotNull final String text, @Nullable final PsiElement context, final LanguageLevel level) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, DECLARATION, level), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiMethod)) { throw newException("Incorrect method '" + text + "'", holder); } return (PsiMethod)element; }
@NotNull @Override public PsiParameter createParameterFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, PARAMETER, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiParameter)) { throw new IncorrectOperationException("Incorrect parameter '" + text + "'"); } return (PsiParameter)element; }
@NotNull @Override public PsiResourceVariable createResourceFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, RESOURCE, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiResourceVariable)) { throw new IncorrectOperationException("Incorrect resource '" + text + "'"); } return (PsiResourceVariable)element; }
@NotNull @Override public PsiTypeElement createTypeElementFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final LanguageLevel level = level(context); final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, TYPE, level), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiTypeElement)) { throw new IncorrectOperationException("Incorrect type '" + text + "' (" + level + ")"); } return (PsiTypeElement)element; }
@NotNull @Override public PsiJavaCodeReferenceElement createReferenceFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final boolean isStaticImport = context instanceof PsiImportStaticStatement && !((PsiImportStaticStatement)context).isOnDemand(); final boolean mayHaveDiamonds = context instanceof PsiNewExpression && PsiUtil.getLanguageLevel(context).isAtLeast(LanguageLevel.JDK_1_7); final JavaParserUtil.ParserWrapper wrapper = isStaticImport ? STATIC_IMPORT_REF : mayHaveDiamonds ? DIAMOND_REF : REFERENCE; final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, wrapper, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiJavaCodeReferenceElement)) { throw new IncorrectOperationException("Incorrect reference '" + text + "'"); } return (PsiJavaCodeReferenceElement)element; }
@NotNull @Override public PsiCodeBlock createCodeBlockFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, CODE_BLOCK, level(context), true), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiCodeBlock)) { throw new IncorrectOperationException("Incorrect code block '" + text + "'"); } return (PsiCodeBlock)element; }
@NotNull @Override public PsiStatement createStatementFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, STATEMENT, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiStatement)) { throw new IncorrectOperationException("Incorrect statement '" + text + "'"); } return (PsiStatement)element; }
@NotNull @Override public PsiExpression createExpressionFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, EXPRESSION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiExpression)) { throw new IncorrectOperationException("Incorrect expression '" + text + "'"); } return (PsiExpression)element; }
@NotNull @Override public PsiTypeParameter createTypeParameterFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, TYPE_PARAMETER, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiTypeParameter)) { throw new IncorrectOperationException("Incorrect type parameter '" + text + "'"); } return (PsiTypeParameter)element; }
@NotNull @Override public PsiEnumConstant createEnumConstantFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, ENUM_CONSTANT, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiEnumConstant)) { throw new IncorrectOperationException("Incorrect enum constant '" + text + "'"); } return (PsiEnumConstant)element; }
private static IncorrectOperationException newException(final String msg, final DummyHolder holder) { final FileElement root = holder.getTreeElement(); if (root instanceof JavaDummyElement) { final Throwable cause = ((JavaDummyElement)root).getParserError(); if (cause != null) { return new IncorrectOperationException(msg, cause); } } return new IncorrectOperationException(msg); }
public static PsiAnnotationMemberValue createMemberValueFromText(@NotNull String text, @NotNull PsiManager manager, @NotNull ClsElementImpl parent) { LanguageLevel level = PsiUtil.getLanguageLevel(parent); DummyHolder holder = DummyHolderFactory.createHolder(manager, new JavaDummyElement(text, ANNOTATION_VALUE, level), null); PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiAnnotationMemberValue)) { LOG.error("Could not parse initializer:'" + text + "'"); return null; } return getMemberValue(element, parent); }
public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, @NotNull TreeElement newChild, @NotNull PsiManager manager) { CompositeElement parenthExpr = ASTFactory.composite(parenthType); TreeElement dummyExpr = (TreeElement)newChild.clone(); final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild); new DummyHolder(manager, parenthExpr, null, charTableByTree); parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "(")); parenthExpr.rawAddChildren(dummyExpr); parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")")); try { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr)); parenthExpr = (CompositeElement)SourceTreeToPsiMap.psiToTreeNotNull(formatted); } catch (IncorrectOperationException e) { LOG.error(e); // should not happen } newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild)); dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild); // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr)); return parenthExpr; }
@NotNull public static LeafElement createSingleLeafElement(@NotNull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, PsiFile originalFile) { DummyHolder dummyHolder = DummyHolderFactory.createHolder(manager, table, type.getLanguage()); dummyHolder.setOriginalFile(originalFile); FileElement holderElement = dummyHolder.getTreeElement(); LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern( buffer, startOffset, endOffset)); holderElement.rawAddChildren(newElement); CodeEditUtil.setNodeGenerated(newElement, true); return newElement; }
private boolean isNonPhysicalOrInjected() { FileElement fileElement = TreeUtil.getFileElement(this); if (fileElement == null || fileElement instanceof DummyHolderElement) return true; if (fileElement.getTreeParent() != null) return true; // dummy holder PsiElement wrapper = this instanceof PsiElement ? (PsiElement)this : myWrapper; if (wrapper == null) return true; PsiFile psiFile = wrapper.getContainingFile(); return psiFile == null || psiFile instanceof DummyHolder || psiFile.getViewProvider() instanceof FreeThreadedFileViewProvider || !psiFile.isPhysical(); }
@Override public void addLeaf(@NotNull final IElementType leafType, final CharSequence leafText, final ASTNode anchorBefore) { FileElement holder = new DummyHolder(getManager(), null).getTreeElement(); final LeafElement leaf = ASTFactory.leaf(leafType, holder.getCharTable().intern(leafText)); CodeEditUtil.setNodeGenerated(leaf, true); holder.rawAddChildren(leaf); addChild(leaf, anchorBefore); }
public static TreeElement copyToElement(PsiElement original) { final DummyHolder holder = DummyHolderFactory.createHolder(original.getManager(), null, original.getLanguage()); final FileElement holderElement = holder.getTreeElement(); final TreeElement treeElement = generateTreeElement(original, holderElement.getCharTable(), original.getManager()); // TreeElement treePrev = treeElement.getTreePrev(); // This is hack to support bug used in formater holderElement.rawAddChildren(treeElement); TreeUtil.clearCaches(holderElement); // treeElement.setTreePrev(treePrev); saveIndentationToCopy((TreeElement)original.getNode(), treeElement); return treeElement; }
static @Nullable VirtualFile getElementFile(@NotNull PsiElement elem) { PsiFile file = elem.getContainingFile(); if (file == null || file instanceof DummyHolder) { if (elem.getContext() != null) return getElementFile(elem.getContext()); else if (elem.getParent() != null) return getElementFile(elem.getParent()); else return null; } else { return file.getVirtualFile(); } }
@NotNull @Override public PsiAnnotation createAnnotationFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, ANNOTATION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiAnnotation)) { throw new IncorrectOperationException("Incorrect annotation \"" + text + "\"."); } return (PsiAnnotation)element; }
@NotNull @Override public PsiField createFieldFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, DECLARATION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiField)) { throw new IncorrectOperationException("Incorrect field \"" + text + "\"."); } return (PsiField)element; }
@NotNull @Override public PsiMethod createMethodFromText(@NotNull final String text, @Nullable final PsiElement context, final LanguageLevel level) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, DECLARATION, level), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiMethod)) { throw newException("Incorrect method \"" + text + "\".", holder); } return (PsiMethod)element; }
@NotNull @Override public PsiParameter createParameterFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, PARAMETER, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiParameter)) { throw new IncorrectOperationException("Incorrect parameter \"" + text + "\"."); } return (PsiParameter)element; }
@NotNull @Override public PsiResourceVariable createResourceFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, RESOURCE, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiResourceVariable)) { throw new IncorrectOperationException("Incorrect resource \"" + text + "\"."); } return (PsiResourceVariable)element; }
@NotNull @Override public PsiTypeElement createTypeElementFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, TYPE, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiTypeElement)) { throw new IncorrectOperationException("Incorrect type \"" + text + "\"."); } return (PsiTypeElement)element; }
@NotNull @Override public PsiJavaCodeReferenceElement createReferenceFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final boolean isStaticImport = context instanceof PsiImportStaticStatement && !((PsiImportStaticStatement)context).isOnDemand(); final boolean mayHaveDiamonds = context instanceof PsiNewExpression && PsiUtil.getLanguageLevel(context).isAtLeast(LanguageLevel.JDK_1_7); final JavaParserUtil.ParserWrapper wrapper = isStaticImport ? STATIC_IMPORT_REF : mayHaveDiamonds ? DIAMOND_REF : REFERENCE; final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, wrapper, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiJavaCodeReferenceElement)) { throw new IncorrectOperationException("Incorrect reference \"" + text + "\"."); } return (PsiJavaCodeReferenceElement)element; }