Search in sources :

Example 11 with HighlightInfoType

use of com.intellij.codeInsight.daemon.impl.HighlightInfoType 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 12 with HighlightInfoType

use of com.intellij.codeInsight.daemon.impl.HighlightInfoType in project intellij-community by JetBrains.

the class HighlightNamesUtil method highlightVariableName.

@Nullable
static HighlightInfo highlightVariableName(@NotNull PsiVariable variable, @NotNull PsiElement elementToHighlight, @NotNull TextAttributesScheme colorsScheme) {
    HighlightInfoType varType = getVariableNameHighlightType(variable);
    if (varType == null) {
        return null;
    }
    if (variable instanceof PsiField) {
        TextAttributes attributes = mergeWithScopeAttributes(variable, varType, colorsScheme);
        HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(varType).range(elementToHighlight.getTextRange());
        if (attributes != null) {
            builder.textAttributes(attributes);
        }
        return builder.createUnconditionally();
    }
    HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(varType).range(elementToHighlight);
    return RainbowHighlighter.isRainbowEnabledWithInheritance(colorsScheme, JavaLanguage.INSTANCE) ? builder.createUnconditionally() : builder.create();
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with HighlightInfoType

use of com.intellij.codeInsight.daemon.impl.HighlightInfoType in project intellij-community by JetBrains.

the class HighlightNamesUtil method highlightMethodName.

/**
   * @param methodOrClass method to highlight; class is passed instead of implicit constructor
   */
@Nullable
static HighlightInfo highlightMethodName(@NotNull PsiMember methodOrClass, @NotNull PsiElement elementToHighlight, @NotNull TextRange range, @NotNull TextAttributesScheme colorsScheme, final boolean isDeclaration) {
    boolean isInherited = false;
    if (!isDeclaration) {
        if (isCalledOnThis(elementToHighlight)) {
            final PsiClass containingClass = methodOrClass instanceof PsiMethod ? methodOrClass.getContainingClass() : null;
            PsiClass enclosingClass = containingClass == null ? null : PsiTreeUtil.getParentOfType(elementToHighlight, PsiClass.class);
            while (enclosingClass != null) {
                isInherited = enclosingClass.isInheritor(containingClass, true);
                if (isInherited)
                    break;
                enclosingClass = PsiTreeUtil.getParentOfType(enclosingClass, PsiClass.class, true);
            }
        }
    }
    LOG.assertTrue(methodOrClass instanceof PsiMethod || !isDeclaration);
    HighlightInfoType type = methodOrClass instanceof PsiMethod ? getMethodNameHighlightType((PsiMethod) methodOrClass, isDeclaration, isInherited) : JavaHighlightInfoTypes.CONSTRUCTOR_CALL;
    if (type != null) {
        TextAttributes attributes = mergeWithScopeAttributes(methodOrClass, type, colorsScheme);
        HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range);
        if (attributes != null) {
            builder.textAttributes(attributes);
        }
        return builder.createUnconditionally();
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with HighlightInfoType

use of com.intellij.codeInsight.daemon.impl.HighlightInfoType in project intellij-community by JetBrains.

the class HighlightNamesUtil method highlightClassName.

@NotNull
static HighlightInfo highlightClassName(@Nullable PsiClass aClass, @NotNull PsiElement elementToHighlight, @NotNull TextAttributesScheme colorsScheme) {
    TextRange range = elementToHighlight.getTextRange();
    if (elementToHighlight instanceof PsiJavaCodeReferenceElement) {
        final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) elementToHighlight;
        PsiElement identifier = referenceElement.getReferenceNameElement();
        if (identifier != null) {
            range = identifier.getTextRange();
        }
    }
    // This will highlight @ sign in annotation as well.
    final PsiElement parent = elementToHighlight.getParent();
    if (parent instanceof PsiAnnotation) {
        final PsiAnnotation psiAnnotation = (PsiAnnotation) parent;
        range = new TextRange(psiAnnotation.getTextRange().getStartOffset(), range.getEndOffset());
    }
    HighlightInfoType type = getClassNameHighlightType(aClass, elementToHighlight);
    TextAttributes attributes = mergeWithScopeAttributes(aClass, type, colorsScheme);
    HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range);
    if (attributes != null) {
        builder.textAttributes(attributes);
    }
    return builder.createUnconditionally();
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with HighlightInfoType

use of com.intellij.codeInsight.daemon.impl.HighlightInfoType in project intellij-community by JetBrains.

the class HighlightNamesUtil method highlightPackage.

static HighlightInfo highlightPackage(@NotNull PsiElement resolved, @NotNull PsiJavaCodeReferenceElement elementToHighlight, @NotNull TextAttributesScheme scheme) {
    PsiElement referenceNameElement = elementToHighlight.getReferenceNameElement();
    TextRange range;
    if (referenceNameElement == null) {
        range = elementToHighlight.getTextRange();
    } else {
        PsiElement nextSibling = PsiTreeUtil.nextLeaf(referenceNameElement);
        if (nextSibling != null && nextSibling.getTextRange().isEmpty()) {
            // empty PsiReferenceParameterList
            nextSibling = PsiTreeUtil.nextLeaf(nextSibling);
        }
        if (nextSibling instanceof PsiJavaToken && ((PsiJavaToken) nextSibling).getTokenType() == JavaTokenType.DOT) {
            range = new TextRange(referenceNameElement.getTextRange().getStartOffset(), nextSibling.getTextRange().getEndOffset());
        } else {
            range = referenceNameElement.getTextRange();
        }
    }
    HighlightInfoType type = JavaHighlightInfoTypes.CLASS_NAME;
    TextAttributes attributes = mergeWithScopeAttributes(resolved, type, scheme);
    HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range);
    if (attributes != null) {
        builder.textAttributes(attributes);
    }
    return builder.createUnconditionally();
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType)

Aggregations

HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)22 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)14 TextRange (com.intellij.openapi.util.TextRange)8 Nullable (org.jetbrains.annotations.Nullable)6 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)5 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)4 SeveritiesProvider (com.intellij.codeInsight.daemon.impl.SeveritiesProvider)3 NotNull (org.jetbrains.annotations.NotNull)3 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)2 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)2 QuickFixActionRegistrarImpl (com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl)1 SeverityEditorDialog (com.intellij.codeInspection.ex.SeverityEditorDialog)1 ASTNode (com.intellij.lang.ASTNode)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)1 Module (com.intellij.openapi.module.Module)1 AttributesDescriptor (com.intellij.openapi.options.colors.AttributesDescriptor)1