Skip to content

Commit

Permalink
Merge pull request #210 from jcreedcmu/jcreed/quick-query
Browse files Browse the repository at this point in the history
Teach extension to do Quick Query
  • Loading branch information
alexet authored Jan 24, 2020
2 parents d69d7dc + 27a3efe commit cf53645
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 13 deletions.
2 changes: 2 additions & 0 deletions extensions/ql-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 1.0.4

- Disable word-based autocomplete by default.
- Add command `CodeQL: Quick Query` for easy query creation without
having to choose a place in the filesystem to store the query file.

## 1.0.3 - 13 January 2020

Expand Down
7 changes: 7 additions & 0 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"onCommand:codeQL.setCurrentDatabase",
"onCommand:codeQLDatabases.chooseDatabase",
"onCommand:codeQLDatabases.setCurrentDatabase",
"onCommand:codeQL.quickQuery",
"onWebviewPanel:resultsView",
"onFileSystem:codeql-zip-archive"
],
Expand Down Expand Up @@ -142,6 +143,10 @@
"command": "codeQL.quickEval",
"title": "CodeQL: Quick Evaluation"
},
{
"command": "codeQL.quickQuery",
"title": "CodeQL: Quick Query"
},
{
"command": "codeQL.chooseDatabase",
"title": "CodeQL: Choose Database",
Expand Down Expand Up @@ -341,6 +346,7 @@
"classnames": "~2.2.6",
"fs-extra": "^8.1.0",
"glob-promise": "^3.4.0",
"js-yaml": "^3.12.0",
"node-fetch": "~2.6.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
Expand All @@ -359,6 +365,7 @@
"@types/glob": "^7.1.1",
"@types/google-protobuf": "^3.2.7",
"@types/gulp": "^4.0.6",
"@types/js-yaml": "~3.12.1",
"@types/jszip": "~3.1.6",
"@types/mocha": "~5.2.7",
"@types/node": "^12.0.8",
Expand Down
21 changes: 20 additions & 1 deletion extensions/ql-vscode/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface UpgradesInfo {
finalDbscheme: string;
}

/**
* The expected output of `codeql resolve qlpacks`.
*/
export type QlpacksInfo = { [name: string]: string[] };

/**
* The expected output of `codeql resolve metadata`.
*/
Expand Down Expand Up @@ -396,7 +401,6 @@ export class CodeQLCliServer implements Disposable {
"Resolving database");
}


