use of com.intellij.pom.event.PomModelEvent in project intellij-community by JetBrains.
the class XmlAttributeImpl method setValue.
@Override
public void setValue(String valueText) throws IncorrectOperationException {
final ASTNode value = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild(this);
final PomModel model = PomManager.getModel(getProject());
final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute("a", valueText, this);
final ASTNode newValue = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild((ASTNode) attribute);
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
model.runTransaction(new PomTransactionBase(this, aspect) {
@Override
public PomModelEvent runInner() {
final XmlAttributeImpl att = XmlAttributeImpl.this;
if (value != null) {
if (newValue != null) {
att.replaceChild(value, newValue.copyElement());
} else {
att.removeChild(value);
}
} else {
if (newValue != null) {
att.addChild(newValue.getTreePrev().copyElement());
att.addChild(newValue.copyElement());
}
}
return XmlAttributeSetImpl.createXmlAttributeSet(model, getParent(), getName(), newValue != null ? newValue.getText() : null);
}
});
}
use of com.intellij.pom.event.PomModelEvent in project intellij-community by JetBrains.
the class XmlAttributeImpl method setName.
@Override
public PsiElement setName(@NotNull final String nameText) throws IncorrectOperationException {
final ASTNode name = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(this);
final String oldName = name.getText();
final PomModel model = PomManager.getModel(getProject());
final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute(nameText, "", this);
final ASTNode newName = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild((ASTNode) attribute);
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
model.runTransaction(new PomTransactionBase(getParent(), aspect) {
@Override
public PomModelEvent runInner() {
final PomModelEvent event = new PomModelEvent(model);
PsiFile file = getContainingFile();
XmlChangeSet xmlAspectChangeSet = new XmlAspectChangeSetImpl(model, file instanceof XmlFile ? (XmlFile) file : null);
xmlAspectChangeSet.add(new XmlAttributeSetImpl(getParent(), oldName, null));
xmlAspectChangeSet.add(new XmlAttributeSetImpl(getParent(), nameText, getValue()));
event.registerChangeSet(model.getModelAspect(XmlAspect.class), xmlAspectChangeSet);
CodeEditUtil.replaceChild(XmlAttributeImpl.this, name, newName);
return event;
}
});
return this;
}
use of com.intellij.pom.event.PomModelEvent 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.pom.event.PomModelEvent in project intellij-community by JetBrains.
the class XmlTextImpl method insertText.
@Override
public void insertText(String text, int displayOffset) throws IncorrectOperationException {
if (text == null || text.isEmpty())
return;
final int physicalOffset = displayToPhysical(displayOffset);
final PsiElement psiElement = findElementAt(physicalOffset);
//if (!(psiElement instanceof XmlTokenImpl)) throw new IncorrectOperationException("Can't insert at offset: " + displayOffset);
final IElementType elementType = psiElement != null ? psiElement.getNode().getElementType() : null;
if (elementType == XmlTokenType.XML_DATA_CHARACTERS) {
int insertOffset = physicalOffset - psiElement.getStartOffsetInParent();
final String oldElementText = psiElement.getText();
final String newElementText = oldElementText.substring(0, insertOffset) + text + oldElementText.substring(insertOffset);
final PomModel model = PomManager.getModel(getProject());
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
model.runTransaction(new PomTransactionBase(this, aspect) {
@Override
public PomModelEvent runInner() {
final String oldText = getText();
final ASTNode e = getPolicy().encodeXmlTextContents(newElementText, XmlTextImpl.this);
final ASTNode node = psiElement.getNode();
final ASTNode treeNext = node.getTreeNext();
addChildren(e, null, treeNext);
deleteChildInternal(node);
clearCaches();
return XmlTextChangedImpl.createXmlTextChanged(model, XmlTextImpl.this, oldText);
}
});
} else {
setValue(new StringBuffer(getValue()).insert(displayOffset, text).toString());
}
}
use of com.intellij.pom.event.PomModelEvent 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());
}
}
Aggregations