Skip to content

Commit

Permalink
feat(test): init intention
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 24, 2023
1 parent f797a1c commit 380a972
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,13 @@ examples:
}

private fun addTestContext() {
val techStackProvider = TechStackProvider.stack(file?.language?.displayName ?: "")
val techStacks = techStackProvider!!.prepareLibrary()
val techStackProvider = TechStackProvider.stack(file?.language?.displayName ?: "") ?: return
val techStacks = techStackProvider.prepareLibrary()
if (techStacks.controllerString().isEmpty() && techStacks.serviceString().isEmpty()) {
additionContext = "no test context"
return
}

when {
isController() -> {
additionContext = "// tech stacks: " + techStacks.controllerString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import org.jetbrains.plugins.gradle.util.GradleConstants

class JvmTechStackService: TechStackProvider() {
override fun prepareLibrary(): TestStack {
val project = ProjectManager.getInstance().defaultProject
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return TestStack()
val basePath = project.basePath ?: return TestStack()
val projectData = ProjectDataManager.getInstance().getExternalProjectData(
project, GradleConstants.SYSTEM_ID, project.basePath!!
project, GradleConstants.SYSTEM_ID, basePath
)

val testStack = TestStack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import cc.unitmesh.devti.gui.chat.ChatActionType
class WriteTestAction : ChatBaseAction() {
override fun getActionType(): ChatActionType {
return ChatActionType.WRITE_TEST

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cc.unitmesh.devti.editor.inlay

import cc.unitmesh.devti.editor.presentation.LLMInlayRenderer
import cc.unitmesh.devti.intentions.editor.CodeCompletionTask
import cc.unitmesh.devti.intentions.editor.CompletionTaskRequest
import cc.unitmesh.devti.intentions.task.CodeCompletionTask
import cc.unitmesh.devti.intentions.task.CompletionTaskRequest
import com.intellij.injected.editor.EditorWindow
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum class ChatActionType {
REVIEW -> "Code Review"
REFACTOR -> "Refactor the following code"
CODE_COMPLETE -> "Complete java code, return rest code, no explaining"
WRITE_TEST -> "Write test for following code"
WRITE_TEST -> "Write unit test for following code"
FIX_ISSUE -> "Help me fix this issue"
GEN_COMMIT_MESSAGE -> "generate commit message"
CREATE_DDL -> "create ddl"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.unitmesh.devti.intentions.editor
package cc.unitmesh.devti.intentions

import cc.unitmesh.devti.gui.chat.*
import cc.unitmesh.devti.intentions.editor.sendToChat
import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.lang.injection.InjectedLanguageManager
Expand Down Expand Up @@ -33,7 +34,7 @@ abstract class AbstractChatIntention : IntentionAction {
if (editor == null || file == null) return

var selectedText = editor.selectionModel.selectedText
val elementToExplain = getElementToExplain(project, editor)
val elementToExplain = getElementToAction(project, editor)

if (selectedText == null) {
if (elementToExplain == null) {
Expand Down Expand Up @@ -65,7 +66,7 @@ abstract class AbstractChatIntention : IntentionAction {
* @param editor the editor in which the element is located (nullable)
* @return the PsiElement to explain, or null if either the project or editor is null, or if no element is found
*/
protected open fun getElementToExplain(project: Project?, editor: Editor?): PsiElement? {
protected open fun getElementToAction(project: Project?, editor: Editor?): PsiElement? {
if (project == null || editor == null) return null

val element = PsiUtilBase.getElementAtCaret(editor) ?: return null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cc.unitmesh.devti.intentions.editor
package cc.unitmesh.devti.intentions

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.editor.inlay.LLMInlayManager
import cc.unitmesh.devti.intentions.task.CompletionTaskRequest
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actions.EditorActionUtil
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.unitmesh.devti.intentions.editor
package cc.unitmesh.devti.intentions

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.chat.ChatActionType
Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/intentions/WriteTestIntention.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cc.unitmesh.devti.intentions

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.intentions.editor.sendToChat
import cc.unitmesh.devti.provider.ContextPrompter
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile

class WriteTestIntention : AbstractChatIntention() {
override fun getText(): String = AutoDevBundle.message("intentions.chat.code.test.name")
override fun getFamilyName(): String = AutoDevBundle.message("intentions.chat.code.test.family.name")

override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (editor == null || file == null) return

val element = getElementToAction(project, editor) ?: return
val selectedText = element.text

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

prompter?.initContext(actionType, selectedText, file, project, editor.caretModel.offset)
sendToChat(project, actionType, prompter!!)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cc.unitmesh.devti.intentions.editor
package cc.unitmesh.devti.intentions.task

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.context.chunks.SimilarChunksWithPaths
import cc.unitmesh.devti.llms.ConnectorFactory
import cc.unitmesh.devti.editor.LLMCoroutineScopeService
import cc.unitmesh.devti.intentions.CodeCompletionIntention
import com.intellij.lang.LanguageCommenters
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.invokeLater
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ abstract class TechStackProvider : LazyExtensionInstance<TechStackProvider>() {
companion object {
private val EP_NAME: ExtensionPointName<TechStackProvider> =
ExtensionPointName.create("cc.unitmesh.techStackProvider")

fun stack(lang: String): TechStackProvider? {
val extensionList = EP_NAME.extensionList
val providers = extensionList.filter {
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/META-INF/autodev-core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@

<extensions defaultExtensionNs="cc.unitmesh">
<aiAssistantIntention>
<className>cc.unitmesh.devti.intentions.editor.CodeCompletionIntention</className>
<className>cc.unitmesh.devti.intentions.CodeCompletionIntention</className>
<categoryKey>Intention.category.llm</categoryKey>
</aiAssistantIntention>
<aiAssistantIntention>
<className>cc.unitmesh.devti.intentions.editor.RefactorThisIntention</className>
<className>cc.unitmesh.devti.intentions.WriteTestIntention</className>
<categoryKey>Intention.category.llm</categoryKey>
</aiAssistantIntention>
<aiAssistantIntention>
<className>cc.unitmesh.devti.intentions.RefactorThisIntention</className>
<categoryKey>Intention.category.llm</categoryKey>
</aiAssistantIntention>
</extensions>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/AutoDevBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ intentions.chat.code.complete.name=AutoDev: Code complete
intentions.chat.code.complete.family.name=Code complete
intentions.chat.code.refactor.name=AutoDev: Refactor this
intentions.chat.code.refactor.family.name=Refactor this
intentions.chat.code.test.name=AutoDev: Test this
intentions.chat.code.test.family.name=Test this
intentions.assistant.name=AutoDev AI Assistant
Intention.category.llm=AutoDev LLM
intentions.assistant.popup.title=AutoDev AI Assistant

0 comments on commit 380a972

Please sign in to comment.