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

clean up goto cell code lenses when an IW is closed #15093

Merged
merged 1 commit into from
Jan 30, 2024
Merged
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
33 changes: 22 additions & 11 deletions src/interactive-window/editor-integration/codewatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
commands,
Event,
EventEmitter,
NotebookDocument,
Position,
Range,
Selection,
Expand All @@ -24,14 +25,15 @@ import { isUri, noop } from '../../platform/common/utils/misc';
import { capturePerfTelemetry, captureUsageTelemetry, sendTelemetryEvent } from '../../telemetry';
import { ICodeExecutionHelper } from '../../platform/terminals/types';
import { InteractiveCellResultError } from '../../platform/errors/interactiveCellResultError';
import { Telemetry, Commands, Identifiers } from '../../platform/common/constants';
import { Telemetry, Commands, Identifiers, InteractiveWindowView } from '../../platform/common/constants';
import { IInteractiveWindowProvider, IInteractiveWindow } from '../types';
import { CellMatcher } from './cellMatcher';
import { ICodeWatcher, ICodeLensFactory } from './types';
import { traceDecoratorVerbose } from '../../platform/logging';
import { TraceOptions } from '../../platform/logging/types';
import * as urlPath from '../../platform/vscode-path/resources';
import { IDataScienceErrorHandler } from '../../kernels/errors/types';
import { dispose } from '../../platform/common/utils/lifecycle';

function getIndex(index: number, length: number): number {
// return index within the length range with negative indexing
Expand Down Expand Up @@ -63,8 +65,7 @@ export class CodeWatcher implements ICodeWatcher {
private codeLenses: CodeLens[] = [];
private cells: ICellRange[] = [];
private codeLensUpdatedEvent: EventEmitter<void> = new EventEmitter<void>();
private updateRequiredDisposable: IDisposable | undefined;
private closeDocumentDisposable: IDisposable | undefined;
private disposables: IDisposable[] = [this.codeLensUpdatedEvent];

constructor(
@inject(IInteractiveWindowProvider) private interactiveWindowProvider: IInteractiveWindowProvider,
Expand All @@ -85,10 +86,13 @@ export class CodeWatcher implements ICodeWatcher {
this.cells = this.codeLensFactory.getCellRanges(document);

// Listen for changes
this.updateRequiredDisposable = this.codeLensFactory.updateRequired(this.onCodeLensFactoryUpdated.bind(this));
this.disposables.push(this.codeLensFactory.updateRequired(this.onCodeLensFactoryUpdated.bind(this)));

// Make sure to stop listening for changes when this document closes.
this.closeDocumentDisposable = workspace.onDidCloseTextDocument(this.onDocumentClosed.bind(this));
this.disposables.push(workspace.onDidCloseTextDocument(this.onDocumentClosed.bind(this)));

// clean up goto code lenses when interactive windows close
this.disposables.push(workspace.onDidCloseNotebookDocument(this.ondidCloseNotebook.bind(this)));
}

public get codeLensUpdated(): Event<void> {
Expand Down Expand Up @@ -131,9 +135,7 @@ export class CodeWatcher implements ICodeWatcher {
});
}

this.codeLensUpdatedEvent.dispose();
this.closeDocumentDisposable?.dispose(); // NOSONAR
this.updateRequiredDisposable?.dispose(); // NOSONAR
dispose(this.disposables);
}

@captureUsageTelemetry(Telemetry.RunAllCells)
Expand Down Expand Up @@ -999,15 +1001,23 @@ export class CodeWatcher implements ICodeWatcher {

private onDocumentClosed(doc: TextDocument): void {
if (this.document && urlPath.isEqual(doc.uri, this.document.uri)) {
this.codeLensUpdatedEvent.dispose();
this.closeDocumentDisposable?.dispose(); // NOSONAR
this.updateRequiredDisposable?.dispose(); // NOSONAR
dispose(this.disposables);
}
}

private ondidCloseNotebook(notebook: NotebookDocument): void {
if (
notebook.notebookType === InteractiveWindowView &&
this.configService.getSettings(this.document?.uri).addGotoCodeLenses
) {
this.onCodeLensFactoryUpdated();
}
}

private getActiveInteractiveWindow() {
return this.interactiveWindowProvider.getOrCreate(this.document?.uri);
}

private async addCode(
interactiveWindow: IInteractiveWindow,
code: string,
Expand All @@ -1030,6 +1040,7 @@ export class CodeWatcher implements ICodeWatcher {

return result;
}

private async runMatchingCell(range: Range, advance?: boolean, debug?: boolean) {
const currentRunCellLens = this.getCurrentCellLens(range.start);
const nextRunCellLens = this.getNextCellLens(range.start);
Expand Down
Loading