use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class GroovyTreeGenerator method generateTreeFor.
@Override
public TreeElement generateTreeFor(@NotNull PsiElement original, @NotNull CharTable table, @NotNull PsiManager manager) {
if (original instanceof GrModifierList) {
final String text = original.getText();
assert text != null : "Text is null for " + original + "; " + original.getClass();
final GrModifierList modifierList = GroovyPsiElementFactory.getInstance(manager.getProject()).createModifierList(text);
return (TreeElement) modifierList.getNode();
}
return null;
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class ClsFileImpl method getMirror.
@Override
public PsiElement getMirror() {
TreeElement mirrorTreeElement = SoftReference.dereference(myMirrorFileElement);
if (mirrorTreeElement == null) {
synchronized (myMirrorLock) {
mirrorTreeElement = SoftReference.dereference(myMirrorFileElement);
if (mirrorTreeElement == null) {
VirtualFile file = getVirtualFile();
PsiClass[] classes = getClasses();
String fileName = (classes.length > 0 ? classes[0].getName() : file.getNameWithoutExtension()) + JavaFileType.DOT_DEFAULT_EXTENSION;
final Document document = FileDocumentManager.getInstance().getDocument(file);
assert document != null : file.getUrl();
CharSequence mirrorText = document.getImmutableCharSequence();
boolean internalDecompiler = StringUtil.startsWith(mirrorText, BANNER);
PsiFileFactory factory = PsiFileFactory.getInstance(getManager().getProject());
PsiFile mirror = factory.createFileFromText(fileName, JavaLanguage.INSTANCE, mirrorText, false, false);
mirror.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, getLanguageLevel());
mirrorTreeElement = SourceTreeToPsiMap.psiToTreeNotNull(mirror);
try {
final TreeElement finalMirrorTreeElement = mirrorTreeElement;
ProgressManager.getInstance().executeNonCancelableSection(() -> {
setMirror(finalMirrorTreeElement);
putUserData(CLS_DOCUMENT_LINK_KEY, document);
});
} catch (InvalidMirrorException e) {
//noinspection ThrowableResultOfMethodCallIgnored
LOG.error(file.getUrl(), internalDecompiler ? e : wrapException(e, file));
}
((PsiFileImpl) mirror).setOriginalFile(this);
myMirrorFileElement = new SoftReference<>(mirrorTreeElement);
}
}
}
return mirrorTreeElement.getPsi();
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class TreeChangeImpl method getOldLength.
@Override
public int getOldLength() {
int oldLength = ((TreeElement) myParent).getNotCachedLength();
for (final Map.Entry<ASTNode, ChangeInfo> entry : myChanges.entrySet()) {
final ASTNode key = entry.getKey();
final ChangeInfo change = entry.getValue();
final int length = ((TreeElement) key).getNotCachedLength();
switch(change.getChangeType()) {
case ChangeInfo.ADD:
oldLength -= length;
break;
case ChangeInfo.REMOVED:
oldLength += length;
break;
case ChangeInfo.REPLACE:
case ChangeInfo.CONTENTS_CHANGED:
oldLength += change.getOldLength() - length;
break;
}
}
return oldLength;
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class XmlTextImpl method _splitText.
@Nullable
private XmlText _splitText(final int displayOffset) throws IncorrectOperationException {
final XmlTag xmlTag = (XmlTag) getParent();
if (displayOffset == 0)
return this;
final int length = getValue().length();
if (displayOffset >= length) {
return null;
}
final PomModel model = PomManager.getModel(xmlTag.getProject());
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
class MyTransaction extends PomTransactionBase {
private XmlTextImpl myRight;
MyTransaction() {
super(xmlTag, aspect);
}
@Override
@Nullable
public PomModelEvent runInner() throws IncorrectOperationException {
final String oldText = getValue();
final int physicalOffset = displayToPhysical(displayOffset);
PsiElement childElement = findElementAt(physicalOffset);
if (childElement != null && childElement.getNode().getElementType() == XmlTokenType.XML_DATA_CHARACTERS) {
FileElement holder = DummyHolderFactory.createHolder(getManager(), null).getTreeElement();
int splitOffset = physicalOffset - childElement.getStartOffsetInParent();
myRight = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
CodeEditUtil.setNodeGenerated(myRight, true);
holder.rawAddChildren(myRight);
PsiElement e = childElement;
while (e != null) {
CodeEditUtil.setNodeGenerated(e.getNode(), true);
e = e.getNextSibling();
}
String leftText = childElement.getText().substring(0, splitOffset);
String rightText = childElement.getText().substring(splitOffset);
LeafElement rightElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(rightText));
CodeEditUtil.setNodeGenerated(rightElement, true);
LeafElement leftElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(leftText));
CodeEditUtil.setNodeGenerated(leftElement, true);
rawInsertAfterMe(myRight);
myRight.rawAddChildren(rightElement);
if (childElement.getNextSibling() != null) {
myRight.rawAddChildren((TreeElement) childElement.getNextSibling());
}
((TreeElement) childElement).rawRemove();
XmlTextImpl.this.rawAddChildren(leftElement);
} else {
final PsiFile containingFile = xmlTag.getContainingFile();
final FileElement holder = DummyHolderFactory.createHolder(containingFile.getManager(), null, ((PsiFileImpl) containingFile).getTreeElement().getCharTable()).getTreeElement();
final XmlTextImpl rightText = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
CodeEditUtil.setNodeGenerated(rightText, true);
holder.rawAddChildren(rightText);
((ASTNode) xmlTag).addChild(rightText, getTreeNext());
final String value = getValue();
setValue(value.substring(0, displayOffset));
rightText.setValue(value.substring(displayOffset));
CodeEditUtil.setNodeGenerated(rightText, true);
myRight = rightText;
}
clearCaches();
myRight.clearCaches();
return createEvent(new XmlTextChangedImpl(XmlTextImpl.this, oldText), new XmlTagChildAddImpl(xmlTag, myRight));
}
public XmlText getResult() {
return myRight;
}
}
final MyTransaction transaction = new MyTransaction();
model.runTransaction(transaction);
return transaction.getResult();
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class DefaultXmlPsiPolicy method encodeXmlTextContents.
@Override
public ASTNode encodeXmlTextContents(String displayText, PsiElement text) {
final PsiFile containingFile = text.getContainingFile();
CharTable charTable = SharedImplUtil.findCharTableByTree(text.getNode());
final FileElement dummyParent = DummyHolderFactory.createHolder(text.getManager(), null, charTable).getTreeElement();
final XmlTag rootTag = ((XmlFile) PsiFileFactory.getInstance(containingFile.getProject()).createFileFromText("a.xml", "<a>" + displayText + "</a>")).getRootTag();
assert rootTag != null;
final XmlTagChild[] tagChildren = rootTag.getValue().getChildren();
final XmlTagChild child = tagChildren.length > 0 ? tagChildren[0] : null;
LOG.assertTrue(child != null, "Child is null for tag: " + rootTag.getText());
final TreeElement element = (TreeElement) child.getNode();
((TreeElement) tagChildren[tagChildren.length - 1].getNode().getTreeNext()).rawRemoveUpToLast();
dummyParent.rawAddChildren(element);
TreeUtil.clearCaches(dummyParent);
return element.getFirstChildNode();
}
Aggregations