Skip to content

Commit 0e79b92

Browse files
Merge branch 'main' into robertbrignull/extension_commands
2 parents 31af28e + 125af11 commit 0e79b92

39 files changed

+277
-252
lines changed

extensions/ql-vscode/src/ast-cfg-commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Uri, window } from "vscode";
2-
import { withProgress } from "./commandRunner";
2+
import { withProgress } from "./progress";
33
import { AstViewer } from "./astViewer";
44
import {
55
TemplatePrintAstProvider,

extensions/ql-vscode/src/astViewer.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import {
2323
isWholeFileLoc,
2424
isLineColumnLoc,
2525
} from "./pure/bqrs-utils";
26-
import { commandRunner } from "./commandRunner";
2726
import { DisposableObject } from "./pure/disposable-object";
2827
import { showAndLogExceptionWithTelemetry } from "./helpers";
2928
import { asError, getErrorMessage } from "./pure/helpers-pure";
3029
import { redactableError } from "./pure/errors";
30+
import { AstViewerCommands } from "./common/commands";
3131

3232
export interface AstItem {
3333
id: BqrsId;
@@ -55,15 +55,6 @@ class AstViewerDataProvider
5555
readonly onDidChangeTreeData: Event<AstItem | undefined> =
5656
this._onDidChangeTreeData.event;
5757

58-
constructor() {
59-
super();
60-
this.push(
61-
commandRunner("codeQLAstViewer.gotoCode", async (item: AstItem) => {
62-
await showLocation(item.fileLocation);
63-
}),
64-
);
65-
}
66-
6758
refresh(): void {
6859
this._onDidChangeTreeData.fire(undefined);
6960
}
@@ -126,16 +117,20 @@ export class AstViewer extends DisposableObject {
126117

127118
this.push(this.treeView);
128119
this.push(this.treeDataProvider);
129-
this.push(
130-
commandRunner("codeQLAstViewer.clear", async () => {
131-
this.clear();
132-
}),
133-
);
134120
this.push(
135121
window.onDidChangeTextEditorSelection(this.updateTreeSelection, this),
136122
);
137123
}
138124

125+
getCommands(): AstViewerCommands {
126+
return {
127+
"codeQLAstViewer.clear": async () => this.clear(),
128+
"codeQLAstViewer.gotoCode": async (item: AstItem) => {
129+
await showLocation(item.fileLocation);
130+
},
131+
};
132+
}
133+
139134
updateRoots(roots: AstItem[], db: DatabaseItem, fileUri: Uri) {
140135
this.treeDataProvider.roots = roots;
141136
this.treeDataProvider.db = db;
Lines changed: 14 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
CancellationToken,
3-
ProgressOptions as VSCodeProgressOptions,
4-
window as Window,
5-
commands,
6-
Disposable,
7-
ProgressLocation,
8-
} from "vscode";
1+
import { CancellationToken, commands, Disposable } from "vscode";
92
import {
103
showAndLogExceptionWithTelemetry,
114
showAndLogWarningMessage,
@@ -14,51 +7,22 @@ import { extLogger } from "./common";
147
import { asError, getErrorMessage, getErrorStack } from "./pure/helpers-pure";
158
import { telemetryListener } from "./telemetry";
169
import { redactableError } from "./pure/errors";
17-
18-
export class UserCancellationException extends Error {
19-
/**
20-
* @param message The error message
21-
* @param silent If silent is true, then this exception will avoid showing a warning message to the user.
22-
*/
23-
constructor(message?: string, public readonly silent = false) {
24-
super(message);
25-
}
26-
}
27-
28-
export interface ProgressUpdate {
29-
/**
30-
* The current step
31-
*/
32-
step: number;
33-
/**
34-
* The maximum step. This *should* be constant for a single job.
35-
*/
36-
maxStep: number;
37-
/**
38-
* The current progress message
39-
*/
40-
message: string;
41-
}
42-
43-
export type ProgressCallback = (p: ProgressUpdate) => void;
44-
45-
// Make certain properties within a type optional
46-
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
47-
48-
export type ProgressOptions = Optional<VSCodeProgressOptions, "location">;
10+
import {
11+
UserCancellationException,
12+
withProgress,
13+
ProgressOptions,
14+
ProgressCallback,
15+
} from "./progress";
4916

5017
/**
51-
* A task that reports progress.
18+
* A task that handles command invocations from `commandRunner`.
19+
* Arguments passed to the command handler are passed along,
20+
* untouched to this `NoProgressTask` instance.
5221
*
53-
* @param progress a progress handler function. Call this
54-
* function with a `ProgressUpdate` instance in order to
55-
* denote some progress being achieved on this task.
56-
* @param token a cancellation token
22+
* @param args arguments passed to this task passed on from
23+
* `commands.registerCommand`.
5724
*/
58-
export type ProgressTask<R> = (
59-
progress: ProgressCallback,
60-
token: CancellationToken,
61-
) => Thenable<R>;
25+
export type NoProgressTask = (...args: any[]) => Promise<any>;
6226

6327
/**
6428
* A task that handles command invocations from `commandRunner`
@@ -75,64 +39,12 @@ export type ProgressTask<R> = (
7539
* @param args arguments passed to this task passed on from
7640
* `commands.registerCommand`.
7741
*/
78-
export type ProgressTaskWithArgs<R> = (
42+
type ProgressTaskWithArgs<R> = (
7943
progress: ProgressCallback,
8044
token: CancellationToken,
8145
...args: any[]
8246
) => Thenable<R>;
8347

84-
/**
85-
* A task that handles command invocations from `commandRunner`.
86-
* Arguments passed to the command handler are passed along,
87-
* untouched to this `NoProgressTask` instance.
88-
*
89-
* @param args arguments passed to this task passed on from
90-
* `commands.registerCommand`.
91-
*/
92-
export type NoProgressTask = (...args: any[]) => Promise<any>;
93-
94-
/**
95-
* This mediates between the kind of progress callbacks we want to
96-
* write (where we *set* current progress position and give
97-
* `maxSteps`) and the kind vscode progress api expects us to write
98-
* (which increment progress by a certain amount out of 100%).
99-
*
100-
* Where possible, the `commandRunner` function below should be used
101-
* instead of this function. The commandRunner is meant for wrapping
102-
* top-level commands and provides error handling and other support
103-
* automatically.
104-
*
105-
* Only use this function if you need a progress monitor and the
106-
* control flow does not always come from a command (eg- during
107-
* extension activation, or from an internal language server
108-
* request).
109-
*/
110-
export function withProgress<R>(
111-
task: ProgressTask<R>,
112-
{
113-
location = ProgressLocation.Notification,
114-
title,
115-
cancellable,
116-
}: ProgressOptions = {},
117-
): Thenable<R> {
118-
let progressAchieved = 0;
119-
return Window.withProgress(
120-
{
121-
location,
122-
title,
123-
cancellable,
124-
},
125-
(progress, token) => {
126-
return task((p) => {
127-
const { message, step, maxStep } = p;
128-
const increment = (100 * (step - progressAchieved)) / maxStep;
129-
progressAchieved = step;
130-
progress.report({ message, increment });
131-
}, token);
132-
},
133-
);
134-
}
135-
13648
/**
13749
* A generic wrapper for command registration. This wrapper adds uniform error handling for commands.
13850
*
@@ -216,48 +128,3 @@ export function commandRunnerWithProgress<R>(
216128
outputLogger,
217129
);
218130
}
219-
220-
/**
221-
* Displays a progress monitor that indicates how much progess has been made
222-
* reading from a stream.
223-
*
224-
* @param readable The stream to read progress from
225-
* @param messagePrefix A prefix for displaying the message
226-
* @param totalNumBytes Total number of bytes in this stream
227-
* @param progress The progress callback used to set messages
228-
*/
229-
export function reportStreamProgress(
230-
readable: NodeJS.ReadableStream,
231-
messagePrefix: string,
232-
totalNumBytes?: number,
233-
progress?: ProgressCallback,
234-
) {
235-
if (progress && totalNumBytes) {
236-
let numBytesDownloaded = 0;
237-
const bytesToDisplayMB = (numBytes: number): string =>
238-
`${(numBytes / (1024 * 1024)).toFixed(1)} MB`;
239-
const updateProgress = () => {
240-
progress({
241-
step: numBytesDownloaded,
242-
maxStep: totalNumBytes,
243-
message: `${messagePrefix} [${bytesToDisplayMB(
244-
numBytesDownloaded,
245-
)} of ${bytesToDisplayMB(totalNumBytes)}]`,
246-
});
247-
};
248-
249-
// Display the progress straight away rather than waiting for the first chunk.
250-
updateProgress();
251-
252-
readable.on("data", (data) => {
253-
numBytesDownloaded += data.length;
254-
updateProgress();
255-
});
256-
} else if (progress) {
257-
progress({
258-
step: 1,
259-
maxStep: 2,
260-
message: `${messagePrefix} (Size unknown)`,
261-
});
262-
}
263-
}

extensions/ql-vscode/src/common/commands.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CommandManager } from "../packages/commands";
22
import type { Uri, Range } from "vscode";
3+
import type { AstItem } from "../astViewer";
34
import type { DbTreeViewItem } from "../databases/ui/db-tree-view-item";
45
import type { DatabaseItem } from "../local-databases";
56
import type { QueryHistoryInfo } from "../query-history/query-history-info";
@@ -59,6 +60,15 @@ export type LocalQueryCommands = {
5960
"codeQL.quickQuery": () => Promise<void>;
6061
};
6162

63+
export type ResultsViewCommands = {
64+
"codeQLQueryResults.up": () => Promise<void>;
65+
"codeQLQueryResults.down": () => Promise<void>;
66+
"codeQLQueryResults.left": () => Promise<void>;
67+
"codeQLQueryResults.right": () => Promise<void>;
68+
"codeQLQueryResults.nextPathStep": () => Promise<void>;
69+
"codeQLQueryResults.previousPathStep": () => Promise<void>;
70+
};
71+
6272
// Commands used for the query history panel
6373
export type QueryHistoryCommands = {
6474
// Commands in the "navigation" group
@@ -183,6 +193,11 @@ export type AstCfgCommands = {
183193
"codeQL.viewCfgContextEditor": () => Promise<void>;
184194
};
185195

196+
export type AstViewerCommands = {
197+
"codeQLAstViewer.clear": () => Promise<void>;
198+
"codeQLAstViewer.gotoCode": (item: AstItem) => Promise<void>;
199+
};
200+
186201
export type PackagingCommands = {
187202
"codeQL.installPackDependencies": () => Promise<void>;
188203
"codeQL.downloadPacks": () => Promise<void>;
@@ -192,15 +207,30 @@ export type EvalLogViewerCommands = {
192207
"codeQLEvalLogViewer.clear": () => Promise<void>;
193208
};
194209

210+
export type SummaryLanguageSupportCommands = {
211+
"codeQL.gotoQL": () => Promise<void>;
212+
};
213+
214+
export type MockGitHubApiServerCommands = {
215+
"codeQL.mockGitHubApiServer.startRecording": () => Promise<void>;
216+
"codeQL.mockGitHubApiServer.saveScenario": () => Promise<void>;
217+
"codeQL.mockGitHubApiServer.cancelRecording": () => Promise<void>;
218+
"codeQL.mockGitHubApiServer.loadScenario": () => Promise<void>;
219+
"codeQL.mockGitHubApiServer.unloadScenario": () => Promise<void>;
220+
};
221+
195222
// All commands where the implementation is provided by this extension.
196223
export type AllCodeQLCommands = BaseCommands &
197224
QueryHistoryCommands &
198225
LocalDatabasesCommands &
199226
VariantAnalysisCommands &
200227
DatabasePanelCommands &
201228
AstCfgCommands &
229+
AstViewerCommands &
202230
PackagingCommands &
203-
EvalLogViewerCommands;
231+
EvalLogViewerCommands &
232+
SummaryLanguageSupportCommands &
233+
MockGitHubApiServerCommands;
204234

205235
export type AllCommands = AllCodeQLCommands & VSCodeCommands;
206236

extensions/ql-vscode/src/contextual/locationFinder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { CodeQLCliServer } from "../cli";
1212
import { DatabaseManager, DatabaseItem } from "../local-databases";
1313
import fileRangeFromURI from "./fileRangeFromURI";
14-
import { ProgressCallback } from "../commandRunner";
14+
import { ProgressCallback } from "../progress";
1515
import { KeyType } from "./keyType";
1616
import {
1717
qlpackOfDatabase,

extensions/ql-vscode/src/contextual/queryResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { DatabaseItem } from "../local-databases";
1616
import { extLogger } from "../common";
1717
import { createInitialQueryInfo } from "../run-queries-shared";
1818
import { CancellationToken, Uri } from "vscode";
19-
import { ProgressCallback } from "../commandRunner";
19+
import { ProgressCallback } from "../progress";
2020
import { QueryRunner } from "../queryRunner";
2121
import { redactableError } from "../pure/errors";
2222
import { QLPACK_FILENAMES } from "../pure/ql";

extensions/ql-vscode/src/contextual/templateProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { CodeQLCliServer } from "../cli";
1919
import { DatabaseManager } from "../local-databases";
2020
import { CachedOperation } from "../helpers";
21-
import { ProgressCallback, withProgress } from "../commandRunner";
21+
import { ProgressCallback, withProgress } from "../progress";
2222
import AstBuilder from "./astBuilder";
2323
import { KeyType } from "./keyType";
2424
import {

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { retry } from "@octokit/plugin-retry";
1818

1919
import { DatabaseManager, DatabaseItem } from "./local-databases";
2020
import { showAndLogInformationMessage, tmpDir } from "./helpers";
21-
import { reportStreamProgress, ProgressCallback } from "./commandRunner";
21+
import { reportStreamProgress, ProgressCallback } from "./progress";
2222
import { extLogger } from "./common";
2323
import { getErrorMessage } from "./pure/helpers-pure";
2424
import {

extensions/ql-vscode/src/databases/ui/db-panel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
window,
88
workspace,
99
} from "vscode";
10-
import { UserCancellationException } from "../../commandRunner";
10+
import { UserCancellationException } from "../../progress";
1111
import {
1212
getNwoFromGitHubUrl,
1313
isValidGitHubNwo,

extensions/ql-vscode/src/distribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "./helpers";
1515
import { extLogger } from "./common";
1616
import { getCodeQlCliVersion } from "./cli-version";
17-
import { ProgressCallback, reportStreamProgress } from "./commandRunner";
17+
import { ProgressCallback, reportStreamProgress } from "./progress";
1818
import {
1919
codeQlLauncherName,
2020
deprecatedCodeQlLauncherName,

0 commit comments

Comments
 (0)