use of com.intellij.pom.PomModel 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.pom.PomModel 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);
}
}
}
use of com.intellij.pom.PomModel in project intellij-community by JetBrains.
the class XmlDocumentImpl method deleteChildInternal.
@Override
public void deleteChildInternal(@NotNull final ASTNode child) {
final PomModel model = PomManager.getModel(getProject());
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
try {
model.runTransaction(new PomTransactionBase(this, aspect) {
@Override
public PomModelEvent runInner() {
XmlDocumentImpl.super.deleteChildInternal(child);
return XmlDocumentChangedImpl.createXmlDocumentChanged(model, XmlDocumentImpl.this);
}
});
} catch (IncorrectOperationException ignored) {
}
}
use of com.intellij.pom.PomModel in project intellij-community by JetBrains.
the class ParsingTestCase method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
initApplication();
ComponentAdapter component = getApplication().getPicoContainer().getComponentAdapter(ProgressManager.class.getName());
if (component == null) {
getApplication().getPicoContainer().registerComponent(new AbstractComponentAdapter(ProgressManager.class.getName(), Object.class) {
@Override
public Object getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException {
return new ProgressManagerImpl();
}
@Override
public void verify(PicoContainer container) throws PicoIntrospectionException {
}
});
}
Extensions.registerAreaClass("IDEA_PROJECT", null);
myProject = new MockProjectEx(getTestRootDisposable());
myPsiManager = new MockPsiManager(myProject);
myFileFactory = new PsiFileFactoryImpl(myPsiManager);
MutablePicoContainer appContainer = getApplication().getPicoContainer();
registerComponentInstance(appContainer, MessageBus.class, getApplication().getMessageBus());
registerComponentInstance(appContainer, SchemeManagerFactory.class, new MockSchemeManagerFactory());
final MockEditorFactory editorFactory = new MockEditorFactory();
registerComponentInstance(appContainer, EditorFactory.class, editorFactory);
registerComponentInstance(appContainer, FileDocumentManager.class, new MockFileDocumentManagerImpl(charSequence -> editorFactory.createDocument(charSequence), FileDocumentManagerImpl.HARD_REF_TO_DOCUMENT_KEY));
registerComponentInstance(appContainer, PsiDocumentManager.class, new MockPsiDocumentManager());
registerApplicationService(PsiBuilderFactory.class, new PsiBuilderFactoryImpl());
registerApplicationService(DefaultASTFactory.class, new DefaultASTFactoryImpl());
registerApplicationService(ReferenceProvidersRegistry.class, new ReferenceProvidersRegistryImpl());
myProject.registerService(CachedValuesManager.class, new CachedValuesManagerImpl(myProject, new PsiCachedValuesFactory(myPsiManager)));
myProject.registerService(PsiManager.class, myPsiManager);
myProject.registerService(StartupManager.class, new StartupManagerImpl(myProject));
registerExtensionPoint(FileTypeFactory.FILE_TYPE_FACTORY_EP, FileTypeFactory.class);
registerExtensionPoint(MetaLanguage.EP_NAME, MetaLanguage.class);
for (ParserDefinition definition : myDefinitions) {
addExplicitExtension(LanguageParserDefinitions.INSTANCE, definition.getFileNodeType().getLanguage(), definition);
}
if (myDefinitions.length > 0) {
configureFromParserDefinition(myDefinitions[0], myFileExt);
}
// That's for reparse routines
final PomModelImpl pomModel = new PomModelImpl(myProject);
myProject.registerService(PomModel.class, pomModel);
new TreeAspect(pomModel);
}
use of com.intellij.pom.PomModel in project intellij-community by JetBrains.
the class ChangeUtil method prepareAndRunChangeAction.
public static void prepareAndRunChangeAction(final ChangeAction action, final TreeElement changedElement) {
final FileElement changedFile = TreeUtil.getFileElement(changedElement);
final PsiManager manager = changedFile.getManager();
final PomModel model = PomManager.getModel(manager.getProject());
final TreeAspect treeAspect = model.getModelAspect(TreeAspect.class);
model.runTransaction(new PomTransactionBase(changedElement.getPsi(), treeAspect) {
@Override
public PomModelEvent runInner() {
final PomModelEvent event = new PomModelEvent(model);
final TreeChangeEvent destinationTreeChange = new TreeChangeEventImpl(treeAspect, changedFile);
event.registerChangeSet(treeAspect, destinationTreeChange);
action.makeChange(destinationTreeChange);
changedElement.clearCaches();
if (changedElement instanceof CompositeElement) {
((CompositeElement) changedElement).subtreeChanged();
}
return event;
}
});
}
Aggregations