Skip to content

Commit

Permalink
watch for IW closing (#15093)
Browse files Browse the repository at this point in the history
  • Loading branch information
amunger authored Jan 30, 2024
1 parent 8ce2b44 commit 0112043
Showing 1 changed file with 22 additions and 11 deletions.
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

0 comments on commit 0112043

Please sign in to comment.