-
Notifications
You must be signed in to change notification settings - Fork 29.5k
/
viewLines.ts
686 lines (566 loc) · 25.5 KB
/
viewLines.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*---------------------------------------------------------------------------------------------
* 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!./viewLines';
import { FastDomNode } from 'vs/base/browser/fastDomNode';
import { RunOnceScheduler } from 'vs/base/common/async';
import { Configuration } from 'vs/editor/browser/config/configuration';
import { IVisibleLinesHost, VisibleLinesCollection } from 'vs/editor/browser/view/viewLayer';
import { PartFingerprint, PartFingerprints, ViewPart } from 'vs/editor/browser/view/viewPart';
import { DomReadingContext, ViewLine, ViewLineOptions } from 'vs/editor/browser/viewParts/lines/viewLine';
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { ScrollType } from 'vs/editor/common/editorCommon';
import { IViewLines, LineVisibleRanges, VisibleRanges, HorizontalPosition } from 'vs/editor/common/view/renderingContext';
import { ViewContext } from 'vs/editor/common/view/viewContext';
import * as viewEvents from 'vs/editor/common/view/viewEvents';
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
import { Viewport } from 'vs/editor/common/viewModel/viewModel';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { Constants } from 'vs/base/common/uint';
class LastRenderedData {
private _currentVisibleRange: Range;
constructor() {
this._currentVisibleRange = new Range(1, 1, 1, 1);
}
public getCurrentVisibleRange(): Range {
return this._currentVisibleRange;
}
public setCurrentVisibleRange(currentVisibleRange: Range): void {
this._currentVisibleRange = currentVisibleRange;
}
}
class HorizontalRevealRequest {
public readonly lineNumber: number;
public readonly startColumn: number;
public readonly endColumn: number;
public readonly startScrollTop: number;
public readonly stopScrollTop: number;
public readonly scrollType: ScrollType;
constructor(lineNumber: number, startColumn: number, endColumn: number, startScrollTop: number, stopScrollTop: number, scrollType: ScrollType) {
this.lineNumber = lineNumber;
this.startColumn = startColumn;
this.endColumn = endColumn;
this.startScrollTop = startScrollTop;
this.stopScrollTop = stopScrollTop;
this.scrollType = scrollType;
}
}
export class ViewLines extends ViewPart implements IVisibleLinesHost<ViewLine>, IViewLines {
/**
* Adds this amount of pixels to the right of lines (no-one wants to type near the edge of the viewport)
*/
private static readonly HORIZONTAL_EXTRA_PX = 30;
private readonly _linesContent: FastDomNode<HTMLElement>;
private readonly _textRangeRestingSpot: HTMLElement;
private readonly _visibleLines: VisibleLinesCollection<ViewLine>;
private readonly domNode: FastDomNode<HTMLElement>;
// --- config
private _lineHeight: number;
private _typicalHalfwidthCharacterWidth: number;
private _isViewportWrapping: boolean;
private _revealHorizontalRightPadding: number;
private _cursorSurroundingLines: number;
private _cursorSurroundingLinesStyle: 'default' | 'all';
private _canUseLayerHinting: boolean;
private _viewLineOptions: ViewLineOptions;
// --- width
private _maxLineWidth: number;
private readonly _asyncUpdateLineWidths: RunOnceScheduler;
private _horizontalRevealRequest: HorizontalRevealRequest | null;
private readonly _lastRenderedData: LastRenderedData;
constructor(context: ViewContext, linesContent: FastDomNode<HTMLElement>) {
super(context);
this._linesContent = linesContent;
this._textRangeRestingSpot = document.createElement('div');
this._visibleLines = new VisibleLinesCollection(this);
this.domNode = this._visibleLines.domNode;
const conf = this._context.configuration;
const options = this._context.configuration.options;
const fontInfo = options.get(EditorOption.fontInfo);
const wrappingInfo = options.get(EditorOption.wrappingInfo);
this._lineHeight = options.get(EditorOption.lineHeight);
this._typicalHalfwidthCharacterWidth = fontInfo.typicalHalfwidthCharacterWidth;
this._isViewportWrapping = wrappingInfo.isViewportWrapping;
this._revealHorizontalRightPadding = options.get(EditorOption.revealHorizontalRightPadding);
this._cursorSurroundingLines = options.get(EditorOption.cursorSurroundingLines);
this._cursorSurroundingLinesStyle = options.get(EditorOption.cursorSurroundingLinesStyle);
this._canUseLayerHinting = !options.get(EditorOption.disableLayerHinting);
this._viewLineOptions = new ViewLineOptions(conf, this._context.theme.type);
PartFingerprints.write(this.domNode, PartFingerprint.ViewLines);
this.domNode.setClassName('view-lines');
Configuration.applyFontInfo(this.domNode, fontInfo);
// --- width & height
this._maxLineWidth = 0;
this._asyncUpdateLineWidths = new RunOnceScheduler(() => {
this._updateLineWidthsSlow();
}, 200);
this._lastRenderedData = new LastRenderedData();
this._horizontalRevealRequest = null;
}
public dispose(): void {
this._asyncUpdateLineWidths.dispose();
super.dispose();
}
public getDomNode(): FastDomNode<HTMLElement> {
return this.domNode;
}
// ---- begin IVisibleLinesHost
public createVisibleLine(): ViewLine {
return new ViewLine(this._viewLineOptions);
}
// ---- end IVisibleLinesHost
// ---- begin view event handlers
public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
this._visibleLines.onConfigurationChanged(e);
if (e.hasChanged(EditorOption.wrappingInfo)) {
this._maxLineWidth = 0;
}
const options = this._context.configuration.options;
const fontInfo = options.get(EditorOption.fontInfo);
const wrappingInfo = options.get(EditorOption.wrappingInfo);
this._lineHeight = options.get(EditorOption.lineHeight);
this._typicalHalfwidthCharacterWidth = fontInfo.typicalHalfwidthCharacterWidth;
this._isViewportWrapping = wrappingInfo.isViewportWrapping;
this._revealHorizontalRightPadding = options.get(EditorOption.revealHorizontalRightPadding);
this._cursorSurroundingLines = options.get(EditorOption.cursorSurroundingLines);
this._cursorSurroundingLinesStyle = options.get(EditorOption.cursorSurroundingLinesStyle);
this._canUseLayerHinting = !options.get(EditorOption.disableLayerHinting);
Configuration.applyFontInfo(this.domNode, fontInfo);
this._onOptionsMaybeChanged();
if (e.hasChanged(EditorOption.layoutInfo)) {
this._maxLineWidth = 0;
}
return true;
}
private _onOptionsMaybeChanged(): boolean {
const conf = this._context.configuration;
const newViewLineOptions = new ViewLineOptions(conf, this._context.theme.type);
if (!this._viewLineOptions.equals(newViewLineOptions)) {
this._viewLineOptions = newViewLineOptions;
const startLineNumber = this._visibleLines.getStartLineNumber();
const endLineNumber = this._visibleLines.getEndLineNumber();
for (let lineNumber = startLineNumber; lineNumber <= endLineNumber; lineNumber++) {
const line = this._visibleLines.getVisibleLine(lineNumber);
line.onOptionsChanged(this._viewLineOptions);
}
return true;
}
return false;
}
public onCursorStateChanged(e: viewEvents.ViewCursorStateChangedEvent): boolean {
const rendStartLineNumber = this._visibleLines.getStartLineNumber();
const rendEndLineNumber = this._visibleLines.getEndLineNumber();
let r = false;
for (let lineNumber = rendStartLineNumber; lineNumber <= rendEndLineNumber; lineNumber++) {
r = this._visibleLines.getVisibleLine(lineNumber).onSelectionChanged() || r;
}
return r;
}
public onDecorationsChanged(e: viewEvents.ViewDecorationsChangedEvent): boolean {
if (true/*e.inlineDecorationsChanged*/) {
const rendStartLineNumber = this._visibleLines.getStartLineNumber();
const rendEndLineNumber = this._visibleLines.getEndLineNumber();
for (let lineNumber = rendStartLineNumber; lineNumber <= rendEndLineNumber; lineNumber++) {
this._visibleLines.getVisibleLine(lineNumber).onDecorationsChanged();
}
}
return true;
}
public onFlushed(e: viewEvents.ViewFlushedEvent): boolean {
const shouldRender = this._visibleLines.onFlushed(e);
this._maxLineWidth = 0;
return shouldRender;
}
public onLinesChanged(e: viewEvents.ViewLinesChangedEvent): boolean {
return this._visibleLines.onLinesChanged(e);
}
public onLinesDeleted(e: viewEvents.ViewLinesDeletedEvent): boolean {
return this._visibleLines.onLinesDeleted(e);
}
public onLinesInserted(e: viewEvents.ViewLinesInsertedEvent): boolean {
return this._visibleLines.onLinesInserted(e);
}
public onRevealRangeRequest(e: viewEvents.ViewRevealRangeRequestEvent): boolean {
// Using the future viewport here in order to handle multiple
// incoming reveal range requests that might all desire to be animated
const desiredScrollTop = this._computeScrollTopToRevealRange(this._context.viewLayout.getFutureViewport(), e.source, e.range, e.verticalType);
// validate the new desired scroll top
let newScrollPosition = this._context.viewLayout.validateScrollPosition({ scrollTop: desiredScrollTop });
if (e.revealHorizontal) {
if (e.range.startLineNumber !== e.range.endLineNumber) {
// Two or more lines? => scroll to base (That's how you see most of the two lines)
newScrollPosition = {
scrollTop: newScrollPosition.scrollTop,
scrollLeft: 0
};
} else {
// We don't necessarily know the horizontal offset of this range since the line might not be in the view...
this._horizontalRevealRequest = new HorizontalRevealRequest(e.range.startLineNumber, e.range.startColumn, e.range.endColumn, this._context.viewLayout.getCurrentScrollTop(), newScrollPosition.scrollTop, e.scrollType);
}
} else {
this._horizontalRevealRequest = null;
}
const scrollTopDelta = Math.abs(this._context.viewLayout.getCurrentScrollTop() - newScrollPosition.scrollTop);
if (e.scrollType === ScrollType.Smooth && scrollTopDelta > this._lineHeight) {
this._context.viewLayout.setScrollPositionSmooth(newScrollPosition);
} else {
this._context.viewLayout.setScrollPositionNow(newScrollPosition);
}
return true;
}
public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
if (this._horizontalRevealRequest && e.scrollLeftChanged) {
// cancel any outstanding horizontal reveal request if someone else scrolls horizontally.
this._horizontalRevealRequest = null;
}
if (this._horizontalRevealRequest && e.scrollTopChanged) {
const min = Math.min(this._horizontalRevealRequest.startScrollTop, this._horizontalRevealRequest.stopScrollTop);
const max = Math.max(this._horizontalRevealRequest.startScrollTop, this._horizontalRevealRequest.stopScrollTop);
if (e.scrollTop < min || e.scrollTop > max) {
// cancel any outstanding horizontal reveal request if someone else scrolls vertically.
this._horizontalRevealRequest = null;
}
}
this.domNode.setWidth(e.scrollWidth);
return this._visibleLines.onScrollChanged(e) || true;
}
public onTokensChanged(e: viewEvents.ViewTokensChangedEvent): boolean {
return this._visibleLines.onTokensChanged(e);
}
public onZonesChanged(e: viewEvents.ViewZonesChangedEvent): boolean {
this._context.viewLayout.onMaxLineWidthChanged(this._maxLineWidth);
return this._visibleLines.onZonesChanged(e);
}
public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
return this._onOptionsMaybeChanged();
}
// ---- end view event handlers
// ----------- HELPERS FOR OTHERS
public getPositionFromDOMInfo(spanNode: HTMLElement, offset: number): Position | null {
const viewLineDomNode = this._getViewLineDomNode(spanNode);
if (viewLineDomNode === null) {
// Couldn't find view line node
return null;
}
const lineNumber = this._getLineNumberFor(viewLineDomNode);
if (lineNumber === -1) {
// Couldn't find view line node
return null;
}
if (lineNumber < 1 || lineNumber > this._context.model.getLineCount()) {
// lineNumber is outside range
return null;
}
if (this._context.model.getLineMaxColumn(lineNumber) === 1) {
// Line is empty
return new Position(lineNumber, 1);
}
const rendStartLineNumber = this._visibleLines.getStartLineNumber();
const rendEndLineNumber = this._visibleLines.getEndLineNumber();
if (lineNumber < rendStartLineNumber || lineNumber > rendEndLineNumber) {
// Couldn't find line
return null;
}
let column = this._visibleLines.getVisibleLine(lineNumber).getColumnOfNodeOffset(lineNumber, spanNode, offset);
const minColumn = this._context.model.getLineMinColumn(lineNumber);
if (column < minColumn) {
column = minColumn;
}
return new Position(lineNumber, column);
}
private _getViewLineDomNode(node: HTMLElement | null): HTMLElement | null {
while (node && node.nodeType === 1) {
if (node.className === ViewLine.CLASS_NAME) {
return node;
}
node = node.parentElement;
}
return null;
}
/**
* @returns the line number of this view line dom node.
*/
private _getLineNumberFor(domNode: HTMLElement): number {
const startLineNumber = this._visibleLines.getStartLineNumber();
const endLineNumber = this._visibleLines.getEndLineNumber();
for (let lineNumber = startLineNumber; lineNumber <= endLineNumber; lineNumber++) {
const line = this._visibleLines.getVisibleLine(lineNumber);
if (domNode === line.getDomNode()) {
return lineNumber;
}
}
return -1;
}
public getLineWidth(lineNumber: number): number {
const rendStartLineNumber = this._visibleLines.getStartLineNumber();
const rendEndLineNumber = this._visibleLines.getEndLineNumber();
if (lineNumber < rendStartLineNumber || lineNumber > rendEndLineNumber) {
// Couldn't find line
return -1;
}
return this._visibleLines.getVisibleLine(lineNumber).getWidth();
}
public linesVisibleRangesForRange(_range: Range, includeNewLines: boolean): LineVisibleRanges[] | null {
if (this.shouldRender()) {
// Cannot read from the DOM because it is dirty
// i.e. the model & the dom are out of sync, so I'd be reading something stale
return null;
}
const originalEndLineNumber = _range.endLineNumber;
const range = Range.intersectRanges(_range, this._lastRenderedData.getCurrentVisibleRange());
if (!range) {
return null;
}
let visibleRanges: LineVisibleRanges[] = [], visibleRangesLen = 0;
const domReadingContext = new DomReadingContext(this.domNode.domNode, this._textRangeRestingSpot);
let nextLineModelLineNumber: number = 0;
if (includeNewLines) {
nextLineModelLineNumber = this._context.model.coordinatesConverter.convertViewPositionToModelPosition(new Position(range.startLineNumber, 1)).lineNumber;
}
const rendStartLineNumber = this._visibleLines.getStartLineNumber();
const rendEndLineNumber = this._visibleLines.getEndLineNumber();
for (let lineNumber = range.startLineNumber; lineNumber <= range.endLineNumber; lineNumber++) {
if (lineNumber < rendStartLineNumber || lineNumber > rendEndLineNumber) {
continue;
}
const startColumn = lineNumber === range.startLineNumber ? range.startColumn : 1;
const endColumn = lineNumber === range.endLineNumber ? range.endColumn : this._context.model.getLineMaxColumn(lineNumber);
const visibleRangesForLine = this._visibleLines.getVisibleLine(lineNumber).getVisibleRangesForRange(startColumn, endColumn, domReadingContext);
if (!visibleRangesForLine) {
continue;
}
if (includeNewLines && lineNumber < originalEndLineNumber) {
const currentLineModelLineNumber = nextLineModelLineNumber;
nextLineModelLineNumber = this._context.model.coordinatesConverter.convertViewPositionToModelPosition(new Position(lineNumber + 1, 1)).lineNumber;
if (currentLineModelLineNumber !== nextLineModelLineNumber) {
visibleRangesForLine.ranges[visibleRangesForLine.ranges.length - 1].width += this._typicalHalfwidthCharacterWidth;
}
}
visibleRanges[visibleRangesLen++] = new LineVisibleRanges(visibleRangesForLine.outsideRenderedLine, lineNumber, visibleRangesForLine.ranges);
}
if (visibleRangesLen === 0) {
return null;
}
return visibleRanges;
}
private _visibleRangesForLineRange(lineNumber: number, startColumn: number, endColumn: number): VisibleRanges | null {
if (this.shouldRender()) {
// Cannot read from the DOM because it is dirty
// i.e. the model & the dom are out of sync, so I'd be reading something stale
return null;
}
if (lineNumber < this._visibleLines.getStartLineNumber() || lineNumber > this._visibleLines.getEndLineNumber()) {
return null;
}
return this._visibleLines.getVisibleLine(lineNumber).getVisibleRangesForRange(startColumn, endColumn, new DomReadingContext(this.domNode.domNode, this._textRangeRestingSpot));
}
public visibleRangeForPosition(position: Position): HorizontalPosition | null {
const visibleRanges = this._visibleRangesForLineRange(position.lineNumber, position.column, position.column);
if (!visibleRanges) {
return null;
}
return new HorizontalPosition(visibleRanges.outsideRenderedLine, visibleRanges.ranges[0].left);
}
// --- implementation
public updateLineWidths(): void {
this._updateLineWidths(false);
}
/**
* Updates the max line width if it is fast to compute.
* Returns true if all lines were taken into account.
* Returns false if some lines need to be reevaluated (in a slow fashion).
*/
private _updateLineWidthsFast(): boolean {
return this._updateLineWidths(true);
}
private _updateLineWidthsSlow(): void {
this._updateLineWidths(false);
}
private _updateLineWidths(fast: boolean): boolean {
const rendStartLineNumber = this._visibleLines.getStartLineNumber();
const rendEndLineNumber = this._visibleLines.getEndLineNumber();
let localMaxLineWidth = 1;
let allWidthsComputed = true;
for (let lineNumber = rendStartLineNumber; lineNumber <= rendEndLineNumber; lineNumber++) {
const visibleLine = this._visibleLines.getVisibleLine(lineNumber);
if (fast && !visibleLine.getWidthIsFast()) {
// Cannot compute width in a fast way for this line
allWidthsComputed = false;
continue;
}
localMaxLineWidth = Math.max(localMaxLineWidth, visibleLine.getWidth());
}
if (allWidthsComputed && rendStartLineNumber === 1 && rendEndLineNumber === this._context.model.getLineCount()) {
// we know the max line width for all the lines
this._maxLineWidth = 0;
}
this._ensureMaxLineWidth(localMaxLineWidth);
return allWidthsComputed;
}
public prepareRender(): void {
throw new Error('Not supported');
}
public render(): void {
throw new Error('Not supported');
}
public renderText(viewportData: ViewportData): void {
// (1) render lines - ensures lines are in the DOM
this._visibleLines.renderLines(viewportData);
this._lastRenderedData.setCurrentVisibleRange(viewportData.visibleRange);
this.domNode.setWidth(this._context.viewLayout.getScrollWidth());
this.domNode.setHeight(Math.min(this._context.viewLayout.getScrollHeight(), 1000000));
// (2) compute horizontal scroll position:
// - this must happen after the lines are in the DOM since it might need a line that rendered just now
// - it might change `scrollWidth` and `scrollLeft`
if (this._horizontalRevealRequest) {
const revealLineNumber = this._horizontalRevealRequest.lineNumber;
const revealStartColumn = this._horizontalRevealRequest.startColumn;
const revealEndColumn = this._horizontalRevealRequest.endColumn;
const scrollType = this._horizontalRevealRequest.scrollType;
// Check that we have the line that contains the horizontal range in the viewport
if (viewportData.startLineNumber <= revealLineNumber && revealLineNumber <= viewportData.endLineNumber) {
this._horizontalRevealRequest = null;
// allow `visibleRangesForRange2` to work
this.onDidRender();
// compute new scroll position
const newScrollLeft = this._computeScrollLeftToRevealRange(revealLineNumber, revealStartColumn, revealEndColumn);
const isViewportWrapping = this._isViewportWrapping;
if (!isViewportWrapping) {
// ensure `scrollWidth` is large enough
this._ensureMaxLineWidth(newScrollLeft.maxHorizontalOffset);
}
// set `scrollLeft`
if (scrollType === ScrollType.Smooth) {
this._context.viewLayout.setScrollPositionSmooth({
scrollLeft: newScrollLeft.scrollLeft
});
} else {
this._context.viewLayout.setScrollPositionNow({
scrollLeft: newScrollLeft.scrollLeft
});
}
}
}
// Update max line width (not so important, it is just so the horizontal scrollbar doesn't get too small)
if (!this._updateLineWidthsFast()) {
// Computing the width of some lines would be slow => delay it
this._asyncUpdateLineWidths.schedule();
}
// (3) handle scrolling
this._linesContent.setLayerHinting(this._canUseLayerHinting);
this._linesContent.setContain('strict');
const adjustedScrollTop = this._context.viewLayout.getCurrentScrollTop() - viewportData.bigNumbersDelta;
this._linesContent.setTop(-adjustedScrollTop);
this._linesContent.setLeft(-this._context.viewLayout.getCurrentScrollLeft());
}
// --- width
private _ensureMaxLineWidth(lineWidth: number): void {
const iLineWidth = Math.ceil(lineWidth);
if (this._maxLineWidth < iLineWidth) {
this._maxLineWidth = iLineWidth;
this._context.viewLayout.onMaxLineWidthChanged(this._maxLineWidth);
}
}
private _computeScrollTopToRevealRange(viewport: Viewport, source: string, range: Range, verticalType: viewEvents.VerticalRevealType): number {
const viewportStartY = viewport.top;
const viewportHeight = viewport.height;
const viewportEndY = viewportStartY + viewportHeight;
let boxStartY: number;
let boxEndY: number;
// Have a box that includes one extra line height (for the horizontal scrollbar)
boxStartY = this._context.viewLayout.getVerticalOffsetForLineNumber(range.startLineNumber);
boxEndY = this._context.viewLayout.getVerticalOffsetForLineNumber(range.endLineNumber) + this._lineHeight;
const shouldIgnoreScrollOff = source === 'mouse' && this._cursorSurroundingLinesStyle === 'default';
if (!shouldIgnoreScrollOff) {
const context = Math.min((viewportHeight / this._lineHeight) / 2, this._cursorSurroundingLines);
boxStartY -= context * this._lineHeight;
boxEndY += Math.max(0, (context - 1)) * this._lineHeight;
}
if (verticalType === viewEvents.VerticalRevealType.Simple || verticalType === viewEvents.VerticalRevealType.Bottom) {
// Reveal one line more when the last line would be covered by the scrollbar - arrow down case or revealing a line explicitly at bottom
boxEndY += this._lineHeight;
}
let newScrollTop: number;
if (boxEndY - boxStartY > viewportHeight) {
// the box is larger than the viewport ... scroll to its top
newScrollTop = boxStartY;
} else if (verticalType === viewEvents.VerticalRevealType.Center || verticalType === viewEvents.VerticalRevealType.CenterIfOutsideViewport) {
if (verticalType === viewEvents.VerticalRevealType.CenterIfOutsideViewport && viewportStartY <= boxStartY && boxEndY <= viewportEndY) {
// Box is already in the viewport... do nothing
newScrollTop = viewportStartY;
} else {
// Box is outside the viewport... center it
const boxMiddleY = (boxStartY + boxEndY) / 2;
newScrollTop = Math.max(0, boxMiddleY - viewportHeight / 2);
}
} else {
newScrollTop = this._computeMinimumScrolling(viewportStartY, viewportEndY, boxStartY, boxEndY, verticalType === viewEvents.VerticalRevealType.Top, verticalType === viewEvents.VerticalRevealType.Bottom);
}
return newScrollTop;
}
private _computeScrollLeftToRevealRange(lineNumber: number, startColumn: number, endColumn: number): { scrollLeft: number; maxHorizontalOffset: number; } {
let maxHorizontalOffset = 0;
const viewport = this._context.viewLayout.getCurrentViewport();
const viewportStartX = viewport.left;
const viewportEndX = viewportStartX + viewport.width;
const visibleRanges = this._visibleRangesForLineRange(lineNumber, startColumn, endColumn);
let boxStartX = Constants.MAX_SAFE_SMALL_INTEGER;
let boxEndX = 0;
if (!visibleRanges) {
// Unknown
return {
scrollLeft: viewportStartX,
maxHorizontalOffset: maxHorizontalOffset
};
}
for (const visibleRange of visibleRanges.ranges) {
if (visibleRange.left < boxStartX) {
boxStartX = visibleRange.left;
}
if (visibleRange.left + visibleRange.width > boxEndX) {
boxEndX = visibleRange.left + visibleRange.width;
}
}
maxHorizontalOffset = boxEndX;
boxStartX = Math.max(0, boxStartX - ViewLines.HORIZONTAL_EXTRA_PX);
boxEndX += this._revealHorizontalRightPadding;
const newScrollLeft = this._computeMinimumScrolling(viewportStartX, viewportEndX, boxStartX, boxEndX);
return {
scrollLeft: newScrollLeft,
maxHorizontalOffset: maxHorizontalOffset
};
}
private _computeMinimumScrolling(viewportStart: number, viewportEnd: number, boxStart: number, boxEnd: number, revealAtStart?: boolean, revealAtEnd?: boolean): number {
viewportStart = viewportStart | 0;
viewportEnd = viewportEnd | 0;
boxStart = boxStart | 0;
boxEnd = boxEnd | 0;
revealAtStart = !!revealAtStart;
revealAtEnd = !!revealAtEnd;
const viewportLength = viewportEnd - viewportStart;
const boxLength = boxEnd - boxStart;
if (boxLength < viewportLength) {
// The box would fit in the viewport
if (revealAtStart) {
return boxStart;
}
if (revealAtEnd) {
return Math.max(0, boxEnd - viewportLength);
}
if (boxStart < viewportStart) {
// The box is above the viewport
return boxStart;
} else if (boxEnd > viewportEnd) {
// The box is below the viewport
return Math.max(0, boxEnd - viewportLength);
}
} else {
// The box would not fit in the viewport
// Reveal the beginning of the box
return boxStart;
}
return viewportStart;
}
}