Skip to content

Commit 0e4c3be

Browse files
authored
Merge pull request #377 from jcreedcmu/jcreed/prefix
Fix naming and availability in command palette of various commands
2 parents 1e86e08 + dd1bdf5 commit 0e4c3be

File tree

4 files changed

+147
-23
lines changed

4 files changed

+147
-23
lines changed

extensions/ql-vscode/package.json

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
"onView:codeQLQueryHistory",
2828
"onView:test-explorer",
2929
"onCommand:codeQL.checkForUpdatesToCLI",
30+
"onCommand:codeQLDatabases.chooseDatabaseFolder",
31+
"onCommand:codeQLDatabases.chooseDatabaseArchive",
32+
"onCommand:codeQLDatabases.chooseDatabaseInternet",
33+
"onCommand:codeQL.setCurrentDatabase",
3034
"onCommand:codeQL.chooseDatabaseFolder",
3135
"onCommand:codeQL.chooseDatabaseArchive",
3236
"onCommand:codeQL.chooseDatabaseInternet",
33-
"onCommand:codeQL.setCurrentDatabase",
34-
"onCommand:codeQL.downloadDatabase",
3537
"onCommand:codeQLDatabases.chooseDatabase",
3638
"onCommand:codeQLDatabases.setCurrentDatabase",
3739
"onCommand:codeQL.quickQuery",
@@ -175,24 +177,24 @@
175177
"title": "CodeQL: Quick Query"
176178
},
177179
{
178-
"command": "codeQL.chooseDatabaseFolder",
180+
"command": "codeQLDatabases.chooseDatabaseFolder",
179181
"title": "Choose Database from Folder",
180182
"icon": {
181183
"light": "media/light/folder-opened-plus.svg",
182184
"dark": "media/dark/folder-opened-plus.svg"
183185
}
184186
},
185187
{
186-
"command": "codeQL.chooseDatabaseArchive",
188+
"command": "codeQLDatabases.chooseDatabaseArchive",
187189
"title": "Choose Database from Archive",
188190
"icon": {
189191
"light": "media/light/archive-plus.svg",
190192
"dark": "media/dark/archive-plus.svg"
191193
}
192194
},
193195
{
194-
"command": "codeQL.chooseDatabaseInternet",
195-
"title": "Download database",
196+
"command": "codeQLDatabases.chooseDatabaseInternet",
197+
"title": "Download Database",
196198
"icon": {
197199
"light": "media/light/cloud-download.svg",
198200
"dark": "media/dark/cloud-download.svg"
@@ -231,8 +233,16 @@
231233
"title": "Show Database Directory"
232234
},
233235
{
234-
"command": "codeQL.downloadDatabase",
235-
"title": "CodeQL: Download database"
236+
"command": "codeQL.chooseDatabaseFolder",
237+
"title": "CodeQL: Choose Database from Folder"
238+
},
239+
{
240+
"command": "codeQL.chooseDatabaseArchive",
241+
"title": "CodeQL: Choose Database from Archive"
242+
},
243+
{
244+
"command": "codeQL.chooseDatabaseInternet",
245+
"title": "CodeQL: Download Database"
236246
},
237247
{
238248
"command": "codeQLDatabases.sortByName",
@@ -312,17 +322,17 @@
312322
"group": "navigation"
313323
},
314324
{
315-
"command": "codeQL.chooseDatabaseFolder",
325+
"command": "codeQLDatabases.chooseDatabaseFolder",
316326
"when": "view == codeQLDatabases",
317327
"group": "navigation"
318328
},
319329
{
320-
"command": "codeQL.chooseDatabaseArchive",
330+
"command": "codeQLDatabases.chooseDatabaseArchive",
321331
"when": "view == codeQLDatabases",
322332
"group": "navigation"
323333
},
324334
{
325-
"command": "codeQL.chooseDatabaseInternet",
335+
"command": "codeQLDatabases.chooseDatabaseInternet",
326336
"when": "view == codeQLDatabases",
327337
"group": "navigation"
328338
}
@@ -406,10 +416,6 @@
406416
"command": "codeQL.runQuery",
407417
"when": "resourceLangId == ql && resourceExtname == .ql"
408418
},
409-
{
410-
"command": "codeQL.downloadDatabase",
411-
"when": "true"
412-
},
413419
{
414420
"command": "codeQL.quickEval",
415421
"when": "editorLangId == ql"
@@ -442,6 +448,22 @@
442448
"command": "codeQLDatabases.removeDatabase",
443449
"when": "false"
444450
},
451+
{
452+
"command": "codeQLDatabases.chooseDatabaseFolder",
453+
"when": "false"
454+
},
455+
{
456+
"command": "codeQLDatabases.chooseDatabaseArchive",
457+
"when": "false"
458+
},
459+
{
460+
"command": "codeQLDatabases.chooseDatabaseInternet",
461+
"when": "false"
462+
},
463+
{
464+
"command": "codeQLDatabases.upgradeDatabase",
465+
"when": "false"
466+
},
445467
{
446468
"command": "codeQLQueryHistory.openQuery",
447469
"when": "false"

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ export class DatabaseUI extends DisposableObject {
174174
this.treeDataProvider = this.push(new DatabaseTreeDataProvider(ctx, databaseManager));
175175
this.push(window.createTreeView('codeQLDatabases', { treeDataProvider: this.treeDataProvider }));
176176

177-
ctx.subscriptions.push(commands.registerCommand('codeQL.chooseDatabaseFolder', this.handleChooseDatabaseFolder));
178-
ctx.subscriptions.push(commands.registerCommand('codeQL.chooseDatabaseArchive', this.handleChooseDatabaseArchive));
179-
ctx.subscriptions.push(commands.registerCommand('codeQL.chooseDatabaseInternet', this.handleChooseDatabaseInternet));
177+
ctx.subscriptions.push(commands.registerCommand('codeQLDatabases.chooseDatabaseFolder', this.handleChooseDatabaseFolder));
178+
ctx.subscriptions.push(commands.registerCommand('codeQLDatabases.chooseDatabaseArchive', this.handleChooseDatabaseArchive));
179+
ctx.subscriptions.push(commands.registerCommand('codeQLDatabases.chooseDatabaseInternet', this.handleChooseDatabaseInternet));
180180
ctx.subscriptions.push(commands.registerCommand('codeQL.setCurrentDatabase', this.handleSetCurrentDatabase));
181181
ctx.subscriptions.push(commands.registerCommand('codeQL.upgradeCurrentDatabase', this.handleUpgradeCurrentDatabase));
182182
ctx.subscriptions.push(commands.registerCommand('codeQL.clearCache', this.handleClearCache));
@@ -193,7 +193,7 @@ export class DatabaseUI extends DisposableObject {
193193
await this.databaseManager.setCurrentDatabaseItem(databaseItem);
194194
}
195195

196-
private handleChooseDatabaseFolder = async (): Promise<DatabaseItem | undefined> => {
196+
handleChooseDatabaseFolder = async (): Promise<DatabaseItem | undefined> => {
197197
try {
198198
return await this.chooseAndSetDatabase(true);
199199
} catch (e) {
@@ -202,7 +202,7 @@ export class DatabaseUI extends DisposableObject {
202202
}
203203
}
204204

205-
private handleChooseDatabaseArchive = async (): Promise<DatabaseItem | undefined> => {
205+
handleChooseDatabaseArchive = async (): Promise<DatabaseItem | undefined> => {
206206
try {
207207
return await this.chooseAndSetDatabase(false);
208208
} catch (e) {
@@ -211,7 +211,7 @@ export class DatabaseUI extends DisposableObject {
211211
}
212212
}
213213

214-
private handleChooseDatabaseInternet = async (): Promise<DatabaseItem | undefined> => {
214+
handleChooseDatabaseInternet = async (): Promise<DatabaseItem | undefined> => {
215215
return await promptImportInternetDatabase(this.databaseManager, this.storagePath);
216216
}
217217

extensions/ql-vscode/src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { displayQuickQuery } from './quick-query';
2020
import { compileAndRunQueryAgainstDatabase, tmpDirDisposal, UserCancellationException } from './run-queries';
2121
import { QLTestAdapterFactory } from './test-adapter';
2222
import { TestUIService } from './test-ui';
23-
import { promptImportInternetDatabase } from './databaseFetcher';
2423

2524
/**
2625
* extension.ts
@@ -335,7 +334,9 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu
335334
await qs.restartQueryServer();
336335
helpers.showAndLogInformationMessage('CodeQL Query Server restarted.', { outputLogger: queryServerLogger });
337336
}));
338-
ctx.subscriptions.push(commands.registerCommand('codeQL.downloadDatabase', () => promptImportInternetDatabase(dbm, getContextStoragePath(ctx))));
337+
ctx.subscriptions.push(commands.registerCommand('codeQL.chooseDatabaseFolder', () => databaseUI.handleChooseDatabaseFolder()));
338+
ctx.subscriptions.push(commands.registerCommand('codeQL.chooseDatabaseArchive', () => databaseUI.handleChooseDatabaseArchive()));
339+
ctx.subscriptions.push(commands.registerCommand('codeQL.chooseDatabaseInternet', () => databaseUI.handleChooseDatabaseInternet()));
339340

340341
ctx.subscriptions.push(client.start());
341342

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { expect } from 'chai';
2+
import * as path from 'path';
3+
import * as fs from 'fs-extra';
4+
5+
type CmdDecl = {
6+
command: string;
7+
when?: string;
8+
title?: string;
9+
}
10+
11+
describe('commands declared in package.json', function() {
12+
const manifest = fs.readJsonSync(path.join(__dirname, '../../package.json'));
13+
const commands = manifest.contributes.commands;
14+
const menus = manifest.contributes.menus;
15+
16+
const disabledInPalette: Set<string> = new Set<string>();
17+
18+
// These commands should appear in the command palette, and so
19+
// should be prefixed with 'CodeQL: '.
20+
const paletteCmds: Set<string> = new Set<string>();
21+
22+
// These commands arising on context menus in non-CodeQL controlled
23+
// panels, (e.g. file browser) and so should be prefixed with 'CodeQL: '.
24+
const contribContextMenuCmds: Set<string> = new Set<string>();
25+
26+
// These are commands used in CodeQL controlled panels, and so don't need any prefixing in their title.
27+
const scopedCmds: Set<string> = new Set<string>();
28+
const commandTitles: { [cmd: string]: string } = {};
29+
30+
commands.forEach((commandDecl: CmdDecl) => {
31+
const { command, title } = commandDecl;
32+
if (command.match(/^codeQL\./)
33+
|| command.match(/^codeQLQueryResults\./)
34+
|| command.match(/^codeQLTests\./)) {
35+
paletteCmds.add(command);
36+
expect(title).not.to.be.undefined;
37+
commandTitles[command] = title!;
38+
}
39+
else if (command.match(/^codeQLDatabases\./)
40+
|| command.match(/^codeQLQueryHistory\./)) {
41+
scopedCmds.add(command);
42+
expect(title).not.to.be.undefined;
43+
commandTitles[command] = title!;
44+
}
45+
else {
46+
expect.fail(`Unexpected command name ${command}`);
47+
}
48+
});
49+
50+
menus['explorer/context'].forEach((commandDecl: CmdDecl) => {
51+
const { command } = commandDecl;
52+
paletteCmds.delete(command);
53+
contribContextMenuCmds.add(command);
54+
});
55+
56+
menus['editor/context'].forEach((commandDecl: CmdDecl) => {
57+
const { command } = commandDecl;
58+
paletteCmds.delete(command);
59+
contribContextMenuCmds.add(command);
60+
});
61+
62+
menus.commandPalette.forEach((commandDecl: CmdDecl) => {
63+
if (commandDecl.when === 'false')
64+
disabledInPalette.add(commandDecl.command);
65+
});
66+
67+
68+
69+
it('should have commands appropriately prefixed', function() {
70+
paletteCmds.forEach(command => {
71+
expect(commandTitles[command], `command ${command} should be prefixed with 'CodeQL: ', since it is accessible from the command palette`).to.match(/^CodeQL: /);
72+
});
73+
74+
contribContextMenuCmds.forEach(command => {
75+
expect(commandTitles[command], `command ${command} should be prefixed with 'CodeQL: ', since it is accessible from a context menu in a non-extension-controlled context`).to.match(/^CodeQL: /);
76+
});
77+
78+
scopedCmds.forEach(command => {
79+
expect(commandTitles[command], `command ${command} should not be prefixed with 'CodeQL: ', since it is accessible from an extension-controlled context`).not.to.match(/^CodeQL: /);
80+
});
81+
});
82+
83+
it('should have the right commands accessible from the command palette', function() {
84+
paletteCmds.forEach(command => {
85+
expect(disabledInPalette.has(command), `command ${command} should be enabled in the command palette`).to.be.false;
86+
});
87+
88+
// Commands in contribContextMenuCmds may reasonbly be enabled or
89+
// disabled in the command palette; for example, codeQL.runQuery
90+
// is available there, since we heuristically figure out which
91+
// query to run, but codeQL.setCurrentDatabase is not.
92+
93+
scopedCmds.forEach(command => {
94+
expect(disabledInPalette.has(command), `command ${command} should be disabled in the command palette`).to.be.true;
95+
});
96+
});
97+
98+
99+
});
100+
101+

0 commit comments

Comments
 (0)