Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ async function activateWithInstalledDistribution(
);
const item = qhm.addQuery(info);
await showResultsForCompletedQuery(item, WebviewReveal.NotForced);
// The call to showResults potentially creates SARIF file;
// Update the tree item context value to allow viewing that
// SARIF file from context menu.
await qhm.updateTreeItemContextValue(item);
} catch (e) {
if (e instanceof UserCancellationException) {
if (e.silent) {
Expand Down
33 changes: 25 additions & 8 deletions extensions/ql-vscode/src/query-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ const SHOW_QUERY_TEXT_QUICK_EVAL_MSG = `\
*/
const FAILED_QUERY_HISTORY_ITEM_ICON = 'media/red-x.svg';

interface QueryHistoryDataProvider extends vscode.TreeDataProvider<CompletedQuery> {
updateTreeItemContextValue(element: CompletedQuery): Promise<void>;
}

/**
* Tree data provider for the query history view.
*/
class HistoryTreeDataProvider
implements vscode.TreeDataProvider<CompletedQuery> {
class HistoryTreeDataProvider implements QueryHistoryDataProvider {
/**
* XXX: This idiom for how to get a `.fire()`-able event emitter was
* cargo culted from another vscode extension. It seems rather
Expand All @@ -76,7 +79,21 @@ class HistoryTreeDataProvider

constructor(private ctx: ExtensionContext) { }

async updateTreeItemContextValue(element: CompletedQuery): Promise<void> {
// Mark this query history item according to whether it has a
// SARIF file so that we can make context menu items conditionally
// available.
const hasResults = await element.query.hasInterpretedResults();
element.treeItem!.contextValue = hasResults
? 'interpretedResultsItem'
: 'rawResultsItem';
this.refresh();
}

async getTreeItem(element: CompletedQuery): Promise<vscode.TreeItem> {
if (element.treeItem !== undefined)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nicer to convert treeItem to a getter on CompletedQuery. And move all this code into the getter. It avoids a type assertion on updateTreeItemContextValue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, no, this function needs to have the context of the treeview. The CompletedQuery doesn't have that information, or at least doesn't plausibly have it until the treeitem is created.

return element.treeItem;

const it = new vscode.TreeItem(element.toString());

it.command = {
Expand All @@ -85,12 +102,8 @@ class HistoryTreeDataProvider
arguments: [element],
};

// Mark this query history item according to whether it has a
// SARIF file so that we can make context menu items conditionally
// available.
it.contextValue = (await element.query.hasInterpretedResults())
? 'interpretedResultsItem'
: 'rawResultsItem';
element.treeItem = it;
this.updateTreeItemContextValue(element);

if (!element.didRunSuccessfully) {
it.iconPath = path.join(
Expand Down Expand Up @@ -615,4 +628,8 @@ the file in the file explorer and dragging it into the workspace.`
this.compareWithItem = undefined;
}
}

async updateTreeItemContextValue(element: CompletedQuery): Promise<void> {
this.treeDataProvider.updateTreeItemContextValue(element);
}
}
3 changes: 2 additions & 1 deletion extensions/ql-vscode/src/query-results.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from 'vscode';
import { env, TreeItem } from 'vscode';

import { QueryWithResults, tmpDir, QueryInfo } from './run-queries';
import * as messages from './messages';
Expand All @@ -18,6 +18,7 @@ export class CompletedQuery implements QueryWithResults {
readonly database: DatabaseInfo;
readonly logFileLocation?: string;
options: QueryHistoryItemOptions;
treeItem?: TreeItem;
dispose: () => void;

/**
Expand Down