forked from microsoft/vscode
/
stickyScrollProvider.ts
170 lines (155 loc) · 6.51 KB
/
stickyScrollProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { OutlineModel, OutlineElement, OutlineGroup } from 'vs/editor/contrib/documentSymbols/browser/outlineModel';
import { CancellationToken, CancellationTokenSource, } from 'vs/base/common/cancellation';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { RunOnceScheduler } from 'vs/base/common/async';
import { Range } from 'vs/editor/common/core/range';
import { Emitter } from 'vs/base/common/event';
export class StickyRange {
constructor(
public readonly startLineNumber: number,
public readonly endLineNumber: number
) { }
}
export class StickyLineCandidate {
constructor(
public readonly startLineNumber: number,
public readonly endLineNumber: number,
public readonly nestingDepth: number,
) { }
}
export class StickyLineCandidateProvider extends Disposable {
private readonly onStickyScrollChangeEmitter = this._register(new Emitter<void>());
public readonly onStickyScrollChange = this.onStickyScrollChangeEmitter.event;
static readonly ID = 'store.contrib.stickyScrollController';
private readonly editor: ICodeEditor;
private readonly languageFeaturesService: ILanguageFeaturesService;
private readonly updateSoon: RunOnceScheduler;
private cts: CancellationTokenSource | undefined;
private outlineModel: StickyOutlineElement | undefined;
private readonly sessionStore: DisposableStore = new DisposableStore();
private modelVersionId: number = 0;
constructor(
editor: ICodeEditor,
@ILanguageFeaturesService _languageFeaturesService: ILanguageFeaturesService,
) {
super();
this.editor = editor;
this.languageFeaturesService = _languageFeaturesService;
this.updateSoon = this._register(new RunOnceScheduler(() => this.update(), 50));
this._register(this.editor.onDidChangeConfiguration(e => {
if (e.hasChanged(EditorOption.experimental)) {
this.readConfiguration();
}
}));
this.readConfiguration();
}
private readConfiguration() {
const options = this.editor.getOption(EditorOption.experimental);
if (options.stickyScroll.enabled === false) {
this.sessionStore.clear();
return;
} else {
this.sessionStore.add(this.editor.onDidChangeModel(() => this.update()));
this.sessionStore.add(this.editor.onDidChangeHiddenAreas(() => this.update()));
this.sessionStore.add(this.editor.onDidChangeModelContent(() => this.updateSoon.schedule()));
this.sessionStore.add(this.languageFeaturesService.documentSymbolProvider.onDidChange(() => this.update()));
this.update();
}
}
public getVersionId() {
return this.modelVersionId;
}
private async update(): Promise<void> {
this.cts?.dispose(true);
this.cts = new CancellationTokenSource();
await this.updateOutlineModel(this.cts.token);
this.onStickyScrollChangeEmitter.fire();
}
private async updateOutlineModel(token: CancellationToken) {
if (this.editor.hasModel()) {
const model = this.editor.getModel();
const modelVersionId = model.getVersionId();
const outlineModel = await OutlineModel.create(this.languageFeaturesService.documentSymbolProvider, model, token) as OutlineModel;
if (token.isCancellationRequested) {
return;
}
this.outlineModel = StickyOutlineElement.fromOutlineModel(outlineModel);
this.modelVersionId = modelVersionId;
}
}
public getCandidateStickyLinesIntersectingFromOutline(range: StickyRange, outlineModel: StickyOutlineElement, result: StickyLineCandidate[], depth: number, lastStartLineNumber: number): void {
let lastLine = lastStartLineNumber;
for (const child of outlineModel.children) {
if (child.range) {
const childStartLine = child.range.startLineNumber;
const childEndLine = child.range.endLineNumber;
if (range.startLineNumber <= childEndLine + 1 && childStartLine - 1 <= range.endLineNumber && childStartLine !== lastLine) {
lastLine = childStartLine;
result.push(new StickyLineCandidate(childStartLine, childEndLine - 1, depth + 1));
this.getCandidateStickyLinesIntersectingFromOutline(range, child, result, depth + 1, childStartLine);
}
} else {
this.getCandidateStickyLinesIntersectingFromOutline(range, child, result, depth, lastStartLineNumber);
}
}
}
public getCandidateStickyLinesIntersecting(range: StickyRange): StickyLineCandidate[] {
let stickyLineCandidates: StickyLineCandidate[] = [];
this.getCandidateStickyLinesIntersectingFromOutline(range, this.outlineModel as StickyOutlineElement, stickyLineCandidates, 0, -1);
const hiddenRanges: Range[] | undefined = this.editor._getViewModel()?.getHiddenAreas();
if (hiddenRanges) {
for (const hiddenRange of hiddenRanges) {
stickyLineCandidates = stickyLineCandidates.filter(stickyLine => !(stickyLine.startLineNumber >= hiddenRange.startLineNumber && stickyLine.endLineNumber <= hiddenRange.endLineNumber + 1));
}
}
return stickyLineCandidates;
}
override dispose(): void {
super.dispose();
this.sessionStore.dispose();
}
}
class StickyOutlineElement {
public static fromOutlineModel(outlineModel: OutlineModel | OutlineElement | OutlineGroup): StickyOutlineElement {
const children = [...outlineModel.children.values()].map(child =>
StickyOutlineElement.fromOutlineModel(child)
);
children.sort((child1, child2) => {
if (!child1.range || !child2.range) {
return 1;
} else if (child1.range.startLineNumber !== child2.range.startLineNumber) {
return child1.range.startLineNumber - child2.range.startLineNumber;
} else {
return child2.range.endLineNumber - child1.range.endLineNumber;
}
});
let range: StickyRange | undefined;
if (outlineModel instanceof OutlineElement) {
range = new StickyRange(outlineModel.symbol.selectionRange.startLineNumber, outlineModel.symbol.range.endLineNumber);
} else {
range = undefined;
}
return new StickyOutlineElement(
range,
children
);
}
constructor(
/**
* Range of line numbers spanned by the current scope
*/
public readonly range: StickyRange | undefined,
/**
* Must be sorted by start line number
*/
public readonly children: readonly StickyOutlineElement[],
) {
}
}