-
Notifications
You must be signed in to change notification settings - Fork 29.4k
/
glyphMargin.ts
457 lines (389 loc) · 15.2 KB
/
glyphMargin.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/css!./glyphMargin';
import { DynamicViewOverlay } from 'vs/editor/browser/view/dynamicViewOverlay';
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
import { Range } from 'vs/editor/common/core/range';
import { IGlyphMarginWidget, IGlyphMarginWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { ViewPart } from 'vs/editor/browser/view/viewPart';
import { ViewContext } from 'vs/editor/common/viewModel/viewContext';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import * as viewEvents from 'vs/editor/common/viewEvents';
import { RenderingContext, RestrictedRenderingContext } from 'vs/editor/browser/view/renderingContext';
export class LineRenderedWidgets {
private readonly lines: GlyphMarginWidget[][] = [];
public add(line: number, decoration: GlyphMarginWidget) {
while (line >= this.lines.length) {
this.lines.push([]);
}
this.lines[line].push(decoration);
}
public getLineDecorations(lineIndex: number): GlyphMarginWidget[] {
if (lineIndex < this.lines.length) {
return this.lines[lineIndex];
}
return [];
}
}
export class GlyphMarginWidget {
constructor(
public domNode: FastDomNode<HTMLElement>,
public className: string,
public lineNumber: number,
public left: number,
public width: number,
public isManaged: boolean,
) {
this.domNode.setPosition('absolute');
this.domNode.setDisplay('none');
this.domNode.setVisibility('hidden');
this.domNode.setMaxWidth(width);
}
}
export class DecorationToRender {
_decorationToRenderBrand: void = undefined;
public startLineNumber: number;
public endLineNumber: number;
public className: string;
public readonly zIndex: number;
public readonly decorationLane: number;
public readonly domNode: FastDomNode<HTMLElement> | undefined;
constructor(startLineNumber: number, endLineNumber: number, className: string, zIndex?: number, decorationLane?: number, domNode?: FastDomNode<HTMLElement>) {
this.startLineNumber = +startLineNumber;
this.endLineNumber = +endLineNumber;
this.className = String(className);
this.zIndex = zIndex ?? 0;
this.decorationLane = decorationLane ?? 1;
this.domNode = domNode;
}
}
export class RenderedDecoration {
constructor(
public readonly className: string,
public readonly zIndex: number,
public readonly domNode?: FastDomNode<HTMLElement>,
) { }
}
export class LineRenderedDecorations {
private readonly lanes: RenderedDecoration[][] = [];
public add(lane: number, decoration: RenderedDecoration) {
while (lane >= this.lanes.length) {
this.lanes.push([]);
}
this.lanes[lane].push(decoration);
}
public getLaneDecorations(laneIndex: number): RenderedDecoration[] {
if (laneIndex < this.lanes.length) {
return this.lanes[laneIndex];
}
return [];
}
public isEmpty(): boolean {
for (const lane of this.lanes) {
if (lane.length > 0) {
return false;
}
}
return true;
}
}
export abstract class DedupOverlay extends DynamicViewOverlay {
protected _render(visibleStartLineNumber: number, visibleEndLineNumber: number, decorations: DecorationToRender[], decorationLaneCount: number): LineRenderedDecorations[] {
const output: LineRenderedDecorations[] = [];
for (let lineNumber = visibleStartLineNumber; lineNumber <= visibleEndLineNumber; lineNumber++) {
const lineIndex = lineNumber - visibleStartLineNumber;
output[lineIndex] = new LineRenderedDecorations();
}
if (decorations.length === 0) {
return output;
}
decorations.sort((a, b) => {
if (a.className === b.className) {
if (a.startLineNumber === b.startLineNumber) {
return a.endLineNumber - b.endLineNumber;
}
return a.startLineNumber - b.startLineNumber;
}
return (a.className < b.className ? -1 : 1);
});
let prevClassName: string | null = null;
let prevEndLineIndex = 0;
for (let i = 0, len = decorations.length; i < len; i++) {
const d = decorations[i];
const className = d.className;
const zIndex = d.zIndex;
let startLineIndex = Math.max(d.startLineNumber, visibleStartLineNumber) - visibleStartLineNumber;
const endLineIndex = Math.min(d.endLineNumber, visibleEndLineNumber) - visibleStartLineNumber;
const lane = Math.min(d.decorationLane, decorationLaneCount);
if (prevClassName === className) {
startLineIndex = Math.max(prevEndLineIndex + 1, startLineIndex);
prevEndLineIndex = Math.max(prevEndLineIndex, endLineIndex);
} else {
prevClassName = className;
prevEndLineIndex = endLineIndex;
}
for (let i = startLineIndex; i <= prevEndLineIndex; i++) {
output[i].add(lane, new RenderedDecoration(className, zIndex, d.domNode));
}
}
return output;
}
}
export interface IWidgetData {
widget: IGlyphMarginWidget;
preference: IGlyphMarginWidgetPosition;
domNode: FastDomNode<HTMLElement>;
}
export class GlyphMarginWidgets extends ViewPart {
public domNode: FastDomNode<HTMLElement>;
private _lineHeight: number;
private _glyphMargin: boolean;
private _glyphMarginLeft: number;
private _glyphMarginWidth: number;
private _glyphMarginDecorationLaneCount: number;
private _previousRenderResult: LineRenderedWidgets | null;
private _renderResult: LineRenderedWidgets | null;
private _widgets: { [key: string]: IWidgetData } = {};
constructor(context: ViewContext) {
super(context);
this._context = context;
const options = this._context.configuration.options;
const layoutInfo = options.get(EditorOption.layoutInfo);
this.domNode = createFastDomNode(document.createElement('div'));
this.domNode.setClassName('glyphMarginWidgets');
this.domNode.setPosition('absolute');
this.domNode.setTop(0);
this._lineHeight = options.get(EditorOption.lineHeight);
this._glyphMargin = options.get(EditorOption.glyphMargin);
this._glyphMarginLeft = layoutInfo.glyphMarginLeft;
this._glyphMarginWidth = layoutInfo.glyphMarginWidth;
this._glyphMarginDecorationLaneCount = layoutInfo.glyphMarginDecorationLaneCount;
this._previousRenderResult = null;
this._renderResult = null;
}
public override dispose(): void {
this._renderResult = null;
this._widgets = {};
super.dispose();
}
public getWidgets(): IWidgetData[] {
return Object.values(this._widgets);
}
// --- begin event handlers
public override onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
const options = this._context.configuration.options;
const layoutInfo = options.get(EditorOption.layoutInfo);
this._lineHeight = options.get(EditorOption.lineHeight);
this._glyphMargin = options.get(EditorOption.glyphMargin);
this._glyphMarginLeft = layoutInfo.glyphMarginLeft;
this._glyphMarginWidth = layoutInfo.glyphMarginWidth;
this._glyphMarginDecorationLaneCount = layoutInfo.glyphMarginDecorationLaneCount;
return true;
}
public override onDecorationsChanged(e: viewEvents.ViewDecorationsChangedEvent): boolean {
return true;
}
public override onFlushed(e: viewEvents.ViewFlushedEvent): boolean {
return true;
}
public override onLinesChanged(e: viewEvents.ViewLinesChangedEvent): boolean {
return true;
}
public override onLinesDeleted(e: viewEvents.ViewLinesDeletedEvent): boolean {
return true;
}
public override onLinesInserted(e: viewEvents.ViewLinesInsertedEvent): boolean {
return true;
}
public override onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
return e.scrollTopChanged;
}
public override onZonesChanged(e: viewEvents.ViewZonesChangedEvent): boolean {
return true;
}
// --- end event handlers
// --- begin widget management
public addWidget(widget: IGlyphMarginWidget): void {
const domNode = createFastDomNode(widget.getDomNode());
this._widgets[widget.getId()] = {
widget: widget,
preference: widget.getPosition(),
domNode: domNode
};
domNode.setPosition('absolute');
domNode.setAttribute('widgetId', widget.getId());
this.domNode.appendChild(domNode);
this.setShouldRender();
}
public setWidgetPosition(widget: IGlyphMarginWidget, preference: IGlyphMarginWidgetPosition): boolean {
const myWidget = this._widgets[widget.getId()];
if (myWidget.preference.lane === preference.lane
&& myWidget.preference.zIndex === preference.zIndex
&& Range.equalsRange(myWidget.preference.range, preference.range)) {
return false;
}
myWidget.preference = preference;
this.setShouldRender();
return true;
}
public removeWidget(widget: IGlyphMarginWidget): void {
const widgetId = widget.getId();
if (this._widgets[widgetId]) {
const widgetData = this._widgets[widgetId];
const domNode = widgetData.domNode.domNode;
delete this._widgets[widgetId];
domNode.parentNode?.removeChild(domNode);
this.setShouldRender();
}
}
// --- end widget management
protected _getDecorations(ctx: RenderingContext): DecorationToRender[] {
const decorations = ctx.getDecorationsInViewport();
const r: DecorationToRender[] = [];
let rLen = 0;
for (let i = 0, len = decorations.length; i < len; i++) {
const d = decorations[i];
const glyphMarginClassName = d.options.glyphMarginClassName;
const zIndex = d.options.zIndex;
const lane = d.options.glyphMargin?.position;
if (glyphMarginClassName) {
r[rLen++] = new DecorationToRender(d.range.startLineNumber, d.range.endLineNumber, glyphMarginClassName, zIndex, lane);
}
}
const widgets = Object.values(this._widgets);
for (let i = 0, len = widgets.length; i < len; i++) {
const w = widgets[i];
const glyphMarginClassName = w.widget.getDomNode().className;
if (glyphMarginClassName) {
r[rLen++] = new DecorationToRender(w.preference.range.startLineNumber, w.preference.range.endLineNumber, glyphMarginClassName, w.preference.zIndex, w.preference.lane, w.domNode);
}
}
return r;
}
protected _render(visibleStartLineNumber: number, visibleEndLineNumber: number, decorations: DecorationToRender[], decorationLaneCount: number): LineRenderedDecorations[] {
const output: LineRenderedDecorations[] = [];
for (let lineNumber = visibleStartLineNumber; lineNumber <= visibleEndLineNumber; lineNumber++) {
const lineIndex = lineNumber - visibleStartLineNumber;
output[lineIndex] = new LineRenderedDecorations();
}
if (decorations.length === 0) {
return output;
}
decorations.sort((a, b) => {
if (a.className === b.className) {
if (a.startLineNumber === b.startLineNumber) {
return a.endLineNumber - b.endLineNumber;
}
return a.startLineNumber - b.startLineNumber;
}
return (a.className < b.className ? -1 : 1);
});
let prevClassName: string | null = null;
let prevEndLineIndex = 0;
for (let i = 0, len = decorations.length; i < len; i++) {
const d = decorations[i];
const className = d.className;
const zIndex = d.zIndex;
let startLineIndex = Math.max(d.startLineNumber, visibleStartLineNumber) - visibleStartLineNumber;
const endLineIndex = Math.min(d.endLineNumber, visibleEndLineNumber) - visibleStartLineNumber;
const lane = Math.min(d.decorationLane, decorationLaneCount);
if (prevClassName === className) {
startLineIndex = Math.max(prevEndLineIndex + 1, startLineIndex);
prevEndLineIndex = Math.max(prevEndLineIndex, endLineIndex);
} else {
prevClassName = className;
prevEndLineIndex = endLineIndex;
}
for (let i = startLineIndex; i <= prevEndLineIndex; i++) {
output[i].add(lane, new RenderedDecoration(className, zIndex, d.domNode));
}
}
return output;
}
public prepareRender(ctx: RenderingContext): void {
if (!this._glyphMargin) {
this._renderResult = null;
return;
}
const visibleStartLineNumber = ctx.visibleRange.startLineNumber;
const visibleEndLineNumber = ctx.visibleRange.endLineNumber;
const decorationsToRender = this._getDecorations(ctx);
const toRender = this._render(visibleStartLineNumber, visibleEndLineNumber, decorationsToRender, this._glyphMarginDecorationLaneCount);
const width = (Math.round(this._glyphMarginWidth / this._glyphMarginDecorationLaneCount));
const output = new LineRenderedWidgets();
for (let lineNumber = visibleStartLineNumber; lineNumber <= visibleEndLineNumber; lineNumber++) {
const lineIndex = lineNumber - visibleStartLineNumber;
const renderInfo = toRender[lineIndex];
if (renderInfo.isEmpty()) {
continue;
} else {
for (let lane = 1; lane <= this._glyphMarginDecorationLaneCount; lane += 1) {
const decorations = renderInfo.getLaneDecorations(lane);
if (decorations.length === 0) {
continue;
}
decorations.sort((a, b) => b.zIndex - a.zIndex);
// Render winning decorations with the same zIndex together
const winningDecoration: RenderedDecoration = decorations[0];
const winningDecorationClassNames = [winningDecoration.className];
for (let i = 1; i < decorations.length; i += 1) {
const decoration = decorations[i];
if (decoration.zIndex !== winningDecoration.zIndex) {
break;
}
winningDecorationClassNames.push(decoration.className);
}
const left = this._glyphMarginLeft + (lane - 1) * this._lineHeight;
output.add(lineNumber, new GlyphMarginWidget(winningDecoration.domNode ?? createFastDomNode(document.createElement('div')), winningDecorationClassNames.join(' '), lineNumber, left, width, winningDecoration.domNode !== undefined));
}
}
}
this._previousRenderResult = this._renderResult;
this._renderResult = output;
}
public render(ctx: RestrictedRenderingContext): void {
const { startLineNumber, endLineNumber } = ctx.viewportData;
// Clean up any existing render results
if (this._previousRenderResult) {
for (let lineNumber = startLineNumber; lineNumber < endLineNumber; lineNumber += 1) {
const decorations = this._previousRenderResult.getLineDecorations(lineNumber);
if (lineNumber < 0 || decorations.length === 0) {
continue;
}
decorations.forEach((widget) => {
widget.domNode.setDisplay('none');
if (!widget.isManaged) {
widget.domNode.domNode.parentNode?.removeChild(widget.domNode.domNode);
}
});
}
}
if (!this._renderResult) {
return;
}
// Render new render results
for (let lineNumber = startLineNumber; lineNumber < endLineNumber; lineNumber += 1) {
const decorations = this._renderResult.getLineDecorations(lineNumber);
if (lineNumber < 0 || decorations.length === 0) {
continue;
}
decorations.forEach((widget) => this._renderWidget(ctx, widget));
}
return;
}
private _renderWidget(ctx: RestrictedRenderingContext, renderedWidget: GlyphMarginWidget): void {
const className = renderedWidget.className.includes('cgmr codicon ')
? renderedWidget.className
: `cgmr codicon ${renderedWidget.className}`;
renderedWidget.domNode.setClassName(className);
renderedWidget.domNode.setLeft(renderedWidget.left);
renderedWidget.domNode.setWidth(renderedWidget.width);
renderedWidget.domNode.setHeight(this._lineHeight);
renderedWidget.domNode.setTop(ctx.getVerticalOffsetForLineNumber(renderedWidget.lineNumber, true));
renderedWidget.domNode.setVisibility('inherit');
renderedWidget.domNode.setDisplay('block');
this.domNode.appendChild(renderedWidget.domNode);
}
}