Skip to content

Commit 79a6425

Browse files
committed
Add a mechanism to debug TM grammars
1 parent 0ca89d9 commit 79a6425

12 files changed

Lines changed: 180 additions & 21 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"sudo-prompt": "9.1.1",
5656
"v8-inspect-profiler": "^0.0.20",
5757
"vscode-nsfw": "1.2.8",
58-
"vscode-oniguruma": "1.1.1",
58+
"vscode-oniguruma": "1.3.0",
5959
"vscode-proxy-agent": "^0.5.2",
6060
"vscode-ripgrep": "^1.5.8",
6161
"vscode-sqlite3": "4.0.10",

remote/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"semver-umd": "^5.5.6",
1717
"spdlog": "^0.11.1",
1818
"vscode-nsfw": "1.2.8",
19-
"vscode-oniguruma": "1.1.1",
19+
"vscode-oniguruma": "1.3.0",
2020
"vscode-proxy-agent": "^0.5.2",
2121
"vscode-ripgrep": "^1.5.8",
2222
"vscode-textmate": "5.0.2",

remote/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"dependencies": {
55
"semver-umd": "^5.5.6",
6-
"vscode-oniguruma": "1.1.1",
6+
"vscode-oniguruma": "1.3.0",
77
"vscode-textmate": "5.0.2",
88
"xterm": "4.6.0-beta.25",
99
"xterm-addon-search": "0.6.0",

remote/web/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ semver-umd@^5.5.6:
77
resolved "https://registry.yarnpkg.com/semver-umd/-/semver-umd-5.5.6.tgz#1d185bbd2caec825c564b54907cd09e14083f228"
88
integrity sha512-6ARYXVi4Y4VO5HfyCjT/6xyykBtJwEXSGQ8ON4UPQSFOjZUDsbAE0J614QcBBsLTTyQMEqvsXN804vAqpydjzw==
99

10-
vscode-oniguruma@1.1.1:
11-
version "1.1.1"
12-
resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.1.1.tgz#81460d148e70668b7c1abfd78ab7fcb2e2c6b316"
13-
integrity sha512-4bygPUQjD5o9nLxp8f0s2TbqLte+Kz3C/IZxheIwunos69jI6OOxAhXW06RgqFbnXAw9ggUydx5qIZ7LD6iNuw==
10+
vscode-oniguruma@1.3.0:
11+
version "1.3.0"
12+
resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.3.0.tgz#6788a9db2f8b0781243b4eb8c7a1dd25f6c0e2c8"
13+
integrity sha512-m4Br19v6XD4MRbVrgsLNSZgQrBzk1BCMCleL8+GrcoGxKEJJd62zOFcTaoQR3hCrSlLqoxWmJ7Cc0VieVV3iTQ==
1414

1515
vscode-textmate@5.0.2:
1616
version "5.0.2"

remote/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ vscode-nsfw@1.2.8:
367367
lodash.isundefined "^3.0.1"
368368
nan "^2.10.0"
369369

370-
vscode-oniguruma@1.1.1:
371-
version "1.1.1"
372-
resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.1.1.tgz#81460d148e70668b7c1abfd78ab7fcb2e2c6b316"
373-
integrity sha512-4bygPUQjD5o9nLxp8f0s2TbqLte+Kz3C/IZxheIwunos69jI6OOxAhXW06RgqFbnXAw9ggUydx5qIZ7LD6iNuw==
370+
vscode-oniguruma@1.3.0:
371+
version "1.3.0"
372+
resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.3.0.tgz#6788a9db2f8b0781243b4eb8c7a1dd25f6c0e2c8"
373+
integrity sha512-m4Br19v6XD4MRbVrgsLNSZgQrBzk1BCMCleL8+GrcoGxKEJJd62zOFcTaoQR3hCrSlLqoxWmJ7Cc0VieVV3iTQ==
374374

375375
vscode-proxy-agent@^0.5.2:
376376
version "0.5.2"

src/vs/workbench/contrib/codeEditor/electron-browser/codeEditor.contribution.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
import './inputClipboardActions';
77
import './sleepResumeRepaintMinimap';
88
import './selectionClipboard';
9+
import './startDebugTextMate';
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 os from 'os';
7+
import * as path from 'path';
8+
9+
import * as nls from 'vs/nls';
10+
import { Range } from 'vs/editor/common/core/range';
11+
import { Action } from 'vs/base/common/actions';
12+
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
13+
import { Registry } from 'vs/platform/registry/common/platform';
14+
import { Extensions as ActionExtensions, IWorkbenchActionRegistry } from 'vs/workbench/common/actions';
15+
import { ITextMateService } from 'vs/workbench/services/textMate/common/textMateService';
16+
import { IModelService } from 'vs/editor/common/services/modelService';
17+
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
18+
import { URI } from 'vs/base/common/uri';
19+
import { createRotatingLogger } from 'vs/platform/log/node/spdlogService';
20+
import { generateUuid } from 'vs/base/common/uuid';
21+
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
22+
import { ITextModel } from 'vs/editor/common/model';
23+
import { Constants } from 'vs/base/common/uint';
24+
25+
class StartDebugTextMate extends Action {
26+
27+
private static resource = URI.parse(`inmemory:///tm-log.txt`);
28+
29+
public static readonly ID = 'editor.action.startDebugTextMate';
30+
public static readonly LABEL = nls.localize('startDebugTextMate', "Start Text Mate Syntax Grammar Logging");
31+
32+
constructor(
33+
id: string,
34+
label: string,
35+
@ITextMateService private readonly _textMateService: ITextMateService,
36+
@IModelService private readonly _modelService: IModelService,
37+
@IEditorService private readonly _editorService: IEditorService,
38+
@ICodeEditorService private readonly _codeEditorService: ICodeEditorService
39+
) {
40+
super(id, label);
41+
}
42+
43+
private _getOrCreateModel(): ITextModel {
44+
const model = this._modelService.getModel(StartDebugTextMate.resource);
45+
if (model) {
46+
return model;
47+
}
48+
return this._modelService.createModel('', null, StartDebugTextMate.resource);
49+
}
50+
51+
private _append(model: ITextModel, str: string) {
52+
const lineCount = model.getLineCount();
53+
model.applyEdits([{
54+
range: new Range(lineCount, Constants.MAX_SAFE_SMALL_INTEGER, lineCount, Constants.MAX_SAFE_SMALL_INTEGER),
55+
text: str
56+
}]);
57+
}
58+
59+
public async run(): Promise<any> {
60+
const pathInTemp = path.join(os.tmpdir(), `vcode-tm-log-${generateUuid()}.txt`);
61+
const logger = createRotatingLogger(`tm-log`, pathInTemp, 1024 * 1024 * 30, 1);
62+
const model = this._getOrCreateModel();
63+
this._append(model, [
64+
`// Open the file you want to test to the side and watch here`,
65+
`// Output mirrored at ${pathInTemp}`
66+
].join('\n'));
67+
const textEditorPane = await this._editorService.openEditor({
68+
resource: model.uri
69+
});
70+
if (!textEditorPane) {
71+
return;
72+
}
73+
const scrollEditor = () => {
74+
const editors = this._codeEditorService.listCodeEditors();
75+
for (const editor of editors) {
76+
if (editor.hasModel()) {
77+
if (editor.getModel().uri.toString() === StartDebugTextMate.resource.toString()) {
78+
editor.revealLine(editor.getModel().getLineCount());
79+
}
80+
}
81+
}
82+
};
83+
this._textMateService.startDebugMode(
84+
(str) => {
85+
this._append(model, str + '\n');
86+
scrollEditor();
87+
logger.info(str);
88+
logger.flush();
89+
},
90+
() => {
91+
92+
}
93+
);
94+
}
95+
}
96+
97+
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
98+
registry.registerWorkbenchAction(SyncActionDescriptor.from(StartDebugTextMate), 'Start Text Mate Syntax Grammar Logging', nls.localize('developer', "Developer"));

src/vs/workbench/services/textMate/browser/abstractTextMateService.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
3030
import { IValidGrammarDefinition, IValidEmbeddedLanguagesMap, IValidTokenTypeMap } from 'vs/workbench/services/textMate/common/TMScopeRegistry';
3131
import { TMGrammarFactory } from 'vs/workbench/services/textMate/common/TMGrammarFactory';
3232
import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
33+
import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress';
3334

3435
export abstract class AbstractTextMateService extends Disposable implements ITextMateService {
3536
public _serviceBrand: undefined;
@@ -41,6 +42,9 @@ export abstract class AbstractTextMateService extends Disposable implements ITex
4142
private readonly _createdModes: string[];
4243
private readonly _encounteredLanguages: boolean[];
4344

45+
private _debugMode: boolean;
46+
private _debugModePrintFunc: (str: string) => void;
47+
4448
private _grammarDefinitions: IValidGrammarDefinition[] | null;
4549
private _grammarFactory: TMGrammarFactory | null;
4650
private _tokenizersRegistrations: IDisposable[];
@@ -54,14 +58,18 @@ export abstract class AbstractTextMateService extends Disposable implements ITex
5458
@INotificationService private readonly _notificationService: INotificationService,
5559
@ILogService private readonly _logService: ILogService,
5660
@IConfigurationService private readonly _configurationService: IConfigurationService,
57-
@IStorageService private readonly _storageService: IStorageService
61+
@IStorageService private readonly _storageService: IStorageService,
62+
@IProgressService private readonly _progressService: IProgressService
5863
) {
5964
super();
6065
this._styleElement = dom.createStyleSheet();
6166
this._styleElement.className = 'vscode-tokens-styles';
6267
this._createdModes = [];
6368
this._encounteredLanguages = [];
6469

70+
this._debugMode = false;
71+
this._debugModePrintFunc = () => { };
72+
6573
this._grammarDefinitions = null;
6674
this._grammarFactory = null;
6775
this._tokenizersRegistrations = [];
@@ -174,6 +182,46 @@ export abstract class AbstractTextMateService extends Disposable implements ITex
174182
});
175183
}
176184

