Skip to content

Commit 102bda2

Browse files
committed
Make "Open Query" show original query of quick queries.
1 parent e98bb1b commit 102bda2

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

extensions/ql-vscode/src/queries.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { logger } from './logging';
1212
import * as messages from './messages';
1313
import * as qsClient from './queryserver-client';
1414
import { promisify } from 'util';
15+
import { QueryHistoryItemOptions } from './query-history';
16+
import { isQuickQueryPath } from './quick-query';
1517

1618
/**
1719
* queries.ts
@@ -205,6 +207,7 @@ export interface EvaluationInfo {
205207
query: QueryInfo;
206208
result: messages.EvaluationResult;
207209
database: DatabaseInfo;
210+
historyItemOptions: QueryHistoryItemOptions;
208211
}
209212

210213
/**
@@ -393,7 +396,7 @@ export async function clearCacheInDatabase(qs: qsClient.QueryServerClient, dbIte
393396
title: "Clearing Cache",
394397
cancellable: false,
395398
}, (progress, token) =>
396-
qs.sendRequest(messages.clearCache, params, token, progress)
399+
qs.sendRequest(messages.clearCache, params, token, progress)
397400
);
398401
}
399402

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

580+
// If this is quick query, store the query text
581+
const historyItemOptions: QueryHistoryItemOptions = {};
582+
if (isQuickQueryPath(queryPath)) {
583+
historyItemOptions.queryText = await fs.readFile(queryPath, 'utf8');
584+
}
585+
577586
// Get the workspace folder paths.
578587
const diskWorkspaceFolders = helpers.getOnDiskWorkspaceFolders();
579588
// Figure out the library path for the query.
@@ -616,7 +625,6 @@ export async function compileAndRunQueryAgainstDatabase(
616625

617626
const errors = await query.compile(qs);
618627

619-
620628
if (errors.length == 0) {
621629
const result = await query.run(qs);
622630
return {
@@ -625,7 +633,8 @@ export async function compileAndRunQueryAgainstDatabase(
625633
database: {
626634
name: db.name,
627635
databaseUri: db.databaseUri.toString(true)
628-
}
636+
},
637+
historyItemOptions
629638
};
630639
} else {
631640
// Error dialogs are limited in size and scrollability,
@@ -650,6 +659,7 @@ export async function compileAndRunQueryAgainstDatabase(
650659
" and the query and database use the same target language. For more details on the error, go to View > Output," +
651660
" and choose CodeQL Query Server from the dropdown.");
652661
}
662+
653663
return {
654664
query,
655665
result: {
@@ -662,7 +672,8 @@ export async function compileAndRunQueryAgainstDatabase(
662672
database: {
663673
name: db.name,
664674
databaseUri: db.databaseUri.toString(true)
665-
}
675+
},
676+
historyItemOptions,
666677
};
667678
}
668679
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class QueryHistoryItem {
3030
constructor(
3131
info: EvaluationInfo,
3232
public config: QueryHistoryConfig,
33-
public options: QueryHistoryItemOptions = {},
33+
public options: QueryHistoryItemOptions = info.historyItemOptions,
3434
) {
3535
this.queryName = helpers.getQueryName(info);
3636
this.databaseName = info.database.name;
@@ -184,9 +184,15 @@ export class QueryHistoryManager {
184184
}
185185
}
186186

187-
async handleOpenQuery(queryHistoryItem: QueryHistoryItem) {
187+
async handleOpenQuery(queryHistoryItem: QueryHistoryItem): Promise<void> {
188188
const textDocument = await vscode.workspace.openTextDocument(vscode.Uri.file(queryHistoryItem.info.query.program.queryPath));
189-
await vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One);
189+
const editor = await vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One);
190+
const queryText = queryHistoryItem.options.queryText;
191+
if (queryText !== undefined) {
192+
await editor.edit(edit => edit.replace(textDocument.validateRange(
193+
new vscode.Range(0, 0, textDocument.lineCount, 0)), queryText)
194+
);
195+
}
190196
}
191197

192198
async handleRemoveHistoryItem(queryHistoryItem: QueryHistoryItem) {
@@ -263,8 +269,8 @@ export class QueryHistoryManager {
263269
});
264270
}
265271

266-
push(evaluationInfo: EvaluationInfo, options: QueryHistoryItemOptions = {}) {
267-
const item = new QueryHistoryItem(evaluationInfo, this.queryHistoryConfigListener, options);
272+
push(evaluationInfo: EvaluationInfo) {
273+
const item = new QueryHistoryItem(evaluationInfo, this.queryHistoryConfigListener);
268274
this.treeDataProvider.push(item);
269275
this.updateTreeViewSelectionIfVisible();
270276
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import { UserCancellationException } from './queries';
1313
const QUICK_QUERIES_DIR_NAME = 'quick-queries';
1414
const QUICK_QUERY_QUERY_NAME = 'quick-query.ql';
1515

16+
export function isQuickQueryPath(queryPath: string): boolean {
17+
return path.basename(queryPath) === QUICK_QUERY_QUERY_NAME;
18+
}
19+
1620
async function getQlPackFor(cliServer: CodeQLCliServer, dbschemePath: string): Promise<string> {
1721
const qlpacks = await cliServer.resolveQlpacks(helpers.getOnDiskWorkspaceFolders());
1822
const packs: { packDir: string | undefined, packName: string }[] =

0 commit comments

Comments
 (0)