Skip to content

Commit

Permalink
fix(runner): An unexpected exception occurred, causing the shire proc…
Browse files Browse the repository at this point in the history
…ess cannot be canceled
  • Loading branch information
lkk214 committed Sep 6, 2024
1 parent a47db5d commit 7eba18c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.awt.event.KeyEvent
import javax.swing.KeyStroke

open class ChatCompletionTask(private val request: CodeCompletionRequest) :
Task.Backgroundable(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name")) {
ShireInteractionTask(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name"), request.postExecute) {
private val logger = logger<ChatCompletionTask>()

private var isCanceled: Boolean = false
Expand Down Expand Up @@ -87,7 +87,7 @@ open class ChatCompletionTask(private val request: CodeCompletionRequest) :
indicator.fraction = 0.8
logger.info("Suggestion: $suggestion")

val textRange: TextRange = TextRange(modifyStart, modifyEnd)
val textRange = TextRange(modifyStart, modifyEnd)

request.postExecute.invoke(suggestion.toString(), textRange)
indicator.fraction = 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.launch

class CodeCompletionTask(private val request: CodeCompletionRequest) :
Task.Backgroundable(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name")) {
ShireInteractionTask(request.project, ShireCoreBundle.message("intentions.chat.code.complete.name"), request.postExecute) {

private var isCanceled: Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ open class FileGenerateTask(
val fileName: String?,
private val codeOnly: Boolean = false,
private val taskName: String = ShireCoreBundle.message("intentions.request.background.process.title"),
val postExecute: PostFunction,
postExecute: PostFunction,
) :
Task.Backgroundable(project, taskName) {
ShireInteractionTask(project, taskName, postExecute) {
private val projectRoot = project.guessProjectDir()!!

override fun run(indicator: ProgressIndicator) {
Expand Down Expand Up @@ -74,7 +74,7 @@ open class FileGenerateTask(
refreshAndOpenInEditor(Path(projectRoot.path), projectRoot)
}

postExecute.invoke(result, null)
postExecute?.invoke(result, null)
}

private fun refreshAndOpenInEditor(file: Path, parentDir: VirtualFile) = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.phodal.shirecore.config.interaction.task

import com.intellij.openapi.progress.Task.Backgroundable
import com.intellij.openapi.project.Project
import com.phodal.shirecore.config.interaction.PostFunction
import java.util.concurrent.CompletableFuture

/**
* @author lk
*/
abstract class ShireInteractionTask(project: Project, taskName: String, val postExecute: PostFunction?): Backgroundable(project, taskName) {

/**
* An unexpected exception occurred, causing the shire process cannot be canceled,
* postExecute was not executed,it may have used the [CompletableFuture].
*/
override fun onThrowable(error: Throwable) {
super.onThrowable(error)
postExecute?.invoke(null, null)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.build.process.BuildProcessHandler
import java.io.OutputStream

class ShireProcessHandler(private val myExecutionName: String) : BuildProcessHandler() {
override fun detachIsDefault(): Boolean = false
override fun detachIsDefault(): Boolean = true
override fun destroyProcessImpl() = Unit
override fun detachProcessImpl() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ShireDefaultLlmExecutor(
postFunction(response, null)
context.processHandler.detachProcess()
}
}, ModalityState.NON_MODAL)
}, ModalityState.nonModal())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ class ShireRunner(
currentFile = file,
editor = editor,
compiledVariables = compiledVariables,
)
PostCodeHandleContext.putData(context)
).also { PostCodeHandleContext.putData(it) }

hobbitHole?.setupStreamingEndProcessor(project, context)

}
Expand Down

0 comments on commit 7eba18c

Please sign in to comment.