Skip to content

Commit b7f86ae

Browse files
committed
Display query text in "virtual" (readonly) file
1 parent 3c73390 commit b7f86ae

8 files changed

Lines changed: 65 additions & 31 deletions

File tree

extensions/ql-vscode/src/extension.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
env,
1313
window,
1414
QuickPickItem,
15-
Range
15+
Range,
16+
workspace,
17+
ProviderResult
1618
} from 'vscode';
1719
import { LanguageClient } from 'vscode-languageclient';
1820
import * as os from 'os';
@@ -78,6 +80,7 @@ import { CodeQlStatusBarHandler } from './status-bar';
7880
import { Credentials } from './authentication';
7981
import { RemoteQueriesManager } from './remote-queries/remote-queries-manager';
8082
import { RemoteQuery } from './remote-queries/remote-query';
83+
import { URLSearchParams } from 'url';
8184

8285
/**
8386
* extension.ts
@@ -773,6 +776,8 @@ async function activateWithInstalledDistribution(
773776
void logger.log('Initializing remote queries interface.');
774777
const rqm = new RemoteQueriesManager(ctx, logger, cliServer);
775778

779+
registerTextProvider();
780+
776781
// The "runRemoteQuery" command is internal-only.
777782
ctx.subscriptions.push(
778783
commandRunnerWithProgress('codeQL.runRemoteQuery', async (
@@ -980,3 +985,20 @@ async function initializeLogging(ctx: ExtensionContext): Promise<void> {
980985
}
981986

982987
const checkForUpdatesCommand = 'codeQL.checkForUpdatesToCLI';
988+
989+
/**
990+
* This text provider lets us open readonly files in the editor.
991+
*
992+
* TODO: Consolidate this with the 'codeql' text provider in query-history.ts.
993+
*/
994+
function registerTextProvider() {
995+
workspace.registerTextDocumentContentProvider('remote-query', {
996+
provideTextDocumentContent(
997+
uri: Uri
998+
): ProviderResult<string> {
999+
const params = new URLSearchParams(uri.query);
1000+
1001+
return params.get('queryText');
1002+
},
1003+
});
1004+
}

extensions/ql-vscode/src/pure/interface-types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ export interface OpenFileMsg {
181181
filePath: string;
182182
}
183183

