Skip to content

Commit 2d1c9a0

Browse files
committed
Add cancellation from query history view
And tweak the commands visible from the view.
1 parent dd0a21a commit 2d1c9a0

6 files changed

Lines changed: 62 additions & 22 deletions

File tree

extensions/ql-vscode/package.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,10 @@
494494
"command": "codeQLQueryHistory.showQueryLog",
495495
"title": "Show Query Log"
496496
},
497+
{
498+
"command": "codeQLQueryHistory.cancel",
499+
"title": "Cancel"
500+
},
497501
{
498502
"command": "codeQLQueryHistory.showQueryText",
499503
"title": "Show Query Text"
@@ -662,7 +666,7 @@
662666
{
663667
"command": "codeQLQueryHistory.removeHistoryItem",
664668
"group": "9_qlCommands",
665-
"when": "view == codeQLQueryHistory"
669+
"when": "view == codeQLQueryHistory && viewItem != inProgressResultsItem"
666670
},
667671
{
668672
"command": "codeQLQueryHistory.setLabel",
@@ -672,12 +676,12 @@
672676
{
673677
"command": "codeQLQueryHistory.compareWith",
674678
"group": "9_qlCommands",
675-
"when": "view == codeQLQueryHistory"
679+
"when": "view == codeQLQueryHistory && (viewItem == rawResultsItem || viewItem == interpretedResultsItem)"
676680
},
677681
{
678682
"command": "codeQLQueryHistory.showQueryLog",
679683
"group": "9_qlCommands",
680-
"when": "view == codeQLQueryHistory"
684+
"when": "view == codeQLQueryHistory && (viewItem == rawResultsItem || viewItem == interpretedResultsItem)"
681685
},
682686
{
683687
"command": "codeQLQueryHistory.showQueryText",
@@ -687,7 +691,7 @@
687691
{
688692
"command": "codeQLQueryHistory.viewCsvResults",
689693
"group": "9_qlCommands",
690-
"when": "view == codeQLQueryHistory && viewItem != interpretedResultsItem"
694+
"when": "view == codeQLQueryHistory && viewItem == rawResultsItem"
691695
},
692696
{
693697
"command": "codeQLQueryHistory.viewCsvAlerts",
@@ -702,12 +706,12 @@
702706
{
703707
"command": "codeQLQueryHistory.viewDil",
704708
"group": "9_qlCommands",
705-
"when": "view == codeQLQueryHistory"
709+
"when": "view == codeQLQueryHistory && (viewItem == rawResultsItem || viewItem == interpretedResultsItem)"
706710
},
707711
{
708-
"command": "codeQL.previewQueryHelp",
712+
"command": "codeQLQueryHistory.cancel",
709713
"group": "9_qlCommands",
710-
"when": "view == codeQLQueryHistory && resourceScheme == .qhelp && isWorkspaceTrusted"
714+
"when": "view == codeQLQueryHistory && viewItem == inProgressResultsItem"
711715
},
712716
{
713717
"command": "codeQLTests.showOutputDifferences",
@@ -860,6 +864,10 @@
860864
"command": "codeQLQueryHistory.showQueryLog",
861865
"when": "false"
862866
},
867+
{
868+
"command": "codeQLQueryHistory.cancel",
869+
"when": "false"
870+
},
863871
{
864872
"command": "codeQLQueryHistory.showQueryText",
865873
"when": "false"

extensions/ql-vscode/src/extension.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CancellationToken,
3+
CancellationTokenSource,
34
commands,
45
Disposable,
56
ExtensionContext,
@@ -496,8 +497,12 @@ async function activateWithInstalledDistribution(
496497
databaseUri: databaseItem.databaseUri.toString(),
497498
};
498499

500+
// handle cancellation from the history view.
501+
const source = new CancellationTokenSource();
502+
token.onCancellationRequested(() => source.cancel());
503+
499504
const initialInfo = await createInitialQueryInfo(selectedQuery, databaseInfo, quickEval, range);
500-
const item = new FullQueryInfo(initialInfo, queryHistoryConfigurationListener);
505+
const item = new FullQueryInfo(initialInfo, queryHistoryConfigurationListener, source);
501506
qhm.addQuery(item);
502507
try {
503508
const completedQueryInfo = await compileAndRunQueryAgainstDatabase(
@@ -506,7 +511,7 @@ async function activateWithInstalledDistribution(
506511
databaseItem,
507512
initialInfo,
508513
progress,
509-
token,
514+
source.token,
510515
);
511516
item.completeThisQuery(completedQueryInfo);
512517
await showResultsForCompletedQuery(item as FullCompletedQueryInfo, WebviewReveal.NotForced);
@@ -517,6 +522,7 @@ async function activateWithInstalledDistribution(
517522
throw e;
518523
} finally {
519524
qhm.refreshTreeView();
525+
source.dispose();
520526
}
521527
}
522528
}

extensions/ql-vscode/src/query-history.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,24 @@ export class HistoryTreeDataProvider extends DisposableObject {
119119
arguments: [element],
120120
};
121121

122-
// Mark this query history item according to whether it has a
123-
// SARIF file so that we can make context menu items conditionally
124-
// available.
125-
const hasResults = await element.completedQuery?.query.hasInterpretedResults();
126-
treeItem.contextValue = hasResults
127-
? 'interpretedResultsItem'
128-
: 'rawResultsItem';
129-
122+
// Populate the icon and the context value. We use the context value to
123+
// control which commands are visible in the context menu.
124+
let hasResults;
130125
switch (element.status) {
131126
case QueryStatus.InProgress:
132-
// TODO this is not a good icon.
133127
treeItem.iconPath = new ThemeIcon('sync~spin');
128+
treeItem.contextValue = 'inProgressResultsItem';
134129
break;
135130
case QueryStatus.Completed:
131+
hasResults = await element.completedQuery?.query.hasInterpretedResults();
136132
treeItem.iconPath = this.localSuccessIconPath;
133+
treeItem.contextValue = hasResults
134+
? 'interpretedResultsItem'
135+
: 'rawResultsItem';
137136
break;
138137
case QueryStatus.Failed:
139138
treeItem.iconPath = this.failedIconPath;
139+
treeItem.contextValue = 'cancelledResultsItem';
140140
break;
141141
default:
142142
assertNever(element.status);
@@ -332,6 +332,12 @@ export class QueryHistoryManager extends DisposableObject {
332332
this.handleShowQueryLog.bind(this)
333333
)
334334
);
335+
this.push(
336+
commandRunner(
337+
'codeQLQueryHistory.cancel',
338+
this.handleCancel.bind(this)
339+
)
340+
);
335341
this.push(
336342
commandRunner(
337343
'codeQLQueryHistory.showQueryText',
@@ -439,7 +445,7 @@ export class QueryHistoryManager extends DisposableObject {
439445
const { finalSingleItem, finalMultiSelect } = this.determineSelection(singleItem, multiSelect);
440446

441447
(finalMultiSelect || [finalSingleItem]).forEach((item) => {
442-
// TODO: Removing in progress queries is not supported yet
448+
// Removing in progress queries is not supported yet
443449
if (item.status !== QueryStatus.InProgress) {
444450
this.treeDataProvider.remove(item);
445451
item.completedQuery?.dispose();
@@ -568,6 +574,19 @@ export class QueryHistoryManager extends DisposableObject {
568574
}
569575
}
570576

577+
async handleCancel(
578+
singleItem: FullQueryInfo,
579+
multiSelect: FullQueryInfo[]
580+
) {
581+
const { finalSingleItem, finalMultiSelect } = this.determineSelection(singleItem, multiSelect);
582+
583+
(finalMultiSelect || [finalSingleItem]).forEach((item) => {
584+
if (item.status === QueryStatus.InProgress) {
585+
item.cancel();
586+
}
587+
});
588+
}
589+
571590
async handleShowQueryText(
572591
singleItem: FullQueryInfo,
573592
multiSelect: FullQueryInfo[]

extensions/ql-vscode/src/query-results.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { env } from 'vscode';
1+
import { CancellationTokenSource, env } from 'vscode';
22

33
import { QueryWithResults, tmpDir, QueryEvaluationInfo } from './run-queries';
44
import * as messages from './pure/messages';
@@ -180,10 +180,15 @@ export class FullQueryInfo {
180180
constructor(
181181
public readonly initialInfo: InitialQueryInfo,
182182
private readonly config: QueryHistoryConfig,
183+
private readonly source: CancellationTokenSource
183184
) {
184185
/**/
185186
}
186187

188+
cancel() {
189+
this.source.cancel();
190+
}
191+
187192
get startTime() {
188193
return this.initialInfo.start.toLocaleString(env.language);
189194
}

extensions/ql-vscode/src/run-queries.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ let queryId = 0;
689689
export async function createInitialQueryInfo(
690690
selectedQueryUri: Uri | undefined,
691691
databaseInfo: DatabaseInfo,
692-
isQuickEval: boolean, range?: Range
692+
isQuickEval: boolean,
693+
range?: Range
693694
): Promise<InitialQueryInfo> {
694695
// Determine which query to run, based on the selection and the active editor.
695696
const { queryPath, quickEvalPosition, quickEvalText } = await determineSelectedQuery(selectedQueryUri, isQuickEval, range);

extensions/ql-vscode/src/vscode-tests/no-workspace/query-history.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ describe('query-history', () => {
515515
start: new Date(),
516516
queryPath: 'hucairz'
517517
} as InitialQueryInfo,
518-
configListener
518+
configListener,
519+
{} as vscode.CancellationTokenSource
519520
);
520521

521522
if (queryWitbResults) {

0 commit comments

Comments
 (0)