11package 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 ;
317import 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
526public 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}
0 commit comments