-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathSymfonyLightCodeInsightFixtureTestCase.java
More file actions
834 lines (648 loc) · 33.7 KB
/
SymfonyLightCodeInsightFixtureTestCase.java
File metadata and controls
834 lines (648 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
package fr.adrienbrault.idea.symfony2plugin.tests;
import com.intellij.codeInsight.completion.CodeCompletionHandlerBase;
import com.intellij.codeInsight.completion.CompletionProgressIndicator;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.codeInsight.daemon.LineMarkerProviders;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.IntentionManager;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
import com.intellij.codeInspection.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.lang.javascript.inspections.JSInspection;
import com.intellij.navigation.GotoRelatedItem;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.FileBasedIndex;
import com.intellij.util.indexing.ID;
import com.intellij.util.ui.UIUtil;
import com.jetbrains.php.lang.psi.elements.Function;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpReference;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.function.Function.identity;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public abstract class SymfonyLightCodeInsightFixtureTestCase extends LightJavaCodeInsightFixtureTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
Settings.getInstance(myFixture.getProject()).pluginEnabled = true;
}
public void assertCompletionContains(LanguageFileType languageFileType, String configureByText, String... lookupStrings) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
checkContainsCompletion(lookupStrings);
}
public void assertAtTextCompletionContains(String findByText, String... lookupStrings) {
final PsiElement element = myFixture.findElementByText(findByText, PsiElement.class);
assert element != null : "No element found by text: " + findByText;
myFixture.getEditor().getCaretModel().moveToOffset(element.getTextOffset() + 1);
myFixture.completeBasic();
checkContainsCompletion(lookupStrings);
}
public void assertCompletionNotContains(String filename, String configureByText, String... lookupStrings) {
myFixture.configureByText(filename, configureByText);
myFixture.completeBasic();
assertFalse(myFixture.getLookupElementStrings().containsAll(Arrays.asList(lookupStrings)));
}
public void assertCompletionResultsAreUnique(
@NotNull LanguageFileType languageFileType,
@NotNull String configureByText,
@NotNull String... lookupStrings
) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
var results = myFixture.getLookupElementStrings();
if (results != null) {
var duplicates = results.stream()
.filter(result -> Arrays.asList(lookupStrings).contains(result))
.collect(Collectors.groupingBy(identity()))
.entrySet()
.stream()
.filter(result -> result.getValue().size() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
assertTrue("The following results are duplicated: " + String.join(", ", duplicates), duplicates.isEmpty());
}
}
public void assertCompletionNotContains(LanguageFileType languageFileType, String configureByText, String... lookupStrings) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
assertFalse(myFixture.getLookupElementStrings().containsAll(Arrays.asList(lookupStrings)));
}
public void assertCompletionContains(String filename, String configureByText, String... lookupStrings) {
myFixture.configureByText(filename, configureByText);
myFixture.completeBasic();
completionContainsAssert(lookupStrings);
}
public void assertCompletionIsEmpty(LanguageFileType languageFileType, String configureByText) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
List<String> lookupElements = myFixture.getLookupElementStrings();
if (lookupElements == null) {
return;
}
if (!lookupElements.isEmpty()) {
fail("Failed that completion is empty.");
}
}
protected void createFileInProjectRoot(@NotNull String relativePath, String content) {
VirtualFile[] created = new VirtualFile[1];
ApplicationManager.getApplication().runWriteAction(() -> {
try {
String[] parts = relativePath.split("/");
VirtualFile baseDir = getProject().getBaseDir();
VirtualFile directory = VfsUtil.createDirectoryIfMissing(
baseDir,
StringUtils.join(Arrays.copyOf(parts, parts.length - 1), "/")
);
created[0] = directory.createChildData(this, parts[parts.length - 1]);
if (content != null) {
created[0].setBinaryContent(content.getBytes());
}
} catch (IOException ignored) {
}
});
}
private void completionContainsAssert(String[] lookupStrings) {
if(lookupStrings.length == 0) {
fail("No lookup element given");
}
List<String> lookupElements = myFixture.getLookupElementStrings();
if(lookupElements == null || lookupElements.isEmpty()) {
fail(String.format("failed that empty completion contains %s", Arrays.toString(lookupStrings)));
}
for (String s : lookupStrings) {
if(!lookupElements.contains(s)) {
fail(String.format("failed that completion contains %s in %s", s, lookupElements.toString()));
}
}
}
public void assertNavigationContains(LanguageFileType languageFileType, String configureByText, String targetShortcut) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
assertNavigationContains(psiElement, targetShortcut);
}
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {
if(!targetShortcut.startsWith("\\")) {
targetShortcut = "\\" + targetShortcut;
}
Set<String> classTargets = new HashSet<>();
for (GotoDeclarationHandler gotoDeclarationHandler : GotoDeclarationHandler.EP_NAME.getExtensionList()) {
PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
if(gotoDeclarationTargets != null) {
for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
if(gotoDeclarationTarget instanceof Method) {
String meName = ((Method) gotoDeclarationTarget).getName();
String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
if(!clName.startsWith("\\")) {
clName = "\\" + clName;
}
classTargets.add(clName + "::" + meName);
} else if(gotoDeclarationTarget instanceof Function) {
classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
}
}
}
}
if(!classTargets.contains(targetShortcut)) {
fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
}
}
public void assertNavigationMatchWithParent(LanguageFileType languageFileType, String configureByText, IElementType iElementType) {
assertNavigationMatch(languageFileType, configureByText, PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(iElementType)));
}
public void assertNavigationMatch(String filename, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(filename, configureByText);
assertNavigationMatch(pattern);
}
public void assertNavigationMatch(LanguageFileType languageFileType, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(languageFileType, configureByText);
assertNavigationMatch(pattern);
}
public void assertNavigationMatch(LanguageFileType languageFileType, String configureByText) {
myFixture.configureByText(languageFileType, configureByText);
assertNavigationMatch(PlatformPatterns.psiElement());
}
public void assertNavigationMatch(String filename, String configureByText) {
myFixture.configureByText(filename, configureByText);
assertNavigationMatch(PlatformPatterns.psiElement());
}
public void assertNavigationIsEmpty(LanguageFileType languageFileType, String configureByText) {
myFixture.configureByText(languageFileType, configureByText);
assertNavigationIsEmpty();
}
public void assertNavigationIsEmpty(String content, String configureByText) {
myFixture.configureByText(content, configureByText);
assertNavigationIsEmpty();
}
private void assertNavigationIsEmpty() {
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
for (GotoDeclarationHandler gotoDeclarationHandler : GotoDeclarationHandler.EP_NAME.getExtensionList()) {
PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {
fail(String.format("failed that PsiElement (%s) navigate is empty; found target in '%s'", psiElement.toString(), gotoDeclarationHandler.getClass()));
}
}
}
public void assertNavigationMatch(ElementPattern<?> pattern) {
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
Set<String> targetStrings = new HashSet<>();
for (GotoDeclarationHandler gotoDeclarationHandler : GotoDeclarationHandler.EP_NAME.getExtensionList()) {
PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, myFixture.getCaretOffset(), myFixture.getEditor());
if(gotoDeclarationTargets == null) {
continue;
}
for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
targetStrings.add(gotoDeclarationTarget.toString());
if(pattern.accepts(gotoDeclarationTarget)) {
return;
}
}
}
fail(String.format("failed that PsiElement (%s) navigate matches one of %s", psiElement.toString(), targetStrings));
}
public void assertNavigationContainsFile(LanguageFileType languageFileType, String configureByText, String targetShortcut) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
Set<String> targets = new HashSet<>();
for (GotoDeclarationHandler gotoDeclarationHandler : GotoDeclarationHandler.EP_NAME.getExtensionList()) {
PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
if (gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {
for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
if(gotoDeclarationTarget instanceof PsiFile) {
targets.add(((PsiFile) gotoDeclarationTarget).getVirtualFile().getUrl());
}
}
}
}
// its possible to have memory fields,
// so simple check for ending conditions
// temp:///src/interchange.en.xlf
for (String target : targets) {
if(target.endsWith(targetShortcut)) {
return;
}
}
fail(String.format("failed that PsiElement (%s) navigate to file %s", psiElement.toString(), targetShortcut));
}
public void assertCompletionLookupContainsPresentableItem(LanguageFileType languageFileType, String configureByText, LookupElementPresentationAssert.Assert presentationAssert) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
LookupElement[] lookupElements = myFixture.getLookupElements();
if(lookupElements == null) {
fail("failed to find lookup presentable on empty collection");
}
for (LookupElement lookupElement : lookupElements) {
LookupElementPresentation presentation = new LookupElementPresentation();
lookupElement.renderElement(presentation);
if(presentationAssert.match(presentation)) {
return;
}
}
fail("failed to find lookup presentable");
}
public void assertCompletionLookupTailEquals(LanguageFileType languageFileType, String configureByText, String lookupString, String tailText) {
myFixture.configureByText(languageFileType, configureByText);
myFixture.completeBasic();
for (LookupElement lookupElement : myFixture.getLookupElements()) {
if(!lookupElement.getLookupString().equals(lookupString)) {
continue;
}
LookupElementPresentation presentation = new LookupElementPresentation();
lookupElement.renderElement(presentation);
if(presentation.getTailText() == null) {
fail(String.format("failed to check '%s'", lookupString));
}
if(!presentation.getTailText().equals(tailText)) {
fail(String.format("failed that on '%s' '%s' is equal '%s'", lookupString, tailText, presentation.getTailText()));
}
return;
}
fail(String.format("failed to check '%s' because it's unknown", lookupString));
}
public void assertPhpReferenceResolveTo(LanguageFileType languageFileType, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
psiElement = PsiTreeUtil.getParentOfType(psiElement, PhpReference.class);
if (psiElement == null) {
fail("Element is not PhpReference.");
}
PsiElement resolve = ((PhpReference) psiElement).resolve();
if(!pattern.accepts(resolve)) {
fail(String.format("failed pattern matches element of '%s'", resolve == null ? "null" : resolve.toString()));
}
assertTrue(pattern.accepts(resolve));
}
public void assertPhpReferenceNotResolveTo(LanguageFileType languageFileType, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
psiElement = PsiTreeUtil.getParentOfType(psiElement, PhpReference.class);
if (psiElement == null) {
fail("Element is not PhpReference.");
}
assertFalse(pattern.accepts(((PhpReference) psiElement).resolve()));
}
public void assertPhpReferenceSignatureEquals(LanguageFileType languageFileType, String configureByText, String typeSignature) {
PsiElement psiElement = assertGetPhpReference(languageFileType, configureByText);
assertEquals(typeSignature, ((PhpReference) psiElement).getSignature());
}
public void assertPhpReferenceSignatureContains(LanguageFileType languageFileType, String configureByText, String typeSignature) {
PsiElement psiElement = assertGetPhpReference(languageFileType, configureByText);
assertTrue(((PhpReference) psiElement).getSignature().contains(typeSignature));
}
@NotNull
private PsiElement assertGetPhpReference(LanguageFileType languageFileType, String configureByText) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
psiElement = PsiTreeUtil.getParentOfType(psiElement, PhpReference.class);
if (psiElement == null) {
fail("Element is not PhpReference.");
}
return psiElement;
}
public void assertCompletionResultEquals(String filename, String complete, String result) {
myFixture.configureByText(filename, complete);
myFixture.completeBasic();
myFixture.checkResult(result);
}
public void assertCompletionResultEquals(@NotNull FileType fileType, @NotNull String contents, @NotNull String result, @NotNull LookupElementInsert.Assert assertion) {
myFixture.configureByText(fileType, contents);
UIUtil.invokeAndWaitIfNeeded(new MyLookupElementConditionalInsertRunnable(assertion));
myFixture.checkResult(result);
}
public void assertCompletionResultEquals(LanguageFileType languageFileType, String complete, String result) {
myFixture.configureByText(languageFileType, complete);
myFixture.completeBasic();
myFixture.checkResult(result);
}
public void assertCheckHighlighting(String filename, String result) {
myFixture.configureByText(filename, result);
myFixture.checkHighlighting();
}
public void assertIndexContains(@NotNull ID<String, ?> id, @NotNull String... keys) {
assertIndex(id, false, keys);
}
public void assertIndexNotContains(@NotNull ID<String, ?> id, @NotNull String... keys) {
assertIndex(id, true, keys);
}
public void assertIndex(@NotNull ID<String, ?> id, boolean notCondition, @NotNull String... keys) {
for (String key : keys) {
final Collection<VirtualFile> virtualFiles = new ArrayList<>();
FileBasedIndex.getInstance().getFilesWithKey(id, new HashSet<>(Collections.singletonList(key)), virtualFile -> {
virtualFiles.add(virtualFile);
return true;
}, GlobalSearchScope.allScope(getProject()));
if(notCondition && !virtualFiles.isEmpty()) {
fail(String.format("Fail that ID '%s' not contains '%s'", id.toString(), key));
} else if(!notCondition && virtualFiles.isEmpty()) {
fail(String.format("Fail that ID '%s' contains '%s'", id.toString(), key));
}
}
}
public void assertIndexContainsKeyWithValue(@NotNull ID<String, String> id, @NotNull String key, @NotNull String value) {
assertContainsElements(FileBasedIndex.getInstance().getValues(id, key, GlobalSearchScope.allScope(getProject())), value);
}
public <T> void assertIndexContainsKeyWithValue(@NotNull ID<String, T> id, @NotNull String key, @NotNull IndexValue.Assert<T> tAssert) {
List<T> values = FileBasedIndex.getInstance().getValues(id, key, GlobalSearchScope.allScope(getProject()));
for (T t : values) {
if(tAssert.match(t)) {
return;
}
}
fail(String.format("Fail that Key '%s' matches on of '%s' values", key, values.size()));
}
public void assertLocalInspectionContains(String filename, String content, String contains) {
Set<String> matches = new HashSet<>();
Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
TextRange textRange = result.getPsiElement().getTextRange();
if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().equals(contains)) {
return;
}
matches.add(result.toString());
}
fail(String.format("Fail matches '%s' with one of %s", contains, matches));
}
public void assertIntentionIsAvailable(LanguageFileType languageFileType, String configureByText, String intentionText) {
myFixture.configureByText(languageFileType, configureByText);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
Set<String> items = new HashSet<>();
for (IntentionAction intentionAction : IntentionManager.getInstance().getIntentionActions()) {
if(!intentionAction.isAvailable(getProject(), getEditor(), psiElement.getContainingFile())) {
continue;
}
String text = intentionAction.getText();
items.add(text);
if(!text.equals(intentionText)) {
continue;
}
return;
}
fail(String.format("Fail intention action '%s' is available in element '%s' with '%s'", intentionText, psiElement.getText(), items));
}
public void assertLocalInspectionNotContains(String filename, String content, String contains) {
Pair<List<ProblemDescriptor>, Integer> localInspectionsAtCaret = getLocalInspectionsAtCaret(filename, content);
for (ProblemDescriptor result : localInspectionsAtCaret.getFirst()) {
TextRange textRange = result.getPsiElement().getTextRange();
if (textRange.contains(localInspectionsAtCaret.getSecond()) && result.toString().contains(contains)) {
fail(String.format("Fail inspection not contains '%s'", contains));
}
}
}
private Pair<List<ProblemDescriptor>, Integer> getLocalInspectionsAtCaret(@NotNull String filename, @NotNull String content) {
PsiElement psiFile = myFixture.configureByText(filename, content);
int caretOffset = myFixture.getCaretOffset();
if(caretOffset <= 0) {
fail("Please provide <caret> tag");
}
ProblemsHolder problemsHolder = new ProblemsHolder(InspectionManager.getInstance(getProject()), psiFile.getContainingFile(), false);
for (LocalInspectionEP localInspectionEP : LocalInspectionEP.LOCAL_INSPECTION.getExtensions()) {
Object object = localInspectionEP.getInstance();
if(!(object instanceof LocalInspectionTool)) {
continue;
}
// fix for: should not be called, use visitFile in createVisitor instead
if(object instanceof JSInspection) {
continue;
}
final PsiElementVisitor psiElementVisitor = ((LocalInspectionTool) object).buildVisitor(problemsHolder, false);
psiFile.acceptChildren(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
psiElementVisitor.visitElement(element);
super.visitElement(element);
}
});
psiElementVisitor.visitFile(psiFile.getContainingFile());
}
return Pair.create(problemsHolder.getResults(), caretOffset);
}
protected void createDummyFiles(String... files) {
for (String file : files) {
myFixture.addFileToProject(file, "");
}
}
private void checkContainsCompletion(String[] lookupStrings) {
completionContainsAssert(lookupStrings);
}
public void assertLineMarker(@NotNull PsiElement psiElement, @NotNull LineMarker.Assert assertMatch) {
final List<PsiElement> elements = collectPsiElementsRecursive(psiElement);
for (LineMarkerProvider lineMarkerProvider : LineMarkerProviders.getInstance().allForLanguage(psiElement.getLanguage())) {
Collection<LineMarkerInfo> lineMarkerInfos = new ArrayList<>();
lineMarkerProvider.collectSlowLineMarkers(elements, lineMarkerInfos);
if(lineMarkerInfos.isEmpty()) {
continue;
}
for (LineMarkerInfo lineMarkerInfo : lineMarkerInfos) {
if(assertMatch.match(lineMarkerInfo)) {
return;
}
}
}
fail(String.format("Fail that '%s' matches on of '%s' PsiElements", assertMatch.getClass(), elements.size()));
}
public void assertLineMarkerIsEmpty(@NotNull PsiElement psiElement) {
final List<PsiElement> elements = collectPsiElementsRecursive(psiElement);
for (LineMarkerProvider lineMarkerProvider : LineMarkerProviders.getInstance().allForLanguage(psiElement.getLanguage())) {
Collection<LineMarkerInfo> lineMarkerInfos = new ArrayList<>();
lineMarkerProvider.collectSlowLineMarkers(elements, lineMarkerInfos);
if(!lineMarkerInfos.isEmpty()) {
fail(String.format("Fail that line marker is empty because it matches '%s'", lineMarkerProvider.getClass()));
}
}
}
public void assertReferenceMatchOnParent(@NotNull String file, @NotNull String contents, @NotNull ElementPattern<?> pattern) {
myFixture.configureByText(file, contents);
assertReferenceMatchOnParent(pattern);
}
public void assertReferenceMatchOnParent(@NotNull FileType fileType, @NotNull String contents, @NotNull ElementPattern<?> pattern) {
myFixture.configureByText(fileType, contents);
assertReferenceMatchOnParent(pattern);
}
private void assertReferenceMatchOnParent(@NotNull ElementPattern<?> pattern) {
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
if(psiElement == null) {
fail("Fail to find element in caret");
}
PsiElement parent = psiElement.getParent();
if(parent == null) {
fail("Fail to find parent element in caret");
}
assertReferences(pattern, parent);
}
public void assertReferenceMatch(@NotNull FileType fileType, @NotNull String contents, @NotNull ElementPattern<?> pattern) {
myFixture.configureByText(fileType, contents);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
if(psiElement == null) {
fail("Fail to find element in caret");
}
assertReferences(pattern, psiElement);
}
private void assertReferences(@NotNull ElementPattern<?> pattern, PsiElement psiElement) {
for (PsiReference reference : psiElement.getReferences()) {
// single resolve; should also match first multi by design
PsiElement element = reference.resolve();
if (pattern.accepts(element)) {
return;
}
// multiResolve support
if(reference instanceof PsiPolyVariantReference) {
for (ResolveResult resolveResult : ((PsiPolyVariantReference) reference).multiResolve(true)) {
if (pattern.accepts(resolveResult.getElement())) {
return;
}
}
}
}
fail(String.format("Fail that '%s' match given pattern", psiElement.toString()));
}
@NotNull
private List<PsiElement> collectPsiElementsRecursive(@NotNull PsiElement psiElement) {
final List<PsiElement> elements = new ArrayList<>();
elements.add(psiElement.getContainingFile());
psiElement.acceptChildren(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement element) {
elements.add(element);
super.visitElement(element);
}
});
return elements;
}
@NotNull
protected PsiElement findElementAt(@NotNull FileType fileType, @NotNull String content) {
myFixture.configureByText(fileType, content);
PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
if(psiElement == null) {
fail("No element at caret found");
}
return psiElement;
}
public static class IndexValue {
public interface Assert<T> {
boolean match(@NotNull T value);
}
}
public static class LineMarker {
public interface Assert {
boolean match(@NotNull LineMarkerInfo markerInfo);
}
public static class ToolTipEqualsAssert implements Assert {
@NotNull
private final String toolTip;
public ToolTipEqualsAssert(@NotNull String toolTip) {
this.toolTip = toolTip;
}
@Override
public boolean match(@NotNull LineMarkerInfo markerInfo) {
return markerInfo.getLineMarkerTooltip() != null && markerInfo.getLineMarkerTooltip().equals(toolTip);
}
}
public static class TargetAcceptsPattern implements Assert {
@NotNull
private final String toolTip;
@NotNull
private final ElementPattern<? extends PsiElement> pattern;
public TargetAcceptsPattern(@NotNull String toolTip, @NotNull ElementPattern<? extends PsiElement> pattern) {
this.toolTip = toolTip;
this.pattern = pattern;
}
@Override
public boolean match(@NotNull LineMarkerInfo markerInfo) {
if(markerInfo.getLineMarkerTooltip() == null || !markerInfo.getLineMarkerTooltip().equals(toolTip)) {
return false;
}
if(!(markerInfo instanceof RelatedItemLineMarkerInfo)) {
return false;
}
for (Object o : ((RelatedItemLineMarkerInfo) markerInfo).createGotoRelatedItems()) {
if(o instanceof GotoRelatedItem && this.pattern.accepts(((GotoRelatedItem) o).getElement())) {
return true;
}
}
return false;
}
}
}
public static class LookupElementInsert {
public interface Assert {
boolean match(@NotNull LookupElement lookupElement);
}
public static class Icon implements Assert {
@NotNull
private final javax.swing.Icon icon;
public Icon(@NotNull javax.swing.Icon icon) {
this.icon = icon;
}
@Override
public boolean match(@NotNull LookupElement lookupElement) {
LookupElementPresentation presentation = new LookupElementPresentation();
lookupElement.renderElement(presentation);
return presentation.getIcon() == this.icon;
}
}
}
public static class LookupElementPresentationAssert {
public interface Assert {
boolean match(@NotNull LookupElementPresentation lookupElement);
}
}
private class MyLookupElementConditionalInsertRunnable implements Runnable {
@NotNull
private final LookupElementInsert.Assert insert;
public MyLookupElementConditionalInsertRunnable(@NotNull LookupElementInsert.Assert insert) {
this.insert = insert;
}
@Override
public void run() {
CommandProcessor.getInstance().executeCommand(getProject(), () -> {
final CodeCompletionHandlerBase handler = new CodeCompletionHandlerBase(CompletionType.BASIC) {
@Override
protected void completionFinished(final CompletionProgressIndicator indicator, boolean hasModifiers) {
// find our lookup element
final LookupElement lookupElement = ContainerUtil.find(
indicator.getLookup().getItems(),
insert::match
);
if(lookupElement == null) {
fail("No matching lookup element found");
}
// overwrite behavior and force completion + insertHandler
CommandProcessor.getInstance().executeCommand(indicator.getProject(), () -> {
CommandProcessor.getInstance().setCurrentCommandGroupId("Completion" + indicator.hashCode());
indicator.getLookup().finishLookup(Lookup.AUTO_INSERT_SELECT_CHAR, lookupElement);
}, "Autocompletion", null);
}
};
handler.invokeCompletion(getProject(), getEditor());
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
}, null, null);
}
}
}