Skip to content

Commit eab438b

Browse files
committed
feat(rust): add RustTestContextProvider and update WriteTestService
This commit adds a new file `RustTestContextProvider.kt` in the `rust` package. The `RustTestContextProvider` class extends the `WriteTestService` class and overrides several methods. It provides implementation for the `runConfigurationClass` method, which returns the run configuration class for the project. It also implements the `isApplicable` method to check if the given element is of `RsLanguage`. The commit also modifies the `WriteTestService.kt` file by adding documentation comments for the `runConfigurationClass`, `findOrCreateTestFile`, and `lookupRelevantClass` methods. These comments provide detailed explanations of the purpose and usage of these methods.
1 parent b9ab08a commit eab438b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cc.unitmesh.rust.provider
2+
3+
import cc.unitmesh.devti.context.ClassContext
4+
import cc.unitmesh.devti.provider.WriteTestService
5+
import cc.unitmesh.devti.provider.context.TestFileContext
6+
import cc.unitmesh.rust.context.RustMethodContextBuilder
7+
import com.intellij.execution.configurations.RunProfile
8+
import com.intellij.openapi.project.Project
9+
import com.intellij.psi.PsiElement
10+
import com.intellij.psi.PsiFile
11+
import com.intellij.psi.util.PsiTreeUtil
12+
import org.rust.cargo.runconfig.command.CargoCommandConfiguration
13+
import org.rust.lang.RsLanguage
14+
import org.rust.lang.core.psi.RsFunction
15+
import org.rust.lang.core.psi.RsUseItem
16+
17+
class RustTestContextProvider : WriteTestService() {
18+
override fun runConfigurationClass(project: Project): Class<out RunProfile> = CargoCommandConfiguration::class.java
19+
20+
override fun isApplicable(element: PsiElement): Boolean = element.language is RsLanguage
21+
22+
override fun findOrCreateTestFile(sourceFile: PsiFile, project: Project, element: PsiElement): TestFileContext? {
23+
val currentObject = when (element) {
24+
is RsFunction -> {
25+
RustMethodContextBuilder().getMethodContext(element, true, false)?.format()
26+
}
27+
28+
else -> null
29+
} ?: return null
30+
31+
val imports = PsiTreeUtil.getChildrenOfTypeAsList(sourceFile, RsUseItem::class.java).map {
32+
it.text
33+
}
34+
35+
return TestFileContext(
36+
false,
37+
sourceFile.virtualFile,
38+
listOf(),
39+
"",
40+
RsLanguage,
41+
currentObject,
42+
imports
43+
)
44+
}
45+
46+
override fun lookupRelevantClass(project: Project, element: PsiElement): List<ClassContext> {
47+
return listOf()
48+
}
49+
50+
}

src/main/kotlin/cc/unitmesh/devti/provider/WriteTestService.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,36 @@ abstract class WriteTestService : LazyExtensionInstance<WriteTestService>() {
3434
var implementationClass: String? = null
3535

3636
override fun getImplementationClassName(): String? = implementationClass
37+
/**
38+
* Retrieves the run configuration class for the given project.
39+
*
40+
* @param project The project for which to retrieve the run configuration class.
41+
* @return The run configuration class for the project.
42+
*/
3743
abstract fun runConfigurationClass(project: Project): Class<out RunProfile>
3844
abstract fun isApplicable(element: PsiElement): Boolean
45+
/**
46+
* Finds or creates a test file for the given source file, project, and element.
47+
*
48+
* @param sourceFile The source file for which to find or create a test file.
49+
* @param project The project in which the test file should be created.
50+
* @param element The element for which the test file should be created.
51+
* @return The TestFileContext object representing the found or created test file, or null if it could not be found or created.
52+
*
53+
* This method is responsible for locating an existing test file associated with the given source file and element,
54+
* or creating a new test file if one does not already exist. The test file is typically used for unit testing purposes.
55+
* The source file, project, and element parameters are used to determine the context in which the test file should be created.
56+
* If a test file is found or created successfully, a TestFileContext object representing the test file is returned.
57+
* If a test file cannot be found or created, null is returned.
58+
*/
3959
abstract fun findOrCreateTestFile(sourceFile: PsiFile, project: Project, element: PsiElement): TestFileContext?
60+
/**
61+
* Looks up the relevant classes in the project for the given element.
62+
*
63+
* @param project the project in which to perform the lookup
64+
* @param element the element for which to find the relevant classes
65+
* @return a list of ClassContext objects representing the relevant classes found in the project
66+
*/
4067
abstract fun lookupRelevantClass(project: Project, element: PsiElement): List<ClassContext>
4168

4269
fun runTest(project: Project, virtualFile: VirtualFile) {

0 commit comments

Comments
 (0)