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();
}
};
}
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);
}
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;
}
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();
}
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();
}
};
}
Aggregations