Search in sources :

Example 51 with WriteCommandAction

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();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with WriteCommandAction

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;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Configuration(org.intellij.plugins.intelliLang.Configuration) Result(com.intellij.openapi.application.Result)

Example 53 with WriteCommandAction

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();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Result(com.intellij.openapi.application.Result)

Example 54 with WriteCommandAction

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PackagingElementResolvingContext(com.intellij.packaging.elements.PackagingElementResolvingContext) Document(com.intellij.openapi.editor.Document) AppEngineWebApp(com.intellij.appengine.descriptor.dom.AppEngineWebApp) Result(com.intellij.openapi.application.Result) AppEngineFacet(com.intellij.appengine.facet.AppEngineFacet) PsiFile(com.intellij.psi.PsiFile) AppEngineSdk(com.intellij.appengine.sdk.AppEngineSdk) AppEngineAuthData(com.intellij.appengine.cloud.AppEngineAuthData) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with WriteCommandAction

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();
        }
    };
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) LocalHistoryAction(com.intellij.history.LocalHistoryAction) NotNull(org.jetbrains.annotations.NotNull) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement) SequentialModalProgressTask(com.intellij.util.SequentialModalProgressTask) Result(com.intellij.openapi.application.Result)

Aggregations

WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)176 Result (com.intellij.openapi.application.Result)175 NotNull (org.jetbrains.annotations.NotNull)62 Project (com.intellij.openapi.project.Project)45 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 XmlFile (com.intellij.psi.xml.XmlFile)28 XmlTag (com.intellij.psi.xml.XmlTag)23 Document (com.intellij.openapi.editor.Document)22 PsiFile (com.intellij.psi.PsiFile)16 Module (com.intellij.openapi.module.Module)14 Nullable (org.jetbrains.annotations.Nullable)12 NlModel (com.android.tools.idea.uibuilder.model.NlModel)11 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)10 Editor (com.intellij.openapi.editor.Editor)10 TextRange (com.intellij.openapi.util.TextRange)8 XmlAttribute (com.intellij.psi.xml.XmlAttribute)8 File (java.io.File)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)7