Skip to content

Commit db962a4

Browse files
committed
Ensure Open Query Text works for in progress queries
Same with "Open query that produced these results". In order to do this, needed to move the query id generation into the InitialQueryInfo.
1 parent a69dbd7 commit db962a4

7 files changed

Lines changed: 22 additions & 59 deletions

File tree

extensions/ql-vscode/src/compare/compare-interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ export class CompareInterfaceManager extends DisposableObject {
8282
// otherwise we will wind up with duplicated rows
8383
name: from.getShortLabel(),
8484
status: from.completedQuery.statusString,
85-
time: from.time,
85+
time: from.startTime,
8686
},
8787
toQuery: {
8888
name: to.getShortLabel(),
8989
status: to.completedQuery.statusString,
90-
time: to.time,
90+
time: to.startTime,
9191
},
9292
},
9393
columns: fromResultSet.schema.columns,

extensions/ql-vscode/src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,19 +498,19 @@ async function activateWithInstalledDistribution(
498498

499499
const initialInfo = await createInitialQueryInfo(selectedQuery, databaseInfo, quickEval, range);
500500
const item = new FullQueryInfo(initialInfo, queryHistoryConfigurationListener);
501-
qhm.addCompletedQuery(item);
501+
qhm.addQuery(item);
502502
await qhm.refreshTreeView(item);
503503

504504
try {
505-
const info = await compileAndRunQueryAgainstDatabase(
505+
const completedQueryInfo = await compileAndRunQueryAgainstDatabase(
506506
cliServer,
507507
qs,
508508
databaseItem,
509509
initialInfo,
510510
progress,
511511
token,
512512
);
513-
item.completeThisQuery(info);
513+
item.completeThisQuery(completedQueryInfo);
514514
await showResultsForCompletedQuery(item as FullCompletedQueryInfo, WebviewReveal.NotForced);
515515
// Note we must update the query history view after showing results as the
516516
// display and sorting might depend on the number of results

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

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,8 @@ export class QueryHistoryManager extends DisposableObject {
406406
throw new Error(NO_QUERY_SELECTED);
407407
}
408408

409-
if (!finalSingleItem.completedQuery) {
410-
throw new Error('Select a completed query.');
411-
}
412-
413409
const textDocument = await workspace.openTextDocument(
414-
Uri.file(finalSingleItem.completedQuery.query.program.queryPath)
410+
Uri.file(finalSingleItem.initialInfo.queryPath)
415411
);
416412
const editor = await window.showTextDocument(
417413
textDocument,
@@ -580,18 +576,14 @@ export class QueryHistoryManager extends DisposableObject {
580576
throw new Error(NO_QUERY_SELECTED);
581577
}
582578

583-
if (!singleItem.completedQuery) {
584-
return;
585-
}
586-
587579
const rawQueryName = singleItem.getQueryName();
588580
const queryName = rawQueryName.endsWith('.ql') ? rawQueryName : rawQueryName + '.ql';
589581
const params = new URLSearchParams({
590-
isQuickEval: String(!!singleItem.completedQuery.query.quickEvalPosition),
582+
isQuickEval: String(!!singleItem.initialInfo.quickEvalPosition),
591583
queryText: encodeURIComponent(await this.getQueryText(singleItem)),
592584
});
593585
const uri = Uri.parse(
594-
`codeql:${singleItem.completedQuery.query.queryID}-${queryName}?${params.toString()}`, true
586+
`codeql:${singleItem.initialInfo.id}-${queryName}?${params.toString()}`, true
595587
);
596588
const doc = await workspace.openTextDocument(uri);
597589
await window.showTextDocument(doc, { preview: false });
@@ -670,32 +662,10 @@ export class QueryHistoryManager extends DisposableObject {
670662
}
671663

672664
async getQueryText(queryHistoryItem: FullQueryInfo): Promise<string> {
673-
if (queryHistoryItem.initialInfo.queryText) {
674-
return queryHistoryItem.initialInfo.queryText;
675-
}
676-
677-
if (!queryHistoryItem.completedQuery) {
678-
return '<No label>';
679-
}
680-
681-
const query = queryHistoryItem.completedQuery.query;
682-
683-
if (query.quickEvalPosition) {
684-
// capture all selected lines
685-
const startLine = query.quickEvalPosition.line;
686-
const endLine = query.quickEvalPosition.endLine;
687-
const textDocument = await workspace.openTextDocument(
688-
query.quickEvalPosition.fileName
689-
);
690-
return textDocument.getText(
691-
new Range(startLine - 1, 0, endLine, 0)
692-
);
693-
} else {
694-
return '';
695-
}
665+
return queryHistoryItem.initialInfo.queryText;
696666
}
697667

698-
addCompletedQuery(item: FullQueryInfo) {
668+
addQuery(item: FullQueryInfo) {
699669
this.treeDataProvider.pushQuery(item);
700670
this.updateTreeViewSelectionIfVisible();
701671
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ import { DatabaseInfo } from './pure/interface-types';
2222
*/
2323
export interface InitialQueryInfo {
2424
userSpecifiedLabel?: string; // if missing, use a default label
25-
readonly queryText?: string; // text of the selected file
25+
readonly queryText: string; // text of the selected file, or the selected text when doing quick eval
2626
readonly isQuickQuery: boolean;
2727
readonly isQuickEval: boolean;
2828
readonly quickEvalPosition?: messages.Position;
2929
readonly queryPath: string;
3030
readonly databaseInfo: DatabaseInfo
3131
readonly start: Date;
32+
readonly id: number; // an incrementing number for each query
3233
}
3334

3435
export enum QueryStatus {
@@ -183,14 +184,14 @@ export class FullQueryInfo {
183184
/**/
184185
}
185186

186-
get time() {
187+
get startTime() {
187188
return this.initialInfo.start.toLocaleString(env.language);
188189
}
189190

190191
interpolate(template: string): string {
191192
const { resultCount = 0, statusString = 'in progress' } = this.completedQuery || {};
192193
const replacements: { [k: string]: string } = {
193-
t: this.time,
194+
t: this.startTime,
194195
q: this.getQueryName(),
195196
d: this.initialInfo.databaseInfo.name,
196197
r: resultCount.toString(),

extensions/ql-vscode/src/run-queries.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,21 @@ export const tmpDirDisposal = {
5454
* output and results.
5555
*/
5656
export class QueryEvaluatonInfo {
57-
private static nextQueryId = 0;
58-
5957
readonly compiledQueryPath: string;
6058
readonly dilPath: string;
6159
readonly csvPath: string;
6260
readonly resultsPaths: ResultsPaths;
6361
readonly dataset: Uri; // guarantee the existence of a well-defined dataset dir at this point
64-
readonly queryID: number;
6562

6663
constructor(
64+
public readonly queryID: number,
6765
public readonly program: messages.QlProgram,
6866
public readonly dbItem: DatabaseItem,
6967
public readonly queryDbscheme: string, // the dbscheme file the query expects, based on library path resolution
7068
public readonly quickEvalPosition?: messages.Position,
7169
public readonly metadata?: QueryMetadata,
72-
public readonly templates?: messages.TemplateDefinitions,
70+
public readonly templates?: messages.TemplateDefinitions
7371
) {
74-
this.queryID = QueryEvaluatonInfo.nextQueryId++;
7572
this.compiledQueryPath = path.join(tmpDir.name, `compiledQuery${this.queryID}.qlo`);
7673
this.dilPath = path.join(tmpDir.name, `results${this.queryID}.dil`);
7774
this.csvPath = path.join(tmpDir.name, `results${this.queryID}.csv`);
@@ -617,7 +614,7 @@ export async function compileAndRunQueryAgainstDatabase(
617614
}
618615
}
619616

620-
const query = new QueryEvaluatonInfo(qlProgram, db, packConfig.dbscheme, initialInfo.quickEvalPosition, metadata, templates);
617+
const query = new QueryEvaluatonInfo(initialInfo.id, qlProgram, db, packConfig.dbscheme, initialInfo.quickEvalPosition, metadata, templates);
621618

622619
const upgradeDir = await tmp.dir({ dir: upgradesTmpDir.name, unsafeCleanup: true });
623620
try {
@@ -688,6 +685,7 @@ export async function compileAndRunQueryAgainstDatabase(
688685
}
689686
}
690687

688+
let queryId = 0;
691689
export async function createInitialQueryInfo(
692690
selectedQueryUri: Uri | undefined,
693691
databaseInfo: DatabaseInfo,
@@ -701,9 +699,10 @@ export async function createInitialQueryInfo(
701699
isQuickEval,
702700
isQuickQuery: isQuickQueryPath(queryPath),
703701
databaseInfo,
702+
id: queryId++,
704703
start: new Date(),
705704
... (isQuickEval ? {
706-
queryText: quickEvalText,
705+
queryText: quickEvalText!, // if this query is quick eval, it must have quick eval text
707706
quickEvalPosition: quickEvalPosition
708707
} : {
709708
queryText: await fs.readFile(queryPath, 'utf8')

extensions/ql-vscode/src/vscode-tests/no-workspace/query-results.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,6 @@ describe('query-results', () => {
170170
await completedQuery.updateSortState(mockServer, 'result-name');
171171
expect(completedQuery.sortedResultsInfo.size).to.eq(0);
172172
});
173-
174-
// interpolate
175-
// time
176-
// label
177-
// getShortLabel
178-
// getQueryFileName
179-
// getQueryName
180-
181-
// status
182173
});
183174

184175
it('should interpretResults', async () => {

extensions/ql-vscode/src/vscode-tests/no-workspace/run-queries.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ describe('run-queries', () => {
8484
});
8585
});
8686

87+
let queryNum = 0;
8788
function createMockQueryInfo() {
8889
return new QueryEvaluatonInfo(
90+
queryNum++,
8991
'my-program' as unknown as QlProgram,
9092
{
9193
contents: {

0 commit comments

Comments
 (0)