Skip to content

Commit 75c9e37

Browse files
committed
Show token on right click command
1 parent 1ef827f commit 75c9e37

File tree

1 file changed

+77
-102
lines changed

1 file changed

+77
-102
lines changed

src/Providers/TreeDataProvider.ts

Lines changed: 77 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import { getScopes, getSubScope } from "../themeScopeColors";
1010

1111

1212
type element = {
13+
type: 'file' | 'root' | 'line' | 'token' | 'scope' | 'rule' | 'name' | 'regex' | 'capture' | 'pattern',
1314
line?: number,
1415
tokenId?: number,
1516
scopeId?: number,
1617
ruleIndex?: number,
1718
ruleId?: RuleId,
1819
token?: IToken,
1920
document: vscode.TextDocument,
20-
type: 'file' | 'root' | 'line' | 'token' | 'scope' | 'rule' | 'name' | 'regex' | 'capture' | 'pattern',
2121
};
2222

2323
// type rule = {
@@ -38,6 +38,7 @@ const grammars: {
3838
} = {};
3939
let grammar: IGrammar;
4040
let callView: CallView = 'tree';
41+
let currentFile: string;
4142

4243
// const ruleChached: number[] = [];
4344

@@ -54,9 +55,10 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
5455
return;
5556
}
5657

58+
currentFile = activeTextEditor.document.uri.toString();
5759
const treeElement: element = {
58-
document: activeTextEditor.document,
5960
type: 'file',
61+
document: activeTextEditor.document,
6062
};
6163
elements.push(treeElement);
6264
return elements;
@@ -67,8 +69,8 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
6769

