Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/inlineCompletions/test/browser/scrollToReveal.test.ts
5283 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { OffsetRange } from '../../../../common/core/ranges/offsetRange.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
9
import { scrollToReveal } from '../../browser/view/inlineEdits/inlineEditsViews/longDistanceHint/inlineEditsLongDistanceHint.js';
10
11
suite('scrollToReveal', () => {
12
ensureNoDisposablesAreLeakedInTestSuite();
13
14
test('should not scroll when content is already visible', () => {
15
// Content range [20, 30) is fully contained in window [10, 50)
16
const result = scrollToReveal(10, 40, new OffsetRange(20, 30));
17
assert.strictEqual(result.newScrollPosition, 10);
18
});
19
20
test('should not scroll when content exactly fits the visible window', () => {
21
// Content range [10, 50) exactly matches visible window [10, 50)
22
const result = scrollToReveal(10, 40, new OffsetRange(10, 50));
23
assert.strictEqual(result.newScrollPosition, 10);
24
});
25
26
test('should scroll left when content starts before visible window', () => {
27
// Content range [5, 15) starts before visible window [20, 60)
28
const result = scrollToReveal(20, 40, new OffsetRange(5, 15));
29
assert.strictEqual(result.newScrollPosition, 5);
30
});
31
32
test('should scroll right when content ends after visible window', () => {
33
// Content range [50, 80) ends after visible window [10, 50)
34
// New scroll position should be 80 - 40 = 40 so window becomes [40, 80)
35
const result = scrollToReveal(10, 40, new OffsetRange(50, 80));
36
assert.strictEqual(result.newScrollPosition, 40);
37
});
38
39
test('should show start of content when content is larger than window', () => {
40
// Content range [20, 100) is larger than window width 40
41
// Should position at start of content
42
const result = scrollToReveal(10, 40, new OffsetRange(20, 100));
43
assert.strictEqual(result.newScrollPosition, 20);
44
});
45
46
test('should handle edge case with zero-width content', () => {
47
// Empty content range [25, 25) in window [10, 50)
48
const result = scrollToReveal(10, 40, new OffsetRange(25, 25));
49
assert.strictEqual(result.newScrollPosition, 10);
50
});
51
52
test('should handle edge case with zero window width', () => {
53
// Any non-empty content with zero window width should position at content start
54
const result = scrollToReveal(10, 0, new OffsetRange(20, 30));
55
assert.strictEqual(result.newScrollPosition, 20);
56
});
57
58
test('should handle content at exact window boundaries - left edge', () => {
59
// Content range [10, 20) starts exactly at visible window start [10, 50)
60
const result = scrollToReveal(10, 40, new OffsetRange(10, 20));
61
assert.strictEqual(result.newScrollPosition, 10);
62
});
63
64
test('should handle content at exact window boundaries - right edge', () => {
65
// Content range [40, 50) ends exactly at visible window end [10, 50)
66
const result = scrollToReveal(10, 40, new OffsetRange(40, 50));
67
assert.strictEqual(result.newScrollPosition, 10);
68
});
69
70
test('should scroll right when content extends beyond right boundary', () => {
71
// Content range [40, 60) extends beyond visible window [10, 50)
72
// New scroll position should be 60 - 40 = 20 so window becomes [20, 60)
73
const result = scrollToReveal(10, 40, new OffsetRange(40, 60));
74
assert.strictEqual(result.newScrollPosition, 20);
75
});
76
77
test('should scroll left when content extends beyond left boundary', () => {
78
// Content range [5, 25) starts before visible window [20, 60)
79
// Should position at start of content
80
const result = scrollToReveal(20, 40, new OffsetRange(5, 25));
81
assert.strictEqual(result.newScrollPosition, 5);
82
});
83
84
test('should handle content overlapping both boundaries', () => {
85
// Content range [5, 70) overlaps both sides of visible window [20, 60)
86
// Since content is larger than window, should position at start of content
87
const result = scrollToReveal(20, 40, new OffsetRange(5, 70));
88
assert.strictEqual(result.newScrollPosition, 5);
89
});
90
91
test('should handle negative scroll positions', () => {
92
// Current scroll at -10, window width 40, so visible range [-10, 30)
93
// Content [35, 45) is beyond the visible window
94
const result = scrollToReveal(-10, 40, new OffsetRange(35, 45));
95
assert.strictEqual(result.newScrollPosition, 5); // 45 - 40 = 5
96
});
97
98
test('should handle large numbers', () => {
99
// Test with large numbers to ensure no overflow issues
100
const result = scrollToReveal(1000000, 500, new OffsetRange(1000600, 1000700));
101
assert.strictEqual(result.newScrollPosition, 1000200); // 1000700 - 500 = 1000200
102
});
103
104
test('should prioritize left scroll when content spans window but starts before', () => {
105
// Content [5, 55) spans wider than window width 40, starting before visible [20, 60)
106
// Should position at start of content
107
const result = scrollToReveal(20, 40, new OffsetRange(5, 55));
108
assert.strictEqual(result.newScrollPosition, 5);
109
});
110
111
test('should handle single character content requiring scroll', () => {
112
// Single character at position [100, 101) with visible window [10, 50)
113
const result = scrollToReveal(10, 40, new OffsetRange(100, 101));
114
assert.strictEqual(result.newScrollPosition, 61); // 101 - 40 = 61
115
});
116
117
test('should handle content just barely outside visible area - left', () => {
118
// Content [9, 19) with one unit outside visible window [10, 50)
119
const result = scrollToReveal(10, 40, new OffsetRange(9, 19));
120
assert.strictEqual(result.newScrollPosition, 9);
121
});
122
123
test('should handle content just barely outside visible area - right', () => {
124
// Content [45, 51) with one unit outside visible window [10, 50)
125
const result = scrollToReveal(10, 40, new OffsetRange(45, 51));
126
assert.strictEqual(result.newScrollPosition, 11); // 51 - 40 = 11
127
});
128
129
test('should handle fractional-like scenarios with minimum window', () => {
130
// Minimum window width 1, content needs to be revealed
131
const result = scrollToReveal(50, 1, new OffsetRange(100, 105));
132
assert.strictEqual(result.newScrollPosition, 100); // Content larger than window, show start
133
});
134
135
test('should preserve scroll when content partially visible on left', () => {
136
// Content [5, 25) partially visible in window [20, 60), overlaps [20, 25)
137
// Since content starts before window, scroll to show start
138
const result = scrollToReveal(20, 40, new OffsetRange(5, 25));
139
assert.strictEqual(result.newScrollPosition, 5);
140
});
141
142
test('should preserve scroll when content partially visible on right', () => {
143
// Content [45, 65) partially visible in window [20, 60), overlaps [45, 60)
144
// Since content extends beyond window, scroll to show end
145
const result = scrollToReveal(20, 40, new OffsetRange(45, 65));
146
assert.strictEqual(result.newScrollPosition, 25); // 65 - 40 = 25
147
});
148
});
149
150