Skip to content

Commit cff558f

Browse files
committed
Fix error: Cannot read properties of undefined (reading '_cachedCompiledPatterns') #10
1 parent a365719 commit cff558f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Providers/TreeDataProvider.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ const TreeDataProvider: vscode.TreeDataProvider<element> = {
294294

295295
if (type == 'root') {
296296
grammar = await tokenizeFile(element.document, false);
297+
// vscode.window.showInformationMessage(`grammar\n${JSON.stringify(grammar, stringify)}`);
297298

298299
if (grammar) {
299300
const time = grammar.lines[grammar.lines.length - 1].time;
@@ -309,7 +310,7 @@ const TreeDataProvider: vscode.TreeDataProvider<element> = {
309310

310311
let regexCount = 0;
311312
for (const rule of grammar.rules) {
312-
if (!rule) {
313+
if (!rule?.parentId) {
313314
continue;
314315
}
315316
regexCount += grammar._ruleId2desc[Math.abs(rule.parentId)]._cachedCompiledPatterns?._items.length ?? 0;
@@ -384,11 +385,11 @@ const TreeDataProvider: vscode.TreeDataProvider<element> = {
384385
// return item;
385386
// }
386387

387-
const ruleId = element.ruleId!;
388+
const ruleId = element.ruleId;
388389
const line = element.line!;
389390
const id = element.ruleIndex!;
390391

391-
const cachedRule = grammar._ruleId2desc[Math.abs(ruleId)];
392+
const cachedRule = ruleId === undefined ? undefined : grammar._ruleId2desc[Math.abs(ruleId)];
392393
const rule = grammar.rules[id]!;
393394
let prevTime = grammar.startTime;
394395
for (let index = id - 1; index >= 0; index--) {
@@ -413,7 +414,7 @@ const TreeDataProvider: vscode.TreeDataProvider<element> = {
413414
};
414415
const item = new vscode.TreeItem(
415416
treeLabel,
416-
cachedRule?._begin && ruleId >= 0 ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.None, // TODO: toggle option
417+
cachedRule?._begin && ruleId !== undefined && ruleId >= 0 ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.None, // TODO: toggle option
417418
);
418419
item.id = `_${id}`;
419420
if (!grammar.lines[line]) {
@@ -428,10 +429,10 @@ const TreeDataProvider: vscode.TreeDataProvider<element> = {
428429
item.iconPath = new vscode.ThemeIcon('regex');
429430
// item.iconPath = new vscode.ThemeIcon('symbol-event');
430431
}
431-
else if (cachedRule?._begin && ruleId < 0) {
432+
else if (cachedRule?._begin && ruleId && ruleId < 0) {
432433
item.iconPath = new vscode.ThemeIcon('chevron-up');
433434
}
434-
item.tooltip = `${stoppedEarly ? `Tokenization for line ${line + 1} was stopped early due to hitting time limit: 15sec\n` : ''}${cachedRule?._match ? `match: ${cachedRule._match.source}` : ''}${ruleId >= 0 ? (cachedRule?._begin ? `begin: ${cachedRule._begin.source}` : '') : (cachedRule?._end ? `end: ${cachedRule._end.source}` : '')}${cachedRule?._while ? `while: ${cachedRule._while.source}` : ''}${ruleId ? '' : '<EndOfLine>'}\nRuleId: ${ruleId}\nParentId: ${rule.parentId}`;
435+
item.tooltip = `${stoppedEarly ? `Tokenization for line ${line + 1} was stopped early due to hitting time limit: 15sec\n` : ''}${cachedRule?._match ? `match: ${cachedRule._match.source}` : ''}${ruleId !== undefined && ruleId >= 0 ? (cachedRule?._begin ? `begin: ${cachedRule._begin.source}` : '') : (cachedRule?._end ? `end: ${cachedRule._end.source}` : '')}${cachedRule?._while ? `while: ${cachedRule._while.source}` : ''}${ruleId ? '' : '<EndOfLine>'}\nRuleId: ${ruleId}\nParentId: ${rule.parentId}`;
435436
item.command = {
436437
title: `Show Call Details`,
437438
command: 'textmate.call.details',
@@ -1096,7 +1097,7 @@ async function gotoGrammar(element: element) {
10961097
// annoyingly the ids are assigned on a first-served basis
10971098
// including across included/embedded grammars
10981099

1099-
let ruleId: RuleId | undefined = undefined;
1100+
let ruleId: RuleId | undefined;
11001101
let capturesIndex = -1;
11011102

11021103
if (callView == 'tree') {

src/textmate/grammar/tokenizeString.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ function _checkWhileConditions(grammar: Grammar, lineText: OnigString, isFirstLi
427427
// @ts-ignore
428428
grammar.rules.push(
429429
{
430+
parentId: whileRule.rule.id,
430431
captureIndices: r.captureIndices,
431432
matchedRuleId: -whileRule.rule.id,
432433
anchorPosition: anchorPosition,

0 commit comments

Comments
 (0)