diff --git a/src/vs/workbench/api/browser/mainThreadNotebook.ts b/src/vs/workbench/api/browser/mainThreadNotebook.ts index d144bec6070..afd3c5345f2 100644 --- a/src/vs/workbench/api/browser/mainThreadNotebook.ts +++ b/src/vs/workbench/api/browser/mainThreadNotebook.ts @@ -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 { @@ -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 })); @@ -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); @@ -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); @@ -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) => { diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 4b794666295..1f3a03d8a5b 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -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 { diff --git a/src/vs/workbench/api/common/extHostNotebook.ts b/src/vs/workbench/api/common/extHostNotebook.ts index 2cd2da1b021..0c4bf7c66aa 100644 --- a/src/vs/workbench/api/common/extHostNotebook.ts +++ b/src/vs/workbench/api/common/extHostNotebook.ts @@ -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) { @@ -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 = { diff --git a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts index 5cc672c00e1..0c49b446411 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts @@ -233,6 +233,7 @@ export interface INotebookEditor extends IEditor { getDomNode(): HTMLElement; getOverflowContainerDomNode(): HTMLElement; getInnerWebview(): Webview | undefined; + getSelectionHandles(): number[]; /** * Focus the notebook editor cell list diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts index b2588dd631b..d6b3bdc6066 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts @@ -215,6 +215,9 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor this._cursorNavigationMode = v; } + private readonly _onDidChangeSelection = this._register(new Emitter()); + get onDidChangeSelection(): Event { return this._onDidChangeSelection.event; } + private readonly _onDidChangeVisibleRanges = this._register(new Emitter()); onDidChangeVisibleRanges: Event = this._onDidChangeVisibleRanges.event; @@ -268,6 +271,10 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor return this._uuid; } + getSelectionHandles(): number[] { + return this.viewModel?.selectionHandles || []; + } + hasModel() { return !!this._notebookViewModel; } @@ -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) { diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts index b06cb8d453a..15d2b30af18 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts @@ -218,7 +218,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD } this._selections = selections; - this._notebook.selections = selections; this._onDidChangeSelection.fire(); } diff --git a/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts b/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts index 805c8bb61a6..10e19185977 100644 --- a/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts +++ b/src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts @@ -118,8 +118,6 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel get emitSelections() { return this._emitSelections.event; } private _onDidModelChangeProxy = this._register(new Emitter()); get onDidModelChangeProxy(): Event { return this._onDidModelChangeProxy.event; } - private _onDidSelectionChangeProxy = this._register(new Emitter()); - get onDidSelectionChange(): Event { return this._onDidSelectionChangeProxy.event; } private _onDidChangeContent = this._register(new Emitter()); onDidChangeContent: Event = this._onDidChangeContent.event; private _onDidChangeMetadata = this._register(new Emitter()); @@ -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()); readonly onDidChangeDirty = this._onDidChangeDirty.event; diff --git a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts index b8753742ac4..e13425768bf 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts @@ -688,6 +688,8 @@ export interface IEditor extends editorCommon.ICompositeCodeEditor { readonly onDidChangeModel: Event; readonly onDidFocusEditorWidget: Event; readonly onDidChangeVisibleRanges: Event; + readonly onDidChangeSelection: Event; + getSelectionHandles(): number[]; isNotebookEditor: boolean; visibleRanges: ICellRange[]; uri?: URI; diff --git a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts index 33146612f34..6872b80d42b 100644 --- a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts @@ -66,6 +66,9 @@ export class TestNotebookEditor implements INotebookEditor { constructor( ) { } + getSelectionHandles(): number[] { + return []; + } setOptions(options: NotebookEditorOptions | undefined): Promise { @@ -82,6 +85,7 @@ export class TestNotebookEditor implements INotebookEditor { onDidScroll = new Emitter().event; onWillDispose = new Emitter().event; onDidChangeVisibleRanges: Event = new Emitter().event; + onDidChangeSelection: Event = new Emitter().event; visibleRanges: ICellRange[] = []; uri?: URI | undefined; textModel?: NotebookTextModel | undefined;