Search in sources :

Example 91 with HighlightInfo

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

the class ExpectedHighlightingData method composeText.

private static Couple<Integer> composeText(StringBuilder sb, List<Pair<String, HighlightInfo>> list, int index, String text, int endPos, int startPos, boolean showAttributesKeys) {
    int i = index;
    while (i < list.size()) {
        Pair<String, HighlightInfo> pair = list.get(i);
        HighlightInfo info = pair.second;
        if (info.endOffset <= startPos) {
            break;
        }
        String severity = pair.first;
        HighlightInfo prev = i < list.size() - 1 ? list.get(i + 1).second : null;
        sb.insert(0, text.substring(info.endOffset, endPos));
        sb.insert(0, "</" + severity + ">");
        endPos = info.endOffset;
        if (prev != null && prev.endOffset > info.startOffset) {
            Couple<Integer> result = composeText(sb, list, i + 1, text, endPos, info.startOffset, showAttributesKeys);
            i = result.first - 1;
            endPos = result.second;
        }
        sb.insert(0, text.substring(info.startOffset, endPos));
        String str = "<" + severity + " descr=\"" + StringUtil.escapeQuotes(String.valueOf(info.getDescription())) + "\"";
        if (showAttributesKeys) {
            str += " textAttributesKey=\"" + info.forcedTextAttributesKey + "\"";
        }
        str += ">";
        sb.insert(0, str);
        endPos = info.startOffset;
        i++;
    }
    return Couple.of(i, endPos);
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 92 with HighlightInfo

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

the class ExpectedHighlightingData method composeText.

public static String composeText(final Map<String, ExpectedHighlightingSet> types, Collection<HighlightInfo> infos, String text) {
    // filter highlighting data and map each highlighting to a tag name
    List<Pair<String, HighlightInfo>> list = ContainerUtil.mapNotNull(infos, (NullableFunction<HighlightInfo, Pair<String, HighlightInfo>>) info -> {
        for (Map.Entry<String, ExpectedHighlightingSet> entry : types.entrySet()) {
            final ExpectedHighlightingSet set = entry.getValue();
            if (set.enabled && set.severity == info.getSeverity() && set.endOfLine == info.isAfterEndOfLine()) {
                return Pair.create(entry.getKey(), info);
            }
        }
        return null;
    });
    boolean showAttributesKeys = false;
    for (ExpectedHighlightingSet eachSet : types.values()) {
        for (HighlightInfo eachInfo : eachSet.infos) {
            if (eachInfo.forcedTextAttributesKey != null) {
                showAttributesKeys = true;
                break;
            }
        }
    }
    // sort filtered highlighting data by end offset in descending order
    Collections.sort(list, (o1, o2) -> {
        HighlightInfo i1 = o1.second;
        HighlightInfo i2 = o2.second;
        int byEnds = i2.endOffset - i1.endOffset;
        if (byEnds != 0)
            return byEnds;
        if (!i1.isAfterEndOfLine() && !i2.isAfterEndOfLine()) {
            int byStarts = i1.startOffset - i2.startOffset;
            if (byStarts != 0)
                return byStarts;
        } else {
            int byEOL = Comparing.compare(i2.isAfterEndOfLine(), i1.isAfterEndOfLine());
            if (byEOL != 0)
                return byEOL;
        }
        int bySeverity = i2.getSeverity().compareTo(i1.getSeverity());
        if (bySeverity != 0)
            return bySeverity;
        return Comparing.compare(i1.getDescription(), i2.getDescription());
    });
    // combine highlighting data with original text
    StringBuilder sb = new StringBuilder();
    Couple<Integer> result = composeText(sb, list, 0, text, text.length(), 0, showAttributesKeys);
    sb.insert(0, text.substring(0, result.second));
    return sb.toString();
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) GutterIconRenderer(com.intellij.openapi.editor.markup.GutterIconRenderer) FileComparisonFailure(com.intellij.rt.execution.junit.FileComparisonFailure) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) CodeInsightTestFixture(com.intellij.testFramework.fixtures.CodeInsightTestFixture) THashSet(gnu.trove.THashSet) SeveritiesProvider(com.intellij.codeInsight.daemon.impl.SeveritiesProvider) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) Matcher(java.util.regex.Matcher) CommonBundle(com.intellij.CommonBundle) EffectType(com.intellij.openapi.editor.markup.EffectType) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Logger(com.intellij.openapi.diagnostic.Logger) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) RangeMarker(com.intellij.openapi.editor.RangeMarker) Extensions(com.intellij.openapi.extensions.Extensions) ConstantFunction(com.intellij.util.ConstantFunction) StringUtil(com.intellij.openapi.util.text.StringUtil) NullableFunction(com.intellij.util.NullableFunction) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) HighlighterColors(com.intellij.openapi.editor.HighlighterColors) Field(java.lang.reflect.Field) Pass(com.intellij.codeHighlighting.Pass) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Result(com.intellij.openapi.application.Result) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 93 with HighlightInfo

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