184+
export interface OpenVirtualFileMsg {
185+
t: 'openVirtualFile';
186+
queryText: string;
187+
}
188+
184189
/**
185190
* Message from the results view to toggle the display of
186191
* query diagnostics.
@@ -369,7 +374,8 @@ export interface ParsedResultSets {
369374
export type FromRemoteQueriesMessage =
370375
| RemoteQueryLoadedMessage
371376
| RemoteQueryErrorMessage
372-
| OpenFileMsg;
377+
| OpenFileMsg
378+
| OpenVirtualFileMsg;
373379

374380
export type ToRemoteQueriesMessage =
375381
| SetRemoteQueryResultMessage;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type QueryHistoryItemOptions = {
3232
isQuickQuery?: boolean;
3333
};
3434

35-
const SHOW_QUERY_TEXT_MSG = `\
35+
export const SHOW_QUERY_TEXT_MSG = `\
3636
////////////////////////////////////////////////////////////////////////////////////
3737
// This is the text of the entire query file when it was executed for this query //
3838
// run. The text or dependent libraries may have changed since then. //

extensions/ql-vscode/src/remote-queries/remote-queries-interface.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { RemoteQuery } from './remote-query';
2121
import { RemoteQueryResult as RemoteQueryResultViewModel } from './shared/remote-query-result';
2222
import { AnalysisResult as AnalysisResultViewModel } from './shared/remote-query-result';
2323
import { showAndLogWarningMessage } from '../helpers';
24+
import { URLSearchParams } from 'url';
25+
import { SHOW_QUERY_TEXT_MSG } from '../query-history';
2426

2527
export class RemoteQueriesInterfaceManager {
2628
private panel: WebviewPanel | undefined;
@@ -65,7 +67,7 @@ export class RemoteQueriesInterfaceManager {
6567
queryTitle: query.queryName,
6668
queryFileName: queryFileName,
6769
queryFilePath: query.queryFilePath,
68-
queryTextTmpFilePath: query.queryTextTmpFilePath,
70+
queryText: query.queryText,
6971
totalRepositoryCount: query.repositories.length,
7072
affectedRepositoryCount: affectedRepositories.length,
7173
totalResultCount: totalResultCount,
@@ -142,6 +144,22 @@ export class RemoteQueriesInterfaceManager {
142144
}
143145
}
144146

147+
private async openVirtualFile(text: string) {
148+
try {
149+
const params = new URLSearchParams({
150+
queryText: encodeURIComponent(SHOW_QUERY_TEXT_MSG + text)
151+
});
152+
const uri = Uri.parse(
153+
`remote-query:query-text.ql?${params.toString()}`,
154+
true
155+
);
156+
const doc = await workspace.openTextDocument(uri);
157+
await Window.showTextDocument(doc, { preview: false });
158+
} catch (error) {
159+
void showAndLogWarningMessage('Could not open query text');
160+
}
161+
}
162+
145163
private async handleMsgFromView(
146164
msg: FromRemoteQueriesMessage
147165
): Promise<void> {
@@ -159,6 +177,9 @@ export class RemoteQueriesInterfaceManager {
159177
case 'openFile':
160178
await this.openFile(msg.filePath);
161179
break;
180+
case 'openVirtualFile':
181+
await this.openVirtualFile(msg.queryText);
182+
break;
162183
default:
163184
assertNever(msg);
164185
}

extensions/ql-vscode/src/remote-queries/remote-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Repository } from './repository';
33
export interface RemoteQuery {
44
queryName: string;
55
queryFilePath: string;
6-
queryTextTmpFilePath: string;
6+
queryText: string;
77
controllerRepository: Repository;
88
repositories: Repository[];
99
executionStartTime: Date;

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -467,26 +467,12 @@ async function buildRemoteQueryEntity(
467467
return { owner: owner, name: repo };
468468
});
469469

470-
// Get the query text from query file and save it in a temporary .ql file.
471-
const queryTextTmpFilePath = path.join(tmpDir.name, `tmp-${queryName}`);
472470
const queryText = await fs.readFile(queryFilePath, 'utf8');
473-
await fs.writeFile(
474-
queryTextTmpFilePath, `\
475-
////////////////////////////////////////////////////////////////////////////////////
476-
// This is the text of the entire query file when it was executed for this query //
477-
// run. The text or dependent libraries may have changed since then. //
478-
// //
479-
// To make changes to the query and the dependent libraries, save this file in a //
480-
// suitable, permanent location. //
481-
////////////////////////////////////////////////////////////////////////////////////
482-
483-
${queryText}`
484-
);
485471

486472
return {
487473
queryName,
488474
queryFilePath,
489-
queryTextTmpFilePath,
475+
queryText,
490476
controllerRepository: {
491477
owner: controllerRepoOwner,
492478
name: controllerRepoName,

extensions/ql-vscode/src/remote-queries/shared/remote-query-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface RemoteQueryResult {
22
queryTitle: string;
33
queryFileName: string;
44
queryFilePath: string;
5-
queryTextTmpFilePath: string;
5+
queryText: string;
66
totalRepositoryCount: number;
77
affectedRepositoryCount: number;
88
totalResultCount: number;

extensions/ql-vscode/src/remote-queries/view/RemoteQueries.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const emptyQueryResult: RemoteQueryResult = {
1313
queryTitle: '',
1414
queryFileName: '',
1515
queryFilePath: '',
16-
queryTextTmpFilePath: '',
16+
queryText: '',
1717
totalRepositoryCount: 0,
1818
affectedRepositoryCount: 0,
1919
totalResultCount: 0,
@@ -40,13 +40,6 @@ const AnalysisResultItem = (props: AnalysisResult) => (
4040
</span>
4141
);
4242

43-
function openFile(filePath: string): void {
44-
vscode.postMessage({
45-
t: 'openFile',
46-
filePath
47-
});
48-
}
49-
5043
export function RemoteQueries(): JSX.Element {
5144
const [queryResult, setQueryResult] = useState<RemoteQueryResult>(emptyQueryResult);
5245

@@ -74,11 +67,17 @@ export function RemoteQueries(): JSX.Element {
7467

7568
try {
7669
const openQueryFile = () => {
77-
openFile(queryResult.queryFilePath);
70+
vscode.postMessage({
71+
t: 'openFile',
72+
filePath: queryResult.queryFilePath
73+
});
7874
};
7975

8076
const openQueryTextTmpFile = () => {
81-
openFile(queryResult.queryTextTmpFilePath);
77+
vscode.postMessage({
78+
t: 'openVirtualFile',
79+
queryText: queryResult.queryText
80+
});
8281
};
8382

8483
return <div className="vscode-codeql__remote-queries-view">

0 commit comments

Comments
 (0)