Skip to content

Commit 463d53f

Browse files
alexdimaaiday-mar
andcommitted
Fixes microsoft#156328: Use editor API to determine the top and bottom for line numbers
Co-authored-by: aiday-mar <[email protected]>
1 parent 09935be commit 463d53f

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

src/vs/editor/browser/editorBrowser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,15 @@ export interface ICodeEditor extends editorCommon.IEditor {
899899
getWhitespaces(): IEditorWhitespace[];
900900

901901
/**
902-
* Get the vertical position (top offset) for the line w.r.t. to the first line.
902+
* Get the vertical position (top offset) for the line's top w.r.t. to the first line.
903903
*/
904904
getTopForLineNumber(lineNumber: number): number;
905905

906+
/**
907+
* Get the vertical position (top offset) for the line's bottom w.r.t. to the first line.
908+
*/
909+
getBottomForLineNumber(lineNumber: number): number;
910+
906911
/**
907912
* Get the vertical position (top offset) for the position w.r.t. to the first line.
908913
*/

src/vs/editor/contrib/stickyScroll/browser/stickyScroll.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ import { LineDecoration } from 'vs/editor/common/viewLayout/lineDecorations';
1919
import { RunOnceScheduler } from 'vs/base/common/async';
2020
import { IModelTokensChangedEvent } from 'vs/editor/common/textModelEvents';
2121

22-
const enum ScrollDirection {
23-
Down = 0,
24-
Up = 1,
25-
None = 2
26-
}
27-
2822
class StickyScrollController extends Disposable implements IEditorContribution {
2923

3024
static readonly ID = 'store.contrib.stickyScrollController';
@@ -36,7 +30,6 @@ class StickyScrollController extends Disposable implements IEditorContribution {
3630
private _ranges: [number, number, number][] = [];
3731
private _rangesVersionId: number = 0;
3832
private _cts: CancellationTokenSource | undefined;
39-
private _lastScrollPosition: number = -1;
4033
private readonly _updateSoon: RunOnceScheduler;
4134

4235
constructor(
@@ -191,42 +184,27 @@ class StickyScrollController extends Disposable implements IEditorContribution {
191184
return;
192185
}
193186
const scrollTop = this._editor.getScrollTop();
194-
let scrollDirection: ScrollDirection;
195-
if (this._lastScrollPosition < scrollTop) {
196-
scrollDirection = ScrollDirection.Down;
197-
} else {
198-
scrollDirection = ScrollDirection.Up;
199-
}
200-
this._lastScrollPosition = scrollTop;
201187

202-
const scrollToBottomOfWidget = this._editor.getScrollTop() + this.stickyScrollWidget.codeLineCount * lineHeight;
203188
this.stickyScrollWidget.emptyRootNode();
204189
const beginningLinesConsidered: Set<number> = new Set<number>();
205-
let topOfElementAtDepth: number;
206-
let bottomOfElementAtDepth: number;
207-
let bottomOfBeginningLine: number;
208-
let topOfEndLine: number;
209-
let bottomOfEndLine: number;
210190

211191
for (const [index, arr] of this._ranges.entries()) {
212192
const [start, end, depth] = arr;
213193
if (end - start > 0 && model.getLineContent(start) !== '') {
214-
topOfElementAtDepth = this._editor.getScrollTop() + (depth - 1) * lineHeight;
215-
bottomOfElementAtDepth = this._editor.getScrollTop() + depth * lineHeight;
216-
bottomOfBeginningLine = start * lineHeight;
217-
topOfEndLine = (end - 1) * lineHeight;
218-
bottomOfEndLine = end * lineHeight;
194+
const topOfElementAtDepth = (depth - 1) * lineHeight;
195+
const bottomOfElementAtDepth = depth * lineHeight;
196+
197+
const bottomOfBeginningLine = this._editor.getBottomForLineNumber(start) - scrollTop;
198+
const topOfEndLine = this._editor.getTopForLineNumber(end) - scrollTop;
199+
const bottomOfEndLine = this._editor.getBottomForLineNumber(end) - scrollTop;
200+
219201
if (!beginningLinesConsidered.has(start)) {
220202
if (topOfElementAtDepth >= topOfEndLine - 1 && topOfElementAtDepth < bottomOfEndLine - 2) {
221203
beginningLinesConsidered.add(start);
222204
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, -1, bottomOfEndLine - bottomOfElementAtDepth));
223205
break;
224206
}
225-
else if (scrollDirection === ScrollDirection.Down && bottomOfElementAtDepth > bottomOfBeginningLine - 1 && bottomOfElementAtDepth < bottomOfEndLine - 1) {
226-
beginningLinesConsidered.add(start);
227-
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, 0, 0));
228-
} else if (scrollDirection === ScrollDirection.Up && scrollToBottomOfWidget > bottomOfBeginningLine - 1 && scrollToBottomOfWidget < bottomOfEndLine ||
229-
scrollDirection === ScrollDirection.Up && bottomOfElementAtDepth > bottomOfBeginningLine && bottomOfElementAtDepth < topOfEndLine - 1) {
207+
else if (bottomOfElementAtDepth > bottomOfBeginningLine - 1 && bottomOfElementAtDepth < bottomOfEndLine - 1) {
230208
beginningLinesConsidered.add(start);
231209
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, 0, 0));
232210
}
@@ -262,7 +240,7 @@ class StickyScrollCodeLine {
262240
getDomNode() {
263241

264242
const root: HTMLElement = document.createElement('div');
265-
const lineRenderingData = this._editor._getViewModel().getViewLineRenderingData(this._editor.getVisibleRangesPlusViewportAboveBelow()[0], this._lineNumber);
243+
const lineRenderingData = this._editor._getViewModel().getViewportViewLineRenderingData(this._editor.getVisibleRangesPlusViewportAboveBelow()[0], this._lineNumber);
266244

267245
let actualInlineDecorations: LineDecoration[];
268246
try {

src/vs/monaco.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5372,9 +5372,13 @@ declare namespace monaco.editor {
53725372
*/
53735373
getVisibleRanges(): Range[];
53745374
/**
5375-
* Get the vertical position (top offset) for the line w.r.t. to the first line.
5375+
* Get the vertical position (top offset) for the line's top w.r.t. to the first line.
53765376
*/
53775377
getTopForLineNumber(lineNumber: number): number;
5378+
/**
5379+
* Get the vertical position (top offset) for the line's bottom w.r.t. to the first line.
5380+
*/
5381+
getBottomForLineNumber(lineNumber: number): number;
53785382
/**
53795383
* Get the vertical position (top offset) for the position w.r.t. to the first line.
53805384
*/

0 commit comments

Comments
 (0)