Search in sources :

Example 96 with TextAttributes

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

the class UsageInfo2UsageAdapter method initChunks.

@NotNull
private TextChunk[] initChunks() {
    PsiFile psiFile = getPsiFile();
    Document document = psiFile == null ? null : PsiDocumentManager.getInstance(getProject()).getDocument(psiFile);
    TextChunk[] chunks;
    if (document == null) {
        // element over light virtual file
        PsiElement element = getElement();
        if (element == null) {
            chunks = new TextChunk[] { new TextChunk(SimpleTextAttributes.ERROR_ATTRIBUTES.toTextAttributes(), UsageViewBundle.message("node.invalid")) };
        } else {
            chunks = new TextChunk[] { new TextChunk(new TextAttributes(), element.getText()) };
        }
    } else {
        chunks = ChunkExtractor.extractChunks(psiFile, this);
    }
    myTextChunks = new SoftReference<>(chunks);
    return chunks;
}
Also used : SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 97 with TextAttributes

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

the class ImmediatePainter method paintImmediately.

private void paintImmediately(final Graphics g, final int offset, final char c2) {
    final EditorImpl editor = myEditor;
    final Document document = editor.getDocument();
    final LexerEditorHighlighter highlighter = (LexerEditorHighlighter) myEditor.getHighlighter();
    final EditorSettings settings = editor.getSettings();
    final boolean isBlockCursor = editor.isInsertMode() == settings.isBlockCursor();
    final int lineHeight = editor.getLineHeight();
    final int ascent = editor.getAscent();
    final int topOverhang = editor.myView.getTopOverhang();
    final int bottomOverhang = editor.myView.getBottomOverhang();
    final char c1 = offset == 0 ? ' ' : document.getCharsSequence().charAt(offset - 1);
    final List<TextAttributes> attributes = highlighter.getAttributesForPreviousAndTypedChars(document, offset, c2);
    updateAttributes(editor, offset, attributes);
    final TextAttributes attributes1 = attributes.get(0);
    final TextAttributes attributes2 = attributes.get(1);
    if (!(canRender(attributes1) && canRender(attributes2))) {
        return;
    }
    FontLayoutService fontLayoutService = FontLayoutService.getInstance();
    final float width1 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes1.getFontType()), c1);
    final float width2 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes2.getFontType()), c2);
    final Font font1 = EditorUtil.fontForChar(c1, attributes1.getFontType(), editor).getFont();
    final Font font2 = EditorUtil.fontForChar(c1, attributes2.getFontType(), editor).getFont();
    final Point2D p2 = editor.offsetToXY(offset, false);
    float p2x = (float) p2.getX();
    int p2y = (int) p2.getY();
    int width1i = (int) (p2x) - (int) (p2x - width1);
    int width2i = (int) (p2x + width2) - (int) p2x;
    Caret caret = editor.getCaretModel().getPrimaryCaret();
    //noinspection ConstantConditions
    final int caretWidth = isBlockCursor ? editor.getCaretLocations(false)[0].myWidth : JBUI.scale(caret.getVisualAttributes().getWidth(settings.getLineCursorWidth()));
    final float caretShift = isBlockCursor ? 0 : caretWidth == 1 ? 0 : 1 / JBUI.sysScale((Graphics2D) g);
    final Rectangle2D caretRectangle = new Rectangle2D.Float((int) (p2x + width2) - caretShift, p2y - topOverhang, caretWidth, lineHeight + topOverhang + bottomOverhang + (isBlockCursor ? -1 : 0));
    final Rectangle rectangle1 = new Rectangle((int) (p2x - width1), p2y, width1i, lineHeight);
    final Rectangle rectangle2 = new Rectangle((int) p2x, p2y, (int) (width2i + caretWidth - caretShift), lineHeight);
    final Consumer<Graphics> painter = graphics -> {
        EditorUIUtil.setupAntialiasing(graphics);
        fillRect(graphics, rectangle2, attributes2.getBackgroundColor());
        drawChar(graphics, c2, p2x, p2y + ascent, font2, attributes2.getForegroundColor());
        fillRect(graphics, caretRectangle, getCaretColor(editor));
        fillRect(graphics, rectangle1, attributes1.getBackgroundColor());
        drawChar(graphics, c1, p2x - width1, p2y + ascent, font1, attributes1.getForegroundColor());
    };
    final Shape originalClip = g.getClip();
    g.setClip(new Rectangle2D.Float((int) p2x - caretShift, p2y, width2i + caretWidth, lineHeight));
    if (DOUBLE_BUFFERING.asBoolean()) {
        paintWithDoubleBuffering(g, painter);
    } else {
        painter.consume(g);
    }
    g.setClip(originalClip);
    if (PIPELINE_FLUSH.asBoolean()) {
        Toolkit.getDefaultToolkit().sync();
    }
    if (DEBUG.asBoolean()) {
        pause();
    }
}
Also used : RegistryValue(com.intellij.openapi.util.registry.RegistryValue) UIUtil(com.intellij.util.ui.UIUtil) Point2D(java.awt.geom.Point2D) com.intellij.openapi.editor(com.intellij.openapi.editor) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Rectangle2D(java.awt.geom.Rectangle2D) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) JBUI(com.intellij.util.ui.JBUI) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) EffectType(com.intellij.openapi.editor.markup.EffectType) Disposer(com.intellij.openapi.util.Disposer) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) IterationState(com.intellij.openapi.editor.impl.view.IterationState) FontLayoutService(com.intellij.openapi.editor.impl.view.FontLayoutService) EditorTextField(com.intellij.ui.EditorTextField) BufferedImage(java.awt.image.BufferedImage) HighlighterTargetArea(com.intellij.openapi.editor.markup.HighlighterTargetArea) java.awt(java.awt) VolatileImage(java.awt.image.VolatileImage) EditorUIUtil(com.intellij.openapi.editor.ex.util.EditorUIUtil) List(java.util.List) EditorColors(com.intellij.openapi.editor.colors.EditorColors) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) Gray(com.intellij.ui.Gray) JBColor(com.intellij.ui.JBColor) Consumer(com.intellij.util.Consumer) javax.swing(javax.swing) Rectangle2D(java.awt.geom.Rectangle2D) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) Point2D(java.awt.geom.Point2D) FontLayoutService(com.intellij.openapi.editor.impl.view.FontLayoutService) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 98 with TextAttributes

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

