Skip to content

Commit aecc0e7

Browse files
committed
[FIX] higher priority for CSV tooltips than for spellchecker
Fixes #40
1 parent 4a794a8 commit aecc0e7

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-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: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
package net.seesharpsoft.intellij.plugins.csv;
22

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

528
public class CsvAnnotatorTest extends LightCodeInsightFixtureTestCase {
629

@@ -11,7 +34,68 @@ protected String getTestDataPath() {
1134

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

17101
}
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)