diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/format/formatting.ts b/src/vs/workbench/contrib/notebook/browser/contrib/format/formatting.ts new file mode 100644 index 0000000000000000000000000000000000000000..a2442dc35a344dc53201bbbdee8d396d817aa88f --- /dev/null +++ b/src/vs/workbench/contrib/notebook/browser/contrib/format/formatting.ts @@ -0,0 +1,88 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { registerAction2, Action2 } from 'vs/platform/actions/common/actions'; +import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; +import { localize } from 'vs/nls'; +import { NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; +import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; +import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; +import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; +import { getActiveNotebookEditor } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; +import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; +import { ITextModelService } from 'vs/editor/common/services/resolverService'; +import { DisposableStore } from 'vs/base/common/lifecycle'; +import { getDocumentFormattingEditsUntilResult } from 'vs/editor/contrib/format/format'; +import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService'; +import { CancellationToken } from 'vs/base/common/cancellation'; +import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService'; +import { WorkspaceTextEdit } from 'vs/editor/common/modes'; +import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; + +registerAction2(class extends Action2 { + constructor() { + super({ + id: 'notebook.format', + title: localize('format.title', 'Format Notebook'), + category: localize('cat', "Notebook"), + keybinding: { + when: ContextKeyExpr.and(NOTEBOOK_IS_ACTIVE_EDITOR, EditorContextKeys.editorTextFocus.toNegated()), + primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_F, + linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I }, + weight: KeybindingWeight.WorkbenchContrib + }, + f1: true + }); + } + + async run(accessor: ServicesAccessor): Promise { + const editorService = accessor.get(IEditorService); + const textModelService = accessor.get(ITextModelService); + const editorWorkerService = accessor.get(IEditorWorkerService); + const bulkEditService = accessor.get(IBulkEditService); + + const editor = getActiveNotebookEditor(editorService); + if (!editor || !editor.viewModel) { + return; + } + + const notebook = editor.viewModel.notebookDocument; + const dispoables = new DisposableStore(); + try { + + const edits: WorkspaceTextEdit[] = []; + + for (let cell of notebook.cells) { + + const ref = await textModelService.createModelReference(cell.uri); + dispoables.add(ref); + + const model = ref.object.textEditorModel; + + const formatEdits = await getDocumentFormattingEditsUntilResult( + editorWorkerService, model, + model.getOptions(), CancellationToken.None + ); + + if (formatEdits) { + formatEdits.forEach(edit => edits.push({ + edit, + resource: model.uri, + modelVersionId: model.getVersionId() + })); + } + } + + await bulkEditService.apply( + { edits }, + { label: localize('label', "Format Notebook") } + ); + + } finally { + dispoables.dispose(); + } + + } +}); diff --git a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts index ed8ba78560f0efa91b339c08b6bf7e21f1ff7708..cedc85fef1f712fca6c6ee65668884985b7ef917 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts @@ -39,8 +39,9 @@ import { CustomEditorInfo } from 'vs/workbench/contrib/customEditor/common/custo // Editor Contribution import 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; -import 'vs/workbench/contrib/notebook/browser/contrib/fold/folding'; import 'vs/workbench/contrib/notebook/browser/contrib/find/findController'; +import 'vs/workbench/contrib/notebook/browser/contrib/fold/folding'; +import 'vs/workbench/contrib/notebook/browser/contrib/format/formatting'; // Output renderers registration diff --git a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts index aa1de467fac762740107dd62266472bc91a5068f..96571b999ff6c4bea29f66de0b2e16f76253735c 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts @@ -17,7 +17,7 @@ import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget'; import { BareFontInfo } from 'vs/editor/common/config/fontInfo'; import { Range } from 'vs/editor/common/core/range'; import { FindMatch } from 'vs/editor/common/model'; -import { RawContextKey } from 'vs/platform/contextkey/common/contextkey'; +import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer'; import { CellViewModel, IModelDecorationsChangeAccessor, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel'; @@ -26,6 +26,9 @@ import { Webview } from 'vs/workbench/contrib/webview/browser/webview'; export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = new RawContextKey('notebookFindWidgetFocused', false); +// Is Notebook +export const NOTEBOOK_IS_ACTIVE_EDITOR = ContextKeyExpr.equals('activeEditor', 'workbench.editor.notebook'); + // Editor keys export const NOTEBOOK_EDITOR_FOCUSED = new RawContextKey('notebookEditorFocused', false); export const NOTEBOOK_EDITOR_EDITABLE = new RawContextKey('notebookEditable', true);