Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re #239460. Empty notebook defaults cell language to last used kernel #239647

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const { notebookEditor } = context;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -193,7 +196,8 @@ registerAction2(class InsertCodeCellAtTopAction extends NotebookAction {

async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
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');
Expand All @@ -220,7 +224,9 @@ registerAction2(class InsertMarkdownCellAtTopAction extends NotebookAction {

async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
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');
Expand Down
Loading