Print file text with placed textAttributesKey to avoid adding them manually

This commit is contained in:
Nikolay Krasko
2013-09-30 19:59:50 +04:00
parent f09ba64234
commit b09ef86eab
@@ -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<HighlightInfo> highlights =
DaemonCodeAnalyzerImpl.getHighlights(myFixture.getDocument(myFixture.getFile()), null, myFixture.getProject());
List<HighlightTagPoint> 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<HighlightTagPoint> 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("</%s>", tagName);
}
builder.insert(point.offset, tagText);
}
return builder.toString();
}
private static class HighlightTagPoint implements Comparable<HighlightTagPoint> {
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;
}
}
}