Search in sources :

Example 11 with MarkupModel

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

the class JumpToColorsAndFontsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    // todo handle ColorKey's as well
    Project project = e.getData(CommonDataKeys.PROJECT);
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (project == null || editor == null)
        return;
    Map<TextAttributesKey, Pair<ColorSettingsPage, AttributesDescriptor>> keyMap = ContainerUtil.newHashMap();
    Processor<RangeHighlighterEx> processor = r -> {
        Object tt = r.getErrorStripeTooltip();
        TextAttributesKey key = tt instanceof HighlightInfo ? ObjectUtils.chooseNotNull(((HighlightInfo) tt).forcedTextAttributesKey, ((HighlightInfo) tt).type.getAttributesKey()) : null;
        Pair<ColorSettingsPage, AttributesDescriptor> p = key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key);
        if (p != null)
            keyMap.put(key, p);
        return true;
    };
    JBIterable<Editor> editors = editor instanceof EditorWindow ? JBIterable.of(editor, ((EditorWindow) editor).getDelegate()) : JBIterable.of(editor);
    for (Editor ed : editors) {
        TextRange selection = EditorUtil.getSelectionInAnyMode(ed);
        MarkupModel forDocument = DocumentMarkupModel.forDocument(ed.getDocument(), project, false);
        if (forDocument != null) {
            ((MarkupModelEx) forDocument).processRangeHighlightersOverlappingWith(selection.getStartOffset(), selection.getEndOffset(), processor);
        }
        ((MarkupModelEx) ed.getMarkupModel()).processRangeHighlightersOverlappingWith(selection.getStartOffset(), selection.getEndOffset(), processor);
        EditorHighlighter highlighter = ed instanceof EditorEx ? ((EditorEx) ed).getHighlighter() : null;
        SyntaxHighlighter syntaxHighlighter = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
        if (syntaxHighlighter != null) {
            HighlighterIterator iterator = highlighter.createIterator(selection.getStartOffset());
            while (!iterator.atEnd()) {
                for (TextAttributesKey key : syntaxHighlighter.getTokenHighlights(iterator.getTokenType())) {
                    Pair<ColorSettingsPage, AttributesDescriptor> p = key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key);
                    if (p != null)
                        keyMap.put(key, p);
                }
                if (iterator.getEnd() >= selection.getEndOffset())
                    break;
                iterator.advance();
            }
        }
    }
    if (keyMap.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, "No text attributes found");
    } else if (keyMap.size() == 1) {
        Pair<ColorSettingsPage, AttributesDescriptor> p = keyMap.values().iterator().next();
        if (!openSettingsAndSelectKey(project, p.first, p.second)) {
            HintManager.getInstance().showErrorHint(editor, "No appropriate settings page found");
        }
    } else {
        ArrayList<Pair<ColorSettingsPage, AttributesDescriptor>> attrs = ContainerUtil.newArrayList(keyMap.values());
        Collections.sort(attrs, (o1, o2) -> StringUtil.naturalCompare(o1.first.getDisplayName() + o1.second.getDisplayName(), o2.first.getDisplayName() + o2.second.getDisplayName()));
        EditorColorsScheme colorsScheme = editor.getColorsScheme();
        JBList<Pair<ColorSettingsPage, AttributesDescriptor>> list = new JBList<>(attrs);
        list.setCellRenderer(new ColoredListCellRenderer<Pair<ColorSettingsPage, AttributesDescriptor>>() {

            @Override
            protected void customizeCellRenderer(@NotNull JList<? extends Pair<ColorSettingsPage, AttributesDescriptor>> list, Pair<ColorSettingsPage, AttributesDescriptor> value, int index, boolean selected, boolean hasFocus) {
                TextAttributes ta = colorsScheme.getAttributes(value.second.getKey());
                Color fg = ObjectUtils.chooseNotNull(ta.getForegroundColor(), colorsScheme.getDefaultForeground());
                Color bg = ObjectUtils.chooseNotNull(ta.getBackgroundColor(), colorsScheme.getDefaultBackground());
                SimpleTextAttributes sa = fromTextAttributes(ta);
                SimpleTextAttributes saOpaque = sa.derive(STYLE_OPAQUE | sa.getStyle(), fg, bg, null);
                SimpleTextAttributes saSelected = REGULAR_ATTRIBUTES.derive(sa.getStyle(), null, null, null);
                SimpleTextAttributes saCur = REGULAR_ATTRIBUTES;
                List<String> split = StringUtil.split(value.first.getDisplayName() + "//" + value.second.getDisplayName(), "//");
                for (int i = 0, len = split.size(); i < len; i++) {
                    boolean last = i == len - 1;
                    saCur = !last ? REGULAR_ATTRIBUTES : selected ? saSelected : saOpaque;
                    if (last)
                        append(" ", saCur);
                    append(split.get(i), saCur);
                    if (last)
                        append(" ", saCur);
                    else
                        append(" > ", GRAYED_ATTRIBUTES);
                }
                Color stripeColor = ta.getErrorStripeColor();
                boolean addStripe = stripeColor != null && stripeColor != saCur.getBgColor();
                boolean addBoxed = ta.getEffectType() == EffectType.BOXED && ta.getEffectColor() != null;
                if (addBoxed) {
                    append("▢" + (addStripe ? "" : " "), saCur.derive(-1, ta.getEffectColor(), null, null));
                }
                if (addStripe) {
                    append(" ", saCur.derive(STYLE_OPAQUE, null, stripeColor, null));
                }
            }
        });
        JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(StringUtil.notNullize(e.getPresentation().getText())).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
            Pair<ColorSettingsPage, AttributesDescriptor> p = list.getSelectedValue();
            if (p != null && !openSettingsAndSelectKey(project, p.first, p.second)) {
                HintManager.getInstance().showErrorHint(editor, "No appropriate settings page found");
            }
        }).createPopup().showInBestPositionFor(editor);
    }
}
Also used : Settings(com.intellij.openapi.options.ex.Settings) JBIterable(com.intellij.util.containers.JBIterable) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ContainerUtil(com.intellij.util.containers.ContainerUtil) ColorSettingsPage(com.intellij.openapi.options.colors.ColorSettingsPage) ArrayList(java.util.ArrayList) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) ShowSettingsUtilImpl(com.intellij.ide.actions.ShowSettingsUtilImpl) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) Map(java.util.Map) EffectType(com.intellij.openapi.editor.markup.EffectType) Project(com.intellij.openapi.project.Project) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) JBList(com.intellij.ui.components.JBList) ColorSettingsPages(com.intellij.openapi.options.colors.ColorSettingsPages) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) StringUtil(com.intellij.openapi.util.text.StringUtil) ActionCallback(com.intellij.openapi.util.ActionCallback) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) SettingsDialog(com.intellij.openapi.options.newEditor.SettingsDialog) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) EditorWindow(com.intellij.injected.editor.EditorWindow) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) java.awt(java.awt) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) Processor(com.intellij.util.Processor) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Pair(com.intellij.openapi.util.Pair) ObjectUtils(com.intellij.util.ObjectUtils) HintManager(com.intellij.codeInsight.hint.HintManager) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) javax.swing(javax.swing) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) ArrayList(java.util.ArrayList) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) NotNull(org.jetbrains.annotations.NotNull) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) ColorSettingsPage(com.intellij.openapi.options.colors.ColorSettingsPage) Pair(com.intellij.openapi.util.Pair) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) TextRange(com.intellij.openapi.util.TextRange) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) EditorWindow(com.intellij.injected.editor.EditorWindow) Project(com.intellij.openapi.project.Project) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) JBList(com.intellij.ui.components.JBList) Editor(com.intellij.openapi.editor.Editor) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 12 with MarkupModel

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

