11package com.github.xepozz.testo.index
22
3+ import com.github.xepozz.testo.isTestoClass
34import com.intellij.openapi.util.Pair
45import com.intellij.openapi.util.text.StringUtil
5- import com.intellij.testIntegration.TestFinderHelper
66import com.intellij.util.indexing.DataIndexer
77import com.intellij.util.indexing.FileBasedIndex
88import com.intellij.util.indexing.FileBasedIndexExtension
@@ -16,6 +16,7 @@ import com.jetbrains.php.lang.psi.elements.Method
1616import com.jetbrains.php.lang.psi.elements.PhpAttribute
1717import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionArgument
1818import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionScalarArgument
19+ import com.jetbrains.rd.util.printlnError
1920import java.io.DataInput
2021import java.io.DataOutput
2122import java.io.IOException
@@ -26,18 +27,16 @@ class TestoDataProvidersIndex : FileBasedIndexExtension<String, TestoDataProvide
2627 override fun getName () = KEY
2728
2829 override fun getIndexer () = DataIndexer <String , TestoDataProvidersIndexType , FileContent ?> { inputData ->
29- val map: MutableMap <String , TestoDataProvidersIndexType > = HashMap ()
30+ val map = mutableMapOf <String , TestoDataProvidersIndexType >()
3031
3132 for (testClass in PhpPsiUtil .findAllClasses(inputData.psiFile)) {
32- if (TestFinderHelper .isTest(testClass)) {
33- for (method in testClass.ownMethods) {
34- val dataProviders = mutableSetOf<Pair <String , String >>()
35- dataProviders.addAll(getDataProvidersFromAttributes(method))
36-
37- for (dataProvider in dataProviders) {
38- map.computeIfAbsent(dataProvider.second) { mutableSetOf () }
39- .add(DataProviderUsage (testClass.fqn, method.name, dataProvider.first))
40- }
33+ if (! testClass.isTestoClass()) continue
34+ for (method in testClass.ownMethods) {
35+ val dataProviders = getDataProvidersFromAttributes(method)
36+
37+ for (dataProvider in dataProviders) {
38+ map.computeIfAbsent(dataProvider.second) { mutableSetOf () }
39+ .add(DataProviderUsage (testClass.fqn, method.name, dataProvider.first))
4140 }
4241 }
4342 }
@@ -108,14 +107,42 @@ class TestoDataProvidersIndex : FileBasedIndexExtension<String, TestoDataProvide
108107 private fun getDataProvidersFromAttributes (method : Method ): MutableSet <Pair <String , String >> {
109108 val result = mutableSetOf<Pair <String , String >>()
110109
110+ val targetFQN = method.containingClass?.fqn ? : method.fqn
111+
111112 for (dataProvider in method.getAttributes(PHPUNIT_DATA_PROVIDER_ATTRIBUTE )) {
112113 val argument = getAttributeArgument(dataProvider, " provider" , 0 ) ? : continue
113114 val methodNameArg = argument as ? PhpExpectedFunctionScalarArgument ? : continue
114- val containingClass = method.containingClass ? : continue
115- if (methodNameArg.isStringLiteral) {
116- result.add(
117- Pair .create(containingClass.fqn, StringUtil .unquoteString(methodNameArg.value))
115+ val attributeValue = methodNameArg.value
116+
117+ when {
118+ methodNameArg.isStringLiteral -> result.add(
119+ Pair .create(targetFQN, StringUtil .unquoteString(attributeValue))
118120 )
121+
122+ attributeValue.startsWith(" [" ) && attributeValue.endsWith(" ]" ) -> {
123+ // todo: replace with PSI creation
124+ val classMethodPair = attributeValue
125+ .substring(1 , attributeValue.length - 1 )
126+ .split(" ," )
127+ .map { it.trim() }
128+ if (classMethodPair.size != 2 ) continue
129+
130+ val classFQN = when {
131+ classMethodPair.first() in arrayOf(" self::class" , " static::class" ) -> targetFQN
132+ else -> classMethodPair.first()
133+ }
134+
135+ result.add(
136+ Pair .create(classFQN, StringUtil .unquoteString(classMethodPair.last()))
137+ )
138+ }
139+
140+ else -> {
141+ printlnError(" Unknown data provider type: $attributeValue " )
142+ // result.add(
143+ // Pair.create(targetFQN, attributeValue)
144+ // )
145+ }
119146 }
120147 }
121148
0 commit comments