6870
if (type == 'file') {
6971
const treeElement: element = {
70-
document: document,
7172
type: 'root',
73+
document: document,
7274
};
7375
elements.push(treeElement);
7476
return elements;
@@ -147,11 +149,11 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
147149

148150
if (depth == 0) {
149151
const childElement: element = {
152+
type: 'rule',
150153
line: line,
151154
ruleId: ruleId,
152155
ruleIndex: index,
153156
document: document,
154-
type: 'rule',
155157
// stage: 'name',
156158
};
157159
elements.push(childElement);
@@ -173,9 +175,9 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
173175
if (type == 'root') {
174176
for (let index = 0; index < grammar.lines.length && index < 125000; index++) {
175177
const element: element = {
178+
type: 'line',
176179
line: index,
177180
document: document,
178-
type: 'line',
179181
};
180182
elements.push(element);
181183
}
@@ -186,11 +188,11 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
186188
const tokens = grammar.lines[element.line].tokens;
187189
for (let index = 0; index < tokens.length && index < 125000; index++) {
188190
const tokenElement: element = {
191+
type: 'token',
189192
token: tokens[index],
190193
tokenId: index,
191194
line: element.line,
192195
document: document,
193-
type: 'token',
194196
};
195197
elements.push(tokenElement);
196198
}
@@ -202,11 +204,11 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
202204
const token = element.tokenId;
203205
for (let index = grammar.lines[element.line].tokens[token].scopes.length - 1; index >= 0; index--) {
204206
const tokenElement: element = {
207+
type: 'scope',
205208
tokenId: element.tokenId,
206209
scopeId: index,
207210
line: element.line,
208211
document: document,
209-
type: 'scope',
210212
};
211213
elements.push(tokenElement);
212214
}
@@ -218,6 +220,16 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
218220
// vscode.window.showInformationMessage(`getTreeItem\n${JSON.stringify(element)}`);
219221

220222
const type = element.type;
223+
const document = element.document;
224+
225+
if (type == 'file') {
226+
const item = new vscode.TreeItem(document.uri, vscode.TreeItemCollapsibleState.Collapsed);
227+
item.description = document.languageId;
228+
item.iconPath = vscode.ThemeIcon.File;
229+
item.contextValue = 'document';
230+
item.id = document.uri.toString();
231+
return item;
232+
}
221233

222234
if (type == 'root') {
223235
grammar = await tokenizeFile(element.document, true);
@@ -233,16 +245,8 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
233245
const item = new vscode.TreeItem(treeLabel, vscode.TreeItemCollapsibleState.Collapsed);
234246
item.iconPath = new vscode.ThemeIcon('symbol-variable');
235247
item.tooltip = `Time: ${time}`;
236-
item.description = timeFixed + "ms" + (time > 500 ? ' ⚠️' : '');
237-
return item;
238-
}
239-
240-
const document = element.document;
241-
if (type == 'file') {
242-
const item = new vscode.TreeItem(document.uri, vscode.TreeItemCollapsibleState.Collapsed);
243-
item.description = document.languageId;
244-
item.iconPath = vscode.ThemeIcon.File;
245-
item.contextValue = 'document';
248+
item.description = `${timeFixed}ms${(time > 500 ? ' ⚠️' : '')}`;
249+
item.id = grammar._rootScopeName;
246250
return item;
247251
}
248252

@@ -450,25 +454,28 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
450454
},
451455
getParent(element: element): element {
452456
// vscode.window.showInformationMessage(`getParent\n${JSON.stringify(element)}`);
453-
const type = element.type;
454-
455-
if (type == 'root') {
456-
const parentElement: element = {
457-
document: element.document,
458-
type: 'file',
459-
};
460-
return parentElement;
461-
}
462-
463-
if (type == 'line') {
464-
const parentElement: element = {
465-
document: element.document,
466-
type: 'root',
467-
};
468-
return parentElement;
457+
const document = element.document;
458+
switch (element.type) {
459+
case 'token':
460+
return {
461+
type: 'line',
462+
line: element.line,
463+
document: document,
464+
};
465+
case 'line':
466+
return {
467+
type: 'root',
468+
document: document,
469+
};
470+
case 'root':
471+
return {
472+
type: 'file',
473+
document: document,
474+
};
475+
case 'file':
476+
default:
477+
return undefined;
469478
}
470-
471-
return undefined;
472479
},
473480
resolveTreeItem(item: vscode.TreeItem, element: element, token: vscode.CancellationToken): vscode.TreeItem {
474481
// vscode.window.showInformationMessage(`resolveTreeItem\n${JSON.stringify(element)}\n${JSON.stringify(item, stringify)}`);
@@ -568,82 +575,50 @@ async function CallStackView(textEditor: vscode.TextEditor, edit: vscode.TextEdi
568575
const document = textEditor.document;
569576
const position = textEditor.selection.active;
570577

571-
// const tokenLineResults = await tokenizeFile(document);
572-
// // vscode.window.showInformationMessage(JSON.stringify(tokenLineResults, stringify));
573-
574-
// let rootParent = true;
575-
// let index = 0;
576-
// const ruleList: {
577-
// scopeName: string,
578-
// ruleId: RuleId,
579-
// depth: number,
580-
// _enterPos: number,
581-
// line: number,
582-
// }[] = [];
583-
// for (const tokenLine of tokenLineResults) {
584-
// let parentRule = <StateStackImpl>tokenLine.ruleStack;
585-
// index++;
586-
// const tempRules = [];
587-
// while (parentRule) {
588-
// if (parentRule._enterPos != -1 || rootParent) {
589-
// const rule = {
590-
// scopeName: parentRule.nameScopesList.scopePath.scopeName,
591-
// ruleId: parentRule.ruleId,
592-
// depth: parentRule.depth,
593-
// _enterPos: parentRule._enterPos,
594-
// line: index,
595-
// };
596-
// tempRules.unshift(rule);
597-
// rootParent = false;
598-
// }
599-
// parentRule = parentRule.parent;
600-
// }
601-
// ruleList.push(...tempRules);
602-
// // ruleList = [...ruleList, ...tempRules];
603-
// }
604-
605-
// const ruleTree = {};
606-
607-
// vscode.window.showInformationMessage(JSON.stringify(ruleList, stringify));
608-
609-
// const grammar = await tokenizeFile(document);
610-
611-
// onDidChangeTreeData.fire(undefined);
612-
613-
const activeTextEditor = vscode.window.activeTextEditor;
614-
if (!activeTextEditor) {
615-
return;
578+
if (currentFile != document.uri.toString()) {
579+
refresh();
616580
}
617-
await treeView.reveal(
618-
undefined,
619-
{
620-
expand: true,
621-
focus: true,
622-
select: true,
623-
}
624-
);
625-
await treeView.reveal(
626-
{
627-
document: activeTextEditor.document,
628-
type: 'file',
629-
},
630-
{
631-
expand: true,
632-
focus: true,
633-
select: true,
634-
}
635-
);
581+
636582
await treeView.reveal(
637583
{
638-
document: activeTextEditor.document,
639584
type: 'root',
585+
document: document,
640586
},
641587
{
642588
expand: true,
643-
focus: true,
644-
select: true,
589+
focus: false,
590+
select: false,
645591
}
646592
);
593+
594+
if (callView == 'list') {
595+
const line = position.line;
596+
const character = position.character;
597+
const tokens = grammar.lines[line].tokens;
598+
let index = 0;
599+
for (const token of tokens) {
600+
if (token.startIndex <= character && token.endIndex >= character) {
601+
break;
602+
}
603+
index++;
604+
}
605+
await treeView.reveal(
606+
{
607+
type: 'token',
608+
token: tokens[index],
609+
tokenId: index,
610+
line: line,
611+
document: document,
612+
},
613+
{
614+
expand: false,
615+
focus: true,
616+
select: true,
617+
},
618+
);
619+
return;
620+
}
621+
647622
// treeView.badge = {
648623
// tooltip: "tooltip badge 56: is there any use for this number?",
649624
// value: 56,

0 commit comments

Comments
 (0)