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