Support custom HintRenderer instances in color settings preview

This commit is contained in:
Nikolaj Schumacher
2018-11-07 13:20:12 +01:00
parent 2801d7fcc6
commit 7c9d45df82
4 changed files with 30 additions and 27 deletions
@@ -54,9 +54,11 @@ public interface ColorSettingsPage extends ColorAndFontDescriptorsProvider {
*/
@Nullable Map<String,TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap();
@Nullable default Map<String,TextAttributesKey> getAdditionalInlineElementToDescriptorMap() { return null; }
/**
* Specifies tag-to-'color key' mapping for regions with presentation containing additional colors from color map.
* It's used to implement navigation between the list of keys and regions in sample editor.
*/
@Nullable default Map<String, ColorKey> getAdditionalHighlightingTagToColorKeyMap() { return null; }
}
}
@@ -41,13 +41,6 @@ import java.util.List;
import java.util.*;
public class SimpleEditorPreview implements PreviewPanel {
private static final Map<String, TextAttributesKey> INLINE_ELEMENTS = new HashMap<>();
static {
INLINE_ELEMENTS.put("parameter_hint", DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT);
INLINE_ELEMENTS.put("parameter_hint_highlighted", DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT_HIGHLIGHTED);
INLINE_ELEMENTS.put("parameter_hint_current", DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT_CURRENT);
}
private final ColorSettingsPage myPage;
private final EditorEx myEditor;
@@ -67,7 +60,8 @@ public class SimpleEditorPreview implements PreviewPanel {
myOptions = options;
myPage = page;
myHighlightsExtractor = new HighlightsExtractor(page.getAdditionalHighlightingTagToDescriptorMap(), INLINE_ELEMENTS,
myHighlightsExtractor = new HighlightsExtractor(page.getAdditionalHighlightingTagToDescriptorMap(),
page.getAdditionalInlineElementToDescriptorMap(),
page.getAdditionalHighlightingTagToColorKeyMap());
myEditor = (EditorEx)FontEditorPreview.createPreviewEditor(
myHighlightsExtractor.extractHighlights(page.getDemoText(), myHighlightData), // text without tags
@@ -1,8 +1,7 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.application.options.colors.highlighting;
import com.intellij.codeInsight.daemon.impl.ParameterHintsPresentationManager;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.codeInsight.daemon.impl.HintRenderer;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorCustomElementRenderer;
import com.intellij.openapi.editor.Inlay;
@@ -10,12 +9,11 @@ import com.intellij.openapi.editor.colors.ColorKey;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.util.Disposer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static com.intellij.openapi.editor.colors.CodeInsightColors.BLINKING_HIGHLIGHTS_ATTRIBUTES;
@@ -41,21 +39,15 @@ public class InlineElementData extends HighlightData {
@Override
public void addHighlToView(Editor view, EditorColorsScheme scheme, Map<TextAttributesKey, String> displayText) {
int offset = getStartOffset();
ParameterHintsPresentationManager hintsPresentationManager = ParameterHintsPresentationManager.getInstance();
Inlay hint = hintsPresentationManager.addHint(view, offset, false, myText, null, false);
hintsPresentationManager.setHighlighted(hint,
DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT_HIGHLIGHTED.equals(getHighlightKey()));
hintsPresentationManager.setCurrent(hint, myText.contains("current"));
List<Inlay> inlays = view.getInlayModel().getInlineElementsInRange(offset, offset);
for (Inlay inlay : inlays) {
EditorCustomElementRenderer renderer = inlay.getRenderer();
if (!(renderer instanceof RendererWrapper)) {
Disposer.dispose(inlay);
RendererWrapper wrapper = new RendererWrapper(renderer);
wrapper.drawBorder = myAddBorder;
view.getInlayModel().addInlineElement(offset, wrapper);
RendererWrapper renderer = new RendererWrapper(new HintRenderer(myText) {
@Nullable
@Override
protected TextAttributes getTextAttributes(@NotNull Editor editor) {
return editor.getColorsScheme().getAttributes(getHighlightKey());
}
}
});
renderer.drawBorder = myAddBorder;
view.getInlayModel().addInlineElement(offset, false, renderer);
}
@Override
@@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
/**
@@ -78,6 +79,14 @@ public class DefaultLanguageColorsPage implements RainbowColorSettingsPage, Disp
TAG_HIGHLIGHTING_MAP.put("reassigned_local", DefaultLanguageHighlighterColors.REASSIGNED_LOCAL_VARIABLE);
}
@NonNls private static final Map<String, TextAttributesKey> INLINE_ELEMENTS = new HashMap<>();
static {
INLINE_ELEMENTS.put("parameter_hint", DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT);
INLINE_ELEMENTS.put("parameter_hint_highlighted", DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT_HIGHLIGHTED);
INLINE_ELEMENTS.put("parameter_hint_current", DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT_CURRENT);
}
private final static AttributesDescriptor[] ATTRIBUTES_DESCRIPTORS = {
new AttributesDescriptor(
OptionsBundle.message("options.java.attribute.descriptor.bad.character"), HighlighterColors.BAD_CHARACTER),
@@ -244,6 +253,12 @@ public class DefaultLanguageColorsPage implements RainbowColorSettingsPage, Disp
return TAG_HIGHLIGHTING_MAP;
}
@Nullable
@Override
public Map<String, TextAttributesKey> getAdditionalInlineElementToDescriptorMap() {
return INLINE_ELEMENTS;
}
@NotNull
@Override
public AttributesDescriptor[] getAttributeDescriptors() {