|
| 1 | +package de.espend.idea.php.phpunit.linemarker; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.daemon.LineMarkerInfo; |
| 4 | +import com.intellij.codeInsight.daemon.LineMarkerProvider; |
| 5 | +import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.jetbrains.php.PhpIcons; |
| 8 | +import com.jetbrains.php.PhpIndex; |
| 9 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 10 | +import com.jetbrains.php.phpunit.PhpUnitUtil; |
| 11 | +import de.espend.idea.php.phpunit.utils.PhpElementsUtil; |
| 12 | +import org.apache.commons.lang.StringUtils; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +/** |
| 19 | + * Attach a test class to a given class name |
| 20 | + * |
| 21 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 22 | + */ |
| 23 | +public class RelatedTestCaseLineMarkerProvider implements LineMarkerProvider { |
| 24 | + @Nullable |
| 25 | + @Override |
| 26 | + public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNull Collection<LineMarkerInfo> result) { |
| 32 | + // we need project element; so get it from first item |
| 33 | + if(psiElements.size() == 0) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + for(PsiElement psiElement: psiElements) { |
| 38 | + if (PhpElementsUtil.getClassNamePattern().accepts(psiElement)) { |
| 39 | + result.addAll(visitClassName(psiElement)); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private Collection<LineMarkerInfo> visitClassName(@NotNull PsiElement psiElement) { |
| 45 | + PsiElement phpClass = psiElement.getContext(); |
| 46 | + if (!(phpClass instanceof PhpClass) || PhpUnitUtil.isTestClass((PhpClass) phpClass)) { |
| 47 | + return Collections.emptyList(); |
| 48 | + } |
| 49 | + |
| 50 | + String className = StringUtils.stripStart(((PhpClass) phpClass).getPresentableFQN(), "\\"); |
| 51 | + |
| 52 | + Collection<String> testClasses = getTestClassesViaPath(className); |
| 53 | + if (testClasses.size() == 0) { |
| 54 | + return Collections.emptyList(); |
| 55 | + } |
| 56 | + |
| 57 | + PhpIndex instance = PhpIndex.getInstance(psiElement.getProject()); |
| 58 | + |
| 59 | + Set<PhpClass> phpClasses = new HashSet<>(); |
| 60 | + for (String testClass : testClasses) { |
| 61 | + Collection<PhpClass> anyByFQN = instance.getAnyByFQN("\\" + testClass); |
| 62 | + for (PhpClass aClass : anyByFQN) { |
| 63 | + if (PhpUnitUtil.isTestClass(aClass)) { |
| 64 | + phpClasses.add(aClass); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (phpClasses.size() == 0) { |
| 70 | + return Collections.emptyList(); |
| 71 | + } |
| 72 | + |
| 73 | + NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(PhpIcons.PHP_TEST_CLASS) |
| 74 | + .setTargets(phpClasses) |
| 75 | + .setTooltipText("Navigate to Test Class"); |
| 76 | + |
| 77 | + return Collections.singletonList(builder.createLineMarkerInfo(psiElement)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Try you find a test class which based on the class namespace |
| 82 | + * |
| 83 | + * Extend class name with a test namespace in between: |
| 84 | + * - App\Foobar => App\FoobarTest |
| 85 | + * - App\Foobar => App\Test\FoobarTest |
| 86 | + * - App\Foobar => App\Tests\FoobarTest |
| 87 | + * - App\Foobar => Test\App\FoobarTest |
| 88 | + */ |
| 89 | + @NotNull |
| 90 | + private static Collection<String> getTestClassesViaPath(@NotNull String className) { |
| 91 | + List<String> classNameParts = Arrays.asList(className.split("\\\\")); |
| 92 | + |
| 93 | + classNameParts.set(classNameParts.size() - 1, classNameParts.get(classNameParts.size() - 1) + "Test"); |
| 94 | + |
| 95 | + Collection<String> testClasses = new HashSet<>(); |
| 96 | + testClasses.add(className + "Test"); |
| 97 | + for (int i = 0; i < classNameParts.size(); i++) { |
| 98 | + List<String> strings = new ArrayList<>(classNameParts); |
| 99 | + strings.add(i, "Test"); |
| 100 | + testClasses.add(StringUtils.join(strings, "\\")); |
| 101 | + |
| 102 | + List<String> strings2 = new ArrayList<>(classNameParts); |
| 103 | + strings2.add(i, "Tests"); |
| 104 | + testClasses.add(StringUtils.join(strings2, "\\")); |
| 105 | + } |
| 106 | + |
| 107 | + return testClasses; |
| 108 | + } |
| 109 | +} |
0 commit comments