diff --git a/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts b/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts index 3432d899d1bf2..0795f4536e703 100644 --- a/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts +++ b/src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts @@ -18,6 +18,7 @@ import { CellEditType, CellKind, ICellEditOperation, ICellReplaceEdit, IOutputDt import { cellRangeContains, cellRangesToIndexes, ICellRange } from '../../common/notebookRange.js'; import { localize } from '../../../../../nls.js'; import { INotificationService } from '../../../../../platform/notification/common/notification.js'; +import { INotebookKernelHistoryService } from '../../common/notebookKernelService.js'; export async function changeCellToKind(kind: CellKind, context: INotebookActionContext, language?: string, mime?: string): Promise { const { notebookEditor } = context; @@ -662,7 +663,8 @@ export function insertCell( type: CellKind, direction: 'above' | 'below' = 'above', initialText: string = '', - ui: boolean = false + ui: boolean = false, + kernelHistoryService?: INotebookKernelHistoryService ) { const viewModel = editor.getViewModel() as NotebookViewModel; const activeKernel = editor.activeKernel; @@ -676,6 +678,7 @@ export function insertCell( if (type === CellKind.Code) { const supportedLanguages = activeKernel?.supportedLanguages ?? languageService.getRegisteredLanguageIds(); const defaultLanguage = supportedLanguages[0] || PLAINTEXT_LANGUAGE_ID; + if (cell?.cellKind === CellKind.Code) { language = cell.language; } else if (cell?.cellKind === CellKind.Markup) { @@ -685,6 +688,15 @@ export function insertCell( } else { language = defaultLanguage; } + } else if (!cell && viewModel.length === 0) { + // No cells in notebook - check kernel history + const lastKernels = kernelHistoryService?.getKernels(viewModel.notebookDocument); + if (lastKernels?.all.length) { + const lastKernel = lastKernels.all[0]; + language = lastKernel.supportedLanguages[0] || defaultLanguage; + } else { + language = defaultLanguage; + } } else { if (cell === undefined && direction === 'above') { // insert cell at the very top diff --git a/src/vs/workbench/contrib/notebook/browser/controller/insertCellActions.ts b/src/vs/workbench/contrib/notebook/browser/controller/insertCellActions.ts index dc3fbcc179db9..b7bdf36f34780 100644 --- a/src/vs/workbench/contrib/notebook/browser/controller/insertCellActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/controller/insertCellActions.ts @@ -18,6 +18,7 @@ import { NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_EDITOR_EDITABLE } from '../../comm import { CellViewModel } from '../viewModel/notebookViewModelImpl.js'; import { CellKind, NotebookSetting } from '../../common/notebookCommon.js'; import { CTX_NOTEBOOK_CHAT_OUTER_FOCUS_POSITION } from './chat/notebookChatContext.js'; +import { INotebookKernelHistoryService } from '../../common/notebookKernelService.js'; const INSERT_CODE_CELL_ABOVE_COMMAND_ID = 'notebook.cell.insertCodeCellAbove'; const INSERT_CODE_CELL_BELOW_COMMAND_ID = 'notebook.cell.insertCodeCellBelow'; @@ -35,13 +36,15 @@ export function insertNewCell(accessor: ServicesAccessor, context: INotebookActi } const languageService = accessor.get(ILanguageService); + const kernelHistoryService = accessor.get(INotebookKernelHistoryService); + if (context.cell) { const idx = context.notebookEditor.getCellIndex(context.cell); - newCell = insertCell(languageService, context.notebookEditor, idx, kind, direction, undefined, true); + newCell = insertCell(languageService, context.notebookEditor, idx, kind, direction, undefined, true, kernelHistoryService); } else { const focusRange = context.notebookEditor.getFocus(); const next = Math.max(focusRange.end - 1, 0); - newCell = insertCell(languageService, context.notebookEditor, next, kind, direction, undefined, true); + newCell = insertCell(languageService, context.notebookEditor, next, kind, direction, undefined, true, kernelHistoryService); } return newCell; @@ -193,7 +196,8 @@ registerAction2(class InsertCodeCellAtTopAction extends NotebookAction { async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise { const languageService = accessor.get(ILanguageService); - const newCell = insertCell(languageService, context.notebookEditor, 0, CellKind.Code, 'above', undefined, true); + const kernelHistoryService = accessor.get(INotebookKernelHistoryService); + const newCell = insertCell(languageService, context.notebookEditor, 0, CellKind.Code, 'above', undefined, true, kernelHistoryService); if (newCell) { await context.notebookEditor.focusNotebookCell(newCell, 'editor'); @@ -220,7 +224,9 @@ registerAction2(class InsertMarkdownCellAtTopAction extends NotebookAction { async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise { const languageService = accessor.get(ILanguageService); - const newCell = insertCell(languageService, context.notebookEditor, 0, CellKind.Markup, 'above', undefined, true); + const kernelHistoryService = accessor.get(INotebookKernelHistoryService); + + const newCell = insertCell(languageService, context.notebookEditor, 0, CellKind.Markup, 'above', undefined, true, kernelHistoryService); if (newCell) { await context.notebookEditor.focusNotebookCell(newCell, 'editor');