Search in sources :

Example 71 with TextAttributes

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

the class InplaceRefactoring method highlightTemplateVariables.

private void highlightTemplateVariables(Template template, Editor topLevelEditor) {
    //add highlights
    if (myHighlighters != null) {
        // can be null if finish is called during testing
        Map<TextRange, TextAttributes> rangesToHighlight = new HashMap<>();
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
        if (templateState != null) {
            EditorColorsManager colorsManager = EditorColorsManager.getInstance();
            for (int i = 0; i < templateState.getSegmentsCount(); i++) {
                final TextRange segmentOffset = templateState.getSegmentRange(i);
                final String name = template.getSegmentName(i);
                TextAttributes attributes = null;
                if (name.equals(PRIMARY_VARIABLE_NAME)) {
                    attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES);
                } else if (name.equals(OTHER_VARIABLE_NAME)) {
                    attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
                }
                if (attributes == null)
                    continue;
                rangesToHighlight.put(segmentOffset, attributes);
            }
        }
        addHighlights(rangesToHighlight, topLevelEditor, myHighlighters, HighlightManager.getInstance(myProject));
    }
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 72 with TextAttributes

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

the class NodeRenderer method customizeCellRenderer.

@Override
public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    Color color = null;
    NodeDescriptor descriptor = null;
    if (value instanceof DefaultMutableTreeNode) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        Object userObject = node.getUserObject();
        if (userObject instanceof NodeDescriptor) {
            descriptor = (NodeDescriptor) userObject;
            color = descriptor.getColor();
            setIcon(descriptor.getIcon());
        }
    }
    if (descriptor instanceof PresentableNodeDescriptor) {
        final PresentableNodeDescriptor node = (PresentableNodeDescriptor) descriptor;
        final PresentationData presentation = node.getPresentation();
        final List<PresentableNodeDescriptor.ColoredFragment> coloredText = presentation.getColoredText();
        if (coloredText.isEmpty()) {
            String text = tree.convertValueToText(value.toString(), selected, expanded, leaf, row, hasFocus);
            SimpleTextAttributes simpleTextAttributes = getSimpleTextAttributes(node, presentation.getForcedTextForeground() != null ? presentation.getForcedTextForeground() : color);
            append(text, simpleTextAttributes);
            String location = presentation.getLocationString();
            if (!StringUtil.isEmpty(location)) {
                SimpleTextAttributes attributes = SimpleTextAttributes.merge(simpleTextAttributes, SimpleTextAttributes.GRAYED_ATTRIBUTES);
                append(presentation.getLocationPrefix() + location + presentation.getLocationSuffix(), attributes, false);
            }
        } else {
            boolean first = true;
            for (PresentableNodeDescriptor.ColoredFragment each : coloredText) {
                SimpleTextAttributes simpleTextAttributes = each.getAttributes();
                if (each.getAttributes().getFgColor() == null && presentation.getForcedTextForeground() != null) {
                    simpleTextAttributes = addColorToSimpleTextAttributes(each.getAttributes(), presentation.getForcedTextForeground() != null ? presentation.getForcedTextForeground() : color);
                }
                if (first) {
                    final TextAttributesKey textAttributesKey = presentation.getTextAttributesKey();
                    if (textAttributesKey != null) {
                        final TextAttributes forcedAttributes = getColorsScheme().getAttributes(textAttributesKey);
                        if (forcedAttributes != null) {
                            simpleTextAttributes = SimpleTextAttributes.merge(simpleTextAttributes, SimpleTextAttributes.fromTextAttributes(forcedAttributes));
                        }
                    }
                    first = false;
                }
                // treat grayed text as non-main
                boolean isMain = simpleTextAttributes != SimpleTextAttributes.GRAYED_ATTRIBUTES;
                append(each.getText(), simpleTextAttributes, isMain);
            }
            String location = presentation.getLocationString();
            if (!StringUtil.isEmpty(location)) {
                append(presentation.getLocationPrefix() + location + presentation.getLocationSuffix(), SimpleTextAttributes.GRAY_ATTRIBUTES, false);
            }
        }
        setToolTipText(presentation.getTooltip());
    } else if (value != null) {
        String text = value.toString();
        if (descriptor != null) {
            text = descriptor.myName;
        }
        text = tree.convertValueToText(text, selected, expanded, leaf, row, hasFocus);
        if (text == null) {
            text = "";
        }
        append(text);
        setToolTipText(null);
    }
    if (!AbstractTreeUi.isLoadingNode(value)) {
        SpeedSearchUtil.applySpeedSearchHighlighting(tree, this, true, selected);
    }
}
Also used : PresentationData(com.intellij.ide.projectView.PresentationData) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes)

Example 73 with TextAttributes

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

the class NodeRenderer method addColorToSimpleTextAttributes.

private static SimpleTextAttributes addColorToSimpleTextAttributes(SimpleTextAttributes simpleTextAttributes, Color color) {
    if (color != null) {
        final TextAttributes textAttributes = simpleTextAttributes.toTextAttributes();
        textAttributes.setForegroundColor(color);
        simpleTextAttributes = SimpleTextAttributes.fromTextAttributes(textAttributes);
    }
    return simpleTextAttributes;
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes)

Example 74 with TextAttributes

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

the class HTMLTextPainter method writeStyles.

private void writeStyles(@NonNls final Writer writer) throws IOException {
    EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
    writer.write("<style type=\"text/css\">\n");
    final Color lineNumbers = scheme.getColor(EditorColors.LINE_NUMBERS_COLOR);
    writer.write(String.format(".ln { color: #%s; font-weight: normal; font-style: normal; }\n", ColorUtil.toHex(lineNumbers == null ? Gray.x00 : lineNumbers)));
    HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
    while (!hIterator.atEnd()) {
        TextAttributes textAttributes = hIterator.getTextAttributes();
        if (!myStyleMap.containsKey(textAttributes)) {
            @NonNls String styleName = "s" + myStyleMap.size();
            myStyleMap.put(textAttributes, styleName);
            writer.write("." + styleName + " { ");
            Color foreColor = textAttributes.getForegroundColor();
            if (foreColor == null)
                foreColor = scheme.getDefaultForeground();
            writer.write("color: " + colorToHtml(foreColor) + "; ");
            if (BitUtil.isSet(textAttributes.getFontType(), Font.BOLD)) {
                writer.write("font-weight: bold; ");
            }
            if (BitUtil.isSet(textAttributes.getFontType(), Font.ITALIC)) {
                writer.write("font-style: italic; ");
            }
            writer.write("}\n");
        }
        hIterator.advance();
    }
    for (LineMarkerInfo separator : myMethodSeparators) {
        Color color = separator.separatorColor;
        if (color != null && !mySeparatorStyles.containsKey(color)) {
            @NonNls String styleName = "ls" + mySeparatorStyles.size();
            mySeparatorStyles.put(color, styleName);
            String htmlColor = colorToHtml(color);
            writer.write("." + styleName + " { height: 1px; border-width: 0; color: " + htmlColor + "; background-color:" + htmlColor + "}\n");
        }
    }
    writer.write("</style>\n");
}
Also used : NonNls(org.jetbrains.annotations.NonNls) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) JBColor(com.intellij.ui.JBColor) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 75 with TextAttributes

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

the class HTMLTextPainter method paint.

@SuppressWarnings({ "HardCodedStringLiteral" })
public void paint(TreeMap refMap, FileType fileType) throws FileNotFoundException {
    HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
    if (hIterator.atEnd())
        return;
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(myHTMLFileName), CharsetToolkit.UTF8_CHARSET);
    lineCount = myFirstLineNumber;
    TextAttributes prevAttributes = null;
    Iterator refKeys = null;
    int refOffset = -1;
    PsiReference ref = null;
    if (refMap != null) {
        refKeys = refMap.keySet().iterator();
        if (refKeys.hasNext()) {
            Integer key = (Integer) refKeys.next();
            ref = (PsiReference) refMap.get(key);
            refOffset = key.intValue();
        }
    }
    int referenceEnd = -1;
    try {
        writeHeader(writer, new File(myFileName).getName());
        if (myFirstLineNumber == 0) {
            writeLineNumber(writer);
        }
        String closeTag = null;
        getMethodSeparator(hIterator.getStart());
        while (!hIterator.atEnd()) {
            TextAttributes textAttributes = hIterator.getTextAttributes();
            int hStart = hIterator.getStart();
            int hEnd = hIterator.getEnd();
            if (hEnd > mySegmentEnd)
                break;
            boolean haveNonWhiteSpace = false;
            for (int offset = hStart; offset < hEnd; offset++) {
                char c = myText.charAt(offset);
                if (c != ' ' && c != '\t') {
                    haveNonWhiteSpace = true;
                    break;
                }
            }
            if (!haveNonWhiteSpace) {
                // don't write separate spans for whitespace-only text fragments
                writeString(writer, myText, hStart, hEnd - hStart, fileType);
                hIterator.advance();
                continue;
            }
            if (refOffset > 0 && hStart <= refOffset && hEnd > refOffset) {
                referenceEnd = writeReferenceTag(writer, ref);
            }
            //        if(myForceFonts || !equals(prevAttributes, textAttributes)) {
            if (!equals(prevAttributes, textAttributes) && referenceEnd < 0) {
                if (closeTag != null) {
                    writer.write(closeTag);
                }
                closeTag = writeFontTag(writer, textAttributes);
                prevAttributes = textAttributes;
            }
            writeString(writer, myText, hStart, hEnd - hStart, fileType);
            //        }
            if (referenceEnd > 0 && hEnd >= referenceEnd) {
                writer.write("</a>");
                referenceEnd = -1;
                if (refKeys.hasNext()) {
                    Integer key = (Integer) refKeys.next();
                    ref = (PsiReference) refMap.get(key);
                    refOffset = key.intValue();
                }
            }
            hIterator.advance();
        }
        if (closeTag != null) {
            writer.write(closeTag);
        }
        writeFooter(writer);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
Also used : PsiReference(com.intellij.psi.PsiReference) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

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