Path: blob/main/src/vs/workbench/contrib/codeEditor/electron-browser/startDebugTextMate.ts
3296 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 * as nls from '../../../../nls.js';6import { Range } from '../../../../editor/common/core/range.js';7import { Action2, registerAction2 } from '../../../../platform/actions/common/actions.js';8import { Categories } from '../../../../platform/action/common/actionCommonCategories.js';9import { ITextMateTokenizationService } from '../../../services/textMate/browser/textMateTokenizationFeature.js';10import { IModelService } from '../../../../editor/common/services/model.js';11import { IEditorService } from '../../../services/editor/common/editorService.js';12import { URI } from '../../../../base/common/uri.js';13import { generateUuid } from '../../../../base/common/uuid.js';14import { ICodeEditorService } from '../../../../editor/browser/services/codeEditorService.js';15import { ITextModel } from '../../../../editor/common/model.js';16import { Constants } from '../../../../base/common/uint.js';17import { IHostService } from '../../../services/host/browser/host.js';18import { INativeWorkbenchEnvironmentService } from '../../../services/environment/electron-browser/environmentService.js';19import { ILoggerService } from '../../../../platform/log/common/log.js';20import { joinPath } from '../../../../base/common/resources.js';21import { IFileService } from '../../../../platform/files/common/files.js';22import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';2324class StartDebugTextMate extends Action2 {2526private static resource = URI.parse(`inmemory:///tm-log.txt`);2728constructor() {29super({30id: 'editor.action.startDebugTextMate',31title: nls.localize2('startDebugTextMate', "Start TextMate Syntax Grammar Logging"),32category: Categories.Developer,33f1: true34});35}3637private _getOrCreateModel(modelService: IModelService): ITextModel {38const model = modelService.getModel(StartDebugTextMate.resource);39if (model) {40return model;41}42return modelService.createModel('', null, StartDebugTextMate.resource);43}4445private _append(model: ITextModel, str: string) {46const lineCount = model.getLineCount();47model.applyEdits([{48range: new Range(lineCount, Constants.MAX_SAFE_SMALL_INTEGER, lineCount, Constants.MAX_SAFE_SMALL_INTEGER),49text: str50}]);51}5253async run(accessor: ServicesAccessor) {54const textMateService = accessor.get(ITextMateTokenizationService);55const modelService = accessor.get(IModelService);56const editorService = accessor.get(IEditorService);57const codeEditorService = accessor.get(ICodeEditorService);58const hostService = accessor.get(IHostService);59const environmentService = accessor.get(INativeWorkbenchEnvironmentService);60const loggerService = accessor.get(ILoggerService);61const fileService = accessor.get(IFileService);6263const pathInTemp = joinPath(environmentService.tmpDir, `vcode-tm-log-${generateUuid()}.txt`);64await fileService.createFile(pathInTemp);65const logger = loggerService.createLogger(pathInTemp, { name: 'debug textmate' });66const model = this._getOrCreateModel(modelService);67const append = (str: string) => {68this._append(model, str + '\n');69scrollEditor();70logger.info(str);71logger.flush();72};73await hostService.openWindow([{ fileUri: pathInTemp }], { forceNewWindow: true });74const textEditorPane = await editorService.openEditor({75resource: model.uri,76options: { pinned: true }77});78if (!textEditorPane) {79return;80}81const scrollEditor = () => {82const editors = codeEditorService.listCodeEditors();83for (const editor of editors) {84if (editor.hasModel()) {85if (editor.getModel().uri.toString() === StartDebugTextMate.resource.toString()) {86editor.revealLine(editor.getModel().getLineCount());87}88}89}90};9192append(`// Open the file you want to test to the side and watch here`);93append(`// Output mirrored at ${pathInTemp}`);9495textMateService.startDebugMode(96(str) => {97this._append(model, str + '\n');98scrollEditor();99logger.info(str);100logger.flush();101},102() => {103104}105);106}107}108109registerAction2(StartDebugTextMate);110111112