Search in sources :

Example 1 with ReplacePromptDialog

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);
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) ReplacePromptDialog(com.intellij.ui.ReplacePromptDialog) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) PsiElement(com.intellij.psi.PsiElement)

Example 2 with ReplacePromptDialog

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;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) ReplacePromptDialog(com.intellij.ui.ReplacePromptDialog) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Result(com.intellij.openapi.application.Result)

Example 3 with ReplacePromptDialog

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();
}
Also used : ReplacePromptDialog(com.intellij.ui.ReplacePromptDialog)

Example 4 with ReplacePromptDialog

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);
        }
    }
}
Also used : ReplacePromptDialog(com.intellij.ui.ReplacePromptDialog)

Aggregations

ReplacePromptDialog (com.intellij.ui.ReplacePromptDialog)4 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1