Path: blob/main/src/vs/editor/contrib/inlineCompletions/test/browser/scrollToReveal.test.ts
5283 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { OffsetRange } from '../../../../common/core/ranges/offsetRange.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';8import { scrollToReveal } from '../../browser/view/inlineEdits/inlineEditsViews/longDistanceHint/inlineEditsLongDistanceHint.js';910suite('scrollToReveal', () => {11ensureNoDisposablesAreLeakedInTestSuite();1213test('should not scroll when content is already visible', () => {14// Content range [20, 30) is fully contained in window [10, 50)15const result = scrollToReveal(10, 40, new OffsetRange(20, 30));16assert.strictEqual(result.newScrollPosition, 10);17});1819test('should not scroll when content exactly fits the visible window', () => {20// Content range [10, 50) exactly matches visible window [10, 50)21const result = scrollToReveal(10, 40, new OffsetRange(10, 50));22assert.strictEqual(result.newScrollPosition, 10);23});2425test('should scroll left when content starts before visible window', () => {26// Content range [5, 15) starts before visible window [20, 60)27const result = scrollToReveal(20, 40, new OffsetRange(5, 15));28assert.strictEqual(result.newScrollPosition, 5);29});3031test('should scroll right when content ends after visible window', () => {32// Content range [50, 80) ends after visible window [10, 50)33// New scroll position should be 80 - 40 = 40 so window becomes [40, 80)34const result = scrollToReveal(10, 40, new OffsetRange(50, 80));35assert.strictEqual(result.newScrollPosition, 40);36});3738test('should show start of content when content is larger than window', () => {39// Content range [20, 100) is larger than window width 4040// Should position at start of content41const result = scrollToReveal(10, 40, new OffsetRange(20, 100));42assert.strictEqual(result.newScrollPosition, 20);43});4445test('should handle edge case with zero-width content', () => {46// Empty content range [25, 25) in window [10, 50)47const result = scrollToReveal(10, 40, new OffsetRange(25, 25));48assert.strictEqual(result.newScrollPosition, 10);49});5051test('should handle edge case with zero window width', () => {52// Any non-empty content with zero window width should position at content start53const result = scrollToReveal(10, 0, new OffsetRange(20, 30));54assert.strictEqual(result.newScrollPosition, 20);55});5657test('should handle content at exact window boundaries - left edge', () => {58// Content range [10, 20) starts exactly at visible window start [10, 50)59const result = scrollToReveal(10, 40, new OffsetRange(10, 20));60assert.strictEqual(result.newScrollPosition, 10);61});6263test('should handle content at exact window boundaries - right edge', () => {64// Content range [40, 50) ends exactly at visible window end [10, 50)65const result = scrollToReveal(10, 40, new OffsetRange(40, 50));66assert.strictEqual(result.newScrollPosition, 10);67});6869test('should scroll right when content extends beyond right boundary', () => {70// Content range [40, 60) extends beyond visible window [10, 50)71// New scroll position should be 60 - 40 = 20 so window becomes [20, 60)72const result = scrollToReveal(10, 40, new OffsetRange(40, 60));73assert.strictEqual(result.newScrollPosition, 20);74});7576test('should scroll left when content extends beyond left boundary', () => {77// Content range [5, 25) starts before visible window [20, 60)78// Should position at start of content79const result = scrollToReveal(20, 40, new OffsetRange(5, 25));80assert.strictEqual(result.newScrollPosition, 5);81});8283test('should handle content overlapping both boundaries', () => {84// Content range [5, 70) overlaps both sides of visible window [20, 60)85// Since content is larger than window, should position at start of content86const result = scrollToReveal(20, 40, new OffsetRange(5, 70));87assert.strictEqual(result.newScrollPosition, 5);88});8990test('should handle negative scroll positions', () => {91// Current scroll at -10, window width 40, so visible range [-10, 30)92// Content [35, 45) is beyond the visible window93const result = scrollToReveal(-10, 40, new OffsetRange(35, 45));94assert.strictEqual(result.newScrollPosition, 5); // 45 - 40 = 595});9697test('should handle large numbers', () => {98// Test with large numbers to ensure no overflow issues99const result = scrollToReveal(1000000, 500, new OffsetRange(1000600, 1000700));100assert.strictEqual(result.newScrollPosition, 1000200); // 1000700 - 500 = 1000200101});102103test('should prioritize left scroll when content spans window but starts before', () => {104// Content [5, 55) spans wider than window width 40, starting before visible [20, 60)105// Should position at start of content106const result = scrollToReveal(20, 40, new OffsetRange(5, 55));107assert.strictEqual(result.newScrollPosition, 5);108});109110test('should handle single character content requiring scroll', () => {111// Single character at position [100, 101) with visible window [10, 50)112const result = scrollToReveal(10, 40, new OffsetRange(100, 101));113assert.strictEqual(result.newScrollPosition, 61); // 101 - 40 = 61114});115116test('should handle content just barely outside visible area - left', () => {117// Content [9, 19) with one unit outside visible window [10, 50)118const result = scrollToReveal(10, 40, new OffsetRange(9, 19));119assert.strictEqual(result.newScrollPosition, 9);120});121122test('should handle content just barely outside visible area - right', () => {123// Content [45, 51) with one unit outside visible window [10, 50)124const result = scrollToReveal(10, 40, new OffsetRange(45, 51));125assert.strictEqual(result.newScrollPosition, 11); // 51 - 40 = 11126});127128test('should handle fractional-like scenarios with minimum window', () => {129// Minimum window width 1, content needs to be revealed130const result = scrollToReveal(50, 1, new OffsetRange(100, 105));131assert.strictEqual(result.newScrollPosition, 100); // Content larger than window, show start132});133134test('should preserve scroll when content partially visible on left', () => {135// Content [5, 25) partially visible in window [20, 60), overlaps [20, 25)136// Since content starts before window, scroll to show start137const result = scrollToReveal(20, 40, new OffsetRange(5, 25));138assert.strictEqual(result.newScrollPosition, 5);139});140141test('should preserve scroll when content partially visible on right', () => {142// Content [45, 65) partially visible in window [20, 60), overlaps [45, 60)143// Since content extends beyond window, scroll to show end144const result = scrollToReveal(20, 40, new OffsetRange(45, 65));145assert.strictEqual(result.newScrollPosition, 25); // 65 - 40 = 25146});147});148149150