use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class XmlEventsTest method testBulkUpdate.
public void testBulkUpdate() throws Exception {
final Listener listener = addPomListener();
final PsiFile file = createFile("a.xml", "<a/>");
new WriteCommandAction(getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
DocumentUtil.executeInBulk(document, true, () -> {
document.insertString(0, " ");
commitDocument(document);
});
}
}.execute();
assertEquals("(Xml document changed)", listener.getEventString().trim());
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class XmlChooseColorIntentionAction method chooseColor.
public static void chooseColor(JComponent editorComponent, PsiElement element) {
String caption = CodeInsightBundle.message("intention.color.chooser.dialog");
final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
if (literal == null)
return;
final String text = StringUtil.unquoteString(literal.getValue());
Color oldColor;
try {
oldColor = Color.decode(text);
} catch (NumberFormatException e) {
oldColor = JBColor.GRAY;
}
Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
if (color == null)
return;
if (!Comparing.equal(color, oldColor)) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
return;
final String newText = "#" + ColorUtil.toHex(color);
final PsiManager manager = literal.getManager();
final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createAttribute("name", newText, element);
final Runnable replaceRunnable = () -> {
final XmlAttributeValue valueElement = newAttribute.getValueElement();
assert valueElement != null;
literal.replace(valueElement);
};
new WriteCommandAction(element.getProject(), caption) {
@Override
protected void run(@NotNull Result result) throws Throwable {
replaceRunnable.run();
}
}.execute();
}
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class MavenOpenOrCreateFilesAction method actionPerformed.
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final Project project = MavenActionUtil.getProject(e.getDataContext());
if (project == null)
return;
final List<File> files = getFiles(e);
final List<VirtualFile> virtualFiles = collectVirtualFiles(files);
if (files.size() == 1 && virtualFiles.isEmpty()) {
new WriteCommandAction(project, e.getPresentation().getText()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
File file = files.get(0);
try {
final VirtualFile virtualFile = VfsUtil.createDirectoryIfMissing(file.getParent());
if (virtualFile != null) {
VirtualFile newFile = virtualFile.createChildData(this, file.getName());
virtualFiles.add(newFile);
MavenUtil.runFileTemplate(project, newFile, getFileTemplate());
}
} catch (IOException ex) {
MavenUtil.showError(project, "Cannot create " + file.getName(), ex);
}
}
}.execute();
return;
}
for (VirtualFile each : virtualFiles) {
new OpenFileDescriptor(project, each).navigate(true);
}
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class MavenTestCase method createModule.
protected Module createModule(final String name, final ModuleType type) throws IOException {
return new WriteCommandAction<Module>(myProject) {
@Override
protected void run(@NotNull Result<Module> moduleResult) throws Throwable {
VirtualFile f = createProjectSubFile(name + "/" + name + ".iml");
Module module = ModuleManager.getInstance(myProject).newModule(f.getPath(), type.getId());
PsiTestUtil.addContentRoot(module, f.getParent());
moduleResult.setResult(module);
}
}.execute().getResultObject();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class DomNamespacesTest method testCopyFromHonorsNamespaces.
public void testCopyFromHonorsNamespaces() throws Throwable {
final MyElement element = createElement("<a xmlns=\"foo\" xmlns:bar=\"bar\"/>", MyElement.class);
registerNamespacePolicies(element);
final MyElement element2 = createElement("<f:a xmlns:f=\"foo1\" xmlns:b=\"bar1\" xmlns=\"foo1\">" + "<foo-child/>" + "<b:bar-child/>" + "<f:some-child/>" + "<f:foo-element attr-2=\"239\" attr=\"42\"/>" + "<f:foo-element/>" + "<f:bool/>" + "<sys:aaa/>" + "</f:a>", MyElement.class);
registerNamespacePolicies(element2, "foo1", "bar1");
new WriteCommandAction(getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
element.copyFrom(element2);
}
}.execute();
assertEquals("<a xmlns=\"foo\" xmlns:bar=\"bar\">" + "<bar:bar-child/>" + "<bool/>" + "<foo-child/>" + "<some-child/>" + "<sys:aaa/>" + "<foo-element attr=\"42\" attr-2=\"239\"/>" + "<foo-element/>" + "</a>", element.getXmlTag().getText());
}
Aggregations