use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.
the class DefaultAddAction method performElementAddition.
@Nullable
protected T performElementAddition() {
final DomElement parent = getParentDomElement();
final DomManager domManager = parent.getManager();
final TypeChooser[] oldChoosers = new TypeChooser[] { null };
final Type[] aClass = new Type[] { null };
final StableElement<T> result = new WriteCommandAction<StableElement<T>>(domManager.getProject(), DomUtil.getFile(parent)) {
@Override
protected void run(@NotNull Result<StableElement<T>> result) throws Throwable {
final DomElement parentDomElement = getParentDomElement();
final T t = (T) getDomCollectionChildDescription().addValue(parentDomElement, getElementType());
tuneNewValue(t);
aClass[0] = parent.getGenericInfo().getCollectionChildDescription(t.getXmlElementName()).getType();
oldChoosers[0] = domManager.getTypeChooserManager().getTypeChooser(aClass[0]);
final SmartPsiElementPointer pointer = SmartPointerManager.getInstance(getProject()).createSmartPsiElementPointer(t.getXmlTag());
domManager.getTypeChooserManager().registerTypeChooser(aClass[0], new TypeChooser() {
@Override
public Type chooseType(final XmlTag tag) {
if (tag == pointer.getElement()) {
return getElementType();
}
return oldChoosers[0].chooseType(tag);
}
@Override
public void distinguishTag(final XmlTag tag, final Type aClass) throws IncorrectOperationException {
oldChoosers[0].distinguishTag(tag, aClass);
}
@Override
public Type[] getChooserTypes() {
return oldChoosers[0].getChooserTypes();
}
});
result.setResult((StableElement<T>) t.createStableCopy());
}
}.execute().getResultObject();
if (result != null) {
domManager.getTypeChooserManager().registerTypeChooser(aClass[0], oldChoosers[0]);
return result.getWrappedElement();
}
return null;
}
use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.
the class XmlAttributeValueImpl method updateText.
@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()).createAttribute("q", contents, this);
XmlAttributeValue newValue = newAttribute.getValueElement();
CheckUtil.checkWritable(this);
replaceAllChildrenToChildrenOf(newValue.getNode());
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return this;
}
use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.
the class XmlTextImpl method insertAtOffset.
@Override
public XmlElement insertAtOffset(final XmlElement element, final int displayOffset) throws IncorrectOperationException {
if (element instanceof XmlText) {
insertText(((XmlText) element).getValue(), displayOffset);
} else {
final PomModel model = PomManager.getModel(getProject());
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
model.runTransaction(new PomTransactionBase(getParent(), aspect) {
@Override
public PomModelEvent runInner() throws IncorrectOperationException {
final XmlTag tag = getParentTag();
assert tag != null;
final XmlText rightPart = _splitText(displayOffset);
PsiElement result;
if (rightPart != null) {
result = tag.addBefore(element, rightPart);
} else {
result = tag.addAfter(element, XmlTextImpl.this);
}
return createEvent(new XmlTagChildAddImpl(tag, (XmlTagChild) result));
}
});
}
return this;
}
use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.
the class XmlTextImpl method removeText.
@Override
public void removeText(int displayStart, int displayEnd) throws IncorrectOperationException {
final String value = getValue();
final int physicalStart = displayToPhysical(displayStart);
final PsiElement psiElement = findElementAt(physicalStart);
if (psiElement != null) {
final IElementType elementType = psiElement.getNode().getElementType();
final int elementDisplayEnd = physicalToDisplay(psiElement.getStartOffsetInParent() + psiElement.getTextLength());
final int elementDisplayStart = physicalToDisplay(psiElement.getStartOffsetInParent());
if (elementType == XmlTokenType.XML_DATA_CHARACTERS || elementType == TokenType.WHITE_SPACE) {
if (elementDisplayEnd >= displayEnd && elementDisplayStart <= displayStart) {
int physicalEnd = physicalStart;
while (physicalEnd < getTextRange().getLength()) {
if (physicalToDisplay(physicalEnd) == displayEnd)
break;
physicalEnd++;
}
int removeStart = physicalStart - psiElement.getStartOffsetInParent();
int removeEnd = physicalEnd - psiElement.getStartOffsetInParent();
final String oldElementText = psiElement.getText();
final String newElementText = oldElementText.substring(0, removeStart) + oldElementText.substring(removeEnd);
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 oldText = getText();
if (!newElementText.isEmpty()) {
final ASTNode e = getPolicy().encodeXmlTextContents(newElementText, XmlTextImpl.this);
replaceChild(psiElement.getNode(), e);
} else {
psiElement.delete();
}
clearCaches();
return XmlTextChangedImpl.createXmlTextChanged(model, XmlTextImpl.this, oldText);
}
});
return;
}
}
}
if (displayStart == 0 && displayEnd == value.length()) {
delete();
} else {
setValue(new StringBuffer(getValue()).replace(displayStart, displayEnd, "").toString());
}
}
use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.
the class XmlTagImpl method deleteChildInternal.
@Override
public void deleteChildInternal(@NotNull final ASTNode child) {
final PomModel model = PomManager.getModel(getProject());
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
try {
model.runTransaction(new PomTransactionBase(this, aspect) {
@Override
public PomModelEvent runInner() {
final String name = ((XmlAttribute) child).getName();
XmlTagImpl.super.deleteChildInternal(child);
return XmlAttributeSetImpl.createXmlAttributeSet(model, XmlTagImpl.this, name, null);
}
});
} catch (IncorrectOperationException e) {
LOG.error(e);
}
} else {
final ASTNode treePrev = child.getTreePrev();
final ASTNode treeNext = child.getTreeNext();
super.deleteChildInternal(child);
if (treePrev != null && treeNext != null && treePrev.getElementType() == XmlElementType.XML_TEXT && treeNext.getElementType() == XmlElementType.XML_TEXT) {
final XmlText prevText = (XmlText) treePrev.getPsi();
final XmlText nextText = (XmlText) treeNext.getPsi();
final String newValue = prevText.getValue() + nextText.getValue();
// merging two XmlText-s should be done in one transaction to preserve smart pointers
ChangeUtil.prepareAndRunChangeAction(new ChangeUtil.ChangeAction() {
@Override
public void makeChange(TreeChangeEvent destinationTreeChange) {
PsiElement anchor = prevText.getPrevSibling();
prevText.delete();
nextText.delete();
XmlText text = (XmlText) addAfter(XmlElementFactory.getInstance(getProject()).createDisplayText("x"), anchor);
text.setValue(newValue);
}
}, this);
}
}
}
Aggregations