use of com.intellij.lang.ASTNode 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.lang.ASTNode in project intellij-community by JetBrains.
the class XmlAttributeValueImpl method getValue.
@Override
public String getValue() {
// it is more correct way to strip quotes since injected xml may have quotes encoded
String text = getText();
ASTNode startQuote = findChildByType(XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER);
if (startQuote != null) {
text = StringUtil.trimStart(text, startQuote.getText());
}
ASTNode endQuote = findChildByType(XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER);
if (endQuote != null) {
text = StringUtil.trimEnd(text, endQuote.getText());
}
return text;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class TagNameReference method getRangeInElement.
@Override
public TextRange getRangeInElement() {
final ASTNode nameElement = getNameElement();
if (nameElement == null) {
return TextRange.EMPTY_RANGE;
}
int colon = nameElement.getText().indexOf(':') + 1;
if (myStartTagFlag) {
final int parentOffset = ((TreeElement) nameElement).getStartOffsetInParent();
return new TextRange(parentOffset + colon, parentOffset + nameElement.getTextLength());
} else {
final PsiElement element = getElement();
if (element == myNameElement)
return new TextRange(colon, myNameElement.getTextLength());
final int elementLength = element.getTextLength();
int diffFromEnd = 0;
for (ASTNode node = element.getNode().getLastChildNode(); node != nameElement && node != null; node = node.getTreePrev()) {
diffFromEnd += node.getTextLength();
}
final int nameEnd = elementLength - diffFromEnd;
return new TextRange(nameEnd - nameElement.getTextLength() + colon, nameEnd);
}
}
use of com.intellij.lang.ASTNode 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.lang.ASTNode 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