From 2756d908c75bde0dffd68f06a1aa00de6fcdb6b2 Mon Sep 17 00:00:00 2001 From: Jonah Iden Date: Tue, 19 Nov 2024 11:02:06 +0100 Subject: [PATCH] fix selection and improve active text editor behaviour Signed-off-by: Jonah Iden --- .../plugin-ext/src/main/browser/main-context.ts | 2 +- .../notebook-documents-and-editors-main.ts | 9 ++++++++- .../src/main/browser/text-editor-main.ts | 15 ++++++++++----- .../plugin-ext/src/plugin/notebook/notebooks.ts | 14 ++++++++------ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/plugin-ext/src/main/browser/main-context.ts b/packages/plugin-ext/src/main/browser/main-context.ts index 6467bf99c2b35..4b3043c858132 100644 --- a/packages/plugin-ext/src/main/browser/main-context.ts +++ b/packages/plugin-ext/src/main/browser/main-context.ts @@ -109,7 +109,7 @@ export function setUpPluginApi(rpc: RPCProtocol, container: interfaces.Container rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOK_RENDERERS_MAIN, new NotebookRenderersMainImpl(rpc, container)); const notebookEditorsMain = new NotebookEditorsMainImpl(rpc, container); rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOK_EDITORS_MAIN, notebookEditorsMain); - rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOK_DOCUMENTS_AND_EDITORS_MAIN, new NotebooksAndEditorsMain(rpc, container, notebookDocumentsMain, notebookEditorsMain)); + rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOK_DOCUMENTS_AND_EDITORS_MAIN, new NotebooksAndEditorsMain(rpc, container, tabsMain, notebookDocumentsMain, notebookEditorsMain)); rpc.set(PLUGIN_RPC_CONTEXT.NOTEBOOK_KERNELS_MAIN, new NotebookKernelsMainImpl(rpc, container)); const editorsMain = new TextEditorsMainImpl(editorsAndDocuments, documentsMain, rpc, container); diff --git a/packages/plugin-ext/src/main/browser/notebooks/notebook-documents-and-editors-main.ts b/packages/plugin-ext/src/main/browser/notebooks/notebook-documents-and-editors-main.ts index 06d20cab9b225..400a771755b2e 100644 --- a/packages/plugin-ext/src/main/browser/notebooks/notebook-documents-and-editors-main.ts +++ b/packages/plugin-ext/src/main/browser/notebooks/notebook-documents-and-editors-main.ts @@ -31,6 +31,7 @@ import { NotebookEditorsMainImpl } from './notebook-editors-main'; import { NotebookDocumentsMainImpl } from './notebook-documents-main'; import { diffMaps, diffSets } from '../../../common/collections'; import { Mutex } from 'async-mutex'; +import { TabsMainImpl } from '../tabs/tabs-main'; interface NotebookAndEditorDelta { removedDocuments: UriComponents[]; @@ -96,6 +97,7 @@ export class NotebooksAndEditorsMain implements NotebookDocumentsAndEditorsMain constructor( rpc: RPCProtocol, container: interfaces.Container, + tabsMain: TabsMainImpl, protected readonly notebookDocumentsMain: NotebookDocumentsMainImpl, protected readonly notebookEditorsMain: NotebookEditorsMainImpl ) { @@ -113,7 +115,12 @@ export class NotebooksAndEditorsMain implements NotebookDocumentsAndEditorsMain // this.WidgetManager.onActiveEditorChanged(() => this.updateState(), this, this.disposables); this.notebookEditorService.onDidAddNotebookEditor(async editor => this.handleEditorAdd(editor), this, this.disposables); this.notebookEditorService.onDidRemoveNotebookEditor(async editor => this.handleEditorRemove(editor), this, this.disposables); - this.notebookEditorService.onDidChangeCurrentEditor(async editor => this.updateState(editor), this, this.disposables); + this.notebookEditorService.onDidChangeCurrentEditor(async editor => { + if (editor) { + await tabsMain.waitForWidget(editor); + } + this.updateState(editor); + }, this, this.disposables); } dispose(): void { diff --git a/packages/plugin-ext/src/main/browser/text-editor-main.ts b/packages/plugin-ext/src/main/browser/text-editor-main.ts index f71d93a18d3fe..0ce4f478ed8a7 100644 --- a/packages/plugin-ext/src/main/browser/text-editor-main.ts +++ b/packages/plugin-ext/src/main/browser/text-editor-main.ts @@ -388,11 +388,16 @@ export class TextEditorPropertiesMain { if (editor && editor instanceof MonacoEditor) { result = editor.getControl().getSelections() || undefined; } else if (editor && editor instanceof SimpleMonacoEditor) { - result = editor.getControl().getSelections()?.map(selection => new monaco.Selection( - selection.startLineNumber, - selection.startColumn, - selection.positionLineNumber, - selection.positionColumn)); + result = editor.getControl().getSelections()?.map(selection => { + const monacoSelection = new monaco.Selection( + selection.selectionStartLineNumber, + selection.selectionStartColumn, + selection.positionLineNumber, + selection.positionColumn); + monacoSelection.setStartPosition(selection.startLineNumber, selection.startColumn); + monacoSelection.setEndPosition(selection.endLineNumber, selection.endColumn); + return monacoSelection; + }); } if (!result && prevProperties) { diff --git a/packages/plugin-ext/src/plugin/notebook/notebooks.ts b/packages/plugin-ext/src/plugin/notebook/notebooks.ts index 65487fffb55aa..7745f267a2866 100644 --- a/packages/plugin-ext/src/plugin/notebook/notebooks.ts +++ b/packages/plugin-ext/src/plugin/notebook/notebooks.ts @@ -20,7 +20,7 @@ import { CancellationToken, Disposable, DisposableCollection, Emitter, Event, URI } from '@theia/core'; import { URI as TheiaURI } from '../types-impl'; -import * as theia from '@theia/plugin'; +import type * as theia from '@theia/plugin'; import { NotebookCellStatusBarListDto, NotebookDataDto, NotebookDocumentsAndEditorsDelta, NotebookDocumentShowOptions, NotebookDocumentsMain, NotebookEditorAddData, NotebookEditorsMain, NotebooksExt, NotebooksMain, Plugin, @@ -337,12 +337,14 @@ export class NotebooksExtImpl implements NotebooksExt { console.error(`FAILED to find active notebook editor ${delta.newActiveEditor}`); } this.activeNotebookEditor = this.editors.get(delta.newActiveEditor); - if (this.textDocumentsAndEditors.activeEditor()?.document.uri.path !== this.activeNotebookEditor?.notebookData.uri.path) { - this.textDocumentsAndEditors.acceptEditorsAndDocumentsDelta({ - newActiveEditor: null - }); - } this.onDidChangeActiveNotebookEditorEmitter.fire(this.activeNotebookEditor?.apiEditor); + + const newActiveCell = this.activeApiNotebookEditor?.notebook.cellAt(this.activeApiNotebookEditor.selection.start); + this.textDocumentsAndEditors.acceptEditorsAndDocumentsDelta({ + newActiveEditor: newActiveCell?.kind === 2 /* code cell */ ? + this.textDocumentsAndEditors.allEditors().find(editor => editor.document.uri.toString() === newActiveCell.document.uri.toString())?.id ?? null : + null + }); } }