Skip to content

Commit

Permalink
debt - Format Cell instead of Format Document, also add Format Notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed May 4, 2020
1 parent ab675cf commit 846aec8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/vs/editor/common/editorContextKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export namespace EditorContextKeys {
export const canUndo = new RawContextKey<boolean>('canUndo', false);
export const canRedo = new RawContextKey<boolean>('canRedo', false);

/**
* A context key that is set when an editor is part of a larger editor, like notebooks or
* (future) a diff editor
*/
export const inCompositeEditor = new RawContextKey<boolean>('inCompositeEditor', undefined);
export const notInCompositeEditor = inCompositeEditor.toNegated();

// -- mode context keys
export const languageId = new RawContextKey<string>('editorLangId', '');
export const hasCompletionItemProvider = new RawContextKey<boolean>('editorHasCompletionItemProvider', false);
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/format/formatActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class FormatDocumentAction extends EditorAction {
id: 'editor.action.formatDocument',
label: nls.localize('formatDocument.label', "Format Document"),
alias: 'Format Document',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasDocumentFormattingProvider),
precondition: ContextKeyExpr.and(EditorContextKeys.notInCompositeEditor, EditorContextKeys.writable, EditorContextKeys.hasDocumentFormattingProvider),
kbOpts: {
kbExpr: ContextKeyExpr.and(EditorContextKeys.editorTextFocus, EditorContextKeys.hasDocumentFormattingProvider),
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_F,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,48 @@
* 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 { registerAction2, Action2, MenuId } 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 { NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_EDITOR_EDITABLE } 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 { ServicesAccessor, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { getActiveNotebookEditor, NOTEBOOK_ACTIONS_CATEGORY } 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 { getDocumentFormattingEditsUntilResult, formatDocumentWithSelectedProvider, FormattingMode } 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';
import { registerEditorAction, EditorAction } from 'vs/editor/browser/editorExtensions';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { Progress } from 'vs/platform/progress/common/progress';

// format notebook
registerAction2(class extends Action2 {
constructor() {
super({
id: 'notebook.format',
title: localize('format.title', 'Format Notebook'),
category: NOTEBOOK_ACTIONS_CATEGORY,
precondition: ContextKeyExpr.and(NOTEBOOK_IS_ACTIVE_EDITOR),
precondition: ContextKeyExpr.and(NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_EDITOR_EDITABLE),
keybinding: {
when: 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
f1: true,
menu: {
id: MenuId.EditorContext,
when: ContextKeyExpr.and(EditorContextKeys.inCompositeEditor, EditorContextKeys.hasDocumentFormattingProvider),
group: '1_modification',
order: 1.3
}
});
}

Expand Down Expand Up @@ -84,6 +94,34 @@ registerAction2(class extends Action2 {
} finally {
dispoables.dispose();
}
}
});

// format cell
registerEditorAction(class FormatCellAction extends EditorAction {
constructor() {
super({
id: 'notebook.formatCell',
label: localize('formatCell.label', "Format Cell"),
alias: 'Format Cell',
precondition: ContextKeyExpr.and(NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_EDITOR_EDITABLE, EditorContextKeys.inCompositeEditor, EditorContextKeys.writable, EditorContextKeys.hasDocumentFormattingProvider),
kbOpts: {
kbExpr: ContextKeyExpr.and(EditorContextKeys.editorTextFocus),
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_F,
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I },
weight: KeybindingWeight.EditorContrib
},
contextMenuOpts: {
group: '1_modification',
order: 1.301
}
});
}

async run(accessor: ServicesAccessor, editor: ICodeEditor): Promise<void> {
if (editor.hasModel()) {
const instaService = accessor.get(IInstantiationService);
await instaService.invokeFunction(formatDocumentWithSelectedProvider, editor, FormattingMode.Explicit, Progress.None, CancellationToken.None);
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {

const dndController = new CellDragAndDropController(this);
const renders = [
this.instantiationService.createInstance(CodeCellRenderer, this, this.contextKeyService, this.renderedEditors, dndController),
this.instantiationService.createInstance(CodeCellRenderer, this, this.renderedEditors, dndController),
this.instantiationService.createInstance(MarkdownCellRenderer, this.contextKeyService, this, dndController, this.renderedEditors),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewMod
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { CellKind, NotebookCellRunState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';

const $ = DOM.$;

Expand Down Expand Up @@ -416,7 +418,7 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR

this.setupBetweenCellToolbarActions(element, templateData, elementDisposables, toolbarContext);

const markdownCell = new StatefullMarkdownCell(this.notebookEditor, element, templateData, this.editorOptions.value, this.renderedEditors, this.instantiationService);
const markdownCell = this.instantiationService.createInstance(StatefullMarkdownCell, this.notebookEditor, element, templateData, this.editorOptions.value, this.renderedEditors);
elementDisposables.add(this.editorOptions.onDidChange(newValue => markdownCell.updateEditorOptions(newValue)));
elementDisposables.add(markdownCell);

Expand Down Expand Up @@ -703,9 +705,9 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende

constructor(
protected notebookEditor: INotebookEditor,
protected contextKeyService: IContextKeyService,
private renderedEditors: Map<ICellViewModel, ICodeEditor | undefined>,
dndController: CellDragAndDropController,
@IContextKeyService protected contextKeyService: IContextKeyService,
@IContextMenuService contextMenuService: IContextMenuService,
@IConfigurationService configurationService: IConfigurationService,
@IInstantiationService instantiationService: IInstantiationService,
Expand Down Expand Up @@ -734,9 +736,14 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende

const executionOrderLabel = DOM.append(runButtonContainer, $('div.execution-count-label'));

// create a special context key service that set the inCompositeEditor-contextkey
const editorContextKeyService = this.contextKeyService.createScoped();
const editorInstaService = this.instantiationService.createChild(new ServiceCollection([IContextKeyService, editorContextKeyService]));
EditorContextKeys.inCompositeEditor.bindTo(editorContextKeyService).set(true);

const editorPart = DOM.append(cellContainer, $('.cell-editor-part'));
const editorContainer = DOM.append(editorPart, $('.cell-editor-container'));
const editor = this.instantiationService.createInstance(CodeEditorWidget, editorContainer, {
const editor = editorInstaService.createInstance(CodeEditorWidget, editorContainer, {
...this.editorOptions.value,
dimension: {
width: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { getResizesObserver } from 'vs/workbench/contrib/notebook/browser/view/r
import { CellFoldingState } from 'vs/workbench/contrib/notebook/browser/contrib/fold/foldingModel';
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';

export class StatefullMarkdownCell extends Disposable {

Expand All @@ -35,7 +38,8 @@ export class StatefullMarkdownCell extends Disposable {
private templateData: MarkdownCellRenderTemplate,
editorOptions: IEditorOptions,
renderedEditors: Map<ICellViewModel, ICodeEditor | undefined>,
instantiationService: IInstantiationService
@IContextKeyService contextKeyService: IContextKeyService,
@IInstantiationService instantiationService: IInstantiationService,
) {
super();

Expand Down Expand Up @@ -70,7 +74,13 @@ export class StatefullMarkdownCell extends Disposable {
editorHeight = Math.max(lineNum, 1) * lineHeight + EDITOR_TOP_PADDING + EDITOR_BOTTOM_PADDING;

this.templateData.editorContainer.innerHTML = '';
this.editor = instantiationService.createInstance(CodeEditorWidget, this.templateData.editorContainer, {

// create a special context key service that set the inCompositeEditor-contextkey
const editorContextKeyService = contextKeyService.createScoped();
const editorInstaService = instantiationService.createChild(new ServiceCollection([IContextKeyService, editorContextKeyService]));
EditorContextKeys.inCompositeEditor.bindTo(editorContextKeyService).set(true);

this.editor = editorInstaService.createInstance(CodeEditorWidget, this.templateData.editorContainer, {
...this.editorOptions,
dimension: {
width: width,
Expand Down

0 comments on commit 846aec8

Please sign in to comment.