use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class XmlElementImpl method getNameFromEntityRef.
@Nullable
protected static String getNameFromEntityRef(final CompositeElement compositeElement, final IElementType xmlEntityDeclStart) {
final ASTNode node = compositeElement.findChildByType(xmlEntityDeclStart);
if (node == null)
return null;
ASTNode name = node.getTreeNext();
if (name != null && name.getElementType() == TokenType.WHITE_SPACE) {
name = name.getTreeNext();
}
if (name != null && name.getElementType() == XmlElementType.XML_ENTITY_REF) {
final StringBuilder builder = new StringBuilder();
((XmlElement) name.getPsi()).processElements(new PsiElementProcessor() {
@Override
public boolean execute(@NotNull final PsiElement element) {
builder.append(element.getText());
return true;
}
}, name.getPsi());
if (builder.length() > 0)
return builder.toString();
}
return null;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class XmlTagImpl method getValueTextRanges.
@NotNull
private TextRange[] getValueTextRanges() {
TextRange[] elements = myTextElements;
if (elements == null) {
List<TextRange> list = ContainerUtil.newSmartList();
// don't use getValue().getXmlElements() because it processes includes & entities, and we only need textual AST here
for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) {
PsiElement psi = child.getPsi();
if (psi instanceof XmlText) {
list.add(TextRange.from(psi.getStartOffsetInParent(), psi.getTextLength()));
}
}
myTextElements = elements = list.toArray(new TextRange[list.size()]);
}
return elements;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class XmlTagImpl method addInternal.
private TreeElement addInternal(TreeElement child, ASTNode anchor, boolean before) throws IncorrectOperationException {
final PomModel model = PomManager.getModel(getProject());
if (anchor != null && child.getElementType() == XmlElementType.XML_TEXT) {
XmlText psi = null;
if (anchor.getPsi() instanceof XmlText) {
psi = (XmlText) anchor.getPsi();
} else {
final ASTNode other = before ? anchor.getTreePrev() : anchor.getTreeNext();
if (other != null && other.getPsi() instanceof XmlText) {
before = !before;
psi = (XmlText) other.getPsi();
}
}
if (psi != null) {
if (before) {
psi.insertText(((XmlText) child.getPsi()).getValue(), 0);
} else {
psi.insertText(((XmlText) child.getPsi()).getValue(), psi.getValue().length());
}
return (TreeElement) psi.getNode();
}
}
LOG.assertTrue(child.getPsi() instanceof XmlAttribute || child.getPsi() instanceof XmlTagChild);
final InsertTransaction transaction;
if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
transaction = new InsertAttributeTransaction(child, anchor, before, model);
} else if (anchor == null) {
transaction = getBodyInsertTransaction(child);
} else {
transaction = new GenericInsertTransaction(child, anchor, before);
}
model.runTransaction(transaction);
return transaction.getFirstInserted();
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class XmlTagImpl method expandTag.
private ASTNode expandTag() throws IncorrectOperationException {
ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(this);
if (endTagStart == null) {
final XmlTagImpl tagFromText = (XmlTagImpl) XmlElementFactory.getInstance(getProject()).createTagFromText("<" + getName() + "></" + getName() + ">");
final ASTNode startTagStart = XmlChildRole.START_TAG_END_FINDER.findChild(tagFromText);
endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(tagFromText);
final LeafElement emptyTagEnd = (LeafElement) XmlChildRole.EMPTY_TAG_END_FINDER.findChild(this);
if (emptyTagEnd != null)
removeChild(emptyTagEnd);
addChildren(startTagStart, null, null);
}
return endTagStart;
}
use of com.intellij.lang.ASTNode 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