the class ExpectedHighlightingData method checkResult.

public void checkResult(Collection<HighlightInfo> infos, String text, @Nullable String filePath) {
    if (filePath == null) {
        VirtualFile virtualFile = myFile == null ? null : myFile.getVirtualFile();
        filePath = virtualFile == null ? null : virtualFile.getUserData(VfsTestUtil.TEST_DATA_FILE_PATH);
    }
    String fileName = myFile == null ? "" : myFile.getName() + ": ";
    String failMessage = "";
    for (HighlightInfo info : reverseCollection(infos)) {
        if (!expectedInfosContainsInfo(info) && !myIgnoreExtraHighlighting) {
            final int startOffset = info.startOffset;
            final int endOffset = info.endOffset;
            String s = text.substring(startOffset, endOffset);
            String desc = info.getDescription();
            if (!failMessage.isEmpty())
                failMessage += '\n';
            failMessage += fileName + "Extra " + rangeString(text, startOffset, endOffset) + " :'" + s + "'" + (desc == null ? "" : " (" + desc + ")") + " [" + info.type + "]";
        }
    }
    final Collection<ExpectedHighlightingSet> expectedHighlights = myHighlightingTypes.values();
    for (ExpectedHighlightingSet highlightingSet : reverseCollection(expectedHighlights)) {
        final Set<HighlightInfo> expInfos = highlightingSet.infos;
        for (HighlightInfo expectedInfo : expInfos) {
            if (!infosContainsExpectedInfo(infos, expectedInfo) && highlightingSet.enabled) {
                final int startOffset = expectedInfo.startOffset;
                final int endOffset = expectedInfo.endOffset;
                String s = text.substring(startOffset, endOffset);
                String desc = expectedInfo.getDescription();
                if (!failMessage.isEmpty())
                    failMessage += '\n';
                failMessage += fileName + "Missing " + rangeString(text, startOffset, endOffset) + " :'" + s + "'" + (desc == null ? "" : " (" + desc + ")");
            }
        }
    }
    if (!failMessage.isEmpty()) {
        compareTexts(infos, text, failMessage + "\n", filePath);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 94 with HighlightInfo

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

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

the class ToggleHighlightingMarkupAction method processStack.

private static int processStack(LinkedList<HighlightInfo> stack, StringBuilder sb, CharSequence sequence, int offset, final int endOffset) {
    if (stack.isEmpty())
        return offset;
    for (HighlightInfo cur = stack.peekLast(); cur != null && cur.getEndOffset() <= endOffset; cur = stack.peekLast()) {
        stack.removeLast();
        if (offset <= cur.getEndOffset()) {
            sb.append(sequence.subSequence(offset, cur.getEndOffset()));
        }
        offset = cur.getEndOffset();
        appendTag(sb, cur, false);
    }
    return offset;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Aggregations

HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)221 Nullable (org.jetbrains.annotations.Nullable)51 TextRange (com.intellij.openapi.util.TextRange)33 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)28 NotNull (org.jetbrains.annotations.NotNull)17 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)16 Document (com.intellij.openapi.editor.Document)12 ArrayList (java.util.ArrayList)11 PsiElement (com.intellij.psi.PsiElement)10 File (java.io.File)8 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 Pair (com.intellij.openapi.util.Pair)6 PsiFile (com.intellij.psi.PsiFile)6 Editor (com.intellij.openapi.editor.Editor)5 NonNls (org.jetbrains.annotations.NonNls)5 StringUtil (com.intellij.openapi.util.text.StringUtil)4 IElementType (com.intellij.psi.tree.IElementType)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4