Search in sources :

Example 11 with TextAttributesKey

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

the class SingleInspectionProfilePanel method copyUsedSeveritiesIfUndefined.

private static void copyUsedSeveritiesIfUndefined(InspectionProfileImpl selectedProfile, BaseInspectionProfileManager profileManager) {
    final SeverityRegistrar registrar = profileManager.getSeverityRegistrar();
    final Set<HighlightSeverity> severities = selectedProfile.getUsedSeverities();
    for (Iterator<HighlightSeverity> iterator = severities.iterator(); iterator.hasNext(); ) {
        HighlightSeverity severity = iterator.next();
        if (registrar.isSeverityValid(severity.getName())) {
            iterator.remove();
        }
    }
    if (!severities.isEmpty()) {
        final SeverityRegistrar oppositeRegister = selectedProfile.getProfileManager().getSeverityRegistrar();
        for (HighlightSeverity severity : severities) {
            final TextAttributesKey attributesKey = TextAttributesKey.find(severity.getName());
            final TextAttributes textAttributes = oppositeRegister.getTextAttributesBySeverity(severity);
            if (textAttributes == null) {
                continue;
            }
            HighlightInfoType.HighlightInfoTypeImpl info = new HighlightInfoType.HighlightInfoTypeImpl(severity, attributesKey);
            registrar.registerSeverity(new SeverityRegistrar.SeverityBasedTextAttributes(textAttributes.clone(), info), textAttributes.getErrorStripeColor());
        }
    }
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) SeverityRegistrar(com.intellij.codeInsight.daemon.impl.SeverityRegistrar) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType)

Example 12 with TextAttributesKey

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

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

the class FindManagerImpl method findInCommentsAndLiterals.

@NotNull
private FindResult findInCommentsAndLiterals(@NotNull CharSequence text, char[] textArray, int offset, @NotNull FindModel model, @NotNull final VirtualFile file) {
    synchronized (model) {
        FileType ftype = file.getFileType();
        Language lang = null;
        if (ftype instanceof LanguageFileType) {
            lang = ((LanguageFileType) ftype).getLanguage();
        }
        CommentsLiteralsSearchData data = model.getUserData(ourCommentsLiteralsSearchDataKey);
        if (data == null || !Comparing.equal(data.lastFile, file) || !data.model.equals(model)) {
            SyntaxHighlighter highlighter = getHighlighter(file, lang);
            if (highlighter == null) {
                // no syntax highlighter -> no search
                return NOT_FOUND_RESULT;
            }
            TokenSet tokensOfInterest = TokenSet.EMPTY;
            Set<Language> relevantLanguages;
            if (lang != null) {
                final Language finalLang = lang;
                relevantLanguages = ApplicationManager.getApplication().runReadAction(new Computable<Set<Language>>() {

                    @Override
                    public Set<Language> compute() {
                        THashSet<Language> result = new THashSet<>();
                        FileViewProvider viewProvider = PsiManager.getInstance(myProject).findViewProvider(file);
                        if (viewProvider != null) {
                            result.addAll(viewProvider.getLanguages());
                        }
                        if (result.isEmpty()) {
                            result.add(finalLang);
                        }
                        return result;
                    }
                });
                for (Language relevantLanguage : relevantLanguages) {
                    tokensOfInterest = addTokenTypesForLanguage(model, relevantLanguage, tokensOfInterest);
                }
                if (model.isInStringLiteralsOnly()) {
                    // TODO: xml does not have string literals defined so we add XmlAttributeValue element type as convenience
                    final Lexer xmlLexer = getHighlighter(null, Language.findLanguageByID("XML")).getHighlightingLexer();
                    final String marker = "xxx";
                    xmlLexer.start("<a href=\"" + marker + "\" />");
                    while (!marker.equals(xmlLexer.getTokenText())) {
                        xmlLexer.advance();
                        if (xmlLexer.getTokenType() == null)
                            break;
                    }
                    IElementType convenienceXmlAttrType = xmlLexer.getTokenType();
                    if (convenienceXmlAttrType != null) {
                        tokensOfInterest = TokenSet.orSet(tokensOfInterest, TokenSet.create(convenienceXmlAttrType));
                    }
                }
            } else {
                relevantLanguages = ContainerUtil.newHashSet();
                if (ftype instanceof AbstractFileType) {
                    if (model.isInCommentsOnly()) {
                        tokensOfInterest = TokenSet.create(CustomHighlighterTokenType.LINE_COMMENT, CustomHighlighterTokenType.MULTI_LINE_COMMENT);
                    }
                    if (model.isInStringLiteralsOnly()) {
                        tokensOfInterest = TokenSet.orSet(tokensOfInterest, TokenSet.create(CustomHighlighterTokenType.STRING, CustomHighlighterTokenType.SINGLE_QUOTED_STRING));
                    }
                }
            }
            Matcher matcher = model.isRegularExpressions() ? compileRegExp(model, "") : null;
            StringSearcher searcher = matcher != null ? null : new StringSearcher(model.getStringToFind(), model.isCaseSensitive(), true);
            SyntaxHighlighterOverEditorHighlighter highlighterAdapter = new SyntaxHighlighterOverEditorHighlighter(highlighter, file, myProject);
            data = new CommentsLiteralsSearchData(file, relevantLanguages, highlighterAdapter, tokensOfInterest, searcher, matcher, model.clone());
            data.highlighter.restart(text);
            model.putUserData(ourCommentsLiteralsSearchDataKey, data);
        }
        int initialStartOffset = model.isForward() && data.startOffset < offset ? data.startOffset : 0;
        data.highlighter.resetPosition(initialStartOffset);
        final Lexer lexer = data.highlighter.getHighlightingLexer();
        IElementType tokenType;
        TokenSet tokens = data.tokensOfInterest;
        int lastGoodOffset = 0;
        boolean scanningForward = model.isForward();
        FindResultImpl prevFindResult = NOT_FOUND_RESULT;
        while ((tokenType = lexer.getTokenType()) != null) {
            if (lexer.getState() == 0)
                lastGoodOffset = lexer.getTokenStart();
            final TextAttributesKey[] keys = data.highlighter.getTokenHighlights(tokenType);
            if (tokens.contains(tokenType) || (model.isInStringLiteralsOnly() && ChunkExtractor.isHighlightedAsString(keys)) || (model.isInCommentsOnly() && ChunkExtractor.isHighlightedAsComment(keys))) {
                int start = lexer.getTokenStart();
                int end = lexer.getTokenEnd();
                if (model.isInStringLiteralsOnly()) {
                    // skip literal quotes itself from matching
                    char c = text.charAt(start);
                    if (c == '"' || c == '\'') {
                        while (start < end && c == text.charAt(start)) {
                            ++start;
                            if (c == text.charAt(end - 1) && start < end)
                                --end;
                        }
                    }
                }
                while (true) {
                    FindResultImpl findResult = null;
                    if (data.searcher != null) {
                        int matchStart = data.searcher.scan(text, textArray, start, end);
                        if (matchStart != -1 && matchStart >= start) {
                            final int matchEnd = matchStart + model.getStringToFind().length();
                            if (matchStart >= offset || !scanningForward)
                                findResult = new FindResultImpl(matchStart, matchEnd);
                            else {
                                start = matchEnd;
                                continue;
                            }
                        }
                    } else if (start <= end) {
                        data.matcher.reset(StringPattern.newBombedCharSequence(text.subSequence(start, end)));
                        if (data.matcher.find()) {
                            final int matchEnd = start + data.matcher.end();
                            int matchStart = start + data.matcher.start();
                            if (matchStart >= offset || !scanningForward) {
                                findResult = new FindResultImpl(matchStart, matchEnd);
                            } else {
                                int diff = 0;
                                if (start == end) {
                                    diff = scanningForward ? 1 : -1;
                                }
                                start = matchEnd + diff;
                                continue;
                            }
                        }
                    }
                    if (findResult != null) {
                        if (scanningForward) {
                            data.startOffset = lastGoodOffset;
                            return findResult;
                        } else {
                            if (findResult.getEndOffset() >= offset)
                                return prevFindResult;
                            prevFindResult = findResult;
                            start = findResult.getEndOffset();
                            continue;
                        }
                    }
                    break;
                }
            } else {
                Language tokenLang = tokenType.getLanguage();
                if (tokenLang != lang && tokenLang != Language.ANY && !data.relevantLanguages.contains(tokenLang)) {
                    tokens = addTokenTypesForLanguage(model, tokenLang, tokens);
                    data.tokensOfInterest = tokens;
                    data.relevantLanguages.add(tokenLang);
                }
            }
            lexer.advance();
        }
        return prevFindResult;
    }
}
Also used : AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) Matcher(java.util.regex.Matcher) TokenSet(com.intellij.psi.tree.TokenSet) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) THashSet(gnu.trove.THashSet) LightweightHint(com.intellij.ui.LightweightHint) IElementType(com.intellij.psi.tree.IElementType) Lexer(com.intellij.lexer.Lexer) Language(com.intellij.lang.Language) AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) SyntaxHighlighterOverEditorHighlighter(com.intellij.usages.impl.SyntaxHighlighterOverEditorHighlighter) Computable(com.intellij.openapi.util.Computable) StringSearcher(com.intellij.util.text.StringSearcher) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with TextAttributesKey

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

