Skip to content

Commit

Permalink
re #105735. selections as NotebookEditor properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Sep 4, 2020
1 parent 45c70c2 commit 11158cc
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
19 changes: 10 additions & 9 deletions src/vs/workbench/api/browser/mainThreadNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DocumentAndEditorState {
const apiEditors = [];
for (let id in after.textEditors) {
const editor = after.textEditors.get(id)!;
apiEditors.push({ id, documentUri: editor.uri!, selections: editor!.textModel!.selections, visibleRanges: editor.visibleRanges });
apiEditors.push({ id, documentUri: editor.uri!, selections: editor!.getSelectionHandles(), visibleRanges: editor.visibleRanges });
}

return {
Expand All @@ -72,7 +72,7 @@ class DocumentAndEditorState {
const addedAPIEditors = editorDelta.added.map(add => ({
id: add.getId(),
documentUri: add.uri!,
selections: add.textModel!.selections || [],
selections: add.getSelectionHandles(),
visibleRanges: add.visibleRanges
}));

Expand Down Expand Up @@ -231,7 +231,12 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
if (!this._editorEventListenersMapping.has(editor.getId())) {
const disposableStore = new DisposableStore();
disposableStore.add(editor.onDidChangeVisibleRanges(() => {
this._proxy.$acceptEditorPropertiesChanged(editor.getId(), { visibleRanges: { ranges: editor.visibleRanges } });
this._proxy.$acceptEditorPropertiesChanged(editor.getId(), { visibleRanges: { ranges: editor.visibleRanges }, selections: null });
}));

disposableStore.add(editor.onDidChangeSelection(() => {
const selectionHandles = editor.getSelectionHandles();
this._proxy.$acceptEditorPropertiesChanged(editor.getId(), { visibleRanges: null, selections: { selections: selectionHandles } });
}));

this._editorEventListenersMapping.set(editor.getId(), disposableStore);
Expand Down Expand Up @@ -262,11 +267,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
const textModel = this._notebookService.getNotebookTextModel(doc);
disposableStore.add(textModel!.onDidModelChangeProxy(e => {
this._proxy.$acceptModelChanged(textModel!.uri, e, textModel!.isDirty);
this._proxy.$acceptDocumentPropertiesChanged(doc, { selections: { selections: textModel!.selections }, metadata: null });
}));
disposableStore.add(textModel!.onDidSelectionChange(e => {
const selectionsChange = e ? { selections: e } : null;
this._proxy.$acceptDocumentPropertiesChanged(doc, { selections: selectionsChange, metadata: null });
this._proxy.$acceptDocumentPropertiesChanged(doc, { metadata: null });
}));

this._editorEventListenersMapping.set(textModel!.uri.toString(), disposableStore);
Expand Down Expand Up @@ -448,7 +449,7 @@ export class MainThreadNotebooks extends Disposable implements MainThreadNoteboo
textModel.insertTemplateCell(mainCell);
}

this._proxy.$acceptDocumentPropertiesChanged(textModel.uri, { selections: null, metadata: textModel.metadata });
this._proxy.$acceptDocumentPropertiesChanged(textModel.uri, { metadata: textModel.metadata });
return;
},
resolveNotebookEditor: async (viewType: string, uri: URI, editorId: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1639,11 +1639,11 @@ export interface INotebookVisibleRangesEvent {

export interface INotebookEditorPropertiesChangeData {
visibleRanges: INotebookVisibleRangesEvent | null;
selections: INotebookSelectionChangeEvent | null;
}

export interface INotebookDocumentPropertiesChangeData {
metadata: NotebookDocumentMetadata | null;
selections: INotebookSelectionChangeEvent | null;
}

export interface INotebookModelAddedData {
Expand Down
17 changes: 8 additions & 9 deletions src/vs/workbench/api/common/extHostNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1315,15 +1315,6 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
visibleRanges: editor.editor.visibleRanges
});
}
}

$acceptDocumentPropertiesChanged(uriComponents: UriComponents, data: INotebookDocumentPropertiesChangeData): void {
this.logService.debug('ExtHostNotebook#$acceptDocumentPropertiesChanged', uriComponents.path, data);
const editor = this._getEditorFromURI(uriComponents);

if (!editor) {
return;
}

if (data.selections) {
if (data.selections.selections.length) {
Expand All @@ -1338,7 +1329,15 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
selection: editor.editor.selection
});
}
}