the class SelectionModelImpl method getTextAttributes.

@Override
public TextAttributes getTextAttributes() {
    if (myTextAttributes == null) {
        TextAttributes textAttributes = new TextAttributes();
        EditorColorsScheme scheme = myEditor.getColorsScheme();
        textAttributes.setForegroundColor(scheme.getColor(EditorColors.SELECTION_FOREGROUND_COLOR));
        textAttributes.setBackgroundColor(scheme.getColor(EditorColors.SELECTION_BACKGROUND_COLOR));
        myTextAttributes = textAttributes;
    }
    return myTextAttributes;
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme)

Example 99 with TextAttributes

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

the class ExpectedHighlightingData method extractExpectedHighlight.

private int extractExpectedHighlight(final Matcher matcher, final String text, final Document document, final Ref<Integer> textOffset) {
    document.deleteString(textOffset.get(), textOffset.get() + matcher.end() - matcher.start());
    int groupIdx = 1;
    final String marker = matcher.group(groupIdx++);
    String descr = matcher.group(groupIdx++);
    final String typeString = matcher.group(groupIdx++);
    final String foregroundColor = matcher.group(groupIdx++);
    final String backgroundColor = matcher.group(groupIdx++);
    final String effectColor = matcher.group(groupIdx++);
    final String effectType = matcher.group(groupIdx++);
    final String fontType = matcher.group(groupIdx++);
    final String attrKey = matcher.group(groupIdx++);
    final String bundleMessage = matcher.group(groupIdx++);
    final boolean closed = matcher.group(groupIdx) != null;
    if (descr == null) {
        // no descr means any string by default
        descr = ANY_TEXT;
    } else if (descr.equals("null")) {
        // explicit "null" descr
        descr = null;
    }
    if (descr != null) {
        // replace: \\" to ", doesn't check symbol before sequence \\"
        descr = descr.replaceAll("\\\\\\\\\"", "\"");
        descr = descr.replaceAll("\\\\\"", "\"");
    }
    HighlightInfoType type = WHATEVER;
    if (typeString != null) {
        try {
            type = getTypeByName(typeString);
        } catch (Exception e) {
            LOG.error(e);
        }
        LOG.assertTrue(type != null, "Wrong highlight type: " + typeString);
    }
    TextAttributes forcedAttributes = null;
    if (foregroundColor != null) {
        //noinspection MagicConstant
        forcedAttributes = new TextAttributes(Color.decode(foregroundColor), Color.decode(backgroundColor), Color.decode(effectColor), EffectType.valueOf(effectType), Integer.parseInt(fontType));
    }
    final int rangeStart = textOffset.get();
    final int toContinueFrom;
    if (closed) {
        toContinueFrom = matcher.end();
    } else {
        int pos = matcher.end();
        final Matcher closingTagMatcher = Pattern.compile("</" + marker + ">").matcher(text);
        while (true) {
            if (!closingTagMatcher.find(pos)) {
                LOG.error("Cannot find closing </" + marker + "> in position " + pos);
            }
            final int nextTagStart = matcher.find(pos) ? matcher.start() : text.length();
            if (closingTagMatcher.start() < nextTagStart) {
                textOffset.set(textOffset.get() + closingTagMatcher.start() - pos);
                document.deleteString(textOffset.get(), textOffset.get() + closingTagMatcher.end() - closingTagMatcher.start());
                toContinueFrom = closingTagMatcher.end();
                break;
            }
            textOffset.set(textOffset.get() + nextTagStart - pos);
            pos = extractExpectedHighlight(matcher, text, document, textOffset);
        }
    }
    final ExpectedHighlightingSet expectedHighlightingSet = myHighlightingTypes.get(marker);
    if (expectedHighlightingSet.enabled) {
        TextAttributesKey forcedTextAttributesKey = attrKey == null ? null : TextAttributesKey.createTextAttributesKey(attrKey);
        HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(rangeStart, textOffset.get()).severity(expectedHighlightingSet.severity);
        if (forcedAttributes != null)
            builder.textAttributes(forcedAttributes);
        if (forcedTextAttributesKey != null)
            builder.textAttributes(forcedTextAttributesKey);
        if (bundleMessage != null) {
            final List<String> split = StringUtil.split(bundleMessage, "|");
            final ResourceBundle bundle = ResourceBundle.getBundle(split.get(0));
            descr = CommonBundle.message(bundle, split.get(1), split.stream().skip(2).toArray());
        }
        if (descr != null) {
            builder.description(descr);
            builder.unescapedToolTip(descr);
        }
        if (expectedHighlightingSet.endOfLine)
            builder.endOfLine();
        HighlightInfo highlightInfo = builder.createUnconditionally();
        expectedHighlightingSet.infos.add(highlightInfo);
    }
    return toContinueFrom;
}
Also used : Matcher(java.util.regex.Matcher) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 100 with TextAttributes

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

