Skip to content

Commit

Permalink
feat(test): make test file to project
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 26, 2023
1 parent 62111fa commit 628c6ee
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
@@ -1,17 +1,21 @@
package cc.unitmesh.idea.provider

import cc.unitmesh.devti.provider.TestContextProvider
import cc.unitmesh.devti.provider.TestFileContext
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiJavaFile

class JavaTestContextProvider : TestContextProvider() {

override fun prepareTestFile(sourceFile: PsiFile, project: Project): VirtualFile? {
override fun prepareTestFile(sourceFile: PsiFile, project: Project): TestFileContext? {
val sourceFilePath = sourceFile.virtualFile
val sourceDir = sourceFilePath.parent

val packageName = (sourceFile as PsiJavaFile).packageName

// Check if the source file is in the src/main/java directory
if (!sourceDir?.path?.contains("/src/main/java/")!!) {
// Not in the src/main/java directory, return null (cannot find test directory)
Expand All @@ -27,7 +31,8 @@ class JavaTestContextProvider : TestContextProvider() {
val testDirCreated = LocalFileSystem.getInstance().refreshAndFindFileByPath(testDirPath)
return if (testDirCreated != null) {
// Successfully created the test directory
createTestFile(sourceFile, testDirCreated)
val targetFile = createTestFile(sourceFile, testDirCreated, packageName)
TestFileContext(true, targetFile)
} else {
// Failed to create the test directory, return null
null
Expand All @@ -39,21 +44,23 @@ class JavaTestContextProvider : TestContextProvider() {
val testFile = LocalFileSystem.getInstance().findFileByPath(testFilePath)

return if (testFile != null) {
testFile
TestFileContext(false, testFile)
} else {
createTestFile(sourceFile, testDir)
val targetFile = createTestFile(sourceFile, testDir, packageName)
TestFileContext(true, targetFile)
}
}

override fun insertTestMethod(methodName: String, code: String): String {
TODO("Not yet implemented")
}

private fun createTestFile(sourceFile: PsiFile, testDir: VirtualFile): VirtualFile? {
private fun createTestFile(sourceFile: PsiFile, testDir: VirtualFile, packageName: String): VirtualFile {
// Create the test file content based on the source file
val sourceFileName = sourceFile.name
val testFileName = sourceFileName.replace(".java", "Test.java")
val testFileContent = "<AutoDevPlaceHolder>"
val testFileContent = """package $packageName;
|$AUTO_DEV_PLACEHOLDER""".trimMargin()

// Create the test file in the test directory
val testFile = testDir.createChildData(this, testFileName)
Expand Down
Expand Up @@ -40,10 +40,7 @@ abstract class AbstractChatIntention : IntentionAction {
if (elementToExplain == null) {
return
}
val startOffset = elementToExplain.textRange.startOffset
val endOffset = elementToExplain.textRange.endOffset

editor.selectionModel.setSelection(startOffset, endOffset)
selectElement(elementToExplain, editor)
selectedText = editor.selectionModel.selectedText
}

Expand All @@ -59,6 +56,13 @@ abstract class AbstractChatIntention : IntentionAction {
sendToChat(project, actionType, prompter!!)
}

protected fun selectElement(elementToExplain: PsiElement, editor: Editor) {
val startOffset = elementToExplain.textRange.startOffset
val endOffset = elementToExplain.textRange.endOffset

editor.selectionModel.setSelection(startOffset, endOffset)
}

/**
* Returns the PsiElement to explain in the given project and editor.
*
Expand Down
30 changes: 25 additions & 5 deletions src/main/kotlin/cc/unitmesh/devti/intentions/WriteTestIntention.kt
@@ -1,14 +1,21 @@
package cc.unitmesh.devti.intentions

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.editor.LLMCoroutineScopeService
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.intentions.editor.sendToChat
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TestContextProvider
import cc.unitmesh.devti.provider.context.ChatContextProvider
import cc.unitmesh.devti.provider.context.ChatCreationContext
import cc.unitmesh.devti.provider.context.ChatOrigin
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class WriteTestIntention : AbstractChatIntention() {
override fun getText(): String = AutoDevBundle.message("intentions.chat.code.test.name")
Expand All @@ -18,18 +25,31 @@ class WriteTestIntention : AbstractChatIntention() {
if (editor == null || file == null) return

val element = getElementToAction(project, editor) ?: return
selectElement(element, editor)

val selectedText = element.text

val prompter = ContextPrompter.prompter(file.language.displayName)
val actionType = ChatActionType.WRITE_TEST

val lang = file.language.displayName

WriteAction.runAndWait<Throwable> {
val context = TestContextProvider.context(lang)?.prepareTestFile(file, project)
LLMCoroutineScopeService.scope(project).launch {
WriteAction.runAndWait<Throwable> {
val testContext = TestContextProvider.context(lang)?.prepareTestFile(file, project)
if (testContext == null) {
logger<WriteTestIntention>().error("Failed to create test file for: $file")
return@runAndWait
}

runBlocking {
val creationContext = ChatCreationContext(ChatOrigin.Intention, actionType, file)
ChatContextProvider.collectChatContextList(project, creationContext)

prompter.initContext(actionType, selectedText, file, project, editor.caretModel.offset)
sendToChat(project, actionType, prompter)
}
}
}

prompter?.initContext(actionType, selectedText, file, project, editor.caretModel.offset)
sendToChat(project, actionType, prompter!!)
}
}
Expand Up @@ -7,7 +7,14 @@ import com.intellij.psi.PsiFile
import com.intellij.serviceContainer.LazyExtensionInstance
import com.intellij.util.xmlb.annotations.Attribute

data class TestFileContext(
val isNewFile: Boolean,
val file: VirtualFile,
)

abstract class TestContextProvider : LazyExtensionInstance<TestContextProvider>() {
val AUTO_DEV_PLACEHOLDER = "<AUTO_DEV_PLACEHOLDER>"

@Attribute("language")
var language: String? = null

Expand All @@ -18,7 +25,7 @@ abstract class TestContextProvider : LazyExtensionInstance<TestContextProvider>(
return implementationClass
}

abstract fun prepareTestFile(sourceFile: PsiFile, project: Project): VirtualFile?
abstract fun prepareTestFile(sourceFile: PsiFile, project: Project): TestFileContext?

abstract fun insertTestMethod(methodName: String, code: String): String

Expand Down
Expand Up @@ -2,5 +2,5 @@ package cc.unitmesh.devti.provider.context

enum class ChatOrigin {
ChatAction,
CustomIntention
Intention
}

0 comments on commit 628c6ee

Please sign in to comment.