Skip to content

Commit b2e5c56

Browse files
committed
Fix goto variant because of caching issues.
1 parent b130559 commit b2e5c56

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,21 @@
326326
"category": "Shader validator",
327327
"icon": "$(trash)",
328328
"enablement": "view == shader-validator-variants"
329+
},
330+
{
331+
"command": "shader-validator.gotoShaderEntryPoint",
332+
"title": "Go to shader entry point",
333+
"category": "Shader validator",
334+
"icon": "$(arrow-right)"
329335
}
330336
],
331337
"menus": {
338+
"commandPalette": [
339+
{
340+
"command": "shader-validator.gotoShaderEntryPoint",
341+
"when": "false"
342+
}
343+
],
332344
"view/title": [
333345
{
334346
"command": "shader-validator.addCurrentFile",

src/shaderVariant.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export type ShaderVariantFile = {
105105
variants: ShaderVariant[],
106106
};
107107

108-
export type ShaderFunctionList = {
108+
export type ShaderEntryPoint = {
109109
entryPoint: string,
110110
range: vscode.Range,
111111
};
@@ -125,7 +125,7 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
125125
private client: LanguageClient;
126126
private decorator: vscode.TextEditorDecorationType;
127127
private workspaceState: vscode.Memento;
128-
private shaderFunctionCache: Map<string, ShaderFunctionList[]>;
128+
private shaderEntryPointList: Map<string, ShaderEntryPoint[]>;
129129

130130
private load() {
131131
let variants : ShaderVariantFile[] = this.workspaceState.get<ShaderVariantFile[]>(shaderVariantTreeKey, []);
@@ -147,7 +147,7 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
147147
this.workspaceState = context.workspaceState;
148148
this.files = new Map;
149149
this.load();
150-
this.shaderFunctionCache = new Map;
150+
this.shaderEntryPointList = new Map;
151151
this.client = client;
152152
this.tree = vscode.window.createTreeView<ShaderVariantNode>("shader-validator-variants", {
153153
treeDataProvider: this
@@ -203,8 +203,6 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
203203
borderStyle: 'solid',
204204
});
205205
context.subscriptions.push(vscode.commands.registerCommand("shader-validator.addCurrentFile", (node: any): void => {
206-
// undefined means called from title.
207-
console.log("adding", node);
208206
let supportedLangId = ["hlsl", "glsl", "wgsl"];
209207
if (vscode.window.activeTextEditor && supportedLangId.includes(vscode.window.activeTextEditor.document.languageId)) {
210208
this.open(vscode.window.activeTextEditor.document.uri);
@@ -223,19 +221,34 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
223221
await this.edit(node);
224222
this.save();
225223
}));
224+
context.subscriptions.push(vscode.commands.registerCommand("shader-validator.gotoShaderEntryPoint", (uri: vscode.Uri, entryPointName: string) => {
225+
let shaderEntryPointList = this.shaderEntryPointList.get(uri.path);
226+
let entryPoint = shaderEntryPointList?.find(e => e.entryPoint === entryPointName);
227+
if (entryPoint) {
228+
vscode.commands.executeCommand('vscode.open', uri, <vscode.TextDocumentShowOptions>{
229+
selection: entryPoint.range
230+
});
231+
} else {
232+
vscode.window.showWarningMessage(`Failed to find entry point ${entryPointName} for file ${vscode.workspace.asRelativePath(uri)}`);
233+
// Still go to the file.
234+
vscode.commands.executeCommand('vscode.open', uri, <vscode.TextDocumentShowOptions>{
235+
selection: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0))
236+
});
237+
}
238+
}));
226239
// Prepare entry point symbol cache
227240
for (let editor of vscode.window.visibleTextEditors) {
228241
if (editor.document.uri.scheme === 'file') {
229-
this.shaderFunctionCache.set(editor.document.uri.path, []);
242+
this.shaderEntryPointList.set(editor.document.uri.path, []);
230243
}
231244
}
232245
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(document => {
233246
if (document.uri.scheme === 'file') {
234-
this.shaderFunctionCache.set(document.uri.path, []);
247+
this.shaderEntryPointList.set(document.uri.path, []);
235248
}
236249
}));
237250
context.subscriptions.push(vscode.workspace.onDidCloseTextDocument(document => {
238-
this.shaderFunctionCache.delete(document.uri.path);
251+
this.shaderEntryPointList.delete(document.uri.path);
239252
}));
240253
this.updateDependencies();
241254
}
@@ -334,22 +347,16 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
334347
//});
335348
}
336349
public onDocumentSymbols(uri: vscode.Uri, symbols: vscode.SymbolInformation[]) {
337-
let shaderFunctionCache = this.shaderFunctionCache.get(uri.path);
338-
if (shaderFunctionCache) {
339-
let shaderFunctionList = symbols.filter(symbol => symbol.kind === vscode.SymbolKind.Function);
340-
shaderFunctionCache = [];
341-
for (let symbol of shaderFunctionList) {
342-
shaderFunctionCache.push({
343-
entryPoint: symbol.name,
344-
range: new vscode.Range(
345-
new vscode.Position(symbol.location.range.start.line, symbol.location.range.start.character),
346-
new vscode.Position(symbol.location.range.end.line, symbol.location.range.end.character)
347-
)
348-
});
349-
}
350-
this.shaderFunctionCache.set(uri.path, shaderFunctionCache);
351-
this.updateDecorations();
352-
}
350+
this.shaderEntryPointList.set(uri.path, symbols.filter(symbol => symbol.kind === vscode.SymbolKind.Function).map(symbol => {
351+
return {
352+
entryPoint: symbol.name,
353+
range: new vscode.Range(
354+
new vscode.Position(symbol.location.range.start.line, symbol.location.range.start.character),
355+
new vscode.Position(symbol.location.range.end.line, symbol.location.range.end.character)
356+
)
357+
};
358+
}));
359+
this.updateDecorations();
353360
}
354361
private updateDependencies() {
355362
for (let [_, file] of this.files) {
@@ -360,14 +367,13 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
360367
public getTreeItem(element: ShaderVariantNode): vscode.TreeItem {
361368
if (element.kind === 'variant') {
362369
let item = new vscode.TreeItem(element.name, vscode.TreeItemCollapsibleState.Collapsed);
370+
// Need to use a middleware command because item is not updated on collapse change.
363371
item.command = {
364372
title: "Go to variant",
365-
command: 'vscode.open',
373+
command: 'shader-validator.gotoShaderEntryPoint',
366374
arguments: [
367375
element.uri,
368-
<vscode.TextDocumentShowOptions>{
369-
selection: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0))
370-
}
376+
element.name
371377
]
372378
};
373379
item.description = `[${element.defines.defines.map(d => d.label).join(",")}]`;
@@ -376,7 +382,7 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
376382
item.contextValue = element.kind;
377383
return item;
378384
} else if (element.kind === 'file') {
379-
let item = new vscode.TreeItem(vscode.workspace.asRelativePath(element.uri.path), vscode.TreeItemCollapsibleState.Expanded);
385+
let item = new vscode.TreeItem(vscode.workspace.asRelativePath(element.uri), vscode.TreeItemCollapsibleState.Expanded);
380386
item.description = `${element.variants.length}`;
381387
item.resourceUri = element.uri;
382388
item.tooltip = `File ${element.uri.fsPath}`;
@@ -631,7 +637,7 @@ export class ShaderVariantTreeDataProvider implements vscode.TreeDataProvider<Sh
631637
}
632638
private updateDecoration(editor: vscode.TextEditor) {
633639
let file = this.files.get(editor.document.uri.path);
634-
let entryPoints = this.shaderFunctionCache.get(editor.document.uri.path);
640+
let entryPoints = this.shaderEntryPointList.get(editor.document.uri.path);
635641

636642
if (file && entryPoints) {
637643
let variant = getActiveFileVariant(file);

0 commit comments

Comments
 (0)