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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { h } from 'vs/base/browser/dom';
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
import { autorun, IReader, observableFromEvent, observableSignalFromEvent } from 'vs/base/common/observable';
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
import { LineRange } from 'vs/workbench/contrib/mergeEditor/browser/model/lineRange';
export class EditorGutter<T extends IGutterItemInfo = IGutterItemInfo> extends Disposable {
private readonly scrollTop = observableFromEvent(
this._editor.onDidScrollChange,
(e) => /** @description editor.onDidScrollChange */ this._editor.getScrollTop()
);
private readonly isScrollTopZero = this.scrollTop.map((scrollTop) => /** @description isScrollTopZero */ scrollTop === 0);
private readonly modelAttached = observableFromEvent(
this._editor.onDidChangeModel,
(e) => /** @description editor.onDidChangeModel */ this._editor.hasModel()
);
private readonly editorOnDidChangeViewZones = observableSignalFromEvent('onDidChangeViewZones', this._editor.onDidChangeViewZones);
private readonly editorOnDidContentSizeChange = observableSignalFromEvent('onDidContentSizeChange', this._editor.onDidContentSizeChange);
constructor(
private readonly _editor: CodeEditorWidget,
private readonly _domNode: HTMLElement,
private readonly itemProvider: IGutterItemProvider<T>
) {
super();
this._domNode.className = 'gutter monaco-editor';
const scrollDecoration = this._domNode.appendChild(
h('div.scroll-decoration', { role: 'presentation', ariaHidden: 'true', style: { width: '100%' } })
.root
);
this._register(autorun('update scroll decoration', (reader) => {
scrollDecoration.className = this.isScrollTopZero.read(reader) ? '' : 'scroll-decoration';
}));
this._register(autorun('EditorGutter.Render', (reader) => this.render(reader)));
}
private readonly views = new Map<string, ManagedGutterItemView>();
private render(reader: IReader): void {
if (!this.modelAttached.read(reader)) {
return;
}
this.editorOnDidChangeViewZones.read(reader);
this.editorOnDidContentSizeChange.read(reader);
const scrollTop = this.scrollTop.read(reader);
const visibleRanges = this._editor.getVisibleRanges();
const unusedIds = new Set(this.views.keys());
if (visibleRanges.length > 0) {
const visibleRange = visibleRanges[0];
const visibleRange2 = new LineRange(
visibleRange.startLineNumber,
visibleRange.endLineNumber - visibleRange.startLineNumber
).deltaEnd(1);
const gutterItems = this.itemProvider.getIntersectingGutterItems(
visibleRange2,
reader
);
for (const gutterItem of gutterItems) {
if (!gutterItem.range.touches(visibleRange2)) {
continue;
}
unusedIds.delete(gutterItem.id);
let view = this.views.get(gutterItem.id);
if (!view) {
const viewDomNode = document.createElement('div');
viewDomNode.className = 'gutter-item';
this._domNode.appendChild(viewDomNode);
const itemView = this.itemProvider.createView(
gutterItem,
viewDomNode
);
view = new ManagedGutterItemView(itemView, viewDomNode);
this.views.set(gutterItem.id, view);
} else {
view.gutterItemView.update(gutterItem);
}
const top =
gutterItem.range.startLineNumber <= this._editor.getModel()!.getLineCount()
? this._editor.getTopForLineNumber(gutterItem.range.startLineNumber, true) - scrollTop
: this._editor.getBottomForLineNumber(gutterItem.range.startLineNumber - 1, false) - scrollTop;
const bottom = this._editor.getBottomForLineNumber(gutterItem.range.endLineNumberExclusive - 1, true) - scrollTop;
const height = bottom - top;
view.domNode.style.top = `${top}px`;
view.domNode.style.height = `${height}px`;
view.gutterItemView.layout(top, height, 0, -1);
}
}
for (const id of unusedIds) {
const view = this.views.get(id)!;
view.gutterItemView.dispose();
this._domNode.removeChild(view.domNode);
this.views.delete(id);
}
}
}
class ManagedGutterItemView {
constructor(
public readonly gutterItemView: IGutterItemView<any>,
public readonly domNode: HTMLDivElement
) { }
}
export interface IGutterItemProvider<TItem extends IGutterItemInfo> {
getIntersectingGutterItems(range: LineRange, reader: IReader): TItem[];
createView(item: TItem, target: HTMLElement): IGutterItemView<TItem>;
}
export interface IGutterItemInfo {
id: string;
range: LineRange;
/*
// To accommodate view zones:
offsetInPx: number;
additionalHeightInPx: number;
*/
}
export interface IGutterItemView<T extends IGutterItemInfo> extends IDisposable {
update(item: T): void;
layout(top: number, height: number, viewTop: number, viewHeight: number): void;
}
|