use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class PsiTestUtil method addModule.
public static Module addModule(Project project, ModuleType type, String name, VirtualFile root) {
return new WriteCommandAction<Module>(project) {
@Override
protected void run(@NotNull Result<Module> result) throws Throwable {
String moduleName;
ModifiableModuleModel moduleModel = ModuleManager.getInstance(project).getModifiableModel();
try {
moduleName = moduleModel.newModule(root.getPath() + "/" + name + ".iml", type.getId()).getName();
moduleModel.commit();
} catch (Throwable t) {
moduleModel.dispose();
throw t;
}
Module dep = ModuleManager.getInstance(project).findModuleByName(moduleName);
assert dep != null : moduleName;
ModifiableRootModel model = ModuleRootManager.getInstance(dep).getModifiableModel();
try {
model.addContentEntry(root).addSourceFolder(root, false);
model.commit();
} catch (Throwable t) {
model.dispose();
throw t;
}
result.setResult(dep);
}
}.execute().getResultObject();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class JavaLanguageInjectionSupport method doAddLanguageAnnotation.
public static boolean doAddLanguageAnnotation(Project project, @Nullable final PsiModifierListOwner modifierListOwner, @NotNull final PsiLanguageInjectionHost host, final String languageId, Processor<PsiLanguageInjectionHost> annotationFixer) {
final boolean addAnnotation = isAnnotationsJarInPath(ModuleUtilCore.findModuleForPsiElement(modifierListOwner)) && PsiUtil.isLanguageLevel5OrHigher(modifierListOwner) && modifierListOwner.getModifierList() != null;
final PsiStatement statement = PsiTreeUtil.getParentOfType(host, PsiStatement.class);
if (!addAnnotation && statement == null)
return false;
Configuration.AdvancedConfiguration configuration = Configuration.getProjectInstance(project).getAdvancedConfiguration();
if (!configuration.isSourceModificationAllowed()) {
host.putUserData(InjectLanguageAction.FIX_KEY, annotationFixer);
return false;
}
new WriteCommandAction(modifierListOwner.getProject(), modifierListOwner.getContainingFile()) {
protected void run(@NotNull Result result) throws Throwable {
PsiElementFactory javaFacade = JavaPsiFacade.getElementFactory(getProject());
if (addAnnotation) {
JVMElementFactory factory = ObjectUtils.chooseNotNull(JVMElementFactories.getFactory(modifierListOwner.getLanguage(), getProject()), javaFacade);
PsiAnnotation annotation = factory.createAnnotationFromText("@" + AnnotationUtil.LANGUAGE + "(\"" + languageId + "\")", modifierListOwner);
PsiModifierList list = ObjectUtils.assertNotNull(modifierListOwner.getModifierList());
final PsiAnnotation existingAnnotation = list.findAnnotation(AnnotationUtil.LANGUAGE);
if (existingAnnotation != null) {
existingAnnotation.replace(annotation);
} else {
list.addAfter(annotation, null);
}
JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(list);
} else {
statement.getParent().addBefore(javaFacade.createCommentFromText("//language=" + languageId, host), statement);
}
}
}.execute();
return true;
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class AbstractFileProcessor method execute.
private void execute(final Runnable readAction, final Runnable writeAction) {
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> ApplicationManager.getApplication().runReadAction(() -> readAction.run()), title, true, myProject);
new WriteCommandAction(myProject, title) {
@Override
protected void run(@NotNull Result result) throws Throwable {
writeAction.run();
}
}.execute();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class AppEngineUploader method createUploader.
@Nullable
public static AppEngineUploader createUploader(@NotNull Project project, @NotNull Artifact artifact, @NotNull AppEngineServerConfiguration configuration, @NotNull ServerRuntimeInstance.DeploymentOperationCallback callback, @NotNull LoggingHandler loggingHandler) {
final String explodedPath = artifact.getOutputPath();
if (explodedPath == null) {
callback.errorOccurred("Output path isn't specified for '" + artifact.getName() + "' artifact");
return null;
}
final AppEngineFacet appEngineFacet = AppEngineUtil.findAppEngineFacet(project, artifact);
if (appEngineFacet == null) {
callback.errorOccurred("App Engine facet not found in '" + artifact.getName() + "' artifact");
return null;
}
final AppEngineSdk sdk = appEngineFacet.getSdk();
if (!sdk.getAppCfgFile().exists()) {
callback.errorOccurred("Path to App Engine SDK isn't specified correctly in App Engine Facet settings");
return null;
}
PackagingElementResolvingContext context = ArtifactManager.getInstance(project).getResolvingContext();
VirtualFile descriptorFile = ArtifactUtil.findSourceFileByOutputPath(artifact, "WEB-INF/appengine-web.xml", context);
final AppEngineWebApp root = AppEngineFacet.getDescriptorRoot(descriptorFile, appEngineFacet.getModule().getProject());
if (root != null) {
final GenericDomValue<String> application = root.getApplication();
if (StringUtil.isEmptyOrSpaces(application.getValue())) {
final String name = Messages.showInputDialog(project, "<html>Application name is not specified in appengine-web.xml.<br>" + "Enter application name (see your <a href=\"http://appengine.google.com\">AppEngine account</a>):</html>", CommonBundle.getErrorTitle(), null, "", null);
if (name == null)
return null;
final PsiFile file = application.getXmlTag().getContainingFile();
new WriteCommandAction(project, file) {
protected void run(@NotNull final Result result) {
application.setStringValue(name);
}
}.execute();
final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
if (document != null) {
FileDocumentManager.getInstance().saveDocument(document);
}
}
}
AppEngineAuthData authData = AppEngineAccountDialog.createAuthData(project, configuration);
if (authData == null)
return null;
return new AppEngineUploader(project, artifact, appEngineFacet, sdk, authData, callback, loggingHandler);
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class AndroidInferNullityAnnotationAction method applyRunnable.
// Intellij code from InferNullityAnnotationsAction.
private static Runnable applyRunnable(Project project, Computable<UsageInfo[]> computable) {
return () -> {
LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_NULLITY_ANNOTATIONS);
try {
new WriteCommandAction(project, INFER_NULLITY_ANNOTATIONS) {
@Override
protected void run(@NotNull Result result) throws Throwable {
UsageInfo[] infos = computable.compute();
if (infos.length > 0) {
Set<PsiElement> elements = new LinkedHashSet<>();
for (UsageInfo info : infos) {
PsiElement element = info.getElement();
if (element != null) {
ContainerUtil.addIfNotNull(elements, element.getContainingFile());
}
}
if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements))
return;
SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_NULLITY_ANNOTATIONS, false);
progressTask.setMinIterationTime(200);
progressTask.setTask(new AnnotateTask(project, progressTask, infos));
ProgressManager.getInstance().run(progressTask);
} else {
NullityInferrer.nothingFoundMessage(project);
}
}
}.execute();
} finally {
action.finish();
}
};
}
Aggregations