/**
* Gets information necessary for upgrading a database.
* @param dbScheme the path to the dbscheme of the database to be upgraded.
Expand All @@ -412,6 +416,21 @@ export class CodeQLCliServer implements Disposable {
"Resolving database upgrade scripts",
);
}

/**
* Gets information about available qlpacks
* @param searchPath A list of directories to search for qlpacks
* @returns A dictionary mapping qlpack name to the directory it comes from
*/
resolveQlpacks(searchPath: string[]): Promise<QlpacksInfo> {
const args = ['--additional-packs', searchPath.join(path.delimiter)];

return this.runJsonCodeQlCliCommand<QlpacksInfo>(
['resolve', 'qlpacks'],
args,
"Resolving qlpack information",
);
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions extensions/ql-vscode/src/databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ export interface DatabaseItem {
*/
getSourceLocationPrefix(server: cli.CodeQLCliServer): Promise<string>;

/**
* Returns dataset folder of exported database.
*/
getDatasetFolder(server: cli.CodeQLCliServer): Promise<string>;

/**
* Returns the root uri of the virtual filesystem for this database's source archive,
* as displayed in the filesystem explorer.
Expand Down Expand Up @@ -385,6 +390,14 @@ class DatabaseItemImpl implements DatabaseItem {
return dbInfo.sourceLocationPrefix;
}

/**
* Returns path to dataset folder of database.
*/
public async getDatasetFolder(server: cli.CodeQLCliServer): Promise<string> {
const dbInfo = await this.getDbInfo(server);
return dbInfo.datasetFolder;
}

/**
* Returns the root uri of the virtual filesystem for this database's source archive.
*/
Expand Down
2 changes: 2 additions & 0 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { QueryHistoryManager } from './query-history';
import * as qsClient from './queryserver-client';
import { CodeQLCliServer } from './cli';
import { assertNever } from './helpers-pure';
import { displayQuickQuery } from './quick-query';

/**
* extension.ts
Expand Down Expand Up @@ -305,6 +306,7 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu

ctx.subscriptions.push(commands.registerCommand('codeQL.runQuery', async (uri: Uri | undefined) => await compileAndRunQuery(false, uri)));
ctx.subscriptions.push(commands.registerCommand('codeQL.quickEval', async (uri: Uri | undefined) => await compileAndRunQuery(true, uri)));
ctx.subscriptions.push(commands.registerCommand('codeQL.quickQuery', async () => displayQuickQuery(ctx, cliServer, databaseUI)));

ctx.subscriptions.push(client.start());
}
Expand Down
19 changes: 15 additions & 4 deletions extensions/ql-vscode/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { logger } from './logging';
import * as messages from './messages';
import * as qsClient from './queryserver-client';
import { promisify } from 'util';
import { QueryHistoryItemOptions } from './query-history';
import { isQuickQueryPath } from './quick-query';

/**
* queries.ts
Expand Down Expand Up @@ -205,6 +207,7 @@ export interface EvaluationInfo {
query: QueryInfo;
result: messages.EvaluationResult;
database: DatabaseInfo;
historyItemOptions: QueryHistoryItemOptions;
}

/**
Expand Down Expand Up @@ -393,7 +396,7 @@ export async function clearCacheInDatabase(qs: qsClient.QueryServerClient, dbIte
title: "Clearing Cache",
cancellable: false,
}, (progress, token) =>
qs.sendRequest(messages.clearCache, params, token, progress)
qs.sendRequest(messages.clearCache, params, token, progress)
);
}

Expand Down Expand Up @@ -574,6 +577,12 @@ export async function compileAndRunQueryAgainstDatabase(
// Determine which query to run, based on the selection and the active editor.
const { queryPath, quickEvalPosition } = await determineSelectedQuery(selectedQueryUri, quickEval);

// If this is quick query, store the query text
const historyItemOptions: QueryHistoryItemOptions = {};
if (isQuickQueryPath(queryPath)) {
historyItemOptions.queryText = await fs.readFile(queryPath, 'utf8');
}

// Get the workspace folder paths.
const diskWorkspaceFolders = helpers.getOnDiskWorkspaceFolders();
// Figure out the library path for the query.
Expand Down Expand Up @@ -616,7 +625,6 @@ export async function compileAndRunQueryAgainstDatabase(

const errors = await query.compile(qs);


if (errors.length == 0) {
const result = await query.run(qs);
return {
Expand All @@ -625,7 +633,8 @@ export async function compileAndRunQueryAgainstDatabase(
database: {
name: db.name,
databaseUri: db.databaseUri.toString(true)
}
},
historyItemOptions
};
} else {
// Error dialogs are limited in size and scrollability,
Expand All @@ -650,6 +659,7 @@ export async function compileAndRunQueryAgainstDatabase(
" and the query and database use the same target language. For more details on the error, go to View > Output," +
" and choose CodeQL Query Server from the dropdown.");
}

return {
query,
result: {
Expand All @@ -662,7 +672,8 @@ export async function compileAndRunQueryAgainstDatabase(
database: {
name: db.name,
databaseUri: db.databaseUri.toString(true)
}
},
historyItemOptions,
};
}
}
27 changes: 19 additions & 8 deletions extensions/ql-vscode/src/query-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { QueryHistoryConfig } from './config';
* `TreeDataProvider` subclass below.
*/

export type QueryHistoryItemOptions = {
label?: string, // user-settable label
queryText?: string, // stored query for quick query
}

/**
* One item in the user-displayed list of queries that have been run.
*/
Expand All @@ -25,7 +30,7 @@ export class QueryHistoryItem {
constructor(
info: EvaluationInfo,
public config: QueryHistoryConfig,
public label?: string, // user-settable label
public options: QueryHistoryItemOptions = info.historyItemOptions,
) {
this.queryName = helpers.getQueryName(info);
this.databaseName = info.database.name;
Expand Down Expand Up @@ -65,8 +70,8 @@ export class QueryHistoryItem {
}

getLabel(): string {
if (this.label !== undefined)
return this.label;
if (this.options.label !== undefined)
return this.options.label;
return this.config.format;
}

Expand Down Expand Up @@ -179,9 +184,15 @@ export class QueryHistoryManager {
}
}

async handleOpenQuery(queryHistoryItem: QueryHistoryItem) {
async handleOpenQuery(queryHistoryItem: QueryHistoryItem): Promise<void> {
const textDocument = await vscode.workspace.openTextDocument(vscode.Uri.file(queryHistoryItem.info.query.program.queryPath));
await vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One);
const editor = await vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One);
const queryText = queryHistoryItem.options.queryText;
if (queryText !== undefined) {
await editor.edit(edit => edit.replace(textDocument.validateRange(
new vscode.Range(0, 0, textDocument.lineCount, 0)), queryText)
);
}
}

async handleRemoveHistoryItem(queryHistoryItem: QueryHistoryItem) {
Expand All @@ -203,9 +214,9 @@ export class QueryHistoryManager {
if (response !== undefined) {
if (response === '')
// Interpret empty string response as "go back to using default"
queryHistoryItem.label = undefined;
queryHistoryItem.options.label = undefined;
else
queryHistoryItem.label = response;
queryHistoryItem.options.label = response;
this.treeDataProvider.refresh();
}
}
Expand Down Expand Up @@ -277,7 +288,7 @@ export class QueryHistoryManager {
const current = this.treeDataProvider.getCurrent();
if (current != undefined) {
// We must fire the onDidChangeTreeData event to ensure the current element can be selected
// using `reveal` if the tree view was not visible when the current element was added.
// using `reveal` if the tree view was not visible when the current element was added.
this.treeDataProvider.refresh();
this.treeView.reveal(current);
}
Expand Down
Loading

0 comments on commit cf53645

Please sign in to comment.