Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/test/browser/view/viewController.test.ts
5222 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 { DisposableStore } from '../../../../base/common/lifecycle.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
9
import { TestInstantiationService } from '../../../../platform/instantiation/test/common/instantiationServiceMock.js';
10
import { TestThemeService } from '../../../../platform/theme/test/common/testThemeService.js';
11
import { NavigationCommandRevealType } from '../../../browser/coreCommands.js';
12
import { ViewController } from '../../../browser/view/viewController.js';
13
import { ViewUserInputEvents } from '../../../browser/view/viewUserInputEvents.js';
14
import { Position } from '../../../common/core/position.js';
15
import { ILanguageService } from '../../../common/languages/language.js';
16
import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';
17
import { MonospaceLineBreaksComputerFactory } from '../../../common/viewModel/monospaceLineBreaksComputer.js';
18
import { ViewModel } from '../../../common/viewModel/viewModelImpl.js';
19
import { instantiateTextModel } from '../../../test/common/testTextModel.js';
20
import { TestLanguageConfigurationService } from '../../common/modes/testLanguageConfigurationService.js';
21
import { TestConfiguration } from '../config/testConfiguration.js';
22
import { createCodeEditorServices } from '../testCodeEditor.js';
23
24
suite('ViewController - Bracket content selection', () => {
25
let disposables: DisposableStore;
26
let instantiationService: TestInstantiationService;
27
let languageConfigurationService: ILanguageConfigurationService;
28
let languageService: ILanguageService;
29
let viewModel: ViewModel | undefined;
30
31
setup(() => {
32
disposables = new DisposableStore();
33
instantiationService = createCodeEditorServices(disposables);
34
languageConfigurationService = instantiationService.get(ILanguageConfigurationService);
35
languageService = instantiationService.get(ILanguageService);
36
viewModel = undefined;
37
});
38
39
teardown(() => {
40
viewModel?.dispose();
41
viewModel = undefined;
42
disposables.dispose();
43
});
44
45
ensureNoDisposablesAreLeakedInTestSuite();
46
47
function createViewControllerWithText(text: string): ViewController {
48
const languageId = 'testMode';
49
disposables.add(languageService.registerLanguage({ id: languageId }));
50
disposables.add(languageConfigurationService.register(languageId, {
51
brackets: [
52
['{', '}'],
53
['[', ']'],
54
['(', ')'],
55
]
56
}));
57
58
const configuration = disposables.add(new TestConfiguration({}));
59
const monospaceLineBreaksComputerFactory = MonospaceLineBreaksComputerFactory.create(configuration.options);
60
61
viewModel = new ViewModel(
62
1, // editorId
63
configuration,
64
disposables.add(instantiateTextModel(instantiationService, text, languageId)),
65
monospaceLineBreaksComputerFactory,
66
monospaceLineBreaksComputerFactory,
67
null!,
68
disposables.add(new TestLanguageConfigurationService()),
69
new TestThemeService(),
70
{ setVisibleLines() { } },
71
{ batchChanges: (cb: any) => cb() }
72
);
73
74
return new ViewController(
75
configuration,
76
viewModel,
77
new ViewUserInputEvents(viewModel.coordinatesConverter),
78
{
79
paste: () => { },
80
type: () => { },
81
compositionType: () => { },
82
startComposition: () => { },
83
endComposition: () => { },
84
cut: () => { }
85
}
86
);
87
}
88
89
function testBracketSelection(text: string, position: Position, expectedText: string | undefined) {
90
const controller = createViewControllerWithText(text);
91
controller.dispatchMouse({
92
position,
93
mouseColumn: position.column,
94
startedOnLineNumbers: false,
95
revealType: NavigationCommandRevealType.Minimal,
96
mouseDownCount: 2,
97
inSelectionMode: false,
98
altKey: false,
99
ctrlKey: false,
100
metaKey: false,
101
shiftKey: false,
102
leftButton: true,
103
middleButton: false,
104
onInjectedText: false
105
});
106
107
const selections = viewModel!.getSelections();
108
const selectedText = viewModel!.model.getValueInRange(selections[0]);
109
if (expectedText === undefined) {
110
assert.notStrictEqual(selectedText, expectedText);
111
} else {
112
assert.strictEqual(selectedText, expectedText);
113
}
114
}
115
116
test('Select content after opening curly brace', () => {
117
testBracketSelection('var x = { hello };', new Position(1, 10), ' hello ');
118
});
119
120
test('Select content before closing curly brace', () => {
121
testBracketSelection('var x = { hello };', new Position(1, 17), ' hello ');
122
});
123
124
test('Select content after opening parenthesis', () => {
125
testBracketSelection('function foo(arg1, arg2) {}', new Position(1, 14), 'arg1, arg2');
126
});
127
128
test('Select content before closing parenthesis', () => {
129
testBracketSelection('function foo(arg1, arg2) {}', new Position(1, 24), 'arg1, arg2');
130
});
131
132
test('Select content after opening square bracket', () => {
133
testBracketSelection('const arr = [ 1, 2, 3 ];', new Position(1, 14), ' 1, 2, 3 ');
134
});
135
136
test('Select content before closing square bracket', () => {
137
testBracketSelection('const arr = [ 1, 2, 3 ];', new Position(1, 23), ' 1, 2, 3 ');
138
});
139
140
test('Select innermost bracket content with nested brackets', () => {
141
testBracketSelection('var x = { a: { b: 123 }};', new Position(1, 15), ' b: 123 ');
142
});
143
144
test('Empty brackets create empty selection', () => {
145
testBracketSelection('var x = {};', new Position(1, 10), '');
146
});
147
});
148
149