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