Search in sources :

Example 31 with HighlightManager

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

the class GroovyRefactoringUtil method highlightOccurrencesByRanges.

public static void highlightOccurrencesByRanges(Project project, Editor editor, TextRange[] ranges) {
    if (editor == null)
        return;
    ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
    HighlightManager highlightManager = HighlightManager.getInstance(project);
    EditorColorsManager colorsManager = EditorColorsManager.getInstance();
    TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    for (TextRange range : ranges) {
        highlightManager.addRangeHighlight(editor, range.getStartOffset(), range.getEndOffset(), attributes, false, highlighters);
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager)

Example 32 with HighlightManager

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

the class GrIntroduceHandlerBase method showDialog.

@Nullable
private Settings showDialog(@NotNull GrIntroduceContext context) {
    // Add occurrences highlighting
    ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
    HighlightManager highlightManager = null;
    if (context.getEditor() != null) {
        highlightManager = HighlightManager.getInstance(context.getProject());
        EditorColorsManager colorsManager = EditorColorsManager.getInstance();
        TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
        if (context.getOccurrences().length > 1) {
            highlightManager.addOccurrenceHighlights(context.getEditor(), context.getOccurrences(), attributes, true, highlighters);
        }
    }
    GrIntroduceDialog<Settings> dialog = getDialog(context);
    dialog.show();
    if (dialog.isOK()) {
        if (context.getEditor() != null) {
            for (RangeHighlighter highlighter : highlighters) {
                highlightManager.removeSegmentHighlighter(context.getEditor(), highlighter);
            }
        }
        return dialog.getSettings();
    } else {
        if (context.getOccurrences().length > 1) {
            WindowManager.getInstance().getStatusBar(context.getProject()).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
        }
    }
    return null;
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with HighlightManager

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

the class BreadcrumbsXmlWrapper method itemHovered.

@Override
public void itemHovered(@Nullable BreadcrumbsPsiItem item) {
    if (!Registry.is("editor.breadcrumbs.highlight.on.hover")) {
        return;
    }
    HighlightManager hm = HighlightManager.getInstance(myProject);
    if (myHighlighed != null) {
        for (RangeHighlighter highlighter : myHighlighed) {
            hm.removeSegmentHighlighter(myEditor, highlighter);
        }
        myHighlighed = null;
    }
    if (item != null) {
        final TextRange range = item.getPsiElement().getTextRange();
        final TextAttributes attributes = new TextAttributes();
        final CrumbPresentation p = item.getPresentation();
        final Color color = p != null ? p.getBackgroundColor(false, false, false) : BreadcrumbsComponent.ButtonSettings.getBackgroundColor(false, false, false, false);
        final Color background = EditorColorsManager.getInstance().getGlobalScheme().getColor(EditorColors.CARET_ROW_COLOR);
        attributes.setBackgroundColor(XmlTagTreeHighlightingUtil.makeTransparent(color, background != null ? background : Gray._200, 0.3));
        myHighlighed = new ArrayList<>(1);
        int flags = HighlightManager.HIDE_BY_ESCAPE | HighlightManager.HIDE_BY_TEXT_CHANGE | HighlightManager.HIDE_BY_ANY_KEY;
        hm.addOccurrenceHighlight(myEditor, range.getStartOffset(), range.getEndOffset(), attributes, flags, myHighlighed, null);
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange)

Example 34 with HighlightManager

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

the class PyInlineLocalHandler method invoke.

private static void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PyTargetExpression local, @Nullable PyReferenceExpression refExpr) {
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, local))
        return;
    final HighlightManager highlightManager = HighlightManager.getInstance(project);
    final TextAttributes writeAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES);
    final String localName = local.getName();
    final ScopeOwner containerBlock = getContext(local);
    LOG.assertTrue(containerBlock != null);
    final Pair<PyStatement, Boolean> defPair = getAssignmentToInline(containerBlock, refExpr, local, project);
    final PyStatement def = defPair.first;
    if (def == null || getValue(def) == null) {
        final String key = defPair.second ? "variable.has.no.dominating.definition" : "variable.has.no.initializer";
        final String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message(key, localName));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HELP_ID);
        return;
    }
    if (def instanceof PyAssignmentStatement && ((PyAssignmentStatement) def).getTargets().length > 1) {
        highlightManager.addOccurrenceHighlights(editor, new PsiElement[] { def }, writeAttributes, true, null);
        final String message = RefactoringBundle.getCannotRefactorMessage(PyBundle.message("refactoring.inline.local.multiassignment", localName));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HELP_ID);
        return;
    }
    final PsiElement[] refsToInline = PyDefUseUtil.getPostRefs(containerBlock, local, getObject(def));
    if (refsToInline.length == 0) {
        final String message = RefactoringBundle.message("variable.is.never.used", localName);
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HELP_ID);
        return;
    }
    final TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        highlightManager.addOccurrenceHighlights(editor, refsToInline, attributes, true, null);
        final int occurrencesCount = refsToInline.length;
        final String occurrencesString = RefactoringBundle.message("occurrences.string", occurrencesCount);
        final String question = RefactoringBundle.message("inline.local.variable.prompt", localName) + " " + occurrencesString;
        final RefactoringMessageDialog dialog = new RefactoringMessageDialog(REFACTORING_NAME, question, HELP_ID, "OptionPane.questionIcon", true, project);
        if (!dialog.showAndGet()) {
            WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
            return;
        }
    }
    final PsiFile workingFile = local.getContainingFile();
    for (PsiElement ref : refsToInline) {
        final PsiFile otherFile = ref.getContainingFile();
        if (!otherFile.equals(workingFile)) {
            final String message = RefactoringBundle.message("variable.is.referenced.in.multiple.files", localName);
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HELP_ID);
            return;
        }
    }
    for (final PsiElement ref : refsToInline) {
        final List<PsiElement> elems = new ArrayList<>();
        final List<Instruction> latestDefs = PyDefUseUtil.getLatestDefs(containerBlock, local.getName(), ref, false, false);
        for (Instruction i : latestDefs) {
            elems.add(i.getElement());
        }
        final PsiElement[] defs = elems.toArray(new PsiElement[elems.size()]);
        boolean isSameDefinition = true;
        for (PsiElement otherDef : defs) {
            isSameDefinition &= isSameDefinition(def, otherDef);
        }
        if (!isSameDefinition) {
            highlightManager.addOccurrenceHighlights(editor, defs, writeAttributes, true, null);
            highlightManager.addOccurrenceHighlights(editor, new PsiElement[] { ref }, attributes, true, null);
            final String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.accessed.for.writing.and.used.with.inlined", localName));
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HELP_ID);
            WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
            return;
        }
    }
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            final RefactoringEventData afterData = new RefactoringEventData();
            afterData.addElement(local);
            project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(getRefactoringId(), afterData);
            final PsiElement[] exprs = new PsiElement[refsToInline.length];
            final PyExpression value = prepareValue(def, localName, project);
            final PyExpression withParenthesis = PyElementGenerator.getInstance(project).createExpressionFromText("(" + value.getText() + ")");
            final PsiElement lastChild = def.getLastChild();
            if (lastChild != null && lastChild.getNode().getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
                final PsiElement parent = def.getParent();
                if (parent != null)
                    parent.addBefore(lastChild, def);
            }
            for (int i = 0, refsToInlineLength = refsToInline.length; i < refsToInlineLength; i++) {
                final PsiElement element = refsToInline[i];
                if (PyReplaceExpressionUtil.isNeedParenthesis((PyExpression) element, value)) {
                    exprs[i] = element.replace(withParenthesis);
                } else {
                    exprs[i] = element.replace(value);
                }
            }
            final PsiElement next = def.getNextSibling();
            if (next instanceof PsiWhiteSpace) {
                PyPsiUtils.removeElements(next);
            }
            PyPsiUtils.removeElements(def);
            final List<TextRange> ranges = ContainerUtil.mapNotNull(exprs, element -> {
                final PyStatement parentalStatement = PsiTreeUtil.getParentOfType(element, PyStatement.class, false);
                return parentalStatement != null ? parentalStatement.getTextRange() : null;
            });
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            CodeStyleManager.getInstance(project).reformatText(workingFile, ranges);
            if (!ApplicationManager.getApplication().isUnitTestMode()) {
                highlightManager.addOccurrenceHighlights(editor, exprs, attributes, true, null);
                WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
            }
        } finally {
            final RefactoringEventData afterData = new RefactoringEventData();
            afterData.addElement(local);
            project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(getRefactoringId(), afterData);
        }
    }), RefactoringBundle.message("inline.command", localName), null);
}
Also used : Language(com.intellij.lang.Language) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) PythonLanguage(com.jetbrains.python.PythonLanguage) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyPsiUtils(com.jetbrains.python.psi.impl.PyPsiUtils) ArrayList(java.util.ArrayList) Query(com.intellij.util.Query) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) Project(com.intellij.openapi.project.Project) com.jetbrains.python.psi(com.jetbrains.python.psi) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Logger(com.intellij.openapi.diagnostic.Logger) Instruction(com.intellij.codeInsight.controlflow.Instruction) PyDefUseUtil(com.jetbrains.python.refactoring.PyDefUseUtil) Extensions(com.intellij.openapi.extensions.Extensions) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) PyBundle(com.jetbrains.python.PyBundle) InlineActionHandler(com.intellij.lang.refactoring.InlineActionHandler) PyReplaceExpressionUtil(com.jetbrains.python.refactoring.PyReplaceExpressionUtil) WindowManager(com.intellij.openapi.wm.WindowManager) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PyTokenTypes(com.jetbrains.python.PyTokenTypes) TextRange(com.intellij.openapi.util.TextRange) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) Editor(com.intellij.openapi.editor.Editor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) List(java.util.List) EditorColors(com.intellij.openapi.editor.colors.EditorColors) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Function(com.intellij.util.Function) Pair(com.intellij.openapi.util.Pair) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ArrayList(java.util.ArrayList) Instruction(com.intellij.codeInsight.controlflow.Instruction) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) ArrayList(java.util.ArrayList) List(java.util.List)

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