Search in sources :

Example 46 with RangeHighlighter

use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.

the class ExtractMethodHelper method highlightInEditor.

private static void highlightInEditor(@NotNull final Project project, @NotNull final SimpleMatch match, @NotNull final Editor editor, Map<SimpleMatch, RangeHighlighter> highlighterMap) {
    final List<RangeHighlighter> highlighters = new ArrayList<>();
    final HighlightManager highlightManager = HighlightManager.getInstance(project);
    final EditorColorsManager colorsManager = EditorColorsManager.getInstance();
    final TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    final int startOffset = match.getStartElement().getTextRange().getStartOffset();
    final int endOffset = match.getEndElement().getTextRange().getEndOffset();
    highlightManager.addRangeHighlight(editor, startOffset, endOffset, attributes, true, highlighters);
    highlighterMap.put(match, highlighters.get(0));
    final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(startOffset);
    editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.MAKE_VISIBLE);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager)

Example 47 with RangeHighlighter

use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.

the class InplaceChangeSignature method detach.

public void detach() {
    myEditor.getDocument().removeDocumentListener(this);
    HighlightManager highlightManager = HighlightManager.getInstance(myProject);
    for (RangeHighlighter highlighter : myHighlighters) {
        highlightManager.removeSegmentHighlighter(myEditor, highlighter);
    }
    myHighlighters.clear();
    myBalloon.hide();
    myDetector = null;
    FinishMarkAction.finish(myProject, myEditor, myMarkAction);
    myEditor.putUserData(INPLACE_CHANGE_SIGNATURE, null);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager)

Example 48 with RangeHighlighter

use of com.intellij.openapi.editor.markup.RangeHighlighter 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 49 with RangeHighlighter

use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.

the class ScopeHighlighter method addHighlighter.

private void addHighlighter(TextRange r, int level, TextAttributes attr) {
    MarkupModel markupModel = myEditor.getMarkupModel();
    RangeHighlighter highlighter = markupModel.addRangeHighlighter(r.getStartOffset(), r.getEndOffset(), level, attr, HighlighterTargetArea.EXACT_RANGE);
    myActiveHighliters.add(highlighter);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel)

Example 50 with RangeHighlighter

use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.

the class TemplateState method initTabStopHighlighters.

private void initTabStopHighlighters() {
    final Set<String> vars = new HashSet<>();
    for (int i = 0; i < myTemplate.getVariableCount(); i++) {
        String variableName = myTemplate.getVariableNameAt(i);
        if (!vars.add(variableName))
            continue;
        int segmentNumber = myTemplate.getVariableSegmentNumber(variableName);
        if (segmentNumber < 0)
            continue;
        RangeHighlighter segmentHighlighter = getSegmentHighlighter(segmentNumber, false, false);
        myTabStopHighlighters.add(segmentHighlighter);
    }
    int endSegmentNumber = myTemplate.getEndSegmentNumber();
    if (endSegmentNumber >= 0) {
        RangeHighlighter segmentHighlighter = getSegmentHighlighter(endSegmentNumber, false, true);
        myTabStopHighlighters.add(segmentHighlighter);
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter)

Aggregations

RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)97 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)26 TextRange (com.intellij.openapi.util.TextRange)22 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)18 Editor (com.intellij.openapi.editor.Editor)15 ArrayList (java.util.ArrayList)15 NotNull (org.jetbrains.annotations.NotNull)15 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)10 Project (com.intellij.openapi.project.Project)10 RelativePoint (com.intellij.ui.awt.RelativePoint)8 Nullable (org.jetbrains.annotations.Nullable)8 Document (com.intellij.openapi.editor.Document)7 EditorEx (com.intellij.openapi.editor.ex.EditorEx)7 MarkupModelEx (com.intellij.openapi.editor.ex.MarkupModelEx)7 MarkupModel (com.intellij.openapi.editor.markup.MarkupModel)7 PsiElement (com.intellij.psi.PsiElement)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 EditorWindow (com.intellij.injected.editor.EditorWindow)4 Result (com.intellij.openapi.application.Result)4 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)4