Search in sources :

Example 16 with HighlightManager

use of com.intellij.codeInsight.highlighting.HighlightManager 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 17 with HighlightManager

use of com.intellij.codeInsight.highlighting.HighlightManager 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 18 with HighlightManager

use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.

the class TempWithQueryHandler method invokeOnVariable.

private static void invokeOnVariable(final PsiFile file, final Project project, final PsiLocalVariable local, final Editor editor) {
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file))
        return;
    String localName = local.getName();
    final PsiExpression initializer = local.getInitializer();
    if (initializer == null) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.has.no.initializer", localName));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        return;
    }
    final PsiReference[] refs = ReferencesSearch.search(local, GlobalSearchScope.projectScope(project), false).toArray(PsiReference.EMPTY_ARRAY);
    if (refs.length == 0) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.never.used", localName));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        return;
    }
    final HighlightManager highlightManager = HighlightManager.getInstance(project);
    ArrayList<PsiReference> array = new ArrayList<>();
    EditorColorsManager manager = EditorColorsManager.getInstance();
    final TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    for (PsiReference ref : refs) {
        PsiElement refElement = ref.getElement();
        if (PsiUtil.isAccessedForWriting((PsiExpression) refElement)) {
            array.add(ref);
        }
        if (!array.isEmpty()) {
            PsiReference[] refsForWriting = array.toArray(new PsiReference[array.size()]);
            highlightManager.addOccurrenceHighlights(editor, refsForWriting, attributes, true, null);
            String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.accessed.for.writing", localName));
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
            WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
            return;
        }
    }
    final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, new PsiElement[] { initializer }, local.getType(), REFACTORING_NAME, localName, HelpID.REPLACE_TEMP_WITH_QUERY);
    try {
        if (!processor.prepare())
            return;
    } catch (PrepareFailedException e) {
        CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        ExtractMethodHandler.highlightPrepareError(e, file, editor, project);
        return;
    }
    final PsiClass targetClass = processor.getTargetClass();
    if (targetClass != null && targetClass.isInterface()) {
        String message = RefactoringBundle.message("cannot.replace.temp.with.query.in.interface");
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.REPLACE_TEMP_WITH_QUERY);
        return;
    }
    if (processor.showDialog()) {
        CommandProcessor.getInstance().executeCommand(project, () -> {
            final Runnable action = () -> {
                try {
                    processor.doRefactoring();
                    local.normalizeDeclaration();
                    PsiExpression initializer1 = local.getInitializer();
                    PsiExpression[] exprs = new PsiExpression[refs.length];
                    for (int idx = 0; idx < refs.length; idx++) {
                        PsiElement ref = refs[idx].getElement();
                        exprs[idx] = (PsiExpression) ref.replace(initializer1);
                    }
                    PsiDeclarationStatement declaration = (PsiDeclarationStatement) local.getParent();
                    declaration.delete();
                    highlightManager.addOccurrenceHighlights(editor, exprs, attributes, true, null);
                } catch (IncorrectOperationException e) {
                    LOG.error(e);
                }
            };
            PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
                ApplicationManager.getApplication().runWriteAction(action);
                DuplicatesImpl.processDuplicates(processor, project, editor);
            });
        }, REFACTORING_NAME, null);
    }
    WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
}
Also used : PrepareFailedException(com.intellij.refactoring.extractMethod.PrepareFailedException) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ExtractMethodProcessor(com.intellij.refactoring.extractMethod.ExtractMethodProcessor) ArrayList(java.util.ArrayList) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 19 with HighlightManager

use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.

the class ThreadDumpPanel method highlightOccurrences.

private static void highlightOccurrences(String filter, Project project, Editor editor) {
    final HighlightManager highlightManager = HighlightManager.getInstance(project);
    EditorColorsManager colorManager = EditorColorsManager.getInstance();
    final TextAttributes attributes = colorManager.getGlobalScheme().getAttributes(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES);
    String documentText = editor.getDocument().getText();
    int i = -1;
    while (true) {
        int nextOccurrence = StringUtil.indexOfIgnoreCase(documentText, filter, i + 1);
        if (nextOccurrence < 0) {
            break;
        }
        i = nextOccurrence;
        highlightManager.addOccurrenceHighlight(editor, i, i + filter.length(), attributes, HighlightManager.HIDE_BY_TEXT_CHANGE, null, null);
    }
}
Also used : HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager)

Example 20 with HighlightManager

use of com.intellij.codeInsight.highlighting.HighlightManager in project intellij-community by JetBrains.

the class HighlightUtil method highlightElements.

public static void highlightElements(@NotNull final Collection<? extends PsiElement> elementCollection, @NotNull final String statusBarText) {
    if (elementCollection.isEmpty()) {
        return;
    }
    final Application application = ApplicationManager.getApplication();
    application.invokeLater(() -> {
        final PsiElement[] elements = PsiUtilCore.toPsiElementArray(elementCollection);
        final PsiElement firstElement = elements[0];
        if (!firstElement.isValid()) {
            return;
        }
        final Project project = firstElement.getProject();
        final FileEditorManager editorManager = FileEditorManager.getInstance(project);
        final EditorColorsManager editorColorsManager = EditorColorsManager.getInstance();
        final Editor editor = editorManager.getSelectedTextEditor();
        if (editor == null) {
            return;
        }
        final EditorColorsScheme globalScheme = editorColorsManager.getGlobalScheme();
        final TextAttributes textattributes = globalScheme.getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
        final HighlightManager highlightManager = HighlightManager.getInstance(project);
        highlightManager.addOccurrenceHighlights(editor, elements, textattributes, true, null);
        final FindManager findmanager = FindManager.getInstance(project);
        FindModel findmodel = findmanager.getFindNextModel();
        if (findmodel == null) {
            findmodel = findmanager.getFindInFileModel();
        }
        findmodel.setSearchHighlighters(true);
        findmanager.setFindWasPerformed();
        findmanager.setFindNextModel(findmodel);
        application.invokeLater(() -> {
            final WindowManager windowManager = WindowManager.getInstance();
            final StatusBar statusBar = windowManager.getStatusBar(project);
            if (statusBar != null) {
                statusBar.setInfo(statusBarText);
            }
        });
    });
}
Also used : FindModel(com.intellij.find.FindModel) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) StatusBar(com.intellij.openapi.wm.StatusBar) WindowManager(com.intellij.openapi.wm.WindowManager) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) FindManager(com.intellij.find.FindManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) Editor(com.intellij.openapi.editor.Editor) Application(com.intellij.openapi.application.Application) PsiElement(com.intellij.psi.PsiElement)

Aggregations

HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)34 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)26 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)23 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)18 Editor (com.intellij.openapi.editor.Editor)9 Project (com.intellij.openapi.project.Project)9 TextRange (com.intellij.openapi.util.TextRange)9 ArrayList (java.util.ArrayList)8 PsiElement (com.intellij.psi.PsiElement)6 WindowManager (com.intellij.openapi.wm.WindowManager)4 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)3 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)3 XmlTag (com.intellij.psi.xml.XmlTag)3 Nullable (org.jetbrains.annotations.Nullable)3 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)2 FindManager (com.intellij.find.FindManager)2 FindModel (com.intellij.find.FindModel)2 EditorWindow (com.intellij.injected.editor.EditorWindow)2 Application (com.intellij.openapi.application.Application)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2