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);
}
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();
}
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);
}
}
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;
}
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;
}
Aggregations