use of com.intellij.openapi.editor.markup.MarkupModel in project sonarlint-intellij by SonarSource.

the class RangeBlinker method removeHighlights.

private void removeHighlights() {
    MarkupModel markupModel = myEditor.getMarkupModel();
    RangeHighlighter[] allHighlighters = markupModel.getAllHighlighters();
    myAddedHighlighters.stream().filter(h -> !ArrayUtil.contains(allHighlighters, h)).forEach(RangeHighlighter::dispose);
    myAddedHighlighters.clear();
}
Also used : ArrayUtil(com.intellij.util.ArrayUtil) Editor(com.intellij.openapi.editor.Editor) HighlighterTargetArea(com.intellij.openapi.editor.markup.HighlighterTargetArea) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) ArrayList(java.util.ArrayList) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) ApplicationManager(com.intellij.openapi.application.ApplicationManager) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) Project(com.intellij.openapi.project.Project) Segment(com.intellij.openapi.util.Segment) Alarm(com.intellij.util.Alarm) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel)

Example 14 with MarkupModel

use of com.intellij.openapi.editor.markup.MarkupModel in project sonarlint-intellij by SonarSource.

the class RangeBlinker method startBlinking.

public void startBlinking() {
    Project project = myEditor.getProject();
    if (ApplicationManager.getApplication().isDisposed() || myEditor.isDisposed() || (project != null && project.isDisposed())) {
        return;
    }
    MarkupModel markupModel = myEditor.getMarkupModel();
    if (show) {
        for (Segment segment : myMarkers) {
            if (segment.getEndOffset() > myEditor.getDocument().getTextLength()) {
                continue;
            }
            RangeHighlighter highlighter = markupModel.addRangeHighlighter(segment.getStartOffset(), segment.getEndOffset(), HighlighterLayer.ADDITIONAL_SYNTAX, myAttributes, HighlighterTargetArea.EXACT_RANGE);
            myAddedHighlighters.add(highlighter);
        }
    } else {
        removeHighlights();
    }
    myBlinkingAlarm.cancelAllRequests();
    myBlinkingAlarm.addRequest(() -> {
        if (myTimeToLive > 0 || show) {
            myTimeToLive--;
            show = !show;
            startBlinking();
        }
    }, 400);
}
Also used : Project(com.intellij.openapi.project.Project) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) Segment(com.intellij.openapi.util.Segment)

