use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class TreeIncrementalUpdateTest method testAddFixedElement.
public void testAddFixedElement() throws Throwable {
final MyElement element = createPhysicalElement("<a>" + "<child/>" + "<child><child/></child>" + "<child/></a>");
final MyElement child = element.getChild();
final MyElement child2 = element.getChild2();
final XmlTag leafTag = child2.getChild().getXmlTag();
new WriteCommandAction(getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
element.getXmlTag().addAfter(createTag("<child/>"), child.getXmlTag());
}
}.execute();
assertNoCache(leafTag);
final XmlTag[] subTags = element.getXmlTag().getSubTags();
assertFalse(child2.isValid());
assertEquals(child, element.getChild());
assertFalse(child2.equals(element.getChild2()));
assertCached(child, subTags[0]);
assertNoCache(subTags[2]);
assertNoCache(subTags[3]);
putExpected(new DomEvent(element, false));
putExpected(new DomEvent(element, false));
assertResultsAndClear();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class TreeIncrementalUpdateTest method testRemoveAttributeParent.
public void testRemoveAttributeParent() throws Throwable {
final XmlFile file = (XmlFile) createFile("file.xml", "<?xml version='1.0' encoding='UTF-8'?>\n" + "<!DOCTYPE ejb-jar PUBLIC \"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN\" \"http://java.sun.com/dtd/ejb-jar_2_0.dtd\">\n" + "<a>\n" + " <child-element xxx=\"239\"/>\n" + "</a>");
final DomFileElementImpl<MyElement> fileElement = getDomManager().getFileElement(file, MyElement.class, "a");
myCallRegistry.clear();
final MyElement rootElement = fileElement.getRootElement();
final MyElement oldLeaf = rootElement.getChildElements().get(0);
final GenericAttributeValue<String> xxx = oldLeaf.getXxx();
final XmlTag oldLeafTag = oldLeaf.getXmlTag();
new WriteCommandAction(getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
oldLeafTag.delete();
}
}.execute();
assertFalse(oldLeaf.isValid());
assertFalse(xxx.isValid());
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class TreeIncrementalUpdateTest method testTypeBeforeRootTag.
public void testTypeBeforeRootTag() throws Throwable {
getDomManager().registerFileDescription(new DomFileDescription<>(MyElement.class, "a"), getTestRootDisposable());
final XmlFile file = (XmlFile) createFile("file.xml", "<?xml version='1.0' encoding='UTF-8'?>\n" + "<a/>");
assertTrue(getDomManager().isDomFile(file));
final DomFileElementImpl<MyElement> fileElement = getDomManager().getFileElement(file, MyElement.class);
assertTrue(fileElement.isValid());
myCallRegistry.clear();
putExpected(new DomEvent(fileElement, false));
new WriteCommandAction(getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
final Document document = getDocument(file);
final int i = document.getText().indexOf("<a");
document.insertString(i, "a");
commitDocument(document);
}
}.execute();
assertFalse(getDomManager().isDomFile(file));
assertFalse(fileElement.isValid());
assertResultsAndClear();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class CollapseTagIntention method applyFix.
private static void applyFix(@NotNull final Project project, @NotNull final PsiElement tag) {
if (!FileModificationService.getInstance().prepareFileForWrite(tag.getContainingFile())) {
return;
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
final ASTNode child = XmlChildRole.START_TAG_END_FINDER.findChild(tag.getNode());
if (child == null)
return;
final int offset = child.getTextRange().getStartOffset();
VirtualFile file = tag.getContainingFile().getVirtualFile();
final Document document = FileDocumentManager.getInstance().getDocument(file);
new WriteCommandAction(project) {
@Override
protected void run(@NotNull final Result result) throws Throwable {
assert document != null;
document.replaceString(offset, tag.getTextRange().getEndOffset(), "/>");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformat(tag);
}
}.execute();
}
use of com.intellij.openapi.command.WriteCommandAction in project kotlin by JetBrains.
the class KotlinInplaceVariableIntroducer method runWriteActionAndRestartRefactoring.
protected final void runWriteActionAndRestartRefactoring(final Runnable runnable) {
final Ref<Boolean> greedyToRight = new Ref<Boolean>();
new WriteCommandAction(myProject, getCommandName(), getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
ASTNode identifier = myDeclaration.getNode().findChildByType(KtTokens.IDENTIFIER);
if (identifier != null) {
TextRange range = identifier.getTextRange();
RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
for (RangeHighlighter highlighter : highlighters) {
if (highlighter.getStartOffset() == range.getStartOffset()) {
if (highlighter.getEndOffset() == range.getEndOffset()) {
greedyToRight.set(highlighter.isGreedyToRight());
highlighter.setGreedyToRight(false);
}
}
}
}
runnable.run();
TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(myEditor));
if (templateState != null) {
myEditor.putUserData(INTRODUCE_RESTART, true);
templateState.cancelTemplate();
}
}
}.execute();
ApplicationManager.getApplication().runReadAction(new Runnable() {
@Override
public void run() {
ASTNode identifier = myDeclaration.getNode().findChildByType(KtTokens.IDENTIFIER);
if (identifier != null) {
TextRange range = identifier.getTextRange();
RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
for (RangeHighlighter highlighter : highlighters) {
if (highlighter.getStartOffset() == range.getStartOffset()) {
if (highlighter.getEndOffset() == range.getEndOffset()) {
highlighter.setGreedyToRight(greedyToRight.get());
}
}
}
}
}
});
if (myEditor.getUserData(INTRODUCE_RESTART) == Boolean.TRUE) {
myInitialName = myDeclaration.getName();
performInplaceRefactoring(getSuggestionsForNextRun());
}
}
Aggregations