Skip to content

Commit

Permalink
fix #104128.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Aug 12, 2020
1 parent 7d9f54c commit 37697bc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { InputFocusedContext, InputFocusedContextKey } from 'vs/platform/context
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IQuickInputService, IQuickPickItem, QuickPickInput } from 'vs/platform/quickinput/common/quickInput';
import { BaseCellRenderTemplate, CellEditState, CellFocusMode, ICellViewModel, INotebookEditor, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_OUTPUT_FOCUSED, EXPAND_CELL_CONTENT_COMMAND_ID } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { BaseCellRenderTemplate, CellEditState, CellFocusMode, ICellViewModel, INotebookEditor, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_OUTPUT_FOCUSED, EXPAND_CELL_CONTENT_COMMAND_ID, NOTEBOOK_CELL_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { CellKind, CellUri, NotebookCellRunState, NOTEBOOK_EDITOR_CURSOR_BOUNDARY } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
Expand Down Expand Up @@ -1178,7 +1178,7 @@ registerAction2(class extends NotebookCellAction {
title: localize('clearActiveCellOutputs', 'Clear Active Cell Outputs'),
menu: {
id: MenuId.NotebookCellTitle,
when: ContextKeyExpr.and(NOTEBOOK_CELL_TYPE.isEqualTo('code'), NOTEBOOK_EDITOR_RUNNABLE),
when: ContextKeyExpr.and(NOTEBOOK_CELL_TYPE.isEqualTo('code'), NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_CELL_HAS_OUTPUTS),
order: CellToolbarOrder.ClearCellOutput,
group: CELL_TITLE_OUTPUT_GROUP_ID
},
Expand Down Expand Up @@ -1341,7 +1341,7 @@ registerAction2(class extends NotebookCellAction {
title: localize('notebookActions.splitCell', "Split Cell"),
menu: {
id: MenuId.NotebookCellTitle,
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_CELL_EDITABLE, InputFocusedContext),
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_FOCUSED, InputFocusedContext),
order: CellToolbarOrder.SplitCell,
group: CELL_TITLE_CELL_GROUP_ID,
// alt: {
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK = new RawContextKey<boolean>('no
export const NOTEBOOK_VIEW_TYPE = new RawContextKey<string>('notebookViewType', undefined);
export const NOTEBOOK_CELL_TYPE = new RawContextKey<string>('notebookCellType', undefined); // code, markdown
export const NOTEBOOK_CELL_EDITABLE = new RawContextKey<boolean>('notebookCellEditable', false); // bool
export const NOTEBOOK_CELL_FOCUSED = new RawContextKey<boolean>('notebookCellFocused', false); // bool
export const NOTEBOOK_CELL_RUNNABLE = new RawContextKey<boolean>('notebookCellRunnable', false); // bool
export const NOTEBOOK_CELL_MARKDOWN_EDIT_MODE = new RawContextKey<boolean>('notebookCellMarkdownEditMode', false); // bool
export const NOTEBOOK_CELL_RUN_STATE = new RawContextKey<string>('notebookCellRunState', undefined); // idle, running
Expand Down Expand Up @@ -188,6 +189,7 @@ export interface INotebookEditor extends IEditor {
multipleKernelsAvailable: boolean;
readonly onDidChangeAvailableKernels: Event<void>;
readonly onDidChangeKernel: Event<void>;
readonly onDidChangeActiveCell: Event<void>;

isDisposed: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
return this._renderedEditors.get(focused);
}

private readonly _onDidChangeActiveCell = this._register(new Emitter<void>());
readonly onDidChangeActiveCell: Event<void> = this._onDidChangeActiveCell.event;


private _cursorNavigationMode: boolean = false;
get cursorNavigationMode(): boolean {
return this._cursorNavigationMode;
Expand Down Expand Up @@ -460,6 +464,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor

this._register(this._list.onDidChangeFocus(_e => {
this._onDidChangeActiveEditor.fire(this);
this._onDidChangeActiveCell.fire();
this._cursorNavigationMode = false;
}));

Expand Down Expand Up @@ -556,7 +561,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
const focused = this._list!.getFocusedElements()[0];
if (focused) {
if (!this._cellContextKeyManager) {
this._cellContextKeyManager = this._localStore.add(new CellContextKeyManager(this.contextKeyService, textModel, focused as CellViewModel));
this._cellContextKeyManager = this._localStore.add(new CellContextKeyManager(this.contextKeyService, this, textModel, focused as CellViewModel));
}

this._cellContextKeyManager.updateForElement(focused as CellViewModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { INotebookTextModel, NotebookCellRunState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { BaseCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/baseCellViewModel';
import { NOTEBOOK_CELL_TYPE, NOTEBOOK_VIEW_TYPE, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_HAS_OUTPUTS, CellViewModelStateChangeEvent, CellEditState, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_OUTPUT_COLLAPSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { NOTEBOOK_CELL_TYPE, NOTEBOOK_VIEW_TYPE, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_HAS_OUTPUTS, CellViewModelStateChangeEvent, CellEditState, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_FOCUSED, INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
Expand All @@ -17,6 +17,7 @@ export class CellContextKeyManager extends Disposable {
private viewType!: IContextKey<string>;
private cellEditable!: IContextKey<boolean>;
private cellRunnable!: IContextKey<boolean>;
private cellFocused!: IContextKey<boolean>;
private cellRunState!: IContextKey<string>;
private cellHasOutputs!: IContextKey<boolean>;
private cellContentCollapsed!: IContextKey<boolean>;
Expand All @@ -28,6 +29,7 @@ export class CellContextKeyManager extends Disposable {

constructor(
private readonly contextKeyService: IContextKeyService,
private readonly notebookEditor: INotebookEditor,
private readonly notebookTextModel: INotebookTextModel,
private element: BaseCellViewModel
) {
Expand All @@ -37,6 +39,7 @@ export class CellContextKeyManager extends Disposable {
this.cellType = NOTEBOOK_CELL_TYPE.bindTo(this.contextKeyService);
this.viewType = NOTEBOOK_VIEW_TYPE.bindTo(this.contextKeyService);
this.cellEditable = NOTEBOOK_CELL_EDITABLE.bindTo(this.contextKeyService);
this.cellFocused = NOTEBOOK_CELL_FOCUSED.bindTo(this.contextKeyService);
this.cellRunnable = NOTEBOOK_CELL_RUNNABLE.bindTo(this.contextKeyService);
this.markdownEditMode = NOTEBOOK_CELL_MARKDOWN_EDIT_MODE.bindTo(this.contextKeyService);
this.cellRunState = NOTEBOOK_CELL_RUN_STATE.bindTo(this.contextKeyService);
Expand All @@ -57,6 +60,7 @@ export class CellContextKeyManager extends Disposable {
}

this.elementDisposables.add(element.model.onDidChangeMetadata(() => this.updateForCollapseState()));
this.elementDisposables.add(this.notebookEditor.onDidChangeActiveCell(() => this.updateForFocusState()));

this.element = element;
if (this.element instanceof MarkdownCellViewModel) {
Expand All @@ -66,6 +70,7 @@ export class CellContextKeyManager extends Disposable {
}

this.contextKeyService.bufferChangeEvents(() => {
this.updateForFocusState();
this.updateForMetadata();
this.updateForEditState();
this.updateForCollapseState();
Expand All @@ -91,6 +96,10 @@ export class CellContextKeyManager extends Disposable {
});
}

private updateForFocusState() {
this.cellFocused.set(this.notebookEditor.getActiveCell() === this.element);
}

private updateForMetadata() {
const metadata = this.element.getEvaluatedMetadata(this.notebookTextModel.metadata);
this.cellEditable.set(!!metadata.editable);
Expand All @@ -115,8 +124,10 @@ export class CellContextKeyManager extends Disposable {

private updateForOutputs() {
if (this.element instanceof CodeCellViewModel) {
console.log(this.element, this.element.outputs.length > 0);
this.cellHasOutputs.set(this.element.outputs.length > 0);
} else {
console.log(this.element, false);
this.cellHasOutputs.set(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR

const elementDisposables = templateData.elementDisposables;

elementDisposables.add(new CellContextKeyManager(templateData.contextKeyService, this.notebookEditor.viewModel?.notebookDocument!, element));
elementDisposables.add(new CellContextKeyManager(templateData.contextKeyService, this.notebookEditor, this.notebookEditor.viewModel?.notebookDocument!, element));

// render toolbar first
this.setupCellToolbarActions(templateData, elementDisposables);
Expand Down Expand Up @@ -1147,7 +1147,7 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
elementDisposables.add(this.instantiationService.createInstance(CodeCell, this.notebookEditor, element, templateData));
this.renderedEditors.set(element, templateData.editor);

elementDisposables.add(new CellContextKeyManager(templateData.contextKeyService, this.notebookEditor.viewModel?.notebookDocument!, element));
elementDisposables.add(new CellContextKeyManager(templateData.contextKeyService, this.notebookEditor, this.notebookEditor.viewModel?.notebookDocument!, element));

this.updateForLayout(element, templateData);
elementDisposables.add(element.onDidChangeLayout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class TestNotebookEditor implements INotebookEditor {

multipleKernelsAvailable: boolean = false;
onDidChangeAvailableKernels: Event<void> = new Emitter<void>().event;

onDidChangeActiveCell: Event<void> = new Emitter<void>().event;

uri?: URI | undefined;
textModel?: NotebookTextModel | undefined;
Expand Down

0 comments on commit 37697bc

Please sign in to comment.