Skip to content

Commit

Permalink
Fixes #156328: Use editor API to determine the top and bottom for lin…
Browse files Browse the repository at this point in the history
…e numbers

Co-authored-by: aiday-mar <t-aidaym@microsoft.com>
  • Loading branch information
alexdima and aiday-mar committed Jul 28, 2022
1 parent 09935be commit 463d53f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
7 changes: 6 additions & 1 deletion src/vs/editor/browser/editorBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,15 @@ export interface ICodeEditor extends editorCommon.IEditor {
getWhitespaces(): IEditorWhitespace[];

/**
* Get the vertical position (top offset) for the line w.r.t. to the first line.
* Get the vertical position (top offset) for the line's top w.r.t. to the first line.
*/
getTopForLineNumber(lineNumber: number): number;

/**
* Get the vertical position (top offset) for the line's bottom w.r.t. to the first line.
*/
getBottomForLineNumber(lineNumber: number): number;

/**
* Get the vertical position (top offset) for the position w.r.t. to the first line.
*/
Expand Down
40 changes: 9 additions & 31 deletions src/vs/editor/contrib/stickyScroll/browser/stickyScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ import { LineDecoration } from 'vs/editor/common/viewLayout/lineDecorations';
import { RunOnceScheduler } from 'vs/base/common/async';
import { IModelTokensChangedEvent } from 'vs/editor/common/textModelEvents';

const enum ScrollDirection {
Down = 0,
Up = 1,
None = 2
}

class StickyScrollController extends Disposable implements IEditorContribution {

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

constructor(
Expand Down Expand Up @@ -191,42 +184,27 @@ class StickyScrollController extends Disposable implements IEditorContribution {
return;
}
const scrollTop = this._editor.getScrollTop();
let scrollDirection: ScrollDirection;
if (this._lastScrollPosition < scrollTop) {
scrollDirection = ScrollDirection.Down;
} else {
scrollDirection = ScrollDirection.Up;
}
this._lastScrollPosition = scrollTop;

const scrollToBottomOfWidget = this._editor.getScrollTop() + this.stickyScrollWidget.codeLineCount * lineHeight;
this.stickyScrollWidget.emptyRootNode();
const beginningLinesConsidered: Set<number> = new Set<number>();
let topOfElementAtDepth: number;
let bottomOfElementAtDepth: number;
let bottomOfBeginningLine: number;
let topOfEndLine: number;
let bottomOfEndLine: number;

for (const [index, arr] of this._ranges.entries()) {
const [start, end, depth] = arr;
if (end - start > 0 && model.getLineContent(start) !== '') {
topOfElementAtDepth = this._editor.getScrollTop() + (depth - 1) * lineHeight;
bottomOfElementAtDepth = this._editor.getScrollTop() + depth * lineHeight;
bottomOfBeginningLine = start * lineHeight;
topOfEndLine = (end - 1) * lineHeight;
bottomOfEndLine = end * lineHeight;
const topOfElementAtDepth = (depth - 1) * lineHeight;
const bottomOfElementAtDepth = depth * lineHeight;

const bottomOfBeginningLine = this._editor.getBottomForLineNumber(start) - scrollTop;
const topOfEndLine = this._editor.getTopForLineNumber(end) - scrollTop;
const bottomOfEndLine = this._editor.getBottomForLineNumber(end) - scrollTop;

if (!beginningLinesConsidered.has(start)) {
if (topOfElementAtDepth >= topOfEndLine - 1 && topOfElementAtDepth < bottomOfEndLine - 2) {
beginningLinesConsidered.add(start);
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, -1, bottomOfEndLine - bottomOfElementAtDepth));
break;
}
else if (scrollDirection === ScrollDirection.Down && bottomOfElementAtDepth > bottomOfBeginningLine - 1 && bottomOfElementAtDepth < bottomOfEndLine - 1) {
beginningLinesConsidered.add(start);
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, 0, 0));
} else if (scrollDirection === ScrollDirection.Up && scrollToBottomOfWidget > bottomOfBeginningLine - 1 && scrollToBottomOfWidget < bottomOfEndLine ||
scrollDirection === ScrollDirection.Up && bottomOfElementAtDepth > bottomOfBeginningLine && bottomOfElementAtDepth < topOfEndLine - 1) {
else if (bottomOfElementAtDepth > bottomOfBeginningLine - 1 && bottomOfElementAtDepth < bottomOfEndLine - 1) {
beginningLinesConsidered.add(start);
this.stickyScrollWidget.pushCodeLine(new StickyScrollCodeLine(model.getLineContent(start), start, this._editor, 0, 0));
}
Expand Down Expand Up @@ -262,7 +240,7 @@ class StickyScrollCodeLine {
getDomNode() {

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

let actualInlineDecorations: LineDecoration[];
try {
Expand Down
6 changes: 5 additions & 1 deletion src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5372,9 +5372,13 @@ declare namespace monaco.editor {
*/
getVisibleRanges(): Range[];
/**
* Get the vertical position (top offset) for the line w.r.t. to the first line.
* Get the vertical position (top offset) for the line's top w.r.t. to the first line.
*/
getTopForLineNumber(lineNumber: number): number;
/**
* Get the vertical position (top offset) for the line's bottom w.r.t. to the first line.
*/
getBottomForLineNumber(lineNumber: number): number;
/**
* Get the vertical position (top offset) for the position w.r.t. to the first line.
*/
Expand Down

0 comments on commit 463d53f

Please sign in to comment.