Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/test/browser/editorTestServices.ts
3294 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 { Emitter, Event } from '../../../base/common/event.js';
7
import { ICodeEditor } from '../../browser/editorBrowser.js';
8
import { AbstractCodeEditorService, GlobalStyleSheet } from '../../browser/services/abstractCodeEditorService.js';
9
import { CommandsRegistry, ICommandEvent, ICommandService } from '../../../platform/commands/common/commands.js';
10
import { IResourceEditorInput } from '../../../platform/editor/common/editor.js';
11
import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';
12
13
export class TestCodeEditorService extends AbstractCodeEditorService {
14
15
public readonly globalStyleSheet = new TestGlobalStyleSheet();
16
17
protected override _createGlobalStyleSheet(): GlobalStyleSheet {
18
return this.globalStyleSheet;
19
}
20
21
getActiveCodeEditor(): ICodeEditor | null {
22
return null;
23
}
24
public lastInput?: IResourceEditorInput;
25
override openCodeEditor(input: IResourceEditorInput, source: ICodeEditor | null, sideBySide?: boolean): Promise<ICodeEditor | null> {
26
this.lastInput = input;
27
return Promise.resolve(null);
28
}
29
}
30
31
export class TestGlobalStyleSheet extends GlobalStyleSheet {
32
33
public rules: string[] = [];
34
35
constructor() {
36
super(null!);
37
}
38
39
public override insertRule(selector: string, rule: string): void {
40
this.rules.unshift(`${selector} {${rule}}`);
41
}
42
43
public override removeRulesContainingSelector(ruleName: string): void {
44
for (let i = 0; i < this.rules.length; i++) {
45
if (this.rules[i].indexOf(ruleName) >= 0) {
46
this.rules.splice(i, 1);
47
i--;
48
}
49
}
50
}
51
52
public read(): string {
53
return this.rules.join('\n');
54
}
55
}
56
57
export class TestCommandService implements ICommandService {
58
declare readonly _serviceBrand: undefined;
59
60
private readonly _instantiationService: IInstantiationService;
61
62
private readonly _onWillExecuteCommand = new Emitter<ICommandEvent>();
63
public readonly onWillExecuteCommand: Event<ICommandEvent> = this._onWillExecuteCommand.event;
64
65
private readonly _onDidExecuteCommand = new Emitter<ICommandEvent>();
66
public readonly onDidExecuteCommand: Event<ICommandEvent> = this._onDidExecuteCommand.event;
67
68
constructor(instantiationService: IInstantiationService) {
69
this._instantiationService = instantiationService;
70
}
71
72
public executeCommand<T>(id: string, ...args: any[]): Promise<T> {
73
const command = CommandsRegistry.getCommand(id);
74
if (!command) {
75
return Promise.reject(new Error(`command '${id}' not found`));
76
}
77
78
try {
79
this._onWillExecuteCommand.fire({ commandId: id, args });
80
const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command.handler, ...args]) as T;
81
this._onDidExecuteCommand.fire({ commandId: id, args });
82
return Promise.resolve(result);
83
} catch (err) {
84
return Promise.reject(err);
85
}
86
}
87
}
88
89