forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeeditor.test.ts
More file actions
169 lines (135 loc) · 7.24 KB
/
codeeditor.test.ts
File metadata and controls
169 lines (135 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { URI } from 'vs/base/common/uri';
import { workbenchInstantiationService, TestEditorService } from 'vs/workbench/test/browser/workbenchTestServices';
import { IModelService } from 'vs/editor/common/services/model';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { LanguageService } from 'vs/editor/common/services/languageService';
import { RangeHighlightDecorations } from 'vs/workbench/browser/codeeditor';
import { TextModel } from 'vs/editor/common/model/textModel';
import { createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { Range, IRange } from 'vs/editor/common/core/range';
import { Position } from 'vs/editor/common/core/position';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ModelService } from 'vs/editor/common/services/modelService';
import { CoreNavigationCommands } from 'vs/editor/browser/coreCommands';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { createTextModel } from 'vs/editor/test/common/testTextModel';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { DisposableStore } from 'vs/base/common/lifecycle';
suite('Editor - Range decorations', () => {
let disposables: DisposableStore;
let instantiationService: TestInstantiationService;
let codeEditor: ICodeEditor;
let model: TextModel;
let text: string;
let testObject: RangeHighlightDecorations;
const modelsToDispose: TextModel[] = [];
setup(() => {
disposables = new DisposableStore();
instantiationService = <TestInstantiationService>workbenchInstantiationService(undefined, disposables);
instantiationService.stub(IEditorService, new TestEditorService());
instantiationService.stub(ILanguageService, LanguageService);
instantiationService.stub(IModelService, stubModelService(instantiationService));
text = 'LINE1' + '\n' + 'LINE2' + '\n' + 'LINE3' + '\n' + 'LINE4' + '\r\n' + 'LINE5';
model = aModel(URI.file('some_file'));
codeEditor = createTestCodeEditor(model);
instantiationService.stub(IEditorService, 'activeEditor', { get resource() { return codeEditor.getModel()!.uri; } });
instantiationService.stub(IEditorService, 'activeTextEditorControl', codeEditor);
testObject = instantiationService.createInstance(RangeHighlightDecorations);
});
teardown(() => {
codeEditor.dispose();
modelsToDispose.forEach(model => model.dispose());
disposables.dispose();
});
test('highlight range for the resource if it is an active editor', function () {
const range: IRange = new Range(1, 1, 1, 1);
testObject.highlightRange({ resource: model.uri, range });
const actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, [range]);
});
test('remove highlight range', function () {
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
testObject.removeHighlightRange();
const actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, []);
});
test('highlight range for the resource removes previous highlight', function () {
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
const range: IRange = new Range(2, 2, 4, 3);
testObject.highlightRange({ resource: model.uri, range });
const actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, [range]);
});
test('highlight range for a new resource removes highlight of previous resource', function () {
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
const anotherModel = prepareActiveEditor('anotherModel');
const range: IRange = new Range(2, 2, 4, 3);
testObject.highlightRange({ resource: anotherModel.uri, range });
let actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, []);
actuals = rangeHighlightDecorations(anotherModel);
assert.deepStrictEqual(actuals, [range]);
});
test('highlight is removed on model change', function () {
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
prepareActiveEditor('anotherModel');
const actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, []);
});
test('highlight is removed on cursor position change', function () {
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
codeEditor.trigger('mouse', CoreNavigationCommands.MoveTo.id, {
position: new Position(2, 1)
});
const actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, []);
});
test('range is not highlight if not active editor', function () {
const model = aModel(URI.file('some model'));
testObject.highlightRange({ resource: model.uri, range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 } });
const actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, []);
});
test('previous highlight is not removed if not active editor', function () {
const range = new Range(1, 1, 1, 1);
testObject.highlightRange({ resource: model.uri, range });
const model1 = aModel(URI.file('some model'));
testObject.highlightRange({ resource: model1.uri, range: { startLineNumber: 2, startColumn: 1, endLineNumber: 2, endColumn: 1 } });
const actuals = rangeHighlightDecorations(model);
assert.deepStrictEqual(actuals, [range]);
});
function prepareActiveEditor(resource: string): TextModel {
const model = aModel(URI.file(resource));
codeEditor.setModel(model);
return model;
}
function aModel(resource: URI, content: string = text): TextModel {
const model = createTextModel(content, undefined, undefined, resource);
modelsToDispose.push(model);
return model;
}
function rangeHighlightDecorations(m: TextModel): IRange[] {
const rangeHighlights: IRange[] = [];
for (const dec of m.getAllDecorations()) {
if (dec.options.className === 'rangeHighlight') {
rangeHighlights.push(dec.range);
}
}
rangeHighlights.sort(Range.compareRangesUsingStarts);
return rangeHighlights;
}
function stubModelService(instantiationService: TestInstantiationService): IModelService {
instantiationService.stub(IConfigurationService, new TestConfigurationService());
instantiationService.stub(IThemeService, new TestThemeService());
return instantiationService.createInstance(ModelService);
}
});