use of com.intellij.ui.ReplacePromptDialog in project intellij-community by JetBrains.
the class ExtractMethodHelper method replaceDuplicates.
/**
* Notifies user about found duplicates and then highlights each of them in the editor and asks user how to proceed.
*
* @param callElement generated expression or statement that contains invocation of the new method
* @param editor instance of editor where refactoring is performed
* @param replacer strategy of substituting each duplicate occurence with the replacement fragment
* @param duplicates discovered duplicates of extracted code fragment
* @see #collectDuplicates(SimpleDuplicatesFinder, List, PsiElement)
*/
public static void replaceDuplicates(@NotNull PsiElement callElement, @NotNull Editor editor, @NotNull Consumer<Pair<SimpleMatch, PsiElement>> replacer, @NotNull List<SimpleMatch> duplicates) {
if (!duplicates.isEmpty()) {
final String message = RefactoringBundle.message("0.has.detected.1.code.fragments.in.this.file.that.can.be.replaced.with.a.call.to.extracted.method", ApplicationNamesInfo.getInstance().getProductName(), duplicates.size());
final boolean isUnittest = ApplicationManager.getApplication().isUnitTestMode();
final Project project = callElement.getProject();
final int exitCode = !isUnittest ? Messages.showYesNoDialog(project, message, RefactoringBundle.message("refactoring.extract.method.dialog.title"), Messages.getInformationIcon()) : Messages.YES;
if (exitCode == Messages.YES) {
boolean replaceAll = false;
final Map<SimpleMatch, RangeHighlighter> highlighterMap = new HashMap<>();
for (SimpleMatch match : duplicates) {
if (!match.getStartElement().isValid() || !match.getEndElement().isValid())
continue;
final Pair<SimpleMatch, PsiElement> replacement = Pair.create(match, callElement);
if (!replaceAll) {
highlightInEditor(project, match, editor, highlighterMap);
int promptResult = FindManager.PromptResult.ALL;
//noinspection ConstantConditions
if (!isUnittest) {
ReplacePromptDialog promptDialog = new ReplacePromptDialog(false, RefactoringBundle.message("replace.fragment"), project);
promptDialog.show();
promptResult = promptDialog.getExitCode();
}
if (promptResult == FindManager.PromptResult.SKIP) {
final HighlightManager highlightManager = HighlightManager.getInstance(project);
final RangeHighlighter highlighter = highlighterMap.get(match);
if (highlighter != null)
highlightManager.removeSegmentHighlighter(editor, highlighter);
continue;
}
if (promptResult == FindManager.PromptResult.CANCEL)
break;
if (promptResult == FindManager.PromptResult.OK) {
replaceDuplicate(project, replacer, replacement);
} else if (promptResult == FindManager.PromptResult.ALL) {
replaceDuplicate(project, replacer, replacement);
replaceAll = true;
}
} else {
replaceDuplicate(project, replacer, replacement);
}
}
}
}
}
use of com.intellij.ui.ReplacePromptDialog 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.ui.ReplacePromptDialog in project intellij-community by JetBrains.
the class FindManagerImpl method showPromptDialogImpl.
@PromptResultValue
public int showPromptDialogImpl(@NotNull final FindModel model, String title, @Nullable final MalformedReplacementStringException exception) {
ReplacePromptDialog replacePromptDialog = new ReplacePromptDialog(model.isMultipleFiles(), title, myProject, exception) {
@Override
@Nullable
public Point getInitialLocation() {
if (model.isMultipleFiles() && myReplaceInProjectPromptPos.x >= 0 && myReplaceInProjectPromptPos.y >= 0) {
return myReplaceInProjectPromptPos;
}
if (!model.isMultipleFiles() && myReplaceInFilePromptPos.x >= 0 && myReplaceInFilePromptPos.y >= 0) {
return myReplaceInFilePromptPos;
}
return null;
}
};
replacePromptDialog.show();
if (model.isMultipleFiles()) {
myReplaceInProjectPromptPos = replacePromptDialog.getLocation();
} else {
myReplaceInFilePromptPos = replacePromptDialog.getLocation();
}
return replacePromptDialog.getExitCode();
}
use of com.intellij.ui.ReplacePromptDialog in project intellij-community by JetBrains.
the class ExtractIncludeFileBase method replaceDuplicates.
private void replaceDuplicates(final String includePath, final List<IncludeDuplicate<T>> duplicates, final Editor editor, final Project project) {
if (duplicates.size() > 0) {
final String message = RefactoringBundle.message("idea.has.found.fragments.that.can.be.replaced.with.include.directive", ApplicationNamesInfo.getInstance().getProductName());
final int exitCode = Messages.showYesNoDialog(project, message, getRefactoringName(), Messages.getInformationIcon());
if (exitCode == Messages.YES) {
CommandProcessor.getInstance().executeCommand(project, () -> {
boolean replaceAll = false;
for (IncludeDuplicate<T> pair : duplicates) {
if (!replaceAll) {
highlightInEditor(project, pair, editor);
ReplacePromptDialog promptDialog = new ReplacePromptDialog(false, RefactoringBundle.message("replace.fragment"), project);
promptDialog.show();
final int promptResult = promptDialog.getExitCode();
if (promptResult == FindManager.PromptResult.SKIP)
continue;
if (promptResult == FindManager.PromptResult.CANCEL)
break;
if (promptResult == FindManager.PromptResult.OK) {
doReplaceRange(includePath, pair.getStart(), pair.getEnd());
} else if (promptResult == FindManager.PromptResult.ALL) {
doReplaceRange(includePath, pair.getStart(), pair.getEnd());
replaceAll = true;
} else {
LOG.error("Unknown return status");
}
} else {
doReplaceRange(includePath, pair.getStart(), pair.getEnd());
}
}
}, RefactoringBundle.message("remove.duplicates.command"), null);
}
}
}
Aggregations