$acceptDocumentPropertiesChanged(uriComponents: UriComponents, data: INotebookDocumentPropertiesChangeData): void {
this.logService.debug('ExtHostNotebook#$acceptDocumentPropertiesChanged', uriComponents.path, data);
const editor = this._getEditorFromURI(uriComponents);

if (!editor) {
return;
}

if (data.metadata) {
editor.editor.notebookData.notebookDocument.metadata = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export interface INotebookEditor extends IEditor {
getDomNode(): HTMLElement;
getOverflowContainerDomNode(): HTMLElement;
getInnerWebview(): Webview | undefined;
getSelectionHandles(): number[];

/**
* Focus the notebook editor cell list
Expand Down
11 changes: 11 additions & 0 deletions src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
this._cursorNavigationMode = v;
}

private readonly _onDidChangeSelection = this._register(new Emitter<void>());
get onDidChangeSelection(): Event<void> { return this._onDidChangeSelection.event; }

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

Expand Down Expand Up @@ -268,6 +271,10 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
return this._uuid;
}

getSelectionHandles(): number[] {
return this.viewModel?.selectionHandles || [];
}

hasModel() {
return !!this._notebookViewModel;
}
Expand Down Expand Up @@ -933,6 +940,10 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor
}
}

this._localStore.add(this.viewModel.onDidChangeSelection(() => {
this._onDidChangeSelection.fire();
}));

this._localStore.add(this._list!.onWillScroll(e => {
this._onWillScroll.fire(e);
if (!this._webviewResolved) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
}

this._selections = selections;
this._notebook.selections = selections;
this._onDidChangeSelection.fire();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
get emitSelections() { return this._emitSelections.event; }
private _onDidModelChangeProxy = this._register(new Emitter<NotebookCellsChangedEvent>());
get onDidModelChangeProxy(): Event<NotebookCellsChangedEvent> { return this._onDidModelChangeProxy.event; }
private _onDidSelectionChangeProxy = this._register(new Emitter<number[] | null>());
get onDidSelectionChange(): Event<number[] | null> { return this._onDidSelectionChangeProxy.event; }
private _onDidChangeContent = this._register(new Emitter<void>());
onDidChangeContent: Event<void> = this._onDidChangeContent.event;
private _onDidChangeMetadata = this._register(new Emitter<NotebookDocumentMetadata>());
Expand Down Expand Up @@ -151,17 +149,6 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
return this._versionId;
}

private _selections: number[] = [];

get selections() {
return this._selections;
}

set selections(selections: number[]) {
this._selections = selections;
this._onDidSelectionChangeProxy.fire(this._selections);
}

private _dirty = false;
protected readonly _onDidChangeDirty = this._register(new Emitter<void>());
readonly onDidChangeDirty = this._onDidChangeDirty.event;
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/notebook/common/notebookCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ export interface IEditor extends editorCommon.ICompositeCodeEditor {
readonly onDidChangeModel: Event<NotebookTextModel | undefined>;
readonly onDidFocusEditorWidget: Event<void>;
readonly onDidChangeVisibleRanges: Event<void>;
readonly onDidChangeSelection: Event<void>;
getSelectionHandles(): number[];
isNotebookEditor: boolean;
visibleRanges: ICellRange[];
uri?: URI;
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export class TestNotebookEditor implements INotebookEditor {

constructor(
) { }
getSelectionHandles(): number[] {
return [];
}


setOptions(options: NotebookEditorOptions | undefined): Promise<void> {
Expand All @@ -82,6 +85,7 @@ export class TestNotebookEditor implements INotebookEditor {
onDidScroll = new Emitter<ScrollEvent>().event;
onWillDispose = new Emitter<void>().event;
onDidChangeVisibleRanges: Event<void> = new Emitter<void>().event;
onDidChangeSelection: Event<void> = new Emitter<void>().event;
visibleRanges: ICellRange[] = [];
uri?: URI | undefined;
textModel?: NotebookTextModel | undefined;
Expand Down

0 comments on commit 11158cc

Please sign in to comment.