Skip to content

Commit cc34298

Browse files
authored
Activate inline chat on empty code cell other than inserting new cell (microsoft#211301)
1 parent dd8b5be commit cc34298

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/vs/workbench/contrib/notebook/browser/controller/chat/cellChatActions.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,24 @@ registerAction2(class extends NotebookAction {
365365
interface IInsertCellWithChatArgs extends INotebookActionContext {
366366
input?: string;
367367
autoSend?: boolean;
368+
source?: string;
368369
}
369370

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) {
371372
const configurationService = accessor.get(IConfigurationService);
372373
const commandService = accessor.get(ICommandService);
373374

374375
if (configurationService.getValue<boolean>(NotebookSetting.cellChat)) {
375376
context.notebookEditor.focusContainer();
376377
NotebookChatController.get(context.notebookEditor)?.run(index, input, autoSend);
377378
} 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];
383386
if (codeEditor) {
384387
codeEditor.focus();
385388
commandService.executeCommand('inlineChat.start');
@@ -497,7 +500,7 @@ registerAction2(class extends NotebookAction {
497500

498501
async runWithContext(accessor: ServicesAccessor, context: IInsertCellWithChatArgs) {
499502
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);
501504
}
502505
});
503506

src/vs/workbench/contrib/notebook/browser/view/cellParts/cellToolbars.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export class BetweenCellToolbar extends CellOverlayPart {
8484
ui: true,
8585
cell: element,
8686
notebookEditor: this._notebookEditor,
87+
source: 'insertToolbar',
8788
$mid: MarshalledId.NotebookCellActionContext
8889
};
8990
this.updateInternalLayoutNow(element);
@@ -205,6 +206,7 @@ export class CellTitleToolbarPart extends CellOverlayPart {
205206
ui: true,
206207
cell: element,
207208
notebookEditor: this._notebookEditor,
209+
source: 'cellToolbar',
208210
$mid: MarshalledId.NotebookCellActionContext
209211
});
210212
}

src/vs/workbench/contrib/notebook/browser/viewParts/notebookEditorToolbar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ export class NotebookEditorWorkbenchToolbar extends Disposable {
320320

321321
const context = {
322322
ui: true,
323-
notebookEditor: this.notebookEditor
323+
notebookEditor: this.notebookEditor,
324+
source: 'notebookToolbar'
324325
};
325326

326327
const actionProvider = (action: IAction, options: IActionViewItemOptions) => {

0 commit comments

Comments
 (0)