Skip to content

Commit ba61231

Browse files
committed
Simplify code
1 parent 4784b1f commit ba61231

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

src/vs/editor/browser/view/renderingContext.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,38 @@ export class RenderingContext extends RestrictedRenderingContext {
8787
}
8888

8989
export class LineVisibleRanges {
90+
/**
91+
* Returns the element with the smallest `lineNumber`.
92+
*/
93+
public static firstLine(ranges: LineVisibleRanges[] | null): LineVisibleRanges | null {
94+
if (!ranges) {
95+
return null;
96+
}
97+
let result: LineVisibleRanges | null = null;
98+
for (const range of ranges) {
99+
if (!result || range.lineNumber < result.lineNumber) {
100+
result = range;
101+
}
102+
}
103+
return result;
104+
}
105+
106+
/**
107+
* Returns the element with the largest `lineNumber`.
108+
*/
109+
public static lastLine(ranges: LineVisibleRanges[] | null): LineVisibleRanges | null {
110+
if (!ranges) {
111+
return null;
112+
}
113+
let result: LineVisibleRanges | null = null;
114+
for (const range of ranges) {
115+
if (!result || range.lineNumber > result.lineNumber) {
116+
result = range;
117+
}
118+
}
119+
return result;
120+
}
121+
90122
constructor(
91123
public readonly outsideRenderedLine: boolean,
92124
public readonly lineNumber: number,

src/vs/editor/browser/viewParts/contentWidgets/contentWidgets.ts

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ContentWidgetPositionPreference, IContentWidget } from 'vs/editor/brows
99
import { PartFingerprint, PartFingerprints, ViewPart } from 'vs/editor/browser/view/viewPart';
1010
import { IRange, Range } from 'vs/editor/common/core/range';
1111
import { Constants } from 'vs/base/common/uint';
12-
import { RenderingContext, RestrictedRenderingContext } from 'vs/editor/browser/view/renderingContext';
12+
import { HorizontalRange, LineVisibleRanges, RenderingContext, RestrictedRenderingContext } from 'vs/editor/browser/view/renderingContext';
1313
import { ViewContext } from 'vs/editor/common/viewModel/viewContext';
1414
import * as viewEvents from 'vs/editor/common/viewEvents';
1515
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
@@ -404,54 +404,42 @@ class Widget {
404404
/**
405405
* Compute `this._topLeft`
406406
*/
407-
private _getTopAndBottomLeft(ctx: RenderingContext): [Coordinate, Coordinate] | [null, null] {
407+
private _getTopAndBottomLeft(ctx: RenderingContext): [Coordinate | null, Coordinate | null] {
408408
if (!this._viewRange) {
409409
return [null, null];
410410
}
411411

412412
const visibleRangesForRange = ctx.linesVisibleRangesForRange(this._viewRange, false);
413-
if (!visibleRangesForRange || visibleRangesForRange.length === 0) {
414-
return [null, null];
415-
}
413+
const topLeft = getCoordinate(LineVisibleRanges.firstLine(visibleRangesForRange), this._affinity, this._viewRange.startColumn, 0);
414+
const bottomLeft = getCoordinate(LineVisibleRanges.lastLine(visibleRangesForRange), this._affinity, this._viewRange.startColumn, this._lineHeight);
416415

417-
let firstLine = visibleRangesForRange[0];
418-
let lastLine = visibleRangesForRange[0];
419-
for (const visibleRangesForLine of visibleRangesForRange) {
420-
if (visibleRangesForLine.lineNumber < firstLine.lineNumber) {
421-
firstLine = visibleRangesForLine;
422-
}
423-
if (visibleRangesForLine.lineNumber > lastLine.lineNumber) {
424-
lastLine = visibleRangesForLine;
425-
}
426-
}
416+
return [topLeft, bottomLeft];
427417

428-
let firstLineMinLeft = Constants.MAX_SAFE_SMALL_INTEGER;//firstLine.Constants.MAX_SAFE_SMALL_INTEGER;
429-
for (const visibleRange of firstLine.ranges) {
430-
if (visibleRange.left < firstLineMinLeft) {
431-
firstLineMinLeft = visibleRange.left;
418+
function getCoordinate(line: LineVisibleRanges | null, affinity: PositionAffinity | null, startColumn: number, deltaTop: number): Coordinate | null {
419+
if (!line) {
420+
return null;
432421
}
433-
}
434-
435-
// Left-align widgets that should appear :before content
436-
if (this._affinity === PositionAffinity.LeftOfInjectedText &&
437-
this._viewRange.startColumn === 1) {
438-
firstLineMinLeft = 0;
439-
}
440-
441-
let lastLineMinLeft = Constants.MAX_SAFE_SMALL_INTEGER;//lastLine.Constants.MAX_SAFE_SMALL_INTEGER;
442-
for (const visibleRange of lastLine.ranges) {
443-
if (visibleRange.left < lastLineMinLeft) {
444-
lastLineMinLeft = visibleRange.left;
422+
const left = getLeft(line.ranges, affinity, startColumn);
423+
const top = ctx.getVerticalOffsetForLineNumber(line.lineNumber) - ctx.scrollTop;
424+
return new Coordinate(top + deltaTop, left);
425+
}
426+
427+
/**
428+
* Return the left-most position from `ranges`.
429+
*/
430+
function getLeft(ranges: HorizontalRange[], affinity: PositionAffinity | null, startColumn: number): number {
431+
// Left-align widgets that should appear :before content
432+
if (startColumn === 1 && affinity === PositionAffinity.LeftOfInjectedText) {
433+
return 0;
434+
}
435+
let result = Constants.MAX_SAFE_SMALL_INTEGER;
436+
for (const range of ranges) {
437+
if (range.left < result) {
438+
result = range.left;
439+
}
445440
}
441+
return result;
446442
}
447-
448-
const topForPosition = ctx.getVerticalOffsetForLineNumber(firstLine.lineNumber) - ctx.scrollTop;
449-
const topLeft = new Coordinate(topForPosition, firstLineMinLeft);
450-
451-
const topForBottomLine = ctx.getVerticalOffsetForLineNumber(lastLine.lineNumber) - ctx.scrollTop;
452-
const bottomLeft = new Coordinate(topForBottomLine + this._lineHeight, lastLineMinLeft);
453-
454-
return [topLeft, bottomLeft];
455443
}
456444

457445
private _prepareRenderWidget(ctx: RenderingContext): IRenderData | null {

0 commit comments

Comments
 (0)