the class BreakpointItem method showInEditor.

protected static void showInEditor(DetailView panel, VirtualFile virtualFile, int line) {
    TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(DebuggerColors.BREAKPOINT_ATTRIBUTES);
    DetailView.PreviewEditorState state = DetailView.PreviewEditorState.create(virtualFile, line, attributes);
    if (state.equals(panel.getEditorState())) {
        return;
    }
    panel.navigateInPreviewEditor(state);
    TextAttributes softerAttributes = attributes.clone();
    Color backgroundColor = softerAttributes.getBackgroundColor();
    if (backgroundColor != null) {
        softerAttributes.setBackgroundColor(ColorUtil.softer(backgroundColor));
    }
    final Editor editor = panel.getEditor();
    if (editor != null) {
        final MarkupModel editorModel = editor.getMarkupModel();
        final MarkupModel documentModel = DocumentMarkupModel.forDocument(editor.getDocument(), editor.getProject(), false);
        for (RangeHighlighter highlighter : documentModel.getAllHighlighters()) {
            if (highlighter.getUserData(DebuggerColors.BREAKPOINT_HIGHLIGHTER_KEY) == Boolean.TRUE) {
                final int line1 = editor.offsetToLogicalPosition(highlighter.getStartOffset()).line;
                if (line1 != line) {
                    editorModel.addLineHighlighter(line1, DebuggerColors.BREAKPOINT_HIGHLIGHTER_LAYER + 1, softerAttributes);
                }
            }
        }
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) DetailView(com.intellij.ui.popup.util.DetailView) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Editor(com.intellij.openapi.editor.Editor) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel)

Aggregations

TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)205 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)40 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)31 NotNull (org.jetbrains.annotations.NotNull)29 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)28 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)26 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)26 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)23 TextRange (com.intellij.openapi.util.TextRange)21 Nullable (org.jetbrains.annotations.Nullable)21 ArrayList (java.util.ArrayList)19 Editor (com.intellij.openapi.editor.Editor)18 Project (com.intellij.openapi.project.Project)17 PsiElement (com.intellij.psi.PsiElement)12 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 JBColor (com.intellij.ui.JBColor)10 Document (com.intellij.openapi.editor.Document)9 ContainerUtil (com.intellij.util.containers.ContainerUtil)9 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)8