Search in sources :

Example 6 with HighlightManager

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

the class VariableInlineHandler method invoke.

public static void invoke(@NotNull final XPathVariable variable, Editor editor) {
    final String type = LanguageFindUsages.INSTANCE.forLanguage(variable.getLanguage()).getType(variable);
    final Project project = variable.getProject();
    final XmlTag tag = ((XsltElement) variable).getTag();
    final String expression = tag.getAttributeValue("select");
    if (expression == null) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' has no value.", StringUtil.capitalize(type), variable.getName()), TITLE, null);
        return;
    }
    final Collection<PsiReference> references = ReferencesSearch.search(variable, new LocalSearchScope(tag.getParentTag()), false).findAll();
    if (references.size() == 0) {
        CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' is never used.", variable.getName()), TITLE, null);
        return;
    }
    boolean hasExternalRefs = false;
    if (XsltSupport.isTopLevelElement(tag)) {
        final Query<PsiReference> query = ReferencesSearch.search(variable, GlobalSearchScope.allScope(project), false);
        hasExternalRefs = !query.forEach(new Processor<PsiReference>() {

            int allRefs = 0;

            public boolean process(PsiReference psiReference) {
                if (++allRefs > references.size()) {
                    return false;
                } else if (!references.contains(psiReference)) {
                    return false;
                }
                return true;
            }
        });
    }
    final HighlightManager highlighter = HighlightManager.getInstance(project);
    final ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
    final PsiReference[] psiReferences = references.toArray(new PsiReference[references.size()]);
    TextRange[] ranges = ContainerUtil.map2Array(psiReferences, TextRange.class, s -> {
        final PsiElement psiElement = s.getElement();
        final XmlAttributeValue context = PsiTreeUtil.getContextOfType(psiElement, XmlAttributeValue.class, true);
        if (psiElement instanceof XPathElement && context != null) {
            return XsltCodeInsightUtil.getRangeInsideHostingFile((XPathElement) psiElement).cutOut(s.getRangeInElement());
        }
        return psiElement.getTextRange().cutOut(s.getRangeInElement());
    });
    final Editor e = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
    for (TextRange range : ranges) {
        final TextAttributes textAttributes = EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes();
        final Color color = getScrollmarkColor(textAttributes);
        highlighter.addOccurrenceHighlight(e, range.getStartOffset(), range.getEndOffset(), textAttributes, HighlightManagerImpl.HIDE_BY_ESCAPE, highlighters, color);
    }
    highlighter.addOccurrenceHighlights(e, new PsiElement[] { ((XsltVariable) variable).getNameIdentifier() }, EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
    if (!hasExternalRefs) {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} occurrence{3})", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getQuestionIcon()) != Messages.YES) {
            return;
        }
    } else {
        if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} local occurrence{3})\n" + "\nWarning: It is being used in external files. Its declaration will not be removed.", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getWarningIcon()) != Messages.YES) {
            return;
        }
    }
    final boolean hasRefs = hasExternalRefs;
    new WriteCommandAction.Simple(project, "XSLT.Inline", tag.getContainingFile()) {

        @Override
        protected void run() throws Throwable {
            try {
                for (PsiReference psiReference : references) {
                    final PsiElement element = psiReference.getElement();
                    if (element instanceof XPathElement) {
                        final XPathElement newExpr = XPathChangeUtil.createExpression(element, expression);
                        element.replace(newExpr);
                    } else {
                        assert false;
                    }
                }
                if (!hasRefs) {
                    tag.delete();
                }
            } catch (IncorrectOperationException e) {
                Logger.getInstance(VariableInlineHandler.class.getName()).error(e);
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XsltElement(org.intellij.lang.xpath.xslt.psi.XsltElement) ArrayList(java.util.ArrayList) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiElement(com.intellij.psi.PsiElement) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XPathElement(org.intellij.lang.xpath.psi.XPathElement) EditorWindow(com.intellij.injected.editor.EditorWindow) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 7 with HighlightManager

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

the class GroovyRefactoringUtil method highlightOccurrences.

public static void highlightOccurrences(Project project, @Nullable Editor editor, PsiElement[] elements) {
    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);
    if (elements.length > 0) {
        highlightManager.addOccurrenceHighlights(editor, elements, 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) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager)

Example 8 with HighlightManager

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

the class LivePreview method doHightlightRange.

private RangeHighlighter doHightlightRange(final TextRange textRange, final TextAttributes attributes, Set<RangeHighlighter> highlighters) {
    HighlightManager highlightManager = HighlightManager.getInstance(mySearchResults.getProject());
    MarkupModelEx markupModel = (MarkupModelEx) mySearchResults.getEditor().getMarkupModel();
    final RangeHighlighter[] candidate = new RangeHighlighter[1];
    boolean notFound = markupModel.processRangeHighlightersOverlappingWith(textRange.getStartOffset(), textRange.getEndOffset(), highlighter -> {
        TextAttributes textAttributes = highlighter.getTextAttributes();
        if (highlighter.getUserData(SEARCH_MARKER) != null && textAttributes != null && textAttributes.equals(attributes) && highlighter.getStartOffset() == textRange.getStartOffset() && highlighter.getEndOffset() == textRange.getEndOffset()) {
            candidate[0] = highlighter;
            return false;
        }
        return true;
    });
    if (!notFound && highlighters.contains(candidate[0])) {
        return candidate[0];
    }
    final ArrayList<RangeHighlighter> dummy = new ArrayList<>();
    highlightManager.addRangeHighlight(mySearchResults.getEditor(), textRange.getStartOffset(), textRange.getEndOffset(), attributes, false, dummy);
    final RangeHighlighter h = dummy.get(0);
    highlighters.add(h);
    h.putUserData(SEARCH_MARKER, YES);
    return h;
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) ArrayList(java.util.ArrayList) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 9 with HighlightManager

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

the class CallerChooserBase method updateEditorTexts.

private void updateEditorTexts(final MethodNodeBase<M> node) {
    final MethodNodeBase<M> parentNode = getCalleeNode(node);
    final MethodNodeBase<M> callerNode = getCallerNode(node);
    final String callerText = node != myRoot ? getText(callerNode.getMethod()) : getEmptyCallerText();
    final Document callerDocument = myCallerEditor.getDocument();
    final String calleeText = node != myRoot ? getText(parentNode.getMethod()) : getEmptyCalleeText();
    final Document calleeDocument = myCalleeEditor.getDocument();
    ApplicationManager.getApplication().runWriteAction(() -> {
        callerDocument.setText(callerText);
        calleeDocument.setText(calleeText);
    });
    final M caller = callerNode.getMethod();
    final PsiElement callee = parentNode != null ? parentNode.getElementToSearch() : null;
    if (caller != null && caller.isPhysical() && callee != null) {
        HighlightManager highlighter = HighlightManager.getInstance(myProject);
        EditorColorsManager colorManager = EditorColorsManager.getInstance();
        TextAttributes attributes = colorManager.getGlobalScheme().getAttributes(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES);
        int start = getStartOffset(caller);
        for (PsiElement element : findElementsToHighlight(caller, callee)) {
            highlighter.addRangeHighlight(myCallerEditor, element.getTextRange().getStartOffset() - start, element.getTextRange().getEndOffset() - start, attributes, false, null);
        }
    }
}
Also used : HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 10 with HighlightManager

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

the class ExtractIncludeFileBase method highlightInEditor.

private static void highlightInEditor(final Project project, final IncludeDuplicate pair, final Editor editor) {
    final HighlightManager highlightManager = HighlightManager.getInstance(project);
    EditorColorsManager colorsManager = EditorColorsManager.getInstance();
    TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    final int startOffset = pair.getStart().getTextRange().getStartOffset();
    final int endOffset = pair.getEnd().getTextRange().getEndOffset();
    highlightManager.addRangeHighlight(editor, startOffset, endOffset, attributes, true, null);
    final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(startOffset);
    editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.MAKE_VISIBLE);
}
Also used : 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)

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