Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/test/browser/codeeditor.test.ts
4778 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 { TestInstantiationService } from '../../../platform/instantiation/test/common/instantiationServiceMock.js';
8
import { URI } from '../../../base/common/uri.js';
9
import { workbenchInstantiationService, TestEditorService } from './workbenchTestServices.js';
10
import { IModelService } from '../../../editor/common/services/model.js';
11
import { ILanguageService } from '../../../editor/common/languages/language.js';
12
import { LanguageService } from '../../../editor/common/services/languageService.js';
13
import { RangeHighlightDecorations } from '../../browser/codeeditor.js';
14
import { TextModel } from '../../../editor/common/model/textModel.js';
15
import { createTestCodeEditor } from '../../../editor/test/browser/testCodeEditor.js';
16
import { Range, IRange } from '../../../editor/common/core/range.js';
17
import { Position } from '../../../editor/common/core/position.js';
18
import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';
19
import { TestConfigurationService } from '../../../platform/configuration/test/common/testConfigurationService.js';
20
import { ModelService } from '../../../editor/common/services/modelService.js';
21
import { CoreNavigationCommands } from '../../../editor/browser/coreCommands.js';
22
import { ICodeEditor } from '../../../editor/browser/editorBrowser.js';
23
import { IEditorService } from '../../services/editor/common/editorService.js';
24
import { createTextModel } from '../../../editor/test/common/testTextModel.js';
25
import { IThemeService } from '../../../platform/theme/common/themeService.js';
26
import { TestThemeService } from '../../../platform/theme/test/common/testThemeService.js';
27
import { DisposableStore } from '../../../base/common/lifecycle.js';
28
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../base/test/common/utils.js';
29
30
suite('Editor - Range decorations', () => {
31
32
let disposables: DisposableStore;
33
let instantiationService: TestInstantiationService;
34
let codeEditor: ICodeEditor;
35
let model: TextModel;
36
let text: string;
37
let testObject: RangeHighlightDecorations;
38
const modelsToDispose: TextModel[] = [];
39
40
setup(() => {
41
disposables = new DisposableStore();
42
instantiationService = workbenchInstantiationService(undefined, disposables);
43
instantiationService.stub(IEditorService, new TestEditorService());
44
instantiationService.stub(ILanguageService, LanguageService);
45
instantiationService.stub(IModelService, stubModelService(instantiationService));
46
text = 'LINE1' + '\n' + 'LINE2' + '\n' + 'LINE3' + '\n' + 'LINE4' + '\r\n' + 'LINE5';
47
model = disposables.add(aModel(URI.file('some_file')));
48
codeEditor = disposables.add(createTestCodeEditor(model));
49
50
instantiationService.stub(IEditorService, 'activeEditor', { get resource() { return codeEditor.getModel()!.uri; } });
51
instantiationService.stub(IEditorService, 'activeTextEditorControl', codeEditor);
52
53
testObject = disposables.add(instantiationService.createInstance(RangeHighlightDecorations));
54
});
55
56
teardown(() => {
57
codeEditor.dispose();
58
modelsToDispose.forEach(model => model.dispose());
59
disposables.dispose();
60
});
61
62
ensureNoDisposablesAreLeakedInTestSuite();
63
64
test('highlight range for the resource if it is an active editor', function () {
65
const range: IRange = new Range(1, 1, 1, 1);
66
testObject.highlightRange({ resource: model.uri, range });
67
68
const actuals = rangeHighlightDecorations(model);
69
70
assert.deepStrictEqual(actuals, [range]);
71
});
72
73
test('remove highlight range', function () {
74
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
75
testObject.removeHighlightRange();
76
77
const actuals = rangeHighlightDecorations(model);
78
79
assert.deepStrictEqual(actuals, []);
80
});
81
82
test('highlight range for the resource removes previous highlight', function () {
83
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
84
const range: IRange = new Range(2, 2, 4, 3);
85
testObject.highlightRange({ resource: model.uri, range });
86
87
const actuals = rangeHighlightDecorations(model);
88
89
assert.deepStrictEqual(actuals, [range]);
90
});
91
92
test('highlight range for a new resource removes highlight of previous resource', function () {
93
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
94
95
const anotherModel = prepareActiveEditor('anotherModel');
96
const range: IRange = new Range(2, 2, 4, 3);
97
testObject.highlightRange({ resource: anotherModel.uri, range });
98
99
let actuals = rangeHighlightDecorations(model);
100
assert.deepStrictEqual(actuals, []);
101
actuals = rangeHighlightDecorations(anotherModel);
102
assert.deepStrictEqual(actuals, [range]);
103
});
104
105
test('highlight is removed on model change', function () {
106
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
107
prepareActiveEditor('anotherModel');
108
109
const actuals = rangeHighlightDecorations(model);
110
assert.deepStrictEqual(actuals, []);
111
});
112
113
test('highlight is removed on cursor position change', function () {
114
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
115
codeEditor.trigger('mouse', CoreNavigationCommands.MoveTo.id, {
116
position: new Position(2, 1)
117
});
118
119
const actuals = rangeHighlightDecorations(model);
120
assert.deepStrictEqual(actuals, []);
121
});
122
123
test('range is not highlight if not active editor', function () {
124
const model = aModel(URI.file('some model'));
125
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
126
127
const actuals = rangeHighlightDecorations(model);
128
assert.deepStrictEqual(actuals, []);
129
});
130
131
test('previous highlight is not removed if not active editor', function () {
132
const range = new Range(1, 1, 1, 1);
133
testObject.highlightRange({ resource: model.uri, range });
134
135
const model1 = aModel(URI.file('some model'));
136
testObject.highlightRange({ resource: model1.uri, range: { startLineNumber: 2, startColumn: 1, endLineNumber: 2, endColumn: 1 } });
137
138
const actuals = rangeHighlightDecorations(model);
139
assert.deepStrictEqual(actuals, [range]);
140
});
141
142
function prepareActiveEditor(resource: string): TextModel {
143
const model = aModel(URI.file(resource));
144
codeEditor.setModel(model);
145
return model;
146
}
147
148
function aModel(resource: URI, content: string = text): TextModel {
149
const model = createTextModel(content, undefined, undefined, resource);
150
modelsToDispose.push(model);
151
return model;
152
}
153
154
function rangeHighlightDecorations(m: TextModel): IRange[] {
155
const rangeHighlights: IRange[] = [];
156
157
for (const dec of m.getAllDecorations()) {
158
if (dec.options.className === 'rangeHighlight') {
159
rangeHighlights.push(dec.range);
160
}
161
}
162
163
rangeHighlights.sort(Range.compareRangesUsingStarts);
164
return rangeHighlights;
165
}
166
167
function stubModelService(instantiationService: TestInstantiationService): IModelService {
168
instantiationService.stub(IConfigurationService, new TestConfigurationService());
169
instantiationService.stub(IThemeService, new TestThemeService());
170
return instantiationService.createInstance(ModelService);
171
}
172
});
173
174