@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; } }
@Override public void delete() throws IncorrectOperationException { PsiElement parent = getParent(); if (parent instanceof ASTDelegatePsiElement) { CheckUtil.checkWritable(this); ((ASTDelegatePsiElement)parent).deleteChildInternal(getNode()); } else if (parent instanceof CompositeElement) { CheckUtil.checkWritable(this); ((CompositeElement)parent).deleteChildInternal(getNode()); } else if (parent instanceof PsiFile) { CheckUtil.checkWritable(this); parent.deleteChildRange(this, this); } else { throw new UnsupportedOperationException(getClass().getName() + " under " + (parent == null ? "null" : parent.getClass().getName())); } }
@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 PsiLanguageInjectionHost updateText(@NotNull String text) { try { final String quoteChar = getTextLength() > 0 ? getText().substring(0, 1) : ""; String contents = StringUtil.containsAnyChar(quoteChar, "'\"") ? StringUtil.trimEnd(StringUtil.trimStart(text, quoteChar), quoteChar) : text; XmlAttribute newAttribute = XmlElementFactory.getInstance(getProject()).createXmlAttribute("q", contents); XmlAttributeValue newValue = newAttribute.getValueElement(); CheckUtil.checkWritable(this); replaceAllChildrenToChildrenOf(newValue.getNode()); } catch (IncorrectOperationException e) { LOG.error(e); } return this; }
private void formatInternal(Formatter formatter, PsiFile file, List<Range<Integer>> ranges) throws IncorrectOperationException { ApplicationManager.getApplication().assertWriteAccessAllowed(); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); CheckUtil.checkWritable(file); Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file); if (document != null) { try { List<Replacement> replacements = formatter .getFormatReplacements(document.getText(), ranges) .stream() .sorted( comparing((Replacement r) -> r.getReplaceRange().lowerEndpoint()).reversed()) .collect(Collectors.toList()); performReplacements(document, replacements); } catch (FormatterException e) { // Do not format on errors } } }
@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 & UNCOMPLETE_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; } }
@Override public void delete() throws IncorrectOperationException { PsiElement parent = getParent(); if (parent instanceof ASTDelegatePsiElement) { CheckUtil.checkWritable(this); ((ASTDelegatePsiElement)parent).deleteChildInternal(getNode()); } else if (parent instanceof CompositePsiElement) { CheckUtil.checkWritable(this); ((CompositePsiElement)parent).deleteChildInternal(getNode()); } else if (parent instanceof PsiFile) { CheckUtil.checkWritable(this); parent.deleteChildRange(this, this); } else { throw new UnsupportedOperationException(getClass().getName() + " under " + (parent == null ? "null" : parent.getClass().getName())); } }
@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()).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()); final PsiElement formatted = SourceTreeToPsiMap.treeElementToPsi(codeFormatter.processRange(treeElement, startOffset, endOffset)); return canChangeWhiteSpacesOnly ? formatted : postProcessElement(formatted); }
public PsiLanguageInjectionHost updateText(@NotNull String text) { try { final String quoteChar = getTextLength() > 0 ? getText().substring(0, 1) : ""; String contents = StringUtil.containsAnyChar(quoteChar, "'\"") ? StringUtil.trimEnd(StringUtil.trimStart(text, quoteChar), quoteChar) : text; XmlAttribute newAttribute = XmlElementFactory.getInstance(getProject()).createXmlAttribute("q", contents); XmlAttributeValue newValue = newAttribute.getValueElement(); CheckUtil.checkWritable(this); replaceAllChildrenToChildrenOf(newValue.getNode()); } catch (IncorrectOperationException e) { LOG.error(e); } return this; }
@Override public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { CheckUtil.checkWritable(this); if(isReferenceTo(element) || !isPhysical()) { return this; } if(element instanceof PsiMethod) { return handleElementRename(((PsiMethod) element).getName()); } else if(element instanceof PsiClass) { return this; } else { throw new IncorrectOperationException(element.toString()); } }
@Override public void deleteChildRange(PsiElement first, PsiElement last) throws IncorrectOperationException { CheckUtil.checkWritable(this); if(first == null) { LOG.assertTrue(last == null); return; } ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first); ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last); CompositeElement treeElement = calcTreeElement(); LOG.assertTrue(firstElement.getTreeParent() == treeElement); LOG.assertTrue(lastElement.getTreeParent() == treeElement); CodeEditUtil.removeChildren(treeElement, firstElement, lastElement); }
@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; } }
@Override public void optimizeImports(@NotNull PsiFile file) throws IncorrectOperationException { CheckUtil.checkWritable(file); if(file instanceof PsiJavaFile) { PsiImportList newList = prepareOptimizeImportsResult((PsiJavaFile) file); if(newList != null) { final PsiImportList importList = ((PsiJavaFile) file).getImportList(); if(importList != null) { importList.replace(newList); } } } }
public static void delete(@NotNull FrogAttribute attribute) throws IncorrectOperationException { // see ASTDelegatePsiElement.delete PsiElement parent = attribute.getParent(); if (parent instanceof ASTDelegatePsiElement) { CheckUtil.checkWritable(attribute); ASTNode node = attribute.getNode(); ASTNode nextNode = node.getTreeNext(); ((ASTDelegatePsiElement) parent).deleteChildInternal(node); if (nextNode != null && nextNode.getElementType() == FrogTypes.FROG_SEMICOLON) { parent.getNode().removeChild(nextNode); } } else { throw new UnsupportedOperationException(attribute.getClass().getName() + " under " + (parent == null ? "null" : parent.getClass().getName())); } }
@Override public PsiElement replace( PsiElement newElement ) throws IncorrectOperationException { // just add new element to the containing class final PsiClass containingClass = getContainingClass(); if( null != containingClass ) { CheckUtil.checkWritable( containingClass ); return containingClass.add( newElement ); } return null; }
@Override public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); calcTreeElement().addInternal(elementCopy, elementCopy, null, null); elementCopy = ChangeUtil.decodeInformation(elementCopy); return SourceTreeToPsiMap.treeElementToPsi(elementCopy); }
@Override public PsiElement addBefore(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); elementCopy = ChangeUtil.decodeInformation(elementCopy); return SourceTreeToPsiMap.treeElementToPsi(elementCopy); }
@Override public PsiElement addAfter(@NotNull PsiElement element, @Nullable PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE); elementCopy = ChangeUtil.decodeInformation(elementCopy); return SourceTreeToPsiMap.treeElementToPsi(elementCopy); }
@Override public void delete() throws IncorrectOperationException { ASTNode treeElement = calcTreeElement(); LOG.assertTrue(treeElement.getTreeParent() != null); CheckUtil.checkWritable(this); ((CompositeElement)treeElement.getTreeParent()).deleteChildInternal(treeElement); }
@Override public void deleteChildRange(PsiElement first, PsiElement last) throws IncorrectOperationException { CheckUtil.checkWritable(this); if (first == null) { LOG.assertTrue(last == null); return; } ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first); ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last); CompositeElement treeElement = calcTreeElement(); LOG.assertTrue(firstElement.getTreeParent() == treeElement); LOG.assertTrue(lastElement.getTreeParent() == treeElement); CodeEditUtil.removeChildren(treeElement, firstElement, lastElement); }
@Override public PsiElement handleElementRename(final String newElementName) throws IncorrectOperationException { CheckUtil.checkWritable(this); final PsiElement firstChildNode = ObjectUtils.assertNotNull(getFirstChild()); final PsiElement firstInIdentifier = getClass().isInstance(firstChildNode) ? ObjectUtils.assertNotNull(firstChildNode.getNextSibling()).getNextSibling() : firstChildNode; getNode().removeRange(firstInIdentifier.getNode(), null); final PsiElement referenceName = ObjectUtils.assertNotNull(parseReference(newElementName).getReferenceNameElement()); getNode().addChild(referenceName.getNode()); return this; }
@Override public void shortenClassReferences(@NotNull PsiElement element, int startOffset, int endOffset) throws IncorrectOperationException { CheckUtil.checkWritable(element); if (SourceTreeToPsiMap.hasTreeElement(element)) { final ReferenceAdjuster adjuster = ReferenceAdjuster.Extension.getReferenceAdjuster(element.getLanguage()); if (adjuster != null) { adjuster.processRange(element.getNode(), startOffset, endOffset, myProject); } } }
@Override public void optimizeImports(@NotNull PsiFile file) throws IncorrectOperationException { CheckUtil.checkWritable(file); if (file instanceof PsiJavaFile) { PsiImportList newList = prepareOptimizeImportsResult((PsiJavaFile)file); if (newList != null) { final PsiImportList importList = ((PsiJavaFile)file).getImportList(); if (importList != null) { importList.replace(newList); } } } }
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); if (treeElement != null) { if (treeElement instanceof TreeElement) { return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi(); } return treeElement.getPsi(); } throw new IncorrectOperationException("Element cannot be added"); }
@Override public PsiElement addAfter(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE); if (treeElement instanceof TreeElement) { return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi(); } return treeElement.getPsi(); }
@Override public void deleteChildRange(final PsiElement first, final PsiElement last) throws IncorrectOperationException { CheckUtil.checkWritable(this); ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first); ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last); LOG.assertTrue(firstElement.getTreeParent() == getNode()); LOG.assertTrue(lastElement.getTreeParent() == getNode()); CodeEditUtil.removeChildren(getNode(), firstElement, lastElement); }
@Override public PsiElement replace(@NotNull final PsiElement newElement) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(newElement); if (getParent() instanceof ASTDelegatePsiElement) { final ASTDelegatePsiElement parentElement = (ASTDelegatePsiElement)getParent(); parentElement.replaceChildInternal(this, elementCopy); } else { CodeEditUtil.replaceChild(getParent().getNode(), getNode(), elementCopy); } elementCopy = ChangeUtil.decodeInformation(elementCopy); return SourceTreeToPsiMap.treeElementToPsi(elementCopy); }
public static PsiElement addRange(PsiElement thisElement, PsiElement first, PsiElement last, ASTNode anchor, Boolean before) throws IncorrectOperationException { CheckUtil.checkWritable(thisElement); final CharTable table = findCharTableByTree(SourceTreeToPsiMap.psiElementToTree(thisElement)); TreeElement copyFirst = null; ASTNode copyLast = null; ASTNode next = SourceTreeToPsiMap.psiElementToTree(last).getTreeNext(); ASTNode parent = null; for (ASTNode element = SourceTreeToPsiMap.psiElementToTree(first); element != next; element = element.getTreeNext()) { TreeElement elementCopy = ChangeUtil.copyElement((TreeElement)element, table); if (element == first.getNode()) { copyFirst = elementCopy; } if (element == last.getNode()) { copyLast = elementCopy; } if (parent == null) { parent = elementCopy.getTreeParent(); } else { if(elementCopy.getElementType() == TokenType.WHITE_SPACE) CodeEditUtil.setNodeGenerated(elementCopy, true); parent.addChild(elementCopy, null); } } if (copyFirst == null) return null; copyFirst = ((CompositeElement)SourceTreeToPsiMap.psiElementToTree(thisElement)).addInternal(copyFirst, copyLast, anchor, before); for (TreeElement element = copyFirst; element != null; element = element.getTreeNext()) { element = ChangeUtil.decodeInformation(element); if (element.getTreePrev() == null) { copyFirst = element; } } return SourceTreeToPsiMap.treeElementToPsi(copyFirst); }
public static PsiElement doReplace(PsiElement psiElement, TreeElement treeElement, PsiElement newElement) { CompositeElement treeParent = treeElement.getTreeParent(); LOG.assertTrue(treeParent != null); CheckUtil.checkWritable(psiElement); TreeElement elementCopy = ChangeUtil.copyToElement(newElement); treeParent.replaceChildInternal(treeElement, elementCopy); elementCopy = ChangeUtil.decodeInformation(elementCopy); final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy); treeElement.invalidate(); return result; }
@Override public void delete() throws IncorrectOperationException { LOG.assertTrue(getTreeParent() != null); CheckUtil.checkWritable(this); getTreeParent().deleteChildInternal(this); this.invalidate(); }
@Override public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException { LOG.assertTrue(getTreeParent() != null); CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(newElement); getTreeParent().replaceChildInternal(this, elementCopy); elementCopy = ChangeUtil.decodeInformation(elementCopy); final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy); this.invalidate(); return result; }
@Override public PsiElement addAfter(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE); return ChangeUtil.decodeInformation(treeElement).getPsi(); }
@Override public void delete() throws IncorrectOperationException { LOG.assertTrue(getTreeParent() != null, "Parent not found for " + this); CheckUtil.checkWritable(this); getTreeParent().deleteChildInternal(this); invalidate(); }
@Override public void deleteChildRange(PsiElement first, PsiElement last) throws IncorrectOperationException { CheckUtil.checkWritable(this); ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first); ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last); LOG.assertTrue(firstElement.getTreeParent() == this); LOG.assertTrue(lastElement.getTreeParent() == this); CodeEditUtil.removeChildren(this, firstElement, lastElement); }