From b09ef86eab8e604a0ec41f7beb6c7f9ed46dd7ed Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Mon, 30 Sep 2013 19:59:50 +0400 Subject: [PATCH] Print file text with placed textAttributesKey to avoid adding them manually --- .../highlighter/AbstractHighlightingTest.java | 96 ++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java b/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java index bbb4af322f9..c9162b5723d 100644 --- a/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java @@ -16,7 +16,13 @@ package org.jetbrains.jet.plugin.highlighter; +import com.beust.jcommander.internal.Lists; +import com.google.common.collect.Ordering; +import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl; +import com.intellij.codeInsight.daemon.impl.HighlightInfo; +import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.rt.execution.junit.FileComparisonFailure; import com.intellij.testFramework.LightProjectDescriptor; import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; @@ -24,6 +30,7 @@ import org.jetbrains.jet.InTextDirectivesUtils; import org.jetbrains.jet.plugin.JetLightProjectDescriptor; import java.io.File; +import java.util.List; public abstract class AbstractHighlightingTest extends LightCodeInsightFixtureTestCase { @NotNull @@ -39,7 +46,16 @@ public abstract class AbstractHighlightingTest extends LightCodeInsightFixtureTe boolean checkWarnings = !InTextDirectivesUtils.isDirectiveDefined(fileText, "// NO_CHECK_WARNINGS"); myFixture.configureByFile(filePath); - myFixture.checkHighlighting(checkWarnings, checkInfos, checkWeakWarnings); + + try { + myFixture.checkHighlighting(checkWarnings, checkInfos, checkWeakWarnings); + } + catch (FileComparisonFailure e) { + //noinspection UseOfSystemOutOrSystemErr + System.out.println(getFileWithTextAttributesKeys()); + + throw e; + } } @NotNull @@ -47,4 +63,82 @@ public abstract class AbstractHighlightingTest extends LightCodeInsightFixtureTe protected String getTestDataPath() { return ""; } + + private String getFileWithTextAttributesKeys() { + List highlights = + DaemonCodeAnalyzerImpl.getHighlights(myFixture.getDocument(myFixture.getFile()), null, myFixture.getProject()); + + List pointsForHighlightInfos = Lists.newArrayList(); + for (HighlightInfo highlight : highlights) { + pointsForHighlightInfos.add(new HighlightTagPoint(highlight.startOffset, true, highlight)); + pointsForHighlightInfos.add(new HighlightTagPoint(highlight.endOffset, false, highlight)); + } + + StringBuilder builder = new StringBuilder(myFixture.getFile().getText()); + + List sortedTagPoints = Ordering.natural().reverse().sortedCopy(pointsForHighlightInfos); + + // Insert tags into text starting from the end for preventing invalidating of highlight offsets + for (HighlightTagPoint point : sortedTagPoints) { + String tagName; + if (point.highlightInfo.getSeverity().equals(HighlightSeverity.INFORMATION)) { + tagName = "info"; + } + else { + tagName = point.highlightInfo.getSeverity().toString().toLowerCase(); + } + + String tagText; + if (point.isStart) { + if (point.highlightInfo.getDescription() != null) { + tagText = String.format("<%s textAttributesKey=\"%s\" descr=%s>", + tagName, + point.highlightInfo.forcedTextAttributesKey, + point.highlightInfo.getDescription()); + } + else { + tagText = String.format("<%s textAttributesKey=\"%s\">", tagName, point.highlightInfo.forcedTextAttributesKey); + } + } + else { + tagText = String.format("", tagName); + } + + builder.insert(point.offset, tagText); + } + + return builder.toString(); + } + + private static class HighlightTagPoint implements Comparable { + private final int offset; + private final boolean isStart; + private final HighlightInfo highlightInfo; + + private HighlightTagPoint(int offset, boolean start, HighlightInfo info) { + this.offset = offset; + isStart = start; + highlightInfo = info; + } + + @Override + public int compareTo(@NotNull HighlightTagPoint other) { + if (offset != other.offset) { + return ((Integer) offset).compareTo(other.offset); + } + + if (isStart != other.isStart) { + // All "starts" should go after "ends" for same offset + return isStart ? -1 : 1; + } + + if (highlightInfo.getSeverity() != other.highlightInfo.getSeverity()) { + // Invert order for end tags + return highlightInfo.getSeverity().compareTo(other.highlightInfo.getSeverity()) * (isStart ? -1 : 1); + } + + // The order isn't important for highlighting with same offset and severity + return 0; + } + } } \ No newline at end of file