the class ColorSettingsUtil method keyToDisplayTextMap.

public static Map<TextAttributesKey, String> keyToDisplayTextMap(final ColorSettingsPage page) {
    final List<AttributesDescriptor> attributeDescriptors = getAllAttributeDescriptors(page);
    final Map<TextAttributesKey, String> displayText = new HashMap<>();
    for (AttributesDescriptor attributeDescriptor : attributeDescriptors) {
        final TextAttributesKey key = attributeDescriptor.getKey();
        displayText.put(key, attributeDescriptor.getDisplayName());
    }
    return displayText;
}
Also used : AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) HashMap(com.intellij.util.containers.HashMap) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey)

Example 15 with TextAttributesKey

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

the class ColorSettingsUtil method addInspectionSeverityAttributes.

private static void addInspectionSeverityAttributes(List<AttributesDescriptor> descriptors) {
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.unknown.symbol"), CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES));
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.deprecated.symbol"), CodeInsightColors.DEPRECATED_ATTRIBUTES));
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.unused.symbol"), CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES));
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.error"), CodeInsightColors.ERRORS_ATTRIBUTES));
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.warning"), CodeInsightColors.WARNINGS_ATTRIBUTES));
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.weak.warning"), CodeInsightColors.WEAK_WARNING_ATTRIBUTES));
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.server.problems"), CodeInsightColors.GENERIC_SERVER_ERROR_OR_WARNING));
    descriptors.add(new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.server.duplicate"), CodeInsightColors.DUPLICATE_FROM_SERVER));
    for (SeveritiesProvider provider : Extensions.getExtensions(SeveritiesProvider.EP_NAME)) {
        for (HighlightInfoType highlightInfoType : provider.getSeveritiesHighlightInfoTypes()) {
            final TextAttributesKey attributesKey = highlightInfoType.getAttributesKey();
            descriptors.add(new AttributesDescriptor(toDisplayName(attributesKey), attributesKey));
        }
    }
}
Also used : AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) SeveritiesProvider(com.intellij.codeInsight.daemon.impl.SeveritiesProvider) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType)

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