Path: blob/main/src/vs/editor/test/browser/editorTestServices.ts
5238 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Emitter, Event } from '../../../base/common/event.js';6import { ICodeEditor } from '../../browser/editorBrowser.js';7import { AbstractCodeEditorService, GlobalStyleSheet } from '../../browser/services/abstractCodeEditorService.js';8import { CommandsRegistry, ICommandEvent, ICommandService } from '../../../platform/commands/common/commands.js';9import { IResourceEditorInput } from '../../../platform/editor/common/editor.js';10import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';1112export class TestCodeEditorService extends AbstractCodeEditorService {1314public readonly globalStyleSheet = new TestGlobalStyleSheet();1516protected override _createGlobalStyleSheet(): GlobalStyleSheet {17return this.globalStyleSheet;18}1920getActiveCodeEditor(): ICodeEditor | null {21const editors = this.listCodeEditors();22return editors.length > 0 ? editors[editors.length - 1] : null;23}24public lastInput?: IResourceEditorInput;25override openCodeEditor(input: IResourceEditorInput, source: ICodeEditor | null, sideBySide?: boolean): Promise<ICodeEditor | null> {26this.lastInput = input;27return Promise.resolve(null);28}29}3031export class TestGlobalStyleSheet extends GlobalStyleSheet {3233public rules: string[] = [];3435constructor() {36super(null!);37}3839public override insertRule(selector: string, rule: string): void {40this.rules.unshift(`${selector} {${rule}}`);41}4243public override removeRulesContainingSelector(ruleName: string): void {44for (let i = 0; i < this.rules.length; i++) {45if (this.rules[i].indexOf(ruleName) >= 0) {46this.rules.splice(i, 1);47i--;48}49}50}5152public read(): string {53return this.rules.join('\n');54}55}5657export class TestCommandService implements ICommandService {58declare readonly _serviceBrand: undefined;5960private readonly _instantiationService: IInstantiationService;6162private readonly _onWillExecuteCommand = new Emitter<ICommandEvent>();63public readonly onWillExecuteCommand: Event<ICommandEvent> = this._onWillExecuteCommand.event;6465private readonly _onDidExecuteCommand = new Emitter<ICommandEvent>();66public readonly onDidExecuteCommand: Event<ICommandEvent> = this._onDidExecuteCommand.event;6768constructor(instantiationService: IInstantiationService) {69this._instantiationService = instantiationService;70}7172public executeCommand<T>(id: string, ...args: unknown[]): Promise<T> {73const command = CommandsRegistry.getCommand(id);74if (!command) {75return Promise.reject(new Error(`command '${id}' not found`));76}7778try {79this._onWillExecuteCommand.fire({ commandId: id, args });80const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command.handler, ...args]) as T;81this._onDidExecuteCommand.fire({ commandId: id, args });82return Promise.resolve(result);83} catch (err) {84return Promise.reject(err);85}86}87}888990