@@ -8,14 +8,21 @@ import cc.unitmesh.devti.util.AutoDevCoroutineScope
8
8
import com.intellij.icons.AllIcons
9
9
import com.intellij.ide.KeyboardAwareFocusOwner
10
10
import com.intellij.openapi.Disposable
11
+ import com.intellij.openapi.actionSystem.DataContext
12
+ import com.intellij.openapi.actionSystem.IdeActions
11
13
import com.intellij.openapi.actionSystem.Presentation
12
14
import com.intellij.openapi.actionSystem.impl.ActionButton
13
15
import com.intellij.openapi.application.ApplicationManager
14
16
import com.intellij.openapi.application.invokeLater
15
17
import com.intellij.openapi.application.runInEdt
18
+ import com.intellij.openapi.editor.Caret
19
+ import com.intellij.openapi.editor.CaretModel
16
20
import com.intellij.openapi.editor.Editor
17
21
import com.intellij.openapi.editor.EditorCustomElementRenderer
18
22
import com.intellij.openapi.editor.Inlay
23
+ import com.intellij.openapi.editor.SelectionModel
24
+ import com.intellij.openapi.editor.actionSystem.EditorActionHandler
25
+ import com.intellij.openapi.editor.actionSystem.EditorActionManager
19
26
import com.intellij.openapi.editor.markup.TextAttributes
20
27
import com.intellij.openapi.project.DumbAwareAction
21
28
import com.intellij.openapi.wm.IdeFocusManager
@@ -184,6 +191,8 @@ class AutoDevInlineChatInput(
184
191
185
192
private var btnPresentation: Presentation ? = null
186
193
194
+ private var escHandler: EscHandler ? = null
195
+
187
196
init {
188
197
layout = BorderLayout ()
189
198
textArea = object : JBTextArea (), KeyboardAwareFocusOwner {
@@ -222,6 +231,11 @@ class AutoDevInlineChatInput(
222
231
}
223
232
})
224
233
textArea.inputMap.put(KeyStroke .getKeyStroke(KeyEvent .VK_ENTER , KeyEvent .SHIFT_DOWN_MASK ), " newlineAction" )
234
+ escHandler = EscHandler (autoDevInlineChatPanel.editor, {
235
+ cancel()
236
+ AutoDevInlineChatService .getInstance().closeInlineChat(autoDevInlineChatPanel.editor)
237
+ escHandler?.dispose()
238
+ })
225
239
226
240
btnPresentation = Presentation ()
227
241
setPresentationTextAndIcon(false )
@@ -246,7 +260,7 @@ class AutoDevInlineChatInput(
246
260
view = onSubmit(trimText) {
247
261
it.addProcessListener(object : SketchProcessListener {
248
262
override fun onBefore () = setPresentationTextAndIcon(true )
249
- override fun onAfter () = setPresentationTextAndIcon(false )
263
+ override fun onAfter () = setPresentationTextAndIcon(false )
250
264
})
251
265
}
252
266
@@ -283,3 +297,41 @@ fun <T : JComponent> Cell<T>.fullWidth(): Cell<T> {
283
297
fun <T : JComponent > Cell<T>.fullHeight (): Cell <T > {
284
298
return this .align(AlignY .FILL )
285
299
}
300
+
301
+
302
+ /* *
303
+ * 监听编辑器的 ESC 按键事件,并在非选中或多光标等需要取消前一状态的状态下执行 [action]
304
+ */
305
+ class EscHandler (private val targetEditor : Editor , private val action : () -> Unit ) : EditorActionHandler(), Disposable {
306
+
307
+ private var oldHandler: EditorActionHandler ? = null
308
+
309
+ init {
310
+ val editorManager = EditorActionManager .getInstance()
311
+ oldHandler = editorManager.getActionHandler(IdeActions .ACTION_EDITOR_ESCAPE )
312
+ editorManager.setActionHandler(IdeActions .ACTION_EDITOR_ESCAPE , this )
313
+ }
314
+
315
+ override fun doExecute (
316
+ editor : Editor ,
317
+ caret : Caret ? ,
318
+ context : DataContext ,
319
+ ) {
320
+ if (editor == targetEditor) {
321
+ val caretModel: CaretModel = editor.caretModel
322
+ val hasMultiCaret = caretModel.caretCount > 1
323
+ val hasSelection = caretModel.allCarets.any { it.hasSelection() }
324
+ if (hasMultiCaret || hasSelection) {
325
+ oldHandler?.execute(editor, caret, context)
326
+ } else {
327
+ action()
328
+ }
329
+ }
330
+ }
331
+
332
+ override fun dispose () {
333
+ oldHandler?.let {
334
+ EditorActionManager .getInstance().setActionHandler(IdeActions .ACTION_EDITOR_ESCAPE , it)
335
+ }
336
+ }
337
+ }
0 commit comments