@@ -26,6 +26,7 @@ import com.intellij.openapi.editor.ScrollType
26
26
import com.intellij.openapi.fileEditor.FileEditor
27
27
import com.intellij.openapi.fileEditor.FileEditorManager
28
28
import com.intellij.openapi.progress.ProgressIndicator
29
+ import com.intellij.openapi.progress.ProcessCanceledException
29
30
import com.intellij.openapi.progress.Task
30
31
import com.intellij.openapi.project.DumbService
31
32
import com.intellij.openapi.project.Project
@@ -53,10 +54,16 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
53
54
indicator.fraction = 0.1
54
55
indicator.text = AutoDevBundle .message(" intentions.chat.code.test.step.prepare-context" )
55
56
57
+ // Check for cancellation early
58
+ indicator.checkCanceled()
59
+
56
60
AutoDevStatusService .notifyApplication(AutoDevStatus .InProgress )
57
61
val testContext = autoTestService.findOrCreateTestFile(request.file, request.project, request.element)
58
62
DumbService .getInstance(request.project).waitForSmartMode()
59
63
64
+ // Check for cancellation after waiting for smart mode
65
+ indicator.checkCanceled()
66
+
60
67
if (testContext == null ) {
61
68
AutoDevStatusService .notifyApplication(AutoDevStatus .Error )
62
69
logger.error(" Failed to create test file for: ${request.file} " )
@@ -66,16 +73,27 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
66
73
indicator.text = AutoDevBundle .message(" intentions.chat.code.test.step.collect-context" )
67
74
indicator.fraction = 0.3
68
75
76
+ // Check for cancellation before collecting context
77
+ indicator.checkCanceled()
78
+
69
79
val testPromptContext = TestCodeGenContext ()
70
80
71
81
val creationContext =
72
82
ChatCreationContext (ChatOrigin .Intention , actionType, request.file, listOf (), element = request.element)
73
83
74
84
val contextItems: List <ChatContextItem > = runBlocking {
85
+ // Check for cancellation in the blocking context
86
+ if (indicator.isCanceled) {
87
+ throw ProcessCanceledException ()
88
+ }
75
89
ChatContextProvider .collectChatContextList(request.project, creationContext)
76
90
}
77
91
78
92
testPromptContext.frameworkContext = contextItems.joinToString(" \n " , transform = ChatContextItem ::text)
93
+
94
+ // Check for cancellation before read actions
95
+ indicator.checkCanceled()
96
+
79
97
ReadAction .compute<Unit , Throwable > {
80
98
if (testContext.relatedClasses.isNotEmpty()) {
81
99
testPromptContext.relatedClasses = testContext.relatedClasses.joinToString(" \n " ) {
@@ -113,6 +131,9 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
113
131
testPromptContext.isNewFile = testContext.isNewFile
114
132
testPromptContext.extContext = getCustomAgentTestContext(testPromptContext)
115
133
134
+ // Check for cancellation before template rendering
135
+ indicator.checkCanceled()
136
+
116
137
templateRender.context = testPromptContext
117
138
val prompter = templateRender.renderTemplate(template)
118
139
@@ -121,6 +142,9 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
121
142
indicator.fraction = 0.6
122
143
indicator.text = AutoDevBundle .message(" intentions.request.background.process.title" )
123
144
145
+ // Check for cancellation before LLM request
146
+ indicator.checkCanceled()
147
+
124
148
val flow: Flow <String > = try {
125
149
LlmFactory .create(request.project).stream(prompter, " " , false )
126
150
} catch (e: Exception ) {
@@ -130,13 +154,28 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
130
154
}
131
155
132
156
runBlocking {
133
- writeTestToFile(request.project, flow, testContext)
157
+ // Check for cancellation before writing to file
158
+ if (indicator.isCanceled) {
159
+ throw ProcessCanceledException ()
160
+ }
161
+
162
+ writeTestToFile(request.project, flow, testContext, indicator)
163
+
164
+ // Check for cancellation before verification
165
+ if (indicator.isCanceled) {
166
+ throw ProcessCanceledException ()
167
+ }
134
168
135
169
indicator.fraction = 1.0
136
170
indicator.text = AutoDevBundle .message(" intentions.chat.code.test.verify" )
137
171
138
172
try {
139
173
autoTestService.collectSyntaxError(testContext.outputFile, request.project) {
174
+ // Check for cancellation before fixing syntax errors
175
+ if (indicator.isCanceled) {
176
+ throw ProcessCanceledException ()
177
+ }
178
+
140
179
autoTestService.tryFixSyntaxError(testContext.outputFile, request.project, it)
141
180
142
181
if (it.isNotEmpty()) {
@@ -146,9 +185,15 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
146
185
)
147
186
indicator.fraction = 1.0
148
187
} else {
149
- autoTestService.runFile(request.project, testContext.outputFile, testContext.testElement, false )
188
+ // Check for cancellation before running tests
189
+ if (! indicator.isCanceled) {
190
+ autoTestService.runFile(request.project, testContext.outputFile, testContext.testElement, false )
191
+ }
150
192
}
151
193
}
194
+ } catch (e: ProcessCanceledException ) {
195
+ // Re-throw cancellation exception
196
+ throw e
152
197
} catch (e: Exception ) {
153
198
AutoDevStatusService .notifyApplication(AutoDevStatus .Ready )
154
199
indicator.fraction = 1.0
@@ -169,6 +214,7 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
169
214
project : Project ,
170
215
flow : Flow <String >,
171
216
context : TestFileContext ,
217
+ indicator : ProgressIndicator
172
218
) {
173
219
val fileEditorManager = FileEditorManager .getInstance(project)
174
220
var editors: Array <FileEditor > = emptyArray()
@@ -183,6 +229,11 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
183
229
val editor = fileEditorManager.selectedTextEditor
184
230
185
231
flow.collect {
232
+ // Check for cancellation during flow collection
233
+ if (indicator.isCanceled) {
234
+ throw ProcessCanceledException ()
235
+ }
236
+
186
237
suggestion.append(it)
187
238
val codeBlocks = MarkdownCodeHelper .parseCodeFromString(suggestion.toString())
188
239
codeBlocks.forEach {
@@ -200,6 +251,10 @@ class TestCodeGenTask(val request: TestCodeGenRequest, displayMessage: String) :
200
251
201
252
val suggestion = StringBuilder ()
202
253
flow.collect {
254
+ // Check for cancellation during flow collection
255
+ if (indicator.isCanceled) {
256
+ throw ProcessCanceledException ()
257
+ }
203
258
suggestion.append(it)
204
259
}
205
260
0 commit comments