viewModelDecorations.test.ts 8.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as assert from 'assert';
import { Range } from 'vs/editor/common/core/range';
9
import { testViewModel } from 'vs/editor/test/common/viewModel/testViewModel';
10
import { MockCodeEditorCreationOptions } from 'vs/editor/test/common/mocks/mockCodeEditor';
11 12 13

suite('ViewModelDecorations', () => {
	test('getDecorationsViewportData', () => {
14 15 16
		const text = [
			'hello world, this is a buffer that will be wrapped'
		];
A
Alex Dima 已提交
17
		const opts: MockCodeEditorCreationOptions = {
A
Alex Dima 已提交
18
			wordWrap: 'wordWrapColumn',
19
			wordWrapColumn: 13
20 21
		};
		testViewModel(text, opts, (viewModel, model) => {
22 23 24 25 26 27
			assert.equal(viewModel.getLineContent(1), 'hello world, ');
			assert.equal(viewModel.getLineContent(2), 'this is a ');
			assert.equal(viewModel.getLineContent(3), 'buffer that ');
			assert.equal(viewModel.getLineContent(4), 'will be ');
			assert.equal(viewModel.getLineContent(5), 'wrapped');

28 29
			//@ts-ignore
			let dec1, dec2, dec3, dec4, dec5, dec6, dec7, dec8, dec9, dec10, dec11, dec12, dec13, dec14, dec15: string;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			model.changeDecorations((accessor) => {
				let createOpts = (id: string) => {
					return {
						className: id,
						inlineClassName: 'i-' + id,
						beforeContentClassName: 'b-' + id,
						afterContentClassName: 'a-' + id
					};
				};

				// VIEWPORT will be (1,14) -> (1,36)

				// completely before viewport
				dec1 = accessor.addDecoration(new Range(1, 2, 1, 3), createOpts('dec1'));
				// starts before viewport, ends at viewport start
				dec2 = accessor.addDecoration(new Range(1, 2, 1, 14), createOpts('dec2'));
				// starts before viewport, ends inside viewport
				dec3 = accessor.addDecoration(new Range(1, 2, 1, 15), createOpts('dec3'));
				// starts before viewport, ends at viewport end
				dec4 = accessor.addDecoration(new Range(1, 2, 1, 36), createOpts('dec4'));
				// starts before viewport, ends after viewport
				dec5 = accessor.addDecoration(new Range(1, 2, 1, 51), createOpts('dec5'));

				// starts at viewport start, ends at viewport start
				dec6 = accessor.addDecoration(new Range(1, 14, 1, 14), createOpts('dec6'));
				// starts at viewport start, ends inside viewport
				dec7 = accessor.addDecoration(new Range(1, 14, 1, 16), createOpts('dec7'));
				// starts at viewport start, ends at viewport end
				dec8 = accessor.addDecoration(new Range(1, 14, 1, 36), createOpts('dec8'));
				// starts at viewport start, ends after viewport
				dec9 = accessor.addDecoration(new Range(1, 14, 1, 51), createOpts('dec9'));

				// starts inside viewport, ends inside viewport
				dec10 = accessor.addDecoration(new Range(1, 16, 1, 18), createOpts('dec10'));
				// starts inside viewport, ends at viewport end
				dec11 = accessor.addDecoration(new Range(1, 16, 1, 36), createOpts('dec11'));
				// starts inside viewport, ends after viewport
				dec12 = accessor.addDecoration(new Range(1, 16, 1, 51), createOpts('dec12'));

				// starts at viewport end, ends at viewport end
				dec13 = accessor.addDecoration(new Range(1, 36, 1, 36), createOpts('dec13'));
				// starts at viewport end, ends after viewport
				dec14 = accessor.addDecoration(new Range(1, 36, 1, 51), createOpts('dec14'));

				// starts after viewport, ends after viewport
				dec15 = accessor.addDecoration(new Range(1, 40, 1, 51), createOpts('dec15'));
			});

78 79 80
			let actualDecorations = viewModel.getDecorationsInViewport(
				new Range(2, viewModel.getLineMinColumn(2), 3, viewModel.getLineMaxColumn(3))
			).map((dec) => {
A
Alex Dima 已提交
81
				return dec.options.className;
82 83 84
			});

			assert.deepEqual(actualDecorations, [
A
Alex Dima 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
				'dec2',
				'dec3',
				'dec4',
				'dec5',
				'dec6',
				'dec7',
				'dec8',
				'dec9',
				'dec10',
				'dec11',
				'dec12',
				'dec13',
				'dec14',
98 99
			]);

100 101 102 103
			let inlineDecorations1 = viewModel.getViewLineRenderingData(
				new Range(2, viewModel.getLineMinColumn(2), 3, viewModel.getLineMaxColumn(3)),
				2
			).inlineDecorations;
104 105 106 107 108

			// view line 2: (1,14 -> 1,24)
			assert.deepEqual(inlineDecorations1, [
				{
					range: new Range(1, 2, 2, 1),
A
Alex Dima 已提交
109 110
					inlineClassName: 'i-dec2',
					insertsBeforeOrAfter: false
111 112 113
				},
				{
					range: new Range(1, 2, 2, 2),
A
Alex Dima 已提交
114 115
					inlineClassName: 'i-dec3',
					insertsBeforeOrAfter: false
116 117 118
				},
				{
					range: new Range(2, 1, 2, 2),
A
Alex Dima 已提交
119 120
					inlineClassName: 'a-dec3',
					insertsBeforeOrAfter: true
121 122 123
				},
				{
					range: new Range(1, 2, 4, 1),
A
Alex Dima 已提交
124 125
					inlineClassName: 'i-dec4',
					insertsBeforeOrAfter: false
126 127 128
				},
				{
					range: new Range(1, 2, 5, 8),
A
Alex Dima 已提交
129 130
					inlineClassName: 'i-dec5',
					insertsBeforeOrAfter: false
131 132 133
				},
				{
					range: new Range(2, 1, 2, 1),
A
Alex Dima 已提交
134 135
					inlineClassName: 'i-dec6',
					insertsBeforeOrAfter: false
136 137 138
				},
				{
					range: new Range(2, 1, 2, 2),
A
Alex Dima 已提交
139 140
					inlineClassName: 'b-dec6',
					insertsBeforeOrAfter: true
141 142 143
				},
				{
					range: new Range(2, 1, 2, 3),
A
Alex Dima 已提交
144 145
					inlineClassName: 'i-dec7',
					insertsBeforeOrAfter: false
146 147 148
				},
				{
					range: new Range(2, 1, 2, 2),
A
Alex Dima 已提交
149 150
					inlineClassName: 'b-dec7',
					insertsBeforeOrAfter: true
151 152 153
				},
				{
					range: new Range(2, 2, 2, 3),
A
Alex Dima 已提交
154 155
					inlineClassName: 'a-dec7',
					insertsBeforeOrAfter: true
156 157 158
				},
				{
					range: new Range(2, 1, 4, 1),
A
Alex Dima 已提交
159 160
					inlineClassName: 'i-dec8',
					insertsBeforeOrAfter: false
161 162 163
				},
				{
					range: new Range(2, 1, 2, 2),
A
Alex Dima 已提交
164 165
					inlineClassName: 'b-dec8',
					insertsBeforeOrAfter: true
166 167 168
				},
				{
					range: new Range(2, 1, 5, 8),
A
Alex Dima 已提交
169 170
					inlineClassName: 'i-dec9',
					insertsBeforeOrAfter: false
171 172 173
				},
				{
					range: new Range(2, 1, 2, 2),
A
Alex Dima 已提交
174 175
					inlineClassName: 'b-dec9',
					insertsBeforeOrAfter: true
176 177 178
				},
				{
					range: new Range(2, 3, 2, 5),
A
Alex Dima 已提交
179 180
					inlineClassName: 'i-dec10',
					insertsBeforeOrAfter: false
181 182 183
				},
				{
					range: new Range(2, 3, 2, 4),
A
Alex Dima 已提交
184 185
					inlineClassName: 'b-dec10',
					insertsBeforeOrAfter: true
186 187 188
				},
				{
					range: new Range(2, 4, 2, 5),
A
Alex Dima 已提交
189 190
					inlineClassName: 'a-dec10',
					insertsBeforeOrAfter: true
191 192 193
				},
				{
					range: new Range(2, 3, 4, 1),
A
Alex Dima 已提交
194 195
					inlineClassName: 'i-dec11',
					insertsBeforeOrAfter: false
196 197 198
				},
				{
					range: new Range(2, 3, 2, 4),
A
Alex Dima 已提交
199 200
					inlineClassName: 'b-dec11',
					insertsBeforeOrAfter: true
201 202 203
				},
				{
					range: new Range(2, 3, 5, 8),
A
Alex Dima 已提交
204 205
					inlineClassName: 'i-dec12',
					insertsBeforeOrAfter: false
206 207 208
				},
				{
					range: new Range(2, 3, 2, 4),
A
Alex Dima 已提交
209 210
					inlineClassName: 'b-dec12',
					insertsBeforeOrAfter: true
211 212 213
				},
			]);

214 215 216 217
			let inlineDecorations2 = viewModel.getViewLineRenderingData(
				new Range(2, viewModel.getLineMinColumn(2), 3, viewModel.getLineMaxColumn(3)),
				3
			).inlineDecorations;
218 219 220 221 222

			// view line 3 (24 -> 36)
			assert.deepEqual(inlineDecorations2, [
				{
					range: new Range(1, 2, 4, 1),
A
Alex Dima 已提交
223 224
					inlineClassName: 'i-dec4',
					insertsBeforeOrAfter: false
225 226 227
				},
				{
					range: new Range(1, 2, 5, 8),
A
Alex Dima 已提交
228 229
					inlineClassName: 'i-dec5',
					insertsBeforeOrAfter: false
230 231 232
				},
				{
					range: new Range(2, 1, 4, 1),
A
Alex Dima 已提交
233 234
					inlineClassName: 'i-dec8',
					insertsBeforeOrAfter: false
235 236 237
				},
				{
					range: new Range(2, 1, 5, 8),
A
Alex Dima 已提交
238 239
					inlineClassName: 'i-dec9',
					insertsBeforeOrAfter: false
240 241 242
				},
				{
					range: new Range(2, 3, 4, 1),
A
Alex Dima 已提交
243 244
					inlineClassName: 'i-dec11',
					insertsBeforeOrAfter: false
245 246 247
				},
				{
					range: new Range(2, 3, 5, 8),
A
Alex Dima 已提交
248 249
					inlineClassName: 'i-dec12',
					insertsBeforeOrAfter: false
250 251 252 253 254 255
				},
			]);
		});
	});

	test('issue #17208: Problem scrolling in 1.8.0', () => {
256 257 258
		const text = [
			'hello world, this is a buffer that will be wrapped'
		];
A
Alex Dima 已提交
259
		const opts: MockCodeEditorCreationOptions = {
A
Alex Dima 已提交
260
			wordWrap: 'wordWrapColumn',
261
			wordWrapColumn: 13
262 263
		};
		testViewModel(text, opts, (viewModel, model) => {
264 265 266 267 268 269
			assert.equal(viewModel.getLineContent(1), 'hello world, ');
			assert.equal(viewModel.getLineContent(2), 'this is a ');
			assert.equal(viewModel.getLineContent(3), 'buffer that ');
			assert.equal(viewModel.getLineContent(4), 'will be ');
			assert.equal(viewModel.getLineContent(5), 'wrapped');

270
			// @ts-ignore unused local
271 272 273 274 275 276 277 278 279 280
			let dec1: string;
			model.changeDecorations((accessor) => {
				dec1 = accessor.addDecoration(
					new Range(1, 50, 1, 51),
					{
						beforeContentClassName: 'dec1'
					}
				);
			});

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
			let decorations = viewModel.getDecorationsInViewport(
				new Range(2, viewModel.getLineMinColumn(2), 3, viewModel.getLineMaxColumn(3))
			);
			assert.deepEqual(decorations, []);

			let inlineDecorations1 = viewModel.getViewLineRenderingData(
				new Range(2, viewModel.getLineMinColumn(2), 3, viewModel.getLineMaxColumn(3)),
				2
			).inlineDecorations;
			assert.deepEqual(inlineDecorations1, []);

			let inlineDecorations2 = viewModel.getViewLineRenderingData(
				new Range(2, viewModel.getLineMinColumn(2), 3, viewModel.getLineMaxColumn(3)),
				3
			).inlineDecorations;
			assert.deepEqual(inlineDecorations2, []);
297 298 299
		});
	});
});