Search in sources :

Example 66 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class ConstructorInsertHandler method startTemplate.

private static void startTemplate(final PsiAnonymousClass aClass, final Editor editor, final Runnable runnable, @NotNull final PsiTypeElement[] parameters) {
    final Project project = aClass.getProject();
    new WriteCommandAction(project, getCommandName(), getCommandName()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
            editor.getCaretModel().moveToOffset(aClass.getTextOffset());
            final TemplateBuilderImpl templateBuilder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(aClass);
            for (int i = 0; i < parameters.length; i++) {
                PsiTypeElement parameter = parameters[i];
                templateBuilder.replaceElement(parameter, "param" + i, new TypeExpression(project, new PsiType[] { parameter.getType() }), true);
            }
            Template template = templateBuilder.buildInlineTemplate();
            TemplateManager.getInstance(project).startTemplate(editor, template, false, null, new TemplateEditingAdapter() {

                @Override
                public void templateFinished(Template template, boolean brokenOff) {
                    if (!brokenOff) {
                        runnable.run();
                    }
                }
            });
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) TypeExpression(com.intellij.codeInsight.intention.impl.TypeExpression) Result(com.intellij.openapi.application.Result)

Example 67 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class InferNullityAnnotationsAction method applyRunnable.

private static Runnable applyRunnable(final Project project, final Computable<UsageInfo[]> computable) {
    return () -> {
        final LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_NULLITY_ANNOTATIONS);
        try {
            new WriteCommandAction(project, INFER_NULLITY_ANNOTATIONS) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    final UsageInfo[] infos = computable.compute();
                    if (infos.length > 0) {
                        final Set<PsiElement> elements = new LinkedHashSet<>();
                        for (UsageInfo info : infos) {
                            final PsiElement element = info.getElement();
                            if (element != null) {
                                ContainerUtil.addIfNotNull(elements, element.getContainingFile());
                            }
                        }
                        if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements))
                            return;
                        final 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) SequentialModalProgressTask(com.intellij.util.SequentialModalProgressTask) Result(com.intellij.openapi.application.Result)

Example 68 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class CreateTestDialog method selectTargetDirectory.

@Nullable
private PsiDirectory selectTargetDirectory() throws IncorrectOperationException {
    final String packageName = getPackageName();
    final PackageWrapper targetPackage = new PackageWrapper(PsiManager.getInstance(myProject), packageName);
    final VirtualFile selectedRoot = new ReadAction<VirtualFile>() {

        protected void run(@NotNull Result<VirtualFile> result) throws Throwable {
            final List<VirtualFile> testFolders = CreateTestAction.computeTestRoots(myTargetModule);
            List<VirtualFile> roots;
            if (testFolders.isEmpty()) {
                roots = new ArrayList<>();
                List<String> urls = CreateTestAction.computeSuitableTestRootUrls(myTargetModule);
                for (String url : urls) {
                    ContainerUtil.addIfNotNull(roots, VfsUtil.createDirectories(VfsUtilCore.urlToPath(url)));
                }
                if (roots.isEmpty()) {
                    JavaProjectRootsUtil.collectSuitableDestinationSourceRoots(myTargetModule, roots);
                }
                if (roots.isEmpty())
                    return;
            } else {
                roots = new ArrayList<>(testFolders);
            }
            if (roots.size() == 1) {
                result.setResult(roots.get(0));
            } else {
                PsiDirectory defaultDir = chooseDefaultDirectory(targetPackage.getDirectories(), roots);
                result.setResult(MoveClassesOrPackagesUtil.chooseSourceRoot(targetPackage, roots, defaultDir));
            }
        }
    }.execute().getResultObject();
    if (selectedRoot == null)
        return null;
    return new WriteCommandAction<PsiDirectory>(myProject, CodeInsightBundle.message("create.directory.command")) {

        protected void run(@NotNull Result<PsiDirectory> result) throws Throwable {
            result.setResult(RefactoringUtil.createPackageDirectoryInSourceRoot(targetPackage, selectedRoot));
        }
    }.execute().getResultObject();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ReadAction(com.intellij.openapi.application.ReadAction) NotNull(org.jetbrains.annotations.NotNull) PackageWrapper(com.intellij.refactoring.PackageWrapper) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Example 69 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class AbstractConvertLineSeparatorsAction method changeLineSeparators.

public static void changeLineSeparators(@NotNull final Project project, @NotNull final VirtualFile virtualFile, @NotNull final String newSeparator) {
    FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    Document document = fileDocumentManager.getCachedDocument(virtualFile);
    if (document != null) {
        fileDocumentManager.saveDocument(document);
    }
    String currentSeparator = LoadTextUtil.detectLineSeparator(virtualFile, false);
    final String commandText;
    if (StringUtil.isEmpty(currentSeparator)) {
        commandText = "Changed line separators to " + LineSeparator.fromString(newSeparator);
    } else {
        commandText = String.format("Changed line separators from %s to %s", LineSeparator.fromString(currentSeparator), LineSeparator.fromString(newSeparator));
    }
    new WriteCommandAction(project, commandText) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            try {
                LoadTextUtil.changeLineSeparators(project, virtualFile, newSeparator, this);
            } catch (IOException e) {
                LOG.info(e);
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) Result(com.intellij.openapi.application.Result)

Example 70 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.

the class AppBarConfigurationDialog method open.

public boolean open(@NotNull final XmlFile file) {
    myCollapsedPreview.setMinimumSize(new Dimension(START_WIDTH, START_HEIGHT));
    myExpandedPreview.setMinimumSize(new Dimension(START_WIDTH, START_HEIGHT));
    pack();
    myCollapsedPreview.setMinimumSize(null);
    myExpandedPreview.setMinimumSize(null);
    Dimension size = getSize();
    Rectangle screen = getGraphicsConfiguration().getBounds();
    setLocation(screen.x + (screen.width - size.width) / 2, screen.y + (screen.height - size.height) / 2);
    updateControls();
    myButtonOK.requestFocus();
    generatePreviews();
    setVisible(true);
    if (myWasAccepted) {
        Project project = file.getProject();
        WriteCommandAction action = new WriteCommandAction(project, "Configure App Bar", file) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                applyChanges(file);
            }
        };
        action.execute();
    }
    return myWasAccepted;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) NotNull(org.jetbrains.annotations.NotNull) 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