diff options
author | Alex Dima <alexdima@microsoft.com> | 2022-07-28 14:12:20 +0300 |
---|---|---|
committer | Alex Dima <alexdima@microsoft.com> | 2022-07-28 14:12:20 +0300 |
commit | 463d53f24eac85c81e57371135c3bc744157d5e1 (patch) | |
tree | a6c887791f0ee9bd1c7e3df3293e65ad6473d9a4 | |
parent | 09935be99be55eb2374927030d3c4de9d5639d95 (diff) |
Fixes #156328: Use editor API to determine the top and bottom for line numbers
Co-authored-by: aiday-mar <t-aidaym@microsoft.com>
-rw-r--r-- | src/vs/editor/browser/editorBrowser.ts | 7 | ||||
-rw-r--r-- | src/vs/editor/contrib/stickyScroll/browser/stickyScroll.ts | 40 | ||||
-rw-r--r-- | src/vs/monaco.d.ts | 6 |
3 files changed, 20 insertions, 33 deletions
diff --git a/src/vs/editor/browser/editorBrowser.ts b/src/vs/editor/browser/editorBrowser.ts index f91574dc4cb..25c2bbe8445 100644 --- a/src/vs/editor/browser/editorBrowser.ts +++ b/src/vs/editor/browser/editorBrowser.ts @@ -899,11 +899,16 @@ 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. */ getTopForPosition(lineNumber: number, column: number): number; diff --git a/src/vs/editor/contrib/stickyScroll/browser/stickyScroll.ts b/src/vs/editor/contrib/stickyScroll/browser/stickyScroll.ts index 002828096d5..d7f669d3caa 100644 --- a/src/vs/editor/contrib/stickyScroll/browser/stickyScroll.ts +++ b/src/vs/editor/contrib/stickyScroll/browser/stickyScroll.ts @@ -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'; @@ -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( @@ -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)); } @@ -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 { diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index db39c17f9c6..91b72dd4d11 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -5372,10 +5372,14 @@ 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. */ getTopForPosition(lineNumber: number, column: number): number; |