Example 15 with MarkupModel

use of com.intellij.openapi.editor.markup.MarkupModel in project sonarlint-intellij by SonarSource.

the class SonarLintHighlighting method highlightFlowsWithHighlightersUtil.

/**
 * Create highlighting with {@link UpdateHighlightersUtil}. It will manage internally the {@link RangeHighlighter}, and get
 * it similarly to the way {@link com.intellij.codeHighlighting.Pass} do it.
 * Tooltip will be displayed on mouse hover by {@link com.intellij.codeInsight.daemon.impl.DaemonListeners}.
 * Creating the {@link HighlightInfo} with high severity will ensure that it will override most other highlighters.
 * The alternative would be to get and manage directly {@link RangeHighlighter} with a {@link MarkupModel} from the
 * document (or editors). This would allow to use a custom renderer, but we would have to manage tooltips by ourselves, separately.
 *
 * @see com.intellij.codeInsight.hint.HintManager
 * @see com.intellij.codeInsight.hint.ShowParameterInfoHandler
 * @see HintHint
 * @see CustomHighlighterRenderer
 * @see com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
 * @see com.intellij.codeInsight.highlighting.BraceHighlightingHandler
 */
public void highlightFlowsWithHighlightersUtil(RangeMarker rangeMarker, @Nullable String message, List<LiveIssue.Flow> flows) {
    stopBlinking();
    HighlightInfo primaryInfo = createHighlight(rangeMarker, message);
    List<HighlightInfo> infos = flows.stream().flatMap(f -> f.locations().stream().filter(Objects::nonNull).map(l -> createHighlight(l.location(), l.message()))).collect(Collectors.toList());
    infos.add(primaryInfo);
    UpdateHighlightersUtil.setHighlightersToEditor(project, rangeMarker.getDocument(), 0, rangeMarker.getDocument().getTextLength(), infos, null, HIGHLIGHT_GROUP_ID);
    currentHighlightedDoc = rangeMarker.getDocument();
    Editor[] editors = EditorFactory.getInstance().getEditors(rangeMarker.getDocument(), project);
    List<Segment> segments = Stream.concat(flows.stream().flatMap(f -> f.locations().stream().map(LiveIssue.IssueLocation::location)), Stream.of(rangeMarker)).collect(Collectors.toList());
    Arrays.stream(editors).forEach(editor -> {
        blinker = new RangeBlinker(editor, new TextAttributes(null, null, JBColor.YELLOW, EffectType.BOXED, Font.PLAIN), 3);
        blinker.resetMarkers(segments);
        blinker.startBlinking();
    });
}
Also used : Arrays(java.util.Arrays) SonarLintTextAttributes(org.sonarlint.intellij.config.SonarLintTextAttributes) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) UpdateHighlightersUtil(com.intellij.codeInsight.daemon.impl.UpdateHighlightersUtil) HintHint(com.intellij.ui.HintHint) EffectType(com.intellij.openapi.editor.markup.EffectType) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) Project(com.intellij.openapi.project.Project) Segment(com.intellij.openapi.util.Segment) Nullable(javax.annotation.Nullable) RangeMarker(com.intellij.openapi.editor.RangeMarker) Font(java.awt.Font) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) Editor(com.intellij.openapi.editor.Editor) Collectors(java.util.stream.Collectors) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) CustomHighlighterRenderer(com.intellij.openapi.editor.markup.CustomHighlighterRenderer) Objects(java.util.Objects) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Stream(java.util.stream.Stream) EditorFactory(com.intellij.openapi.editor.EditorFactory) LiveIssue(org.sonarlint.intellij.issue.LiveIssue) Collections(java.util.Collections) JBColor(com.intellij.ui.JBColor) LiveIssue(org.sonarlint.intellij.issue.LiveIssue) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) SonarLintTextAttributes(org.sonarlint.intellij.config.SonarLintTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Editor(com.intellij.openapi.editor.Editor) Segment(com.intellij.openapi.util.Segment)

Aggregations

MarkupModel (com.intellij.openapi.editor.markup.MarkupModel)23 DocumentMarkupModel (com.intellij.openapi.editor.impl.DocumentMarkupModel)12 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)10 Editor (com.intellij.openapi.editor.Editor)8 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)8 Project (com.intellij.openapi.project.Project)8 TextRange (com.intellij.openapi.util.TextRange)8 List (java.util.List)6 NotNull (org.jetbrains.annotations.NotNull)5 Document (com.intellij.openapi.editor.Document)4 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)4 ArrayList (java.util.ArrayList)4 javax.swing (javax.swing)4 Segment (com.intellij.openapi.util.Segment)3 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)2 EditorFactory (com.intellij.openapi.editor.EditorFactory)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2 MarkupModelEx (com.intellij.openapi.editor.ex.MarkupModelEx)2 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)2 EditorUtil (com.intellij.openapi.editor.ex.util.EditorUtil)2