185+
public startDebugMode(printFn: (str: string) => void, onStop: () => void): void {
186+
if (this._debugMode) {
187+
this._notificationService.error(nls.localize('alreadyDebugging', "Already Logging."));
188+
return;
189+
}
190+
191+
this._debugModePrintFunc = printFn;
192+
this._debugMode = true;
193+
194+
if (this._debugMode) {
195+
this._progressService.withProgress(
196+
{
197+
location: ProgressLocation.Notification,
198+
buttons: [nls.localize('stop', "Stop")]
199+
},
200+
(progress) => {
201+
progress.report({
202+
message: nls.localize('progress1', "Preparing to log TM Grammar parsing. Press Stop when finished.")
203+
});
204+
205+
return this._getVSCodeOniguruma().then((vscodeOniguruma) => {
206+
vscodeOniguruma.setDefaultDebugCall(true);
207+
progress.report({
208+
message: nls.localize('progress2', "Now logging TM Grammar parsing. Press Stop when finished.")
209+
});
210+
return new Promise<void>((resolve, reject) => { });
211+
});
212+
},
213+
(choice) => {
214+
this._getVSCodeOniguruma().then((vscodeOniguruma) => {
215+
this._debugModePrintFunc = () => { };
216+
this._debugMode = false;
217+
vscodeOniguruma.setDefaultDebugCall(false);
218+
onStop();
219+
});
220+
}
221+
);
222+
}
223+
}
224+
177225
private _canCreateGrammarFactory(): boolean {
178226
// Check if extension point is ready
179227
return (this._grammarDefinitions ? true : false);
@@ -354,7 +402,13 @@ export abstract class AbstractTextMateService extends Disposable implements ITex
354402

355403
private async _doGetVSCodeOniguruma(): Promise<typeof import('vscode-oniguruma')> {
356404
const [vscodeOniguruma, wasm] = await Promise.all([import('vscode-oniguruma'), this._loadVSCodeOnigurumWASM()]);
357-
await vscodeOniguruma.loadWASM(wasm);
405+
const options = {
406+
data: wasm,
407+
print: (str: string) => {
408+
this._debugModePrintFunc(str);
409+
}
410+
};
411+
await vscodeOniguruma.loadWASM(options);
358412
return vscodeOniguruma;
359413
}
360414

src/vs/workbench/services/textMate/browser/textMateService.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/work
1313
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1414
import { IStorageService } from 'vs/platform/storage/common/storage';
1515
import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
16+
import { IProgressService } from 'vs/platform/progress/common/progress';
1617

1718
export class TextMateService extends AbstractTextMateService {
1819

@@ -23,9 +24,10 @@ export class TextMateService extends AbstractTextMateService {
2324
@INotificationService notificationService: INotificationService,
2425
@ILogService logService: ILogService,
2526
@IConfigurationService configurationService: IConfigurationService,
26-
@IStorageService storageService: IStorageService
27+
@IStorageService storageService: IStorageService,
28+
@IProgressService progressService: IProgressService
2729
) {
28-
super(modeService, themeService, extensionResourceLoaderService, notificationService, logService, configurationService, storageService);
30+
super(modeService, themeService, extensionResourceLoaderService, notificationService, logService, configurationService, storageService, progressService);
2931
}
3032

3133
protected async _loadVSCodeOnigurumWASM(): Promise<Response | ArrayBuffer> {

src/vs/workbench/services/textMate/common/textMateService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface ITextMateService {
1515
onDidEncounterLanguage: Event<LanguageId>;
1616

1717
createGrammar(modeId: string): Promise<IGrammar | null>;
18+
19+
startDebugMode(printFn: (str: string) => void, onStop: () => void): void;
1820
}
1921

2022
// -------------- Types "liberated" from vscode-textmate due to usage in /common/

0 commit comments

Comments
 (0)