Search in sources :

Example 11 with LocalHistoryAction

use of com.intellij.history.LocalHistoryAction 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 12 with LocalHistoryAction

use of com.intellij.history.LocalHistoryAction in project intellij-community by JetBrains.

the class ExtractInterfaceHandler method doRefactoring.

private void doRefactoring() throws IncorrectOperationException {
    LocalHistoryAction a = LocalHistory.getInstance().startAction(getCommandName());
    final PsiClass anInterface;
    try {
        anInterface = extractInterface(myTargetDir, myClass, myInterfaceName, mySelectedMembers, myJavaDocPolicy);
    } finally {
        a.finish();
    }
    ExtractClassUtil.suggestToTurnRefsToSuper(myProject, anInterface, myClass);
}
Also used : LocalHistoryAction(com.intellij.history.LocalHistoryAction)

Example 13 with LocalHistoryAction

use of com.intellij.history.LocalHistoryAction in project android by JetBrains.

the class InferSupportAnnotationsAction method checkModules.

// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
    Set<Module> modulesWithoutAnnotations = new HashSet<>();
    Set<Module> modulesWithLowVersion = new HashSet<>();
    for (Module module : modules.keySet()) {
        AndroidModuleInfo info = AndroidModuleInfo.get(module);
        if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
            modulesWithLowVersion.add(module);
        }
        GradleBuildModel buildModel = GradleBuildModel.get(module);
        if (buildModel == null) {
            Logger.getInstance(InferSupportAnnotationsAction.class).warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
            continue;
        }
        boolean dependencyFound = false;
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
                String notation = dependency.compactNotation().value();
                if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
                    dependencyFound = true;
                    break;
                }
            }
        }
        if (!dependencyFound) {
            modulesWithoutAnnotations.add(module);
        }
    }
    if (!modulesWithLowVersion.isEmpty()) {
        Messages.showErrorDialog(project, String.format("Infer Support Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Support Annotations");
        return false;
    }
    if (modulesWithoutAnnotations.isEmpty()) {
        return true;
    }
    String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
    int count = modulesWithoutAnnotations.size();
    String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
    if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
        try {
            new WriteCommandAction(project, ADD_DEPENDENCY) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    RepositoryUrlManager manager = RepositoryUrlManager.get();
                    String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
                    for (Module module : modulesWithoutAnnotations) {
                        addDependency(module, annotationsLibraryCoordinate);
                    }
                    GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
                    GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {

                        @Override
                        public void syncSucceeded(@NotNull Project project) {
                            restartAnalysis(project, scope);
                        }
                    });
                }
            }.execute();
        } finally {
            action.finish();
        }
    }
    return false;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) LocalHistoryAction(com.intellij.history.LocalHistoryAction) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) Module(com.intellij.openapi.module.Module)

Example 14 with LocalHistoryAction

use of com.intellij.history.LocalHistoryAction in project intellij by bazelbuild.

the class NewBlazePackageAction method createPackageOnDisk.

private Optional<VirtualFile> createPackageOnDisk(Project project, BlazeContext context, Label newRule, Kind ruleKind) {
    String commandName = String.format("Creating %s package: %s", Blaze.buildSystemName(project), newRule.toString());
    return new WriteCommandAction<Optional<VirtualFile>>(project, commandName) {

        @Override
        protected void run(@NotNull Result<Optional<VirtualFile>> result) throws Throwable {
            LocalHistory localHistory = LocalHistory.getInstance();
            LocalHistoryAction action = localHistory.startAction(commandName);
            try {
                WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
                File dir = workspaceRoot.fileForPath(newRule.blazePackage());
                try {
                    VirtualFile newDirectory = VfsUtil.createDirectories(dir.getPath());
                    VirtualFile newFile = newDirectory.createChildData(this, BUILD_FILE_NAME);
                    BuildFileModifier buildFileModifier = BuildFileModifier.getInstance();
                    buildFileModifier.addRule(project, newRule, ruleKind);
                    result.setResult(Optional.of(newFile));
                } catch (IOException e) {
                    String errorMessage = "Error creating new package: " + e.getMessage();
                    context.output(PrintOutput.error(errorMessage));
                    logger.warn("Error creating new package", e);
                    result.setResult(Optional.empty());
                }
            } finally {
                action.finish();
            }
        }
    }.execute().getResultObject();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalHistoryAction(com.intellij.history.LocalHistoryAction) LocalHistory(com.intellij.history.LocalHistory) BuildFileModifier(com.google.idea.blaze.base.buildmodifier.BuildFileModifier) IOException(java.io.IOException) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) NotNull(org.jetbrains.annotations.NotNull) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Result(com.intellij.openapi.application.Result)

Example 15 with LocalHistoryAction

use of com.intellij.history.LocalHistoryAction in project android by JetBrains.

the class InferSupportAnnotationsAction method applyRunnable.

private static Runnable applyRunnable(Project project, Computable<UsageInfo[]> computable) {
    return () -> {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_SUPPORT_ANNOTATIONS);
        try {
            new WriteCommandAction(project, INFER_SUPPORT_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) {
                                PsiFile containingFile = element.getContainingFile();
                                // Skip results in .class files; these are typically from extracted AAR files
                                VirtualFile virtualFile = containingFile.getVirtualFile();
                                if (virtualFile.getFileType().isBinary()) {
                                    continue;
                                }
                                ContainerUtil.addIfNotNull(elements, containingFile);
                            }
                        }
                        if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements))
                            return;
                        SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_SUPPORT_ANNOTATIONS, false);
                        progressTask.setMinIterationTime(200);
                        progressTask.setTask(new AnnotateTask(project, progressTask, infos));
                        ProgressManager.getInstance().run(progressTask);
                    } else {
                        InferSupportAnnotations.nothingFoundMessage(project);
                    }
                }
            }.execute();
        } finally {
            action.finish();
        }
    };
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull) SequentialModalProgressTask(com.intellij.util.SequentialModalProgressTask) Result(com.intellij.openapi.application.Result) LocalHistoryAction(com.intellij.history.LocalHistoryAction) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

LocalHistoryAction (com.intellij.history.LocalHistoryAction)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 Result (com.intellij.openapi.application.Result)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)7 NotNull (org.jetbrains.annotations.NotNull)7 UsageInfo (com.intellij.usageView.UsageInfo)4 Revision (com.intellij.history.core.revisions.Revision)3 Project (com.intellij.openapi.project.Project)3 PsiElement (com.intellij.psi.PsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 SequentialModalProgressTask (com.intellij.util.SequentialModalProgressTask)3 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)2 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)2 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)2 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)2 RepositoryUrlManager (com.android.tools.idea.templates.RepositoryUrlManager)2 BuildFileModifier (com.google.idea.blaze.base.buildmodifier.BuildFileModifier)2 WorkspaceRoot (com.google.idea.blaze.base.model.primitives.WorkspaceRoot)2 LocalHistory (com.intellij.history.LocalHistory)2 Module (com.intellij.openapi.module.Module)2