@@ -365,21 +365,24 @@ registerAction2(class extends NotebookAction {
365
365
interface IInsertCellWithChatArgs extends INotebookActionContext {
366
366
input ?: string ;
367
367
autoSend ?: boolean ;
368
+ source ?: string ;
368
369
}
369
370
370
- async function startChat ( accessor : ServicesAccessor , context : INotebookActionContext , index : number , input ?: string , autoSend ?: boolean ) {
371
+ async function startChat ( accessor : ServicesAccessor , context : INotebookActionContext , index : number , input ?: string , autoSend ?: boolean , source ?: string ) {
371
372
const configurationService = accessor . get ( IConfigurationService ) ;
372
373
const commandService = accessor . get ( ICommandService ) ;
373
374
374
375
if ( configurationService . getValue < boolean > ( NotebookSetting . cellChat ) ) {
375
376
context . notebookEditor . focusContainer ( ) ;
376
377
NotebookChatController . get ( context . notebookEditor ) ?. run ( index , input , autoSend ) ;
377
378
} else if ( configurationService . getValue < boolean > ( NotebookSetting . cellGenerate ) ) {
378
- const newCell = await insertNewCell ( accessor , context , CellKind . Code , 'below' , true ) ;
379
- if ( newCell ) {
380
- newCell . enableAutoLanguageDetection ( ) ;
381
- await context . notebookEditor . revealFirstLineIfOutsideViewport ( newCell ) ;
382
- const codeEditor = context . notebookEditor . codeEditors . find ( ce => ce [ 0 ] === newCell ) ?. [ 1 ] ;
379
+ const activeCell = context . notebookEditor . getActiveCell ( ) ;
380
+ const targetCell = activeCell ?. getTextLength ( ) === 0 && source !== 'insertToolbar' ? activeCell : ( await insertNewCell ( accessor , context , CellKind . Code , 'below' , true ) ) ;
381
+
382
+ if ( targetCell ) {
383
+ targetCell . enableAutoLanguageDetection ( ) ;
384
+ await context . notebookEditor . revealFirstLineIfOutsideViewport ( targetCell ) ;
385
+ const codeEditor = context . notebookEditor . codeEditors . find ( ce => ce [ 0 ] === targetCell ) ?. [ 1 ] ;
383
386
if ( codeEditor ) {
384
387
codeEditor . focus ( ) ;
385
388
commandService . executeCommand ( 'inlineChat.start' ) ;
@@ -497,7 +500,7 @@ registerAction2(class extends NotebookAction {
497
500
498
501
async runWithContext ( accessor : ServicesAccessor , context : IInsertCellWithChatArgs ) {
499
502
const index = Math . max ( 0 , context . cell ? context . notebookEditor . getCellIndex ( context . cell ) + 1 : 0 ) ;
500
- await startChat ( accessor , context , index , context . input , context . autoSend ) ;
503
+ await startChat ( accessor , context , index , context . input , context . autoSend , context . source ) ;
501
504
}
502
505
} ) ;
503
506
0 commit comments