Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed notebook editor staying current even when selecting sidebar or bottom panel #14262

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
// *****************************************************************************

import { Emitter, URI } from '@theia/core';
import { injectable } from '@theia/core/shared/inversify';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import { SimpleMonacoEditor } from '@theia/monaco/lib/browser/simple-monaco-editor';
import { NotebookEditorWidgetService } from './notebook-editor-widget-service';
import { CellUri } from '../../common';

@injectable()
export class NotebookCellEditorService {

@inject(NotebookEditorWidgetService)
protected readonly notebookEditorWidgetService: NotebookEditorWidgetService;

protected onDidChangeCellEditorsEmitter = new Emitter<void>();
readonly onDidChangeCellEditors = this.onDidChangeCellEditorsEmitter.event;

Expand All @@ -31,6 +36,17 @@ export class NotebookCellEditorService {

protected currentCellEditors: Map<string, SimpleMonacoEditor> = new Map();

@postConstruct()
protected init(): void {
this.notebookEditorWidgetService.onDidChangeCurrentEditor(editor => {
// if defocus notebook editor or another notebook editor is focused, clear the active cell
if (!editor || (this.currentActiveCell && CellUri.parse(this.currentActiveCell.uri)?.notebook.toString() !== editor?.model?.uri.toString())) {
this.currentActiveCell = undefined;
this.onDidChangeFocusedCellEditorEmitter.fire(undefined);
}
});
}

get allCellEditors(): SimpleMonacoEditor[] {
return Array.from(this.currentCellEditors.values());
}
Expand All @@ -46,8 +62,10 @@ export class NotebookCellEditorService {
}

editorFocusChanged(editor?: SimpleMonacoEditor): void {
this.currentActiveCell = editor;
this.onDidChangeFocusedCellEditorEmitter.fire(editor);
if (editor) {
this.currentActiveCell = editor;
this.onDidChangeFocusedCellEditorEmitter.fire(editor);
}
}

getActiveCell(): SimpleMonacoEditor | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,23 @@ export class NotebookEditorWidgetService {
protected readonly onDidChangeFocusedEditorEmitter = new Emitter<NotebookEditorWidget | undefined>();
readonly onDidChangeFocusedEditor = this.onDidChangeFocusedEditorEmitter.event;

protected readonly onDidChangeCurrentEditorEmitter = new Emitter<NotebookEditorWidget | undefined>();
readonly onDidChangeCurrentEditor = this.onDidChangeCurrentEditorEmitter.event;

focusedEditor?: NotebookEditorWidget = undefined;

currentEditor?: NotebookEditorWidget = undefined;

@postConstruct()
protected init(): void {
this.applicationShell.onDidChangeActiveWidget(event => {
this.notebookEditorFocusChanged(event.newValue as NotebookEditorWidget, event.newValue instanceof NotebookEditorWidget);
});
this.applicationShell.onDidChangeCurrentWidget(event => {
if (event.newValue instanceof NotebookEditorWidget || event.oldValue instanceof NotebookEditorWidget) {
this.currentNotebookEditorChanged(event.newValue);
}
});
}

// --- editor management
Expand Down Expand Up @@ -98,4 +108,14 @@ export class NotebookEditorWidgetService {
}
}

currentNotebookEditorChanged(newEditor: unknown): void {
if (newEditor instanceof NotebookEditorWidget) {
this.currentEditor = newEditor;
this.onDidChangeCurrentEditorEmitter.fire(newEditor);
} else if (this.currentEditor?.isDisposed || !this.currentEditor?.isVisible) {
this.currentEditor = undefined;
this.onDidChangeCurrentEditorEmitter.fire(undefined);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class EditorAndDocumentStateComputer implements Disposable {

this.toDispose.push(this.cellEditorService.onDidChangeCellEditors(() => this.update()));

this.toDispose.push(this.notebookWidgetService.onDidChangeFocusedEditor(() => {
this.toDispose.push(this.notebookWidgetService.onDidChangeCurrentEditor(() => {
this.currentState = this.currentState && new EditorAndDocumentState(
this.currentState.documents,
this.currentState.editors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ class NotebookAndEditorState {
const documentDelta = diffSets(before.documents, after.documents);
const editorDelta = diffMaps(before.textEditors, after.textEditors);

const newActiveEditor = before.activeEditor !== after.activeEditor ? after.activeEditor : undefined;
const visibleEditorDelta = diffMaps(before.visibleEditors, after.visibleEditors);

return {
addedDocuments: documentDelta.added,
removedDocuments: documentDelta.removed.map(e => e.uri.toComponents()),
addedEditors: editorDelta.added,
removedEditors: editorDelta.removed.map(removed => removed.id),
newActiveEditor: newActiveEditor,
newActiveEditor: after.activeEditor,
visibleEditors: visibleEditorDelta.added.length === 0 && visibleEditorDelta.removed.length === 0
? undefined
: [...after.visibleEditors].map(editor => editor[0])
Expand Down Expand Up @@ -114,7 +113,7 @@ 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.onDidChangeFocusedEditor(async editor => this.updateState(editor), this, this.disposables);
this.notebookEditorService.onDidChangeCurrentEditor(async editor => this.updateState(editor), this, this.disposables);
}

dispose(): void {
Expand Down Expand Up @@ -221,7 +220,7 @@ export class NotebooksAndEditorsMain implements NotebookDocumentsAndEditorsMain
if (delta.visibleEditors?.length) {
return false;
}
if (delta.newActiveEditor) {
if (delta.newActiveEditor !== undefined) {
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/notebook/notebooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export class NotebooksExtImpl implements NotebooksExt {
});
}
}
if (delta.newActiveEditor !== undefined) {
if (delta.newActiveEditor !== undefined && delta.newActiveEditor !== this.activeNotebookEditor?.id) {
this.onDidChangeActiveNotebookEditorEmitter.fire(this.activeNotebookEditor?.apiEditor);
}
}
Expand Down
Loading