Skip to content

Commit a478488

Browse files
authored
Handle model changes when updating nb outline (microsoft#211205)
* Handle model changes when updating nb outline * More fixes
1 parent 6d23b7f commit a478488

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/vs/workbench/contrib/notebook/browser/viewModel/notebookOutlineProvider.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { URI } from 'vs/base/common/uri';
1010
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1111
import { IMarkerService } from 'vs/platform/markers/common/markers';
1212
import { IThemeService } from 'vs/platform/theme/common/themeService';
13-
import { IActiveNotebookEditor, ICellViewModel, INotebookEditor, type INotebookViewCellsUpdateEvent } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
13+
import { IActiveNotebookEditor, ICellViewModel, INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
1414
import { CellKind, NotebookCellsChangeType, NotebookSetting } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1515
import { INotebookExecutionStateService, NotebookExecutionType } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
1616
import { OutlineChangeEvent, OutlineConfigKeys, OutlineTarget } from 'vs/workbench/services/outline/browser/outline';
@@ -29,6 +29,10 @@ export class NotebookCellOutlineProvider {
2929
private _uri: URI | undefined;
3030
private _entries: OutlineEntry[] = [];
3131
get entries(): OutlineEntry[] {
32+
if (this.delayedOutlineRecompute.isTriggered()) {
33+
this.delayedOutlineRecompute.cancel();
34+
this._recomputeState();
35+
}
3236
return this._entries;
3337
}
3438

@@ -38,11 +42,15 @@ export class NotebookCellOutlineProvider {
3842
readonly outlineKind = 'notebookCells';
3943

4044
get activeElement(): OutlineEntry | undefined {
45+
if (this.delayedOutlineRecompute.isTriggered()) {
46+
this.delayedOutlineRecompute.cancel();
47+
this._recomputeState();
48+
}
4149
return this._activeEntry;
4250
}
4351

4452
private readonly _outlineEntryFactory: NotebookOutlineEntryFactory;
45-
private readonly delayRecomputeActive: () => void;
53+
private readonly delayedOutlineRecompute: Delayer<void>;;
4654
constructor(
4755
private readonly _editor: INotebookEditor,
4856
private readonly _target: OutlineTarget,
@@ -53,29 +61,19 @@ export class NotebookCellOutlineProvider {
5361
@IConfigurationService private readonly _configurationService: IConfigurationService,
5462
) {
5563
this._outlineEntryFactory = new NotebookOutlineEntryFactory(notebookExecutionStateService);
56-
const delayerRecomputeActive = this._disposables.add(new Delayer(10));
57-
this.delayRecomputeActive = () => delayerRecomputeActive.trigger(() => this._recomputeActive());
58-
59-
this._disposables.add(Event.debounce<void, void>(
60-
_editor.onDidChangeSelection,
61-
(last, _current) => last,
62-
200
63-
)(() => {
64-
this.delayRecomputeActive();
64+
65+
const delayerRecomputeActive = this._disposables.add(new Delayer(200));
66+
this._disposables.add(_editor.onDidChangeSelection(() => {
67+
delayerRecomputeActive.trigger(() => this._recomputeActive());
6568
}, this))
66-
this._disposables.add(Event.debounce<INotebookViewCellsUpdateEvent, INotebookViewCellsUpdateEvent>(
67-
_editor.onDidChangeViewCells,
68-
(last, _current) => last ?? _current,
69-
200
70-
)(() => {
71-
this.delayRecomputeActive();
72-
}, this)
73-
);
7469

7570
// .3s of a delay is sufficient, 100-200s is too quick and will unnecessarily block the ui thread.
7671
// Given we're only updating the outline when the user types, we can afford to wait a bit.
77-
const delayer = this._disposables.add(new Delayer<void>(300));
78-
const delayedRecompute = () => delayer.trigger(() => this._recomputeState());
72+
this.delayedOutlineRecompute = this._disposables.add(new Delayer<void>(300));
73+
const delayedRecompute = () => {
74+
delayerRecomputeActive.cancel(); // Active is always recomputed after a recomputing the outline state.
75+
this.delayedOutlineRecompute.trigger(() => this._recomputeState());
76+
};
7977

8078
this._disposables.add(_configurationService.onDidChangeConfiguration(e => {
8179
if (e.affectsConfiguration(NotebookSetting.outlineShowMarkdownHeadersOnly) ||
@@ -106,7 +104,10 @@ export class NotebookCellOutlineProvider {
106104
return;
107105
}
108106
disposable.add(this._editor.textModel.onDidChangeContent(contentChanges => {
109-
if (contentChanges.rawEvents.some(c => c.kind === NotebookCellsChangeType.ChangeCellContent)) {
107+
if (contentChanges.rawEvents.some(c => c.kind === NotebookCellsChangeType.ChangeCellContent ||
108+
c.kind === NotebookCellsChangeType.ChangeCellInternalMetadata ||
109+
c.kind === NotebookCellsChangeType.Move ||
110+
c.kind === NotebookCellsChangeType.ModelChange)) {
110111
delayedRecompute();
111112
}
112113
}));
@@ -259,7 +260,7 @@ export class NotebookCellOutlineProvider {
259260
}
260261
}));
261262

262-
this.delayRecomputeActive();
263+
this._recomputeActive();
263264
}
264265

265266
private _recomputeActive(): { changeEventTriggered: boolean } {

0 commit comments

Comments
 (0)