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

Support insert code block as code cell. #179695

Merged
merged 1 commit into from Apr 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,7 +23,7 @@ export interface IInteractiveSessionCodeBlockActionContext {
element: IInteractiveResponseViewModel;
}

function isCodeBlockActionContext(thing: unknown): thing is IInteractiveSessionCodeBlockActionContext {
export function isCodeBlockActionContext(thing: unknown): thing is IInteractiveSessionCodeBlockActionContext {
return typeof thing === 'object' && thing !== null && 'code' in thing && 'element' in thing;
}

Expand Down
Expand Up @@ -7,16 +7,21 @@ import { Codicon } from 'vs/base/common/codicons';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { localize } from 'vs/nls';
import { IAction2Options, MenuId, MenuRegistry, registerAction2 } from 'vs/platform/actions/common/actions';
import { Action2, IAction2Options, MenuId, MenuRegistry, registerAction2 } from 'vs/platform/actions/common/actions';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { InputFocusedContext } from 'vs/platform/contextkey/common/contextkeys';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { insertCell } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
import { INotebookActionContext, NotebookAction } from 'vs/workbench/contrib/notebook/browser/controller/coreActions';
import { NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_EDITOR_EDITABLE } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
import { NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModelImpl';
import { CellKind, NotebookSetting } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellKind, NOTEBOOK_EDITOR_ID, NotebookSetting } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INTERACTIVE_SESSION_CATEGORY } from 'vs/workbench/contrib/interactiveSession/browser/actions/interactiveSessionActions';
import { isCodeBlockActionContext } from 'vs/workbench/contrib/interactiveSession/browser/actions/interactiveSessionCodeblockActions';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IInteractiveSessionService, IInteractiveSessionUserActionEvent } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionService';
import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';

const INSERT_CODE_CELL_ABOVE_COMMAND_ID = 'notebook.cell.insertCodeCellAbove';
const INSERT_CODE_CELL_BELOW_COMMAND_ID = 'notebook.cell.insertCodeCellBelow';
Expand Down Expand Up @@ -221,6 +226,61 @@ registerAction2(class InsertMarkdownCellAtTopAction extends NotebookAction {
}
});

registerAction2(class InsertCodeBlockAsNotebookCellAction extends Action2 {
constructor() {
super({
id: 'workbench.action.interactiveSession.insertCodeBlockAsNotebookCell',
title: {
value: localize('interactive.insertCodeBlockAsNotebookCell.label', "Insert as Code Cell"),
original: 'Insert as Code Cell'
},
f1: false,
category: INTERACTIVE_SESSION_CATEGORY,
icon: Codicon.notebook,
menu: {
id: MenuId.InteractiveSessionCodeBlock,
group: 'navigation',
when: NOTEBOOK_IS_ACTIVE_EDITOR
}
});
}

async run(accessor: ServicesAccessor, ...args: any[]) {
const context = args[0];
if (!isCodeBlockActionContext(context)) {
return;
}

const editorService = accessor.get(IEditorService);
const languageService = accessor.get(ILanguageService);
const interactiveSessionService = accessor.get(IInteractiveSessionService);

if (editorService.activeEditorPane?.getId() !== NOTEBOOK_EDITOR_ID) {
return;
}

const editor = editorService.activeEditorPane.getControl() as INotebookEditor;

if (!editor || !editor.hasModel()) {
return;
}

const focusRange = editor.getFocus();
const next = Math.max(focusRange.end - 1, 0);
insertCell(languageService, editor, next, CellKind.Code, 'below', context.code, true);

interactiveSessionService.notifyUserAction(<IInteractiveSessionUserActionEvent>{
providerId: context.element.providerId,
action: {
kind: 'insert',
responseId: context.element.providerResponseId,
codeBlockIndex: context.codeBlockIndex,
totalCharacters: context.code.length,
}
});
}
});

MenuRegistry.appendMenuItem(MenuId.NotebookCellBetween, {
command: {
id: INSERT_CODE_CELL_BELOW_COMMAND_ID,
Expand Down