call addResource in writeAction

This commit is contained in:
Alexey Kudravtsev
2011-07-15 13:23:56 +04:00
parent 37ff74f366
commit be0f70e5be
12 changed files with 166 additions and 62 deletions
@@ -18,6 +18,8 @@ package com.intellij.codeInsight;
import com.intellij.codeInsight.highlighting.HighlightUsagesHandler;
import com.intellij.ide.DataManager;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
@@ -40,6 +42,7 @@ import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
@@ -704,4 +707,22 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
assertNotNull("Package " + name + " not found", aPackage);
return aPackage;
}
protected void registerResourceTemporarily(final String url, final String location) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManager.getInstance().addResource(url, location);
}
});
Disposer.register(getTestRootDisposable(), new Disposable() {
@Override
public void dispose() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManager.getInstance().removeResource(url);
}
});
}
});
}
}
@@ -43,8 +43,13 @@ public class PropertiesFileTest extends LightPlatformTestCase {
}
public void testAddPropertyAfterComment() throws Exception {
PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "#xxxxx");
propertiesFile.addProperty(myPropertyToAdd);
final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "#xxxxx");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
propertiesFile.addProperty(myPropertyToAdd);
}
});
List<Property> properties = propertiesFile.getProperties();
Property added = properties.get(0);
@@ -57,8 +62,13 @@ public class PropertiesFileTest extends LightPlatformTestCase {
}
public void testAddPropertyAfterProperty() throws Exception {
PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "xxx=yyy");
propertiesFile.addProperty(myPropertyToAdd);
final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "xxx=yyy");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
propertiesFile.addProperty(myPropertyToAdd);
}
});
List<Property> properties = propertiesFile.getProperties();
assertEquals(2, properties.size());
@@ -112,9 +122,14 @@ public class PropertiesFileTest extends LightPlatformTestCase {
}
public void testAddToEnd() throws IncorrectOperationException {
PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\\nccc");
final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\\nccc");
assertEquals(1,propertiesFile.getProperties().size());
propertiesFile.addProperty(myPropertyToAdd);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
propertiesFile.addProperty(myPropertyToAdd);
}
});
assertEquals("a=b\\nccc\nkkk=vvv", propertiesFile.getText());
}
@@ -129,20 +144,35 @@ public class PropertiesFileTest extends LightPlatformTestCase {
}
public void testAddPropertyAfter() throws IncorrectOperationException {
PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f");
Property c = propertiesFile.findPropertyByKey("c");
propertiesFile.addPropertyAfter(myPropertyToAdd, c);
final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f");
final Property c = propertiesFile.findPropertyByKey("c");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
propertiesFile.addPropertyAfter(myPropertyToAdd, c);
}
});
assertEquals("a=b\nc=d\nkkk=vvv\ne=f", propertiesFile.getText());
}
public void testAddPropertyAfterLast() throws IncorrectOperationException {
PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f");
Property p = propertiesFile.findPropertyByKey("e");
propertiesFile.addPropertyAfter(myPropertyToAdd, p);
final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f");
final Property p = propertiesFile.findPropertyByKey("e");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
propertiesFile.addPropertyAfter(myPropertyToAdd, p);
}
});
assertEquals("a=b\nc=d\ne=f\nkkk=vvv", propertiesFile.getText());
}
public void testAddPropertyAfterInBeginning() throws IncorrectOperationException {
PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f");
propertiesFile.addPropertyAfter(myPropertyToAdd, null);
final PropertiesFile propertiesFile = PropertiesElementFactory.createPropertiesFile(getProject(), "a=b\nc=d\ne=f");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
propertiesFile.addPropertyAfter(myPropertyToAdd, null);
}
});
assertEquals("kkk=vvv\na=b\nc=d\ne=f", propertiesFile.getText());
}
public void testUnescapedKey() throws IncorrectOperationException {
@@ -24,6 +24,7 @@ import com.intellij.codeInspection.InspectionToolProvider;
import com.intellij.codeInspection.htmlInspections.RequiredAttributesInspection;
import com.intellij.javaee.ExternalResourceManagerEx;
import com.intellij.mock.MockProgressIndicator;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.WriteAction;
@@ -158,7 +159,11 @@ public abstract class HighlightingTestBase extends UsefulTestCase implements Ide
public abstract String getTestDataPath();
protected void init() {
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("urn:test:undefined");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("urn:test:undefined");
}
});
}
protected void tearDown() throws Exception {
@@ -16,6 +16,7 @@
package org.intellij.plugins.relaxNG;
import com.intellij.javaee.ExternalResourceManagerEx;
import com.intellij.openapi.application.ApplicationManager;
/**
* @author Eugene.Kudelevsky
@@ -32,8 +33,12 @@ public class RngHtml5CompletionTest extends HighlightingTestBase {
protected void init() {
super.init();
final ExternalResourceManagerEx m = ExternalResourceManagerEx.getInstanceEx();
m.addResource("http://www.w3.org/1999/xhtml/html5", toAbsolutePath("/highlighting/html5/html5.rnc"));
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
final ExternalResourceManagerEx m = ExternalResourceManagerEx.getInstanceEx();
m.addResource("http://www.w3.org/1999/xhtml/html5", toAbsolutePath("/highlighting/html5/html5.rnc"));
}
});
}
public void testHtml5_1() throws Throwable {
@@ -17,6 +17,7 @@
package org.intellij.plugins.relaxNG;
import com.intellij.javaee.ExternalResourceManagerEx;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.StdFileTypes;
import org.intellij.plugins.testUtil.CopyFile;
@@ -36,26 +37,30 @@ public class RngXmlHighlightingTest extends HighlightingTestBase {
super.init();
FileTypeManager.getInstance().registerFileType(StdFileTypes.XML, "fo");
final ExternalResourceManagerEx m = ExternalResourceManagerEx.getInstanceEx();
m.addResource("urn:test:simple.rng", toAbsolutePath("highlighting/simple.rng"));
m.addResource("urn:test:addressBook", toAbsolutePath("highlighting/rnc/addressbook.rnc"));
m.addResource("http://www.w3.org/1999/XSL/Transform", toAbsolutePath("highlighting/relaxng.rng"));
m.addResource("http://www.w3.org/1999/XSL/Format", toAbsolutePath("highlighting/rnc/fo/main.rnc"));
m.addResource("http://docbook.org/ns/docbook", toAbsolutePath("highlighting/docbook.rng"));
m.addResource("urn:intelliForm:AttachmentFilter", toAbsolutePath("highlighting/attachment-filter.rng"));
m.addResource("http://www.w3.org/1999/xhtml/html5", toAbsolutePath("highlighting/html5/xhtml5.rnc"));
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
final ExternalResourceManagerEx m = ExternalResourceManagerEx.getInstanceEx();
m.addResource("urn:test:simple.rng", toAbsolutePath("highlighting/simple.rng"));
m.addResource("urn:test:addressBook", toAbsolutePath("highlighting/rnc/addressbook.rnc"));
m.addResource("http://www.w3.org/1999/XSL/Transform", toAbsolutePath("highlighting/relaxng.rng"));
m.addResource("http://www.w3.org/1999/XSL/Format", toAbsolutePath("highlighting/rnc/fo/main.rnc"));
m.addResource("http://docbook.org/ns/docbook", toAbsolutePath("highlighting/docbook.rng"));
m.addResource("urn:intelliForm:AttachmentFilter", toAbsolutePath("highlighting/attachment-filter.rng"));
m.addResource("http://www.w3.org/1999/xhtml/html5", toAbsolutePath("highlighting/html5/xhtml5.rnc"));
m.addIgnoredResource("urn:intelliForm:Spaces");
m.addIgnoredResource("http://www.w3.org/1999/xlink");
m.addIgnoredResource("http://www.w3.org/2000/svg");
m.addIgnoredResource("http://www.ascc.net/xml/schematron");
m.addIgnoredResource("http://www.w3.org/2000/svg");
m.addIgnoredResource("http://www.w3.org/1998/Math/MathML");
m.addIgnoredResource("http://nwalsh.com/xmlns/schema-control/");
m.addIgnoredResource("http://xml.apache.org/fop/extensions");
m.addIgnoredResource("http://www.antennahouse.com/names/XSL/Extensions");
m.addIgnoredResource("http://www.renderx.com/XSL/Extensions");
m.addIgnoredResource("http://relaxng.org/ns/compatibility/annotations/1.0");
m.addIgnoredResource("urn:intelliForm:Spaces");
m.addIgnoredResource("http://www.w3.org/1999/xlink");
m.addIgnoredResource("http://www.w3.org/2000/svg");
m.addIgnoredResource("http://www.ascc.net/xml/schematron");
m.addIgnoredResource("http://www.w3.org/2000/svg");
m.addIgnoredResource("http://www.w3.org/1998/Math/MathML");
m.addIgnoredResource("http://nwalsh.com/xmlns/schema-control/");
m.addIgnoredResource("http://xml.apache.org/fop/extensions");
m.addIgnoredResource("http://www.antennahouse.com/names/XSL/Extensions");
m.addIgnoredResource("http://www.renderx.com/XSL/Extensions");
m.addIgnoredResource("http://relaxng.org/ns/compatibility/annotations/1.0");
}
});
}
public void testSimpleElement() throws Throwable {
@@ -17,6 +17,7 @@
package org.intellij.plugins.relaxNG;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.openapi.application.ApplicationManager;
import org.intellij.plugins.testUtil.CopyFile;
/**
@@ -84,10 +85,14 @@ public class RngXmlValidationTest extends HighlightingTestBase {
protected void init() {
super.init();
final ExternalResourceManager mgr = ExternalResourceManager.getInstance();
mgr.addResource("urn:test:simple.rng", toAbsolutePath("validation/simple.rng"));
mgr.addResource("urn:test:simple.rnc", toAbsolutePath("validation/simple.rnc"));
mgr.addResource("http://www.w3.org/1999/XSL/Transform", toAbsolutePath("validation/relaxng.rng"));
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
final ExternalResourceManager mgr = ExternalResourceManager.getInstance();
mgr.addResource("urn:test:simple.rng", toAbsolutePath("validation/simple.rng"));
mgr.addResource("urn:test:simple.rnc", toAbsolutePath("validation/simple.rnc"));
mgr.addResource("http://www.w3.org/1999/XSL/Transform", toAbsolutePath("validation/relaxng.rng"));
}
});
}
public String getTestDataPath() {
@@ -16,6 +16,7 @@
package org.intellij.lang.xpath.xslt;
import com.intellij.javaee.ExternalResourceManagerEx;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.util.ArrayUtil;
import org.intellij.lang.xpath.TestBase;
import org.intellij.lang.xpath.xslt.impl.XsltStuffProvider;
@@ -30,8 +31,12 @@ public class Xslt2HighlightingTest extends TestBase {
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(XsltStuffProvider.INSPECTION_CLASSES);
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("urn:my");
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("nsx");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("urn:my");
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("nsx");
}
});
}
public void testCurrentMode() throws Throwable {
@@ -36,7 +36,11 @@ public class XsltHighlightingTest extends TestBase {
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(XsltStuffProvider.INSPECTION_CLASSES);
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("urn:my");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource("urn:my");
}
});
}
public void xtestBackwardIncludedVariable() throws Throwable {
@@ -16,9 +16,11 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.javaee.ExternalResourceManagerEx;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.psi.PsiFile;
import com.intellij.openapi.editor.Editor;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
/**
* @author mike
@@ -28,7 +30,11 @@ public class IgnoreExtResourceAction extends BaseExtResourceAction {
return "ignore.external.resource.text";
}
protected void doInvoke(final PsiFile file, final int offset, final String uri, final Editor editor) throws IncorrectOperationException {
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource(uri);
protected void doInvoke(@NotNull final PsiFile file, final int offset, @NotNull final String uri, final Editor editor) throws IncorrectOperationException {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManagerEx.getInstanceEx().addIgnoredResource(uri);
}
});
}
}
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.javaee.ExternalResourceConfigurable;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
@@ -34,7 +35,12 @@ public class ManuallySetupExtResourceAction extends BaseExtResourceAction {
}
protected void doInvoke(@NotNull final PsiFile file, final int offset, @NotNull final String uri, final Editor editor) throws IncorrectOperationException {
ExternalResourceManager.getInstance().addResource(uri,"");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManager.getInstance().addResource(uri, "");
}
});
final Project project = file.getProject();
final ExternalResourceConfigurable component = new ExternalResourceConfigurable(project);
ShowSettingsUtil.getInstance().editConfigurable(project, component, new Runnable() {
@@ -15,6 +15,7 @@
*/
package com.intellij.javaee;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.options.BaseConfigurable;
import com.intellij.openapi.options.OptionalConfigurable;
import com.intellij.openapi.options.SearchableConfigurable;
@@ -139,24 +140,30 @@ public class ExternalResourceConfigurable extends BaseConfigurable implements Se
}
public void apply() {
ExternalResourceManagerEx manager = ExternalResourceManagerEx.getInstanceEx();
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ExternalResourceManagerEx manager = ExternalResourceManagerEx.getInstanceEx();
manager.clearAllResources(myProject);
for (Object myPair : myPairs) {
EditLocationDialog.NameLocationPair pair = (EditLocationDialog.NameLocationPair)myPair;
String s = pair.myLocation.replace('\\', '/');
if (pair.myShared) {
manager.addResource(pair.myName, s);
} else {
manager.addResource(pair.myName, s, myProject);
manager.clearAllResources(myProject);
for (Object myPair : myPairs) {
EditLocationDialog.NameLocationPair pair = (EditLocationDialog.NameLocationPair)myPair;
String s = pair.myLocation.replace('\\', '/');
if (pair.myShared) {
manager.addResource(pair.myName, s);
}
else {
manager.addResource(pair.myName, s, myProject);
}
}
for (Object myIgnoredUrl : myIgnoredUrls) {
String url = (String)myIgnoredUrl;
manager.addIgnoredResource(url);
}
manager.setDefaultHtmlDoctype(myHtmlLanguageLevelForm.getDoctype(), myProject);
}
}
});
for (Object myIgnoredUrl : myIgnoredUrls) {
String url = (String)myIgnoredUrl;
manager.addIgnoredResource(url);
}
manager.setDefaultHtmlDoctype(myHtmlLanguageLevelForm.getDoctype(), myProject);
setModified(false);
}
@@ -215,6 +215,7 @@ public class ExternalResourceManagerImpl extends ExternalResourceManagerEx imple
}
public void addResource(@NonNls String url, @NonNls String version, @NonNls String location) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
addSilently(url, version, location);
fireExternalResourceChanged();
}
@@ -232,6 +233,7 @@ public class ExternalResourceManagerImpl extends ExternalResourceManagerEx imple
}
public void removeResource(String url, String version) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
Map<String, String> map = getMap(myResources, version, false);
if (map != null) {
String location = map.remove(url);
@@ -272,6 +274,7 @@ public class ExternalResourceManagerImpl extends ExternalResourceManagerEx imple
}
public void clearAllResources(Project project) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
clearAllResources();
getProjectResources(project).clearAllResources();
myModificationCount++;
@@ -279,6 +282,7 @@ public class ExternalResourceManagerImpl extends ExternalResourceManagerEx imple
}
public void addIgnoredResource(String url) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
addIgnoredSilently(url);
fireExternalResourceChanged();
}
@@ -289,6 +293,7 @@ public class ExternalResourceManagerImpl extends ExternalResourceManagerEx imple
}
public void removeIgnoredResource(String url) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
if (myIgnoredResources.remove(url)) {
myModificationCount++;
fireExternalResourceChanged();
@@ -395,7 +400,7 @@ public class ExternalResourceManagerImpl extends ExternalResourceManagerEx imple
}
private final static NotNullLazyKey<ProjectResources, Project> INSTANCE_CACHE = ServiceManager.createLazyKey(ProjectResources.class);
private static final NotNullLazyKey<ProjectResources, Project> INSTANCE_CACHE = ServiceManager.createLazyKey(ProjectResources.class);
private static ExternalResourceManagerImpl getProjectResources(Project project) {
return INSTANCE_CACHE.getValue(project);