Search in sources :

Example 21 with TextAttributesKey

use of com.intellij.openapi.editor.colors.TextAttributesKey in project intellij-community by JetBrains.

the class AdvHighlightingTest method testScopeBased.

public void testScopeBased() throws Exception {
    NamedScope xScope = new NamedScope("xxx", new PatternPackageSet("x..*", PatternPackageSet.SCOPE_SOURCE, null));
    NamedScope utilScope = new NamedScope("util", new PatternPackageSet("java.util.*", PatternPackageSet.SCOPE_LIBRARY, null));
    NamedScopeManager scopeManager = NamedScopeManager.getInstance(getProject());
    scopeManager.addScope(xScope);
    scopeManager.addScope(utilScope);
    EditorColorsManager manager = EditorColorsManager.getInstance();
    EditorColorsScheme scheme = (EditorColorsScheme) manager.getGlobalScheme().clone();
    manager.addColorsScheme(scheme);
    EditorColorsManager.getInstance().setGlobalScheme(scheme);
    TextAttributesKey xKey = ScopeAttributesUtil.getScopeTextAttributeKey(xScope.getName());
    TextAttributes xAttributes = new TextAttributes(Color.cyan, Color.darkGray, Color.blue, EffectType.BOXED, Font.ITALIC);
    scheme.setAttributes(xKey, xAttributes);
    TextAttributesKey utilKey = ScopeAttributesUtil.getScopeTextAttributeKey(utilScope.getName());
    TextAttributes utilAttributes = new TextAttributes(Color.gray, Color.magenta, Color.orange, EffectType.STRIKEOUT, Font.BOLD);
    scheme.setAttributes(utilKey, utilAttributes);
    try {
        testFile(BASE_PATH + "/scopeBased/x/X.java").projectRoot(BASE_PATH + "/scopeBased").checkSymbolNames().test();
    } finally {
        scopeManager.removeAllSets();
    }
}
Also used : NamedScope(com.intellij.psi.search.scope.packageSet.NamedScope) PatternPackageSet(com.intellij.psi.search.scope.packageSet.PatternPackageSet) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) NamedScopeManager(com.intellij.psi.search.scope.packageSet.NamedScopeManager)

Example 22 with TextAttributesKey

use of com.intellij.openapi.editor.colors.TextAttributesKey in project intellij-community by JetBrains.

the class ChunkExtractor method convertAttributes.

@NotNull
private TextAttributes convertAttributes(@NotNull TextAttributesKey[] keys) {
    TextAttributes attrs = myColorsScheme.getAttributes(HighlighterColors.TEXT);
    for (TextAttributesKey key : keys) {
        TextAttributes attrs2 = myColorsScheme.getAttributes(key);
        if (attrs2 != null) {
            attrs = TextAttributes.merge(attrs, attrs2);
        }
    }
    attrs = attrs.clone();
    return attrs;
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with TextAttributesKey

use of com.intellij.openapi.editor.colors.TextAttributesKey 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 24 with TextAttributesKey

use of com.intellij.openapi.editor.colors.TextAttributesKey in project intellij-plugins by JetBrains.

the class DartAnnotator method highlightEscapeSequences.

private static void highlightEscapeSequences(final PsiElement node, final AnnotationHolder holder) {
    final List<Pair<TextRange, Boolean>> escapeSequenceRangesAndValidity = getEscapeSequenceRangesAndValidity(node.getText());
    for (Pair<TextRange, Boolean> rangeAndValidity : escapeSequenceRangesAndValidity) {
        final TextRange range = rangeAndValidity.first.shiftRight(node.getTextRange().getStartOffset());
        final TextAttributesKey attribute = rangeAndValidity.second ? DartSyntaxHighlighterColors.VALID_STRING_ESCAPE : DartSyntaxHighlighterColors.INVALID_STRING_ESCAPE;
        if (rangeAndValidity.second) {
            holder.createInfoAnnotation(range, null).setTextAttributes(attribute);
        } else {
            holder.createErrorAnnotation(range, DartBundle.message("dart.color.settings.description.invalid.string.escape")).setTextAttributes(attribute);
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) Pair(com.intellij.openapi.util.Pair)

Example 25 with TextAttributesKey

use of com.intellij.openapi.editor.colors.TextAttributesKey in project intellij-plugins by JetBrains.

the class MarkdownHighlightingAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    final IElementType type = element.getNode().getElementType();
    if (type == MarkdownTokenTypes.EMPH) {
        final PsiElement parent = element.getParent();
        if (parent == null) {
            return;
        }
        final IElementType parentType = parent.getNode().getElementType();
        if (parentType == MarkdownElementTypes.EMPH || parentType == MarkdownElementTypes.STRONG) {
            final Annotation annotation = holder.createInfoAnnotation(element, null);
            annotation.setTextAttributes(parentType == MarkdownElementTypes.EMPH ? MarkdownHighlighterColors.ITALIC_MARKER_ATTR_KEY : MarkdownHighlighterColors.BOLD_MARKER_ATTR_KEY);
        }
        return;
    }
    if (element instanceof LeafPsiElement) {
        return;
    }
    final TextAttributesKey[] tokenHighlights = SYNTAX_HIGHLIGHTER.getTokenHighlights(type);
    if (tokenHighlights.length > 0 && !MarkdownHighlighterColors.TEXT_ATTR_KEY.equals(tokenHighlights[0])) {
        final Annotation annotation = holder.createInfoAnnotation(element, null);
        annotation.setTextAttributes(tokenHighlights[0]);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Annotation(com.intellij.lang.annotation.Annotation)

Aggregations

TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)49 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)28 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)12 NotNull (org.jetbrains.annotations.NotNull)9 IElementType (com.intellij.psi.tree.IElementType)8 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)7 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)6 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)6 TextRange (com.intellij.openapi.util.TextRange)5 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)4 HighlightData (com.intellij.application.options.colors.highlighting.HighlightData)3 PresentationData (com.intellij.ide.projectView.PresentationData)3 Language (com.intellij.lang.Language)3 Lexer (com.intellij.lexer.Lexer)3 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)3 AttributesDescriptor (com.intellij.openapi.options.colors.AttributesDescriptor)3 Project (com.intellij.openapi.project.Project)3 Pair (com.intellij.openapi.util.Pair)3 PsiElement (com.intellij.psi.PsiElement)3 NamedScope (com.intellij.psi.search.scope.packageSet.NamedScope)3