Path: blob/main/src/vs/editor/test/browser/editorTestServices.ts
3294 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 {21return null;22}23public lastInput?: IResourceEditorInput;24override openCodeEditor(input: IResourceEditorInput, source: ICodeEditor | null, sideBySide?: boolean): Promise<ICodeEditor | null> {25this.lastInput = input;26return Promise.resolve(null);27}28}2930export class TestGlobalStyleSheet extends GlobalStyleSheet {3132public rules: string[] = [];3334constructor() {35super(null!);36}3738public override insertRule(selector: string, rule: string): void {39this.rules.unshift(`${selector} {${rule}}`);40}4142public override removeRulesContainingSelector(ruleName: string): void {43for (let i = 0; i < this.rules.length; i++) {44if (this.rules[i].indexOf(ruleName) >= 0) {45this.rules.splice(i, 1);46i--;47}48}49}5051public read(): string {52return this.rules.join('\n');53}54}5556export class TestCommandService implements ICommandService {57declare readonly _serviceBrand: undefined;5859private readonly _instantiationService: IInstantiationService;6061private readonly _onWillExecuteCommand = new Emitter<ICommandEvent>();62public readonly onWillExecuteCommand: Event<ICommandEvent> = this._onWillExecuteCommand.event;6364private readonly _onDidExecuteCommand = new Emitter<ICommandEvent>();65public readonly onDidExecuteCommand: Event<ICommandEvent> = this._onDidExecuteCommand.event;6667constructor(instantiationService: IInstantiationService) {68this._instantiationService = instantiationService;69}7071public executeCommand<T>(id: string, ...args: any[]): Promise<T> {72const command = CommandsRegistry.getCommand(id);73if (!command) {74return Promise.reject(new Error(`command '${id}' not found`));75}7677try {78this._onWillExecuteCommand.fire({ commandId: id, args });79const result = this._instantiationService.invokeFunction.apply(this._instantiationService, [command.handler, ...args]) as T;80this._onDidExecuteCommand.fire({ commandId: id, args });81return Promise.resolve(result);82} catch (err) {83return Promise.reject(err);84}85}86}878889