Skip to content

Commit a91c6f4

Browse files
authored
Merge pull request #41 from SeeSharpSoft/fb_tooltip_severity
[FIX] higher priority for CSV tooltips than for spellchecker
2 parents 4a794a8 + 70ad56e commit a91c6f4

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

src/main/java/net/seesharpsoft/intellij/plugins/csv/CsvAnnotator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
import net.seesharpsoft.intellij.plugins.csv.psi.CsvTypes;
1313
import org.jetbrains.annotations.NotNull;
1414

15+
import static com.intellij.spellchecker.SpellCheckerSeveritiesProvider.TYPO;
16+
1517
public class CsvAnnotator implements Annotator {
1618

1719
private static final TextAttributes EMPTY_TEXT_ATTRIBUTES = TextAttributes.fromFlyweight(AttributesFlyweight.create(null, null, 0, null, null, null));
1820

21+
public static final HighlightSeverity CSV_COLUMN_INFO_SEVERITY = new HighlightSeverity("CSV_COLUMN_INFO_SEVERITY", TYPO.myVal + 5);
22+
1923
@Override
2024
public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
2125
if (CsvHelper.getElementType(element) != CsvTypes.FIELD || !(element.getContainingFile() instanceof CsvFile)) {
@@ -30,7 +34,7 @@ public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolde
3034
String message = XmlStringUtil.escapeString(headerElement == null ? "" : headerElement.getText(), true);
3135
String tooltip = XmlStringUtil.wrapInHtml(String.format("%s<br /><br />Header: %s<br />Index: %d", XmlStringUtil.escapeString(element.getText(), true), message, columnInfo.getColumnIndex()));
3236

33-
Annotation annotation = holder.createAnnotation(HighlightSeverity.INFORMATION, element.getTextRange(), message, tooltip);
37+
Annotation annotation = holder.createAnnotation(CSV_COLUMN_INFO_SEVERITY, element.getTextRange(), message, tooltip);
3438
annotation.setEnforcedTextAttributes(EMPTY_TEXT_ATTRIBUTES);
3539
annotation.setNeedsUpdateOnTyping(false);
3640
}

src/test/java/net/seesharpsoft/intellij/plugins/csv/CsvAnnotatorTest.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
package net.seesharpsoft.intellij.plugins.csv;
22

3+
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
4+
import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
5+
import com.intellij.openapi.Disposable;
6+
import com.intellij.openapi.project.DumbService;
7+
import com.intellij.openapi.project.Project;
8+
import com.intellij.openapi.util.Disposer;
9+
import com.intellij.psi.PsiDocumentManager;
10+
import com.intellij.psi.PsiFile;
11+
import com.intellij.psi.impl.cache.CacheManager;
12+
import com.intellij.psi.impl.source.PsiFileImpl;
13+
import com.intellij.psi.impl.source.tree.FileElement;
14+
import com.intellij.psi.search.GlobalSearchScope;
15+
import com.intellij.testFramework.EdtTestUtil;
16+
import com.intellij.testFramework.ExpectedHighlightingData;
317
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
18+
import org.jetbrains.annotations.NotNull;
19+
20+
import java.util.Collections;
21+
import java.util.Iterator;
22+
import java.util.List;
23+
24+
import static net.seesharpsoft.intellij.plugins.csv.CsvAnnotator.CSV_COLUMN_INFO_SEVERITY;
425

526
public class CsvAnnotatorTest extends LightCodeInsightFixtureTestCase {
627

@@ -11,7 +32,63 @@ protected String getTestDataPath() {
1132

1233
public void testAnnotator() {
1334
myFixture.configureByFile("AnnotatorTestData.csv");
14-
myFixture.checkHighlighting(true, true, true, true);
35+
this.collectAndCheckHighlighting();
36+
}
37+
38+
private long collectAndCheckHighlighting() {
39+
ExpectedHighlightingData data = new ExpectedHighlightingData(myFixture.getEditor().getDocument(), false, false, true, false, this.getHostFile());
40+
data.registerHighlightingType("csv_column_info", new ExpectedHighlightingData.ExpectedHighlightingSet(CSV_COLUMN_INFO_SEVERITY, false, true));
41+
data.init();
42+
return this.collectAndCheckHighlighting(data);
43+
}
44+
45+
private PsiFile getHostFile() {
46+
return myFixture.getFile();
47+
}
48+
49+
private long collectAndCheckHighlighting(@NotNull ExpectedHighlightingData data) {
50+
Project project = myFixture.getProject();
51+
EdtTestUtil.runInEdtAndWait(() -> {
52+
PsiDocumentManager.getInstance(project).commitAllDocuments();
53+
});
54+
PsiFileImpl file = (PsiFileImpl)this.getHostFile();
55+
FileElement hardRefToFileElement = file.calcTreeElement();
56+
if (!DumbService.isDumb(project)) {
57+
CacheManager.SERVICE.getInstance(project).getFilesWithWord("XXX", (short)2, GlobalSearchScope.allScope(project), true);
58+
}
59+
60+
long start = System.currentTimeMillis();
61+
Disposable disposable = Disposer.newDisposable();
62+
63+
List infos;
64+
try {
65+
infos = myFixture.doHighlighting();
66+
this.removeDuplicatedRangesForInjected(infos);
67+
} finally {
68+
Disposer.dispose(disposable);
69+
}
70+
71+
long elapsed = System.currentTimeMillis() - start;
72+
data.checkResult(infos, file.getText());
73+
hardRefToFileElement.hashCode();
74+
return elapsed;
75+
}
76+
77+
private static void removeDuplicatedRangesForInjected(@NotNull List<HighlightInfo> infos) {
78+
Collections.sort(infos, (o1, o2) -> {
79+
int i = o2.startOffset - o1.startOffset;
80+
return i != 0 ? i : o1.getSeverity().myVal - o2.getSeverity().myVal;
81+
});
82+
HighlightInfo prevInfo = null;
83+
84+
HighlightInfo info;
85+
for(Iterator it = infos.iterator(); it.hasNext(); prevInfo = info.type == HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT ? info : null) {
86+
info = (HighlightInfo)it.next();
87+
if (prevInfo != null && info.getSeverity() == HighlightInfoType.SYMBOL_TYPE_SEVERITY && info.getDescription() == null && info.startOffset == prevInfo.startOffset && info.endOffset == prevInfo.endOffset) {
88+
it.remove();
89+
}
90+
}
91+
1592
}
1693

1794
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<info descr="Header 1">Header 1</info>, <info descr="Header 2">Header 2</info>
2-
<info descr="Header 1">Value 1</info>, <info descr="Header 2">Value 2</info>, <info descr="">Value 3</info>
1+
<csv_column_info descr="Header 1">Header 1</csv_column_info>, <csv_column_info descr="Header 2">Header 2</csv_column_info>
2+
<csv_column_info descr="Header 1">Value 1</csv_column_info>, <csv_column_info descr="Header 2">Value 2</csv_column_info>, <csv_column_info descr="">Value 3</csv_column_info>

0 commit comments

Comments
 (0)