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();
}
};
}
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();
}
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();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class DuplicatesImpl method replaceMatch.
private static boolean replaceMatch(final Project project, final MatchProvider provider, final Match match, @NotNull final Editor editor, final int idx, final int size, Ref<Boolean> showAll, final String confirmDuplicatePrompt, boolean skipPromptWhenOne) {
final ArrayList<RangeHighlighter> highlighters = previewMatch(project, match, editor);
try {
if (!ApplicationManager.getApplication().isUnitTestMode()) {
if ((!skipPromptWhenOne || size > 1) && (showAll.get() == null || !showAll.get())) {
final String prompt = provider.getConfirmDuplicatePrompt(match);
final ReplacePromptDialog promptDialog = new ReplacePromptDialog(false, provider.getReplaceDuplicatesTitle(idx, size), project) {
@Override
protected String getMessage() {
final String message = super.getMessage();
return prompt != null ? message + " " + prompt : message;
}
};
promptDialog.show();
final boolean allChosen = promptDialog.getExitCode() == FindManager.PromptResult.ALL;
showAll.set(allChosen);
if (allChosen && confirmDuplicatePrompt != null && prompt == null) {
if (Messages.showOkCancelDialog(project, "In order to replace all occurrences method signature will be changed. Proceed?", CommonBundle.getWarningTitle(), Messages.getWarningIcon()) != Messages.OK)
return true;
}
if (promptDialog.getExitCode() == FindManager.PromptResult.SKIP)
return false;
if (promptDialog.getExitCode() == FindManager.PromptResult.CANCEL)
return true;
}
}
} finally {
HighlightManager.getInstance(project).removeSegmentHighlighter(editor, highlighters.get(0));
}
// call change signature when needed
provider.prepareSignature(match);
new WriteCommandAction(project, MethodDuplicatesHandler.REFACTORING_NAME, MethodDuplicatesHandler.REFACTORING_NAME) {
@Override
protected void run(@NotNull Result result) throws Throwable {
try {
provider.processMatch(match);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}.execute();
return false;
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class CreateFieldOrPropertyFix method applyFixInner.
private void applyFixInner(final Project project) {
final PsiFile file = myClass.getContainingFile();
final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, myClass.getContainingFile(), myClass);
if (editor != null) {
new WriteCommandAction(project, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
generateMembers(project, editor, file);
}
@Override
protected boolean isGlobalUndoAction() {
// todo check
return true;
}
}.execute();
}
}
Aggregations