提交 846aec81 编写于 作者: J Johannes Rieken

debt - Format Cell instead of Format Document, also add Format Notebook

上级 ab675cf7
......@@ -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);
......
......@@ -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,
......
......@@ -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
}
});
}
......@@ -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);
}
}
});
......@@ -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),
];
......
......@@ -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.$;
......@@ -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);
......@@ -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,
......@@ -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,
......
......@@ -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 {
......@@ -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();
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册