Search in sources :

Example 6 with RangeHighlighter

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

the class XPathEvalAction method execute.

private void execute(Editor editor) {
    final Project project = editor.getProject();
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (psiFile == null) {
        return;
    }
    InputExpressionDialog.Context input;
    XmlElement contextNode = null;
    final Config cfg = XPathAppComponent.getInstance().getConfig();
    do {
        RangeHighlighter contextHighlighter = null;
        if (cfg.isUseContextAtCursor()) {
            // find out current context node
            contextNode = MyPsiUtil.findContextNode(psiFile, editor);
            if (contextNode != null) {
                contextHighlighter = HighlighterUtil.highlightNode(editor, contextNode, cfg.getContextAttributes(), cfg);
            }
        }
        if (contextNode == null) {
            // in XPath data model, / is the document itself, including comments, PIs and the root element
            contextNode = ((XmlFile) psiFile).getDocument();
            if (contextNode == null) {
                FileViewProvider fileViewProvider = psiFile.getViewProvider();
                if (fileViewProvider instanceof TemplateLanguageFileViewProvider) {
                    Language dataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage();
                    PsiFile templateDataFile = fileViewProvider.getPsi(dataLanguage);
                    if (templateDataFile instanceof XmlFile)
                        contextNode = ((XmlFile) templateDataFile).getDocument();
                }
            }
        }
        input = inputXPathExpression(project, contextNode);
        if (contextHighlighter != null) {
            contextHighlighter.dispose();
        }
        if (input == null) {
            return;
        }
        HighlighterUtil.clearHighlighters(editor);
    } while (contextNode != null && evaluateExpression(input, contextNode, editor, cfg));
}
Also used : Project(com.intellij.openapi.project.Project) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) XmlFile(com.intellij.psi.xml.XmlFile) XmlElement(com.intellij.psi.xml.XmlElement) PsiFile(com.intellij.psi.PsiFile) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) InputExpressionDialog(org.intellij.plugins.xpathView.ui.InputExpressionDialog)

Example 7 with RangeHighlighter

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

the class HighlighterUtil method purgeInvalidHighlighters.

@SuppressWarnings({ "unchecked", "RawUseOfParameterizedType" })
private static boolean purgeInvalidHighlighters(Editor editor, List<RangeHighlighter> hl) {
    final Set set = ContainerUtil.newIdentityTroveSet(Arrays.asList(editor.getMarkupModel().getAllHighlighters()));
    boolean hasHighlighter = false;
    for (Iterator<RangeHighlighter> iterator = hl.iterator(); iterator.hasNext(); ) {
        final RangeHighlighter h = iterator.next();
        if (!h.isValid() || !set.contains(h)) {
            iterator.remove();
        } else {
            hasHighlighter = true;
        }
    }
    return hasHighlighter;
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter)

Example 8 with RangeHighlighter

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

the class HighlighterUtil method highlightNode.

/**
     * Highlight a node in the editor.
     * @param editor the editor
     * @param node the node to be highlighted
     * @param attrs the attributes for the highlighter
     * @param cfg the plugin configuration
     * @return The created highlighter object
     */
public static RangeHighlighter highlightNode(Editor editor, final PsiElement node, TextAttributes attrs, Config cfg) {
    TextRange range;
    final PsiElement realElement;
    if ((node instanceof XmlTag) && cfg.isHighlightStartTagOnly()) {
        XmlTag tag = (XmlTag) node;
        realElement = MyPsiUtil.getNameElement(tag);
        range = realElement.getTextRange();
    } else {
        range = node.getTextRange();
        realElement = node;
    }
    // TODO: break at line boundaries
    final ArrayList<RangeHighlighter> highlighters = new ArrayList<>(1);
    final HighlightManager mgr = HighlightManager.getInstance(editor.getProject());
    mgr.addRangeHighlight(editor, range.getStartOffset(), range.getEndOffset(), attrs, false, highlighters);
    final RangeHighlighter rangeHighlighter = highlighters.get(0);
    if (cfg.isAddErrorStripe()) {
        rangeHighlighter.setErrorStripeMarkColor(attrs.getBackgroundColor());
        rangeHighlighter.setErrorStripeTooltip(formatTooltip(editor, realElement));
    } else {
        rangeHighlighter.setErrorStripeMarkColor(null);
    }
    return rangeHighlighter;
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 9 with RangeHighlighter

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

the class HighlighterUtil method clearHighlighters.

/**
     * Clear all highlighters in an editor that are set up by this class
     *
     * @param editor the editor
     */
public static void clearHighlighters(final Editor editor) {
    final List<RangeHighlighter> hl = editor.getUserData(HIGHLIGHTERS_KEY);
    if (hl != null) {
        if (purgeInvalidHighlighters(editor, hl)) {
            final HighlightManager mgr = HighlightManager.getInstance(editor.getProject());
            for (Iterator<RangeHighlighter> iterator = hl.iterator(); iterator.hasNext(); ) {
                RangeHighlighter highlighter = iterator.next();
                mgr.removeSegmentHighlighter(editor, highlighter);
                iterator.remove();
            }
        }
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager)

Example 10 with RangeHighlighter

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

the class ProcessWithConsoleRunner method getHighlightedStringsInConsole.

/**
   * Gets highlighted information from test console. Some parts of output (like file links) may be highlighted, and you need to check them.
   *
   * @return pair of [[ranges], [texts]] where range is [from,to] in doc. for each region, and "text" is text extracted from this region.
   * For example assume that in document "spam eggs ham" words "ham" and "spam" are highlighted.
   * You should have 2 ranges (0, 4) and (10, 13) and 2 strings (spam and ham)
   */
@NotNull
public Pair<List<Pair<Integer, Integer>>, List<String>> getHighlightedStringsInConsole() {
    final List<String> resultStrings = new ArrayList<>();
    final List<Pair<Integer, Integer>> resultRanges = new ArrayList<>();
    ApplicationManager.getApplication().invokeAndWait(() -> {
        myConsole.flushDeferredText();
        final Editor editor = myConsole.getEditor();
        for (final RangeHighlighter highlighter : editor.getMarkupModel().getAllHighlighters()) {
            if (highlighter instanceof RangeHighlighterEx) {
                final int start = ((RangeHighlighterEx) highlighter).getAffectedAreaStartOffset();
                final int end = ((RangeHighlighterEx) highlighter).getAffectedAreaEndOffset();
                resultRanges.add(Pair.create(start, end));
                resultStrings.add(editor.getDocument().getText().substring(start, end));
            }
        }
    }, ModalityState.NON_MODAL);
    return Pair.create(resultRanges, resultStrings);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) ArrayList(java.util.ArrayList) Editor(com.intellij.openapi.editor.Editor) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

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