private void _init(@Nullable final ASTNode child) { if (child == null) return; ASTNode treePrev = child.getTreePrev(); while (treePrev != null && isWhiteSpace(treePrev)) { treePrev = treePrev.getTreePrev(); } if (treePrev == null) { _init(child.getTreeParent()); } else { myChild2 = child; myType2 = myChild2.getElementType(); myChild1 = treePrev; myType1 = myChild1.getElementType(); final CompositeElement parent = (CompositeElement) treePrev.getTreeParent(); myParent = SourceTreeToPsiMap.treeElementToPsi(parent); } }
@NotNull @Override public PsiComment createCommentFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final PsiJavaFile aFile = createDummyJavaFile(text); for (PsiElement aChildren : aFile.getChildren()) { if (aChildren instanceof PsiComment) { if (!aChildren.getText().equals(text)) { break; } final PsiComment comment = (PsiComment)aChildren; DummyHolderFactory.createHolder(myManager, (TreeElement)SourceTreeToPsiMap.psiElementToTree(comment), context); return comment; } } throw new IncorrectOperationException("Incorrect comment '" + text + "'"); }
private void _init(@Nullable final ASTNode child) { if (child == null) return; ASTNode treePrev = child.getTreePrev(); while (treePrev != null && isWhiteSpace(treePrev)) { treePrev = treePrev.getTreePrev(); } if (treePrev == null) { _init(child.getTreeParent()); } else { myChild2 = child; myType2 = myChild2.getElementType(); myChild1 = treePrev; myType1 = myChild1.getElementType(); final CompositeElement parent = (CompositeElement)treePrev.getTreeParent(); myParent = SourceTreeToPsiMap.treeElementToPsi(parent); } }
protected Wrap chooseWrap(final ASTNode child, final Wrap tagBeginWrap, final Wrap attrWrap, final Wrap textWrap) { if (myNode.getElementType() == XmlElementType.XML_TEXT) return textWrap; final IElementType elementType = child.getElementType(); if (elementType == XmlElementType.XML_ATTRIBUTE) return attrWrap; if (elementType == XmlTokenType.XML_START_TAG_START) return tagBeginWrap; if (elementType == XmlTokenType.XML_END_TAG_START) { final PsiElement parent = SourceTreeToPsiMap.treeElementToPsi(child.getTreeParent()); if (parent instanceof XmlTag) { final XmlTag tag = (XmlTag)parent; if (canWrapTagEnd(tag)) { return getTagEndWrapping(tag); } } return null; } if (elementType == XmlElementType.XML_TEXT || elementType == XmlTokenType.XML_DATA_CHARACTERS) return textWrap; return null; }
@Override public void deleteChildInternal(@NotNull ASTNode child) { final boolean isForInitialization = getChildRole(child) == ChildRole.FOR_INITIALIZATION; if (isForInitialization) { try { final PsiStatement emptyStatement = JavaPsiFacade.getInstance(getProject()).getElementFactory().createStatementFromText(";", null); super.replaceChildInternal(child, (TreeElement)SourceTreeToPsiMap.psiElementToTree(emptyStatement)); } catch (IncorrectOperationException e) { LOG.error(e); } } else { super.deleteChildInternal(child); } }
@Override @NotNull public PsiCodeBlock[] getCatchBlocks() { ASTNode tryBlock = SourceTreeToPsiMap.psiElementToTree(getTryBlock()); if (tryBlock != null) { PsiCatchSection[] catchSections = getCatchSections(); if (catchSections.length == 0) return PsiCodeBlock.EMPTY_ARRAY; boolean lastIncomplete = catchSections[catchSections.length - 1].getCatchBlock() == null; PsiCodeBlock[] blocks = new PsiCodeBlock[lastIncomplete ? catchSections.length - 1 : catchSections.length]; for (int i = 0; i < blocks.length; i++) { blocks[i] = catchSections[i].getCatchBlock(); } return blocks; } return PsiCodeBlock.EMPTY_ARRAY; }
private PsiElement getTargetContext(PsiElement context, PsiElement place) { PsiElement targetContext = context; if (place.getParent() instanceof PsiDocMethodOrFieldRef) { PsiDocMethodOrFieldRef methodRef = (PsiDocMethodOrFieldRef) place.getParent(); final IElementType firstChildType = methodRef.getFirstChildNode().getElementType(); if (firstChildType == JavaElementType.JAVA_CODE_REFERENCE || firstChildType == JavaElementType.REFERENCE_EXPRESSION) { PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) SourceTreeToPsiMap.treeElementToPsi(methodRef.getFirstChildNode()); final PsiElement element = referenceElement.resolve(); if (element instanceof PsiClass) { targetContext = element.getFirstChild(); } } } return targetContext; }
@Nullable private PsiClass getScope(){ if (getFirstChildNode().getElementType() == JavaDocElementType.DOC_REFERENCE_HOLDER) { final PsiElement firstChildPsi = SourceTreeToPsiMap.treeElementToPsi(getFirstChildNode().getFirstChildNode()); if (firstChildPsi instanceof PsiJavaCodeReferenceElement) { PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)firstChildPsi; final PsiElement referencedElement = referenceElement.resolve(); if (referencedElement instanceof PsiClass) return (PsiClass)referencedElement; return null; } else if (firstChildPsi instanceof PsiKeyword) { final PsiKeyword keyword = (PsiKeyword)firstChildPsi; if (keyword.getTokenType().equals(THIS_KEYWORD)) { return JavaResolveUtil.getContextClass(this); } else if (keyword.getTokenType().equals(SUPER_KEYWORD)) { final PsiClass contextClass = JavaResolveUtil.getContextClass(this); if (contextClass != null) return contextClass.getSuperClass(); return null; } } } return JavaResolveUtil.getContextClass(this); }
@Override public PsiDocTag findTagByName(String name) { if (getFirstChildNode().getElementType() == JavaDocElementType.DOC_COMMENT) { if (getFirstChildNode().getText().indexOf(name) < 0) return null; } for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) { if (child.getElementType() == DOC_TAG) { PsiDocTag tag = (PsiDocTag)SourceTreeToPsiMap.treeElementToPsi(child); final CharSequence nameText = ((LeafElement)tag.getNameElement()).getChars(); if (nameText.length() > 0 && nameText.charAt(0) == '@' && CharArrayUtil.regionMatches(nameText, 1, name)) { return tag; } } } return null; }
private void init(final ASTNode child) { if (child == null) return; ASTNode treePrev = child.getTreePrev(); while (treePrev != null && isWhiteSpace(treePrev)) { treePrev = treePrev.getTreePrev(); } if (treePrev == null) { init(child.getTreeParent()); } else { myChild2 = child; myChild1 = treePrev; final CompositeElement parent = (CompositeElement)treePrev.getTreeParent(); myParent = SourceTreeToPsiMap.treeElementToPsi(parent); myRole1 = parent.getChildRole(treePrev); myType1 = treePrev.getElementType(); myRole2 = parent.getChildRole(child); myType2 = child.getElementType(); } }
@NotNull @Override public PsiElement shortenClassReferences(@NotNull PsiElement element, int flags) throws IncorrectOperationException { CheckUtil.checkWritable(element); if (!SourceTreeToPsiMap.hasTreeElement(element)) return element; final boolean addImports = (flags & DO_NOT_ADD_IMPORTS) == 0; final boolean incompleteCode = (flags & INCOMPLETE_CODE) != 0; final ReferenceAdjuster adjuster = ReferenceAdjuster.Extension.getReferenceAdjuster(element.getLanguage()); if (adjuster != null) { final ASTNode reference = adjuster.process(element.getNode(), addImports, incompleteCode, myProject); return SourceTreeToPsiMap.treeToPsiNotNull(reference); } else { return element; } }
private static TextRange getSignificantRange(final PsiFile file, final int offset) { final ASTNode elementAtOffset = SourceTreeToPsiMap.psiElementToTree(CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset)); if (elementAtOffset == null) { int significantRangeStart = CharArrayUtil.shiftBackward(file.getText(), offset - 1, "\r\t "); return new TextRange(Math.max(significantRangeStart, 0), offset); } final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file); final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset); if (textRange != null) { return textRange; } return elementAtOffset.getTextRange(); }
@Override @NotNull public PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { CheckUtil.checkWritable(element); if( !SourceTreeToPsiMap.hasTreeElement( element ) ) { return element; } ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(element); final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(new CodeFormatterFacade(getSettings(), element.getLanguage()).processElement(treeElement)); if (!canChangeWhiteSpacesOnly) { return postProcessElement(formatted); } return formatted; }
private PsiElement reformatRangeImpl(final PsiElement element, final int startOffset, final int endOffset, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { LOG.assertTrue(element.isValid()); CheckUtil.checkWritable(element); if( !SourceTreeToPsiMap.hasTreeElement( element ) ) { return element; } ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(element); final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(), element.getLanguage()); final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(codeFormatter.processRange(treeElement, startOffset, endOffset)); return canChangeWhiteSpacesOnly ? formatted : postProcessElement(formatted); }
@Override public Indent getIndent() { if (myNode.getElementType() == XmlElementType.XML_PROLOG || myNode.getElementType() == XmlElementType.XML_DOCTYPE || SourceTreeToPsiMap.treeElementToPsi(myNode) instanceof XmlDocument) { return Indent.getNoneIndent(); } return myIndent; }
@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; }
private static boolean isComment(final ASTNode node) { final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(node); if (psiElement instanceof PsiComment) return true; final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(psiElement.getLanguage()); if (parserDefinition == null) return false; final TokenSet commentTokens = parserDefinition.getCommentTokens(); return commentTokens.contains(node.getElementType()); }
@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; }
@Override public String getPlaceholderText(@NotNull ASTNode node) { final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(node); if (element instanceof PsiLiteralExpression) { return getI18nMessage(element.getProject(), (PsiLiteralExpression)element); } else if (element instanceof PsiMethodCallExpression) { return formatMethodCallExpression(element.getProject(), (PsiMethodCallExpression)element); } return element.getText(); }
@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; }
@Nullable private static ASTNode findPreviousWhiteSpace(final ASTNode leafElement, final IElementType whiteSpaceTokenType) { final int offset = leafElement.getTextRange().getStartOffset() - 1; if (offset < 0) return null; final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(leafElement); if (psiElement == null) { return null; } final PsiElement found = psiElement.getContainingFile().findElementAt(offset); if (found == null) return null; final ASTNode treeElement = found.getNode(); if (treeElement != null && treeElement.getElementType() == whiteSpaceTokenType) return treeElement; return null; }
@Override public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException { PsiElement mirrorElement = SourceTreeToPsiMap.treeToPsiNotNull(element); if (!(mirrorElement instanceof PsiJavaFile)) { throw new InvalidMirrorException("Unexpected mirror file: " + mirrorElement); } PsiJavaFile mirrorFile = (PsiJavaFile)mirrorElement; setMirrorIfPresent(getPackageStatement(), mirrorFile.getPackageStatement()); setMirrors(getClasses(), mirrorFile.getClasses()); }
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); if (treeElement != null) return ChangeUtil.decodeInformation(treeElement).getPsi(); throw new IncorrectOperationException("Element cannot be added"); }
protected static XmlTag getTag(final ASTNode node) { final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(node); if (element instanceof XmlTag) { return (XmlTag)element; } else { return null; } }
@Override public boolean isInternalReference() { for (ASTNode e = getFirstChildNode(); e != null; e = e.getTreeNext()) { if (e.getElementType() instanceof IXmlLeafElementType) { XmlToken token = (XmlToken)SourceTreeToPsiMap.treeElementToPsi(e); if (token.getTokenType() == XmlTokenType.XML_DOCTYPE_PUBLIC || token.getTokenType() == XmlTokenType.XML_DOCTYPE_SYSTEM) { return false; } } } return true; }
@Override public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException { setMirrorCheckingType(element, null); PsiField mirror = SourceTreeToPsiMap.treeToPsiNotNull(element); setMirrorIfPresent(getDocComment(), mirror.getDocComment()); setMirror(getModifierList(), mirror.getModifierList()); setMirror(getNameIdentifier(), mirror.getNameIdentifier()); }
@Override public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException { setMirrorCheckingType(element, null); PsiAnnotation mirror = SourceTreeToPsiMap.treeToPsiNotNull(element); setMirror(getNameReferenceElement(), mirror.getNameReferenceElement()); setMirror(getParameterList(), mirror.getParameterList()); }
@Override public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException { setMirrorCheckingType(element, null); PsiField mirror = SourceTreeToPsiMap.treeToPsiNotNull(element); setMirrorIfPresent(getDocComment(), mirror.getDocComment()); setMirror(getModifierList(), mirror.getModifierList()); setMirror(getTypeElement(), mirror.getTypeElement()); setMirror(getNameIdentifier(), mirror.getNameIdentifier()); }
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); }
@Override public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException { setMirrorCheckingType(element, null); PsiParameter mirror = SourceTreeToPsiMap.treeToPsiNotNull(element); setMirror(getModifierList(), mirror.getModifierList()); setMirror(getTypeElement(), mirror.getTypeElement()); }
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; }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if (DECLARED_ELEMENT_BIT_SET.contains(child.getElementType())) { PsiElement[] declaredElements = getDeclaredElements(); int length = declaredElements.length; if (length > 0) { if (length == 1) { getTreeParent().deleteChildInternal(this); return; } else { if (SourceTreeToPsiMap.psiElementToTree(declaredElements[length - 1]) == child) { removeCommaBefore(child); final LeafElement semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, SharedImplUtil.findCharTableByTree(this), getManager()); SourceTreeToPsiMap.psiElementToTree(declaredElements[length - 2]).addChild(semicolon, null); } else if (SourceTreeToPsiMap.psiElementToTree(declaredElements[0]) == child) { CompositeElement next = (CompositeElement)SourceTreeToPsiMap.psiElementToTree(declaredElements[1]); ASTNode copyChild = child.copyElement(); ASTNode nameChild = ((CompositeElement)copyChild).findChildByRole(ChildRole.NAME); removeCommaBefore(next); next.addInternal((TreeElement)copyChild.getFirstChildNode(), nameChild.getTreePrev(), null, Boolean.FALSE); } else { removeCommaBefore (child); } } } } super.deleteChildInternal(child); }
@Nullable public static TreeElement generateTreeElement(PsiElement original, CharTable table, final PsiManager manager) { PsiUtilCore.ensureValid(original); if (SourceTreeToPsiMap.hasTreeElement(original)) { return copyElement((TreeElement)SourceTreeToPsiMap.psiElementToTree(original), table); } else { for (TreeGenerator generator : Extensions.getExtensions(TreeGenerator.EP_NAME)) { final TreeElement element = generator.generateTreeFor(original, table, manager); if (element != null) return element; } return null; } }
@Override public PsiStatement findExitedStatement() { PsiIdentifier label = getLabelIdentifier(); if (label == null){ for(ASTNode parent = getTreeParent(); parent != null; parent = parent.getTreeParent()){ IElementType i = parent.getElementType(); if (i == FOR_STATEMENT || i == WHILE_STATEMENT || i == DO_WHILE_STATEMENT || i == SWITCH_STATEMENT || i == FOREACH_STATEMENT) { return (PsiStatement)SourceTreeToPsiMap.treeElementToPsi(parent); } else if (i == METHOD || i == CLASS_INITIALIZER) { return null; // do not pass through anonymous/local class } } } else{ String labelName = label.getText(); for(CompositeElement parent = getTreeParent(); parent != null; parent = parent.getTreeParent()){ if (parent.getElementType() == LABELED_STATEMENT){ ASTNode statementLabel = parent.findChildByRole(ChildRole.LABEL_NAME); if (statementLabel.getText().equals(labelName)){ return ((PsiLabeledStatement)SourceTreeToPsiMap.treeElementToPsi(parent)).getStatement(); } } if (parent.getElementType() == METHOD || parent.getElementType() == CLASS_INITIALIZER || parent.getElementType() == LAMBDA_EXPRESSION) return null; // do not pass through anonymous/local class } } return null; }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if (child.getElementType() == JavaElementType.CLASS) { PsiJavaFile file = SourceTreeToPsiMap.treeToPsiNotNull(this); if (file.getClasses().length == 1) { file.delete(); return; } } super.deleteChildInternal(child); }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before){ if (before == null){ if (first == last && (first.getElementType() == JavaElementType.IMPORT_STATEMENT || first.getElementType() == JavaElementType.IMPORT_STATIC_STATEMENT)){ final PsiImportList list = (PsiImportList)SourceTreeToPsiMap.treeElementToPsi(this); final PsiImportStatementBase statement = (PsiImportStatementBase)SourceTreeToPsiMap.treeElementToPsi(first); final JavaPsiImplementationHelper instance = JavaPsiImplementationHelper.getInstance(list.getProject()); if (instance != null) { anchor = instance.getDefaultImportAnchor(list, statement); } before = Boolean.TRUE; } } return super.addInternal(first, last, anchor, before); }