Skip to content

Commit 59378da

Browse files
committed
Merge remote-tracking branch 'origin/main' into koesie10/test-ui-typed-commands
2 parents c6d8a09 + 125af11 commit 59378da

37 files changed

+245
-233
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,
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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ export type LocalQueryCommands = {
5454
"codeQL.quickQuery": () => Promise<void>;
5555
};
5656

57+
export type ResultsViewCommands = {
58+
"codeQLQueryResults.up": () => Promise<void>;
59+
"codeQLQueryResults.down": () => Promise<void>;
60+
"codeQLQueryResults.left": () => Promise<void>;
61+
"codeQLQueryResults.right": () => Promise<void>;
62+
"codeQLQueryResults.nextPathStep": () => Promise<void>;
63+
"codeQLQueryResults.previousPathStep": () => Promise<void>;
64+
};
65+
5766
// Commands used for the query history panel
5867
export type QueryHistoryCommands = {
5968
// Commands in the "navigation" group
@@ -201,7 +210,16 @@ export type TestUICommands = {
201210
"codeQLTests.acceptOutput": (node: TestTreeNode) => Promise<void>;
202211
};
203212

213+
export type MockGitHubApiServerCommands = {
214+
"codeQL.mockGitHubApiServer.startRecording": () => Promise<void>;
215+
"codeQL.mockGitHubApiServer.saveScenario": () => Promise<void>;
216+
"codeQL.mockGitHubApiServer.cancelRecording": () => Promise<void>;
217+
"codeQL.mockGitHubApiServer.loadScenario": () => Promise<void>;
218+
"codeQL.mockGitHubApiServer.unloadScenario": () => Promise<void>;
219+
};
220+
204221
export type AllCommands = BaseCommands &
222+
ResultsViewCommands &
205223
QueryHistoryCommands &
206224
LocalDatabasesCommands &
207225
VariantAnalysisCommands &
@@ -211,7 +229,8 @@ export type AllCommands = BaseCommands &
211229
PackagingCommands &
212230
EvalLogViewerCommands &
213231
SummaryLanguageSupportCommands &
214-
Partial<TestUICommands>;
232+
Partial<TestUICommands> &
233+
MockGitHubApiServerCommands;
215234

216235
export type AppCommandManager = CommandManager<AllCommands>;
217236

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,

extensions/ql-vscode/src/extension.ts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ import { QLTestAdapterFactory } from "./test-adapter";
8989
import { TestUIService } from "./test-ui";
9090
import { CompareView } from "./compare/compare-view";
9191
import { initializeTelemetry } from "./telemetry";
92-
import { commandRunner, ProgressCallback, withProgress } from "./commandRunner";
92+
import { commandRunner } from "./commandRunner";
93+
import { ProgressCallback, withProgress } from "./progress";
9394
import { CodeQlStatusBarHandler } from "./status-bar";
9495
import { getPackagingCommands } from "./packaging";
9596
import { HistoryItemLabelProvider } from "./query-history/history-item-label-provider";
@@ -826,10 +827,14 @@ async function activateWithInstalledDistribution(
826827
const summaryLanguageSupport = new SummaryLanguageSupport();
827828
ctx.subscriptions.push(summaryLanguageSupport);
828829

830+
const mockServer = new VSCodeMockGitHubApiServer(ctx);
831+
ctx.subscriptions.push(mockServer);
832+
829833
void extLogger.log("Registering top-level command palette commands.");
830834

831835
const allCommands: AllCommands = {
832836
...getCommands(cliServer, qs),
837+
...localQueryResultsView.getCommands(),
833838
...qhm.getCommands(),
834839
...variantAnalysisManager.getCommands(),
835840
...databaseUI.getCommands(),
@@ -851,6 +856,7 @@ async function activateWithInstalledDistribution(
851856
...evalLogViewer.getCommands(),
852857
...summaryLanguageSupport.getCommands(),
853858
...testUiCommands,
859+
...mockServer.getCommands(),
854860
};
855861

856862
for (const [commandName, command] of Object.entries(allCommands)) {
@@ -977,39 +983,6 @@ async function activateWithInstalledDistribution(
977983
),
978984
);
979985

980-
const mockServer = new VSCodeMockGitHubApiServer(ctx);
981-
ctx.subscriptions.push(mockServer);
982-
ctx.subscriptions.push(
983-
commandRunner(
984-
"codeQL.mockGitHubApiServer.startRecording",
985-
async () => await mockServer.startRecording(),
986-
),
987-
);
988-
ctx.subscriptions.push(
989-
commandRunner(
990-
"codeQL.mockGitHubApiServer.saveScenario",
991-
async () => await mockServer.saveScenario(),
992-
),
993-
);
994-
ctx.subscriptions.push(
995-
commandRunner(
996-
"codeQL.mockGitHubApiServer.cancelRecording",
997-
async () => await mockServer.cancelRecording(),
998-
),
999-
);
1000-
ctx.subscriptions.push(
1001-
commandRunner(
1002-
"codeQL.mockGitHubApiServer.loadScenario",
1003-
async () => await mockServer.loadScenario(),
1004-
),
1005-
);
1006-
ctx.subscriptions.push(
1007-
commandRunner(
1008-
"codeQL.mockGitHubApiServer.unloadScenario",
1009-
async () => await mockServer.unloadScenario(),
1010-
),
1011-
);
1012-
1013986
await commands.executeCommand("codeQLDatabases.removeOrphanedDatabases");
1014987

1015988
void extLogger.log("Reading query history");

0 commit comments

Comments
 (0)