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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*---------------------------------------------------------------------------------------------
* 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 { IActiveCodeEditor, ICodeEditor, IOverlayWidget, IOverlayWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { OutlineModel, OutlineElement } from 'vs/editor/contrib/documentSymbols/browser/outlineModel';
import { CancellationToken, CancellationTokenSource, } from 'vs/base/common/cancellation';
import * as dom from 'vs/base/browser/dom';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { createStringBuilder } from 'vs/editor/common/core/stringBuilder';
import { RenderLineInput, renderViewLine } from 'vs/editor/common/viewLayout/viewLineRenderer';
import { SymbolKind } from 'vs/editor/common/languages';
import { LineDecoration } from 'vs/editor/common/viewLayout/lineDecorations';
import { RunOnceScheduler } from 'vs/base/common/async';
import { IModelTokensChangedEvent } from 'vs/editor/common/textModelEvents';
class StickyScrollController extends Disposable implements IEditorContribution {
static readonly ID = 'store.contrib.stickyScrollController';
private readonly _editor: ICodeEditor;
private readonly stickyScrollWidget: StickyScrollWidget;
private readonly _languageFeaturesService: ILanguageFeaturesService;
private readonly _sessionStore: DisposableStore = new DisposableStore();
private _ranges: [number, number, number][] = [];
private _rangesVersionId: number = 0;
private _cts: CancellationTokenSource | undefined;
private readonly _updateSoon: RunOnceScheduler;
constructor(
editor: ICodeEditor,
@ILanguageFeaturesService _languageFeaturesService: ILanguageFeaturesService,
) {
super();
this._editor = editor;
this._languageFeaturesService = _languageFeaturesService;
this.stickyScrollWidget = new StickyScrollWidget(this._editor);
this._register(this._editor.onDidChangeConfiguration(e => {
if (e.hasChanged(EditorOption.stickyScroll)) {
this.onConfigurationChange();
}
}));
this._updateSoon = this._register(new RunOnceScheduler(() => this._update(true), 50));
this.onConfigurationChange();
}
private onConfigurationChange() {
const options = this._editor.getOption(EditorOption.stickyScroll);
if (options.enabled === false) {
this.stickyScrollWidget.emptyRootNode();
this._editor.removeOverlayWidget(this.stickyScrollWidget);
this._sessionStore.clear();
return;
} else {
this._editor.addOverlayWidget(this.stickyScrollWidget);
this._sessionStore.add(this._editor.onDidChangeModel(() => this._update(true)));
this._sessionStore.add(this._editor.onDidScrollChange(() => this._update(false)));
this._sessionStore.add(this._editor.onDidChangeModelTokens((e) => this._onTokensChange(e)));
this._sessionStore.add(this._editor.onDidChangeModelContent(() => this._updateSoon.schedule()));
this._sessionStore.add(this._languageFeaturesService.documentSymbolProvider.onDidChange(() => this._update(true)));
this._update(true);
}
}
private _needsUpdate(event: IModelTokensChangedEvent) {
const stickyLineNumbers = this.stickyScrollWidget.getCurrentLines();
for (const stickyLineNumber of stickyLineNumbers) {
for (const range of event.ranges) {
if (stickyLineNumber >= range.fromLineNumber && stickyLineNumber <= range.toLineNumber) {
return true;
}
}
}
return false;
}
private _onTokensChange(event: IModelTokensChangedEvent) {
if (this._needsUpdate(event)) {
this._update(false);
}
}
private async _update(updateOutline: boolean = false): Promise<void> {
if (updateOutline) {
this._cts?.dispose(true);
this._cts = new CancellationTokenSource();
await this._updateOutlineModel(this._cts.token);
}
this._renderStickyScroll();
}
private _findLineRanges(outlineElement: OutlineElement, depth: number) {
if (outlineElement?.children.size) {
let didRecursion: boolean = false;
for (const outline of outlineElement?.children.values()) {
const kind: SymbolKind = outline.symbol.kind;
if (kind === SymbolKind.Class || kind === SymbolKind.Constructor || kind === SymbolKind.Function || kind === SymbolKind.Interface || kind === SymbolKind.Method) {
didRecursion = true;
this._findLineRanges(outline, depth + 1);
}
}
if (!didRecursion) {
this._addOutlineRanges(outlineElement, depth);
}
} else {
this._addOutlineRanges(outlineElement, depth);
}
}
private _addOutlineRanges(outlineElement: OutlineElement, depth: number) {
let currentStartLine: number | undefined = 0;
let currentEndLine: number | undefined = 0;
while (outlineElement) {
const kind: SymbolKind = outlineElement.symbol.kind;
if (kind === SymbolKind.Class || kind === SymbolKind.Constructor || kind === SymbolKind.Function || kind === SymbolKind.Interface || kind === SymbolKind.Method) {
currentStartLine = outlineElement?.symbol.range.startLineNumber as number;
currentEndLine = outlineElement?.symbol.range.endLineNumber as number;
this._ranges.push([currentStartLine, currentEndLine, depth]);
depth--;
}
if (outlineElement.parent instanceof OutlineElement) {
outlineElement = outlineElement.parent;
} else {
break;
}
}
}
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);
if (token.isCancellationRequested) {
return;
}
this._ranges = [];
this._rangesVersionId = modelVersionId;
for (const outline of outlineModel.children.values()) {
if (outline instanceof OutlineElement) {
const kind: SymbolKind = outline.symbol.kind;
if (kind === SymbolKind.Class || kind === SymbolKind.Constructor || kind === SymbolKind.Function || kind === SymbolKind.Interface || kind === SymbolKind.Method) {
this._findLineRanges(outline, 1);
} else {
this._findLineRanges(outline, 0);
}
}
this._ranges = this._ranges.sort(function (a, b) {
if (a[0] !== b[0]) {
return a[0] - b[0];
} else if (a[1] !== b[1]) {
return b[1] - a[1];
} else {
return a[2] - b[2];
}
});
let previous: number[] = [];
for (const [index, arr] of this._ranges.entries()) {
const [start, end, _depth] = arr;
if (previous[0] === start && previous[1] === end) {
this._ranges.splice(index, 1);
} else {
previous = arr;
}
}
}
}
}
private _renderStickyScroll() {
if (!(this._editor.hasModel())) {
return;
}
const lineHeight: number = this._editor.getOption(EditorOption.lineHeight);
const model = this._editor.getModel();
if (this._rangesVersionId !== model.getVersionId()) {
// Old _ranges not updated yet
return;
}
const scrollTop = this._editor.getScrollTop();
this.stickyScrollWidget.emptyRootNode();
const beginningLinesConsidered: Set<number> = new Set<number>();
for (const [index, arr] of this._ranges.entries()) {
const [start, end, depth] = arr;
if (end - start > 0 && model.getLineContent(start) !== '') {
const topOfElementAtDepth = (depth - 1) * lineHeight;
const bottomOfElementAtDepth = depth * lineHeight;
const bottomOfBeginningLine = this._editor.getBottomForLineNumber(start) - scrollTop;
const topOfEndLine = this._editor.getTopForLineNumber(end) - scrollTop;
const bottomOfEndLine = this._editor.getBottomForLineNumber(end) - scrollTop;
if (!beginningLinesConsidered.has(start)) {
if (topOfElementAtDepth >= topOfEndLine - 1 && topOfElementAtDepth < bottomOfEndLine - 2) {
beginningLinesConsidered.add(start);
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, -1, bottomOfEndLine - bottomOfElementAtDepth));
break;
}
else if (bottomOfElementAtDepth > bottomOfBeginningLine - 1 && bottomOfElementAtDepth < bottomOfEndLine - 1) {
beginningLinesConsidered.add(start);
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, 0, 0));
}
} else {
this._ranges.splice(index, 1);
}
}
}
this.stickyScrollWidget.updateRootNode();
}
override dispose(): void {
super.dispose();
this._sessionStore.dispose();
}
}
const _ttPolicy = window.trustedTypes?.createPolicy('stickyScrollViewLayer', { createHTML: value => value });
class StickyScrollCodeLine {
public readonly effectiveLineHeight: number = 0;
constructor(private readonly _line: string, private readonly _lineNumber: number, private readonly _editor: IActiveCodeEditor,
private readonly _zIndex: number, private readonly _relativePosition: number) {
this.effectiveLineHeight = this._editor.getOption(EditorOption.lineHeight) + this._relativePosition;
}
get lineNumber() {
return this._lineNumber;
}
getDomNode() {
const root: HTMLElement = document.createElement('div');
const lineRenderingData = this._editor._getViewModel().getViewportViewLineRenderingData(this._editor.getVisibleRangesPlusViewportAboveBelow()[0], this._lineNumber);
let actualInlineDecorations: LineDecoration[];
try {
actualInlineDecorations = LineDecoration.filter(lineRenderingData.inlineDecorations, this._lineNumber, lineRenderingData.minColumn, lineRenderingData.maxColumn);
} catch (err) {
actualInlineDecorations = [];
}
const renderLineInput: RenderLineInput = new RenderLineInput(true, true, this._line, lineRenderingData.continuesWithWrappedLine,
lineRenderingData.isBasicASCII, lineRenderingData.containsRTL, 0, lineRenderingData.tokens, actualInlineDecorations, lineRenderingData.tabSize,
lineRenderingData.startVisibleColumn, 1, 1, 1, 100, 'none', true, true, null);
const sb = createStringBuilder(400);
renderViewLine(renderLineInput, sb);
let newLine;
if (_ttPolicy) {
newLine = _ttPolicy.createHTML(sb.build() as string);
} else {
newLine = sb.build();
}
const lineHTMLNode = document.createElement('span');
lineHTMLNode.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
lineHTMLNode.style.overflow = 'hidden';
lineHTMLNode.style.whiteSpace = 'nowrap';
lineHTMLNode.style.display = 'inline-block';
lineHTMLNode.style.lineHeight = this._editor.getOption(EditorOption.lineHeight).toString() + 'px';
lineHTMLNode.innerHTML = newLine as string;
const lineNumberHTMLNode = document.createElement('span');
lineNumberHTMLNode.style.width = this._editor.getLayoutInfo().contentLeft.toString() + 'px';
lineNumberHTMLNode.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
lineNumberHTMLNode.style.color = 'var(--vscode-editorLineNumber-foreground)';
lineNumberHTMLNode.style.display = 'inline-block';
lineNumberHTMLNode.style.lineHeight = this._editor.getOption(EditorOption.lineHeight).toString() + 'px';
const innerLineNumberHTML = document.createElement('span');
innerLineNumberHTML.innerText = this._lineNumber.toString();
innerLineNumberHTML.style.paddingLeft = this._editor.getLayoutInfo().lineNumbersLeft.toString() + 'px';
innerLineNumberHTML.style.width = this._editor.getLayoutInfo().lineNumbersWidth.toString() + 'px';
innerLineNumberHTML.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
innerLineNumberHTML.style.textAlign = 'right';
innerLineNumberHTML.style.float = 'left';
innerLineNumberHTML.style.lineHeight = this._editor.getOption(EditorOption.lineHeight).toString() + 'px';
lineNumberHTMLNode.appendChild(innerLineNumberHTML);
root.onclick = e => {
e.stopPropagation();
e.preventDefault();
this._editor.revealLine(this._lineNumber);
};
root.onmouseover = e => {
innerLineNumberHTML.style.background = `var(--vscode-editorStickyScrollHover-background)`;
lineHTMLNode.style.backgroundColor = `var(--vscode-editorStickyScrollHover-background)`;
lineNumberHTMLNode.style.backgroundColor = `var(--vscode-editorStickyScrollHover-background)`;
root.style.backgroundColor = `var(--vscode-editorStickyScrollHover-background)`;
innerLineNumberHTML.style.cursor = `pointer`;
lineHTMLNode.style.cursor = `pointer`;
root.style.cursor = `pointer`;
lineNumberHTMLNode.style.cursor = `pointer`;
};
root.onmouseleave = e => {
innerLineNumberHTML.style.background = `var(--vscode-editorStickyScroll-background)`;
lineHTMLNode.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
lineNumberHTMLNode.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
root.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
};
this._editor.applyFontInfo(lineHTMLNode);
this._editor.applyFontInfo(innerLineNumberHTML);
root.appendChild(lineNumberHTMLNode);
root.appendChild(lineHTMLNode);
root.style.zIndex = this._zIndex.toString();
root.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
root.style.overflow = 'hidden';
root.style.whiteSpace = 'nowrap';
root.style.width = '100%';
root.style.lineHeight = this._editor.getOption(EditorOption.lineHeight).toString() + 'px';
root.style.height = this._editor.getOption(EditorOption.lineHeight).toString() + 'px';
// Special case for last line of sticky scroll
if (this._relativePosition) {
root.style.position = 'relative';
root.style.top = this._relativePosition + 'px';
root.style.width = '100%';
}
return root;
}
}
class StickyScrollWidget implements IOverlayWidget {
private readonly arrayOfCodeLines: StickyScrollCodeLine[] = [];
private readonly rootDomNode: HTMLElement = document.createElement('div');
constructor(public readonly _editor: ICodeEditor) {
this.rootDomNode = document.createElement('div');
this.rootDomNode.style.width = '100%';
this.rootDomNode.style.boxShadow = `var(--vscode-scrollbar-shadow) 0 6px 6px -6px`; // '0px 0px 8px 2px #000000';
}
get codeLineCount() {
return this.arrayOfCodeLines.length;
}
getCurrentLines(): number[] {
const widgetLineRange: number[] = [];
for (const codeLine of this.arrayOfCodeLines) {
widgetLineRange.push(codeLine.lineNumber);
}
return widgetLineRange;
}
pushCodeLine(codeLine: StickyScrollCodeLine) {
this.arrayOfCodeLines.push(codeLine);
}
updateRootNode() {
let widgetHeight: number = 0;
for (const line of this.arrayOfCodeLines) {
widgetHeight += line.effectiveLineHeight;
this.rootDomNode.appendChild(line.getDomNode());
}
this.rootDomNode.style.height = widgetHeight.toString() + 'px';
}
emptyRootNode() {
this.arrayOfCodeLines.length = 0;
dom.clearNode(this.rootDomNode);
}
getId(): string {
return 'editor.contrib.stickyScrollWidget';
}
getDomNode(): HTMLElement {
this.rootDomNode.style.zIndex = '2';
this.rootDomNode.style.backgroundColor = `var(--vscode-editorStickyScroll-background)`;
return this.rootDomNode;
}
getPosition(): IOverlayWidgetPosition | null {
return {
preference: null
};
}
}
registerEditorContribution(StickyScrollController.ID, StickyScrollController);
|