Skip to content

Commit c89a4be

Browse files
Current changes to Code Lens - not working
1 parent 657debe commit c89a4be

4 files changed

Lines changed: 48 additions & 39 deletions

File tree

extensions/ql-vscode/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/ql-vscode/src/CodeLensProvider.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@ import {
22
CodeLensProvider,
33
TextDocument,
44
CodeLens,
5-
Range,
65
Command
76
} from 'vscode';
87

98
class MyCodeLensProvider implements CodeLensProvider {
109
// Each provider requires a provideCodeLenses function which will give the various documents
1110
// the code lenses
12-
async provideCodeLenses(_document: TextDocument): Promise<CodeLens[]> {
13-
// Define where the CodeLens will exist
14-
const topOfDocument = new Range(0, 0, 0, 0);
11+
async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
1512

16-
// Define what command we want to trigger when activating the CodeLens
17-
const command: Command = {
18-
command: 'codeQL.quickEval',
19-
title: 'CodeQL: Quick Evaluation'
20-
};
13+
const codeLenses: CodeLens[] = [];
2114

22-
const codeLens = new CodeLens(topOfDocument, command);
23-
24-
return [codeLens];
15+
for (let index = 0; index < document.lineCount; index++) {
16+
const textLine = document.lineAt(index);
17+
if (textLine.text.includes('predicate')) {
18+
// const regex = new RegExp(/predicate/);
19+
const args = textLine.text.search((new RegExp(/predicate/)));
20+
console.log(textLine, args);
21+
const range = textLine.range;
22+
const command: Command = {
23+
command: 'codeQL.codeLensQuickEval',
24+
title: 'CodeQL: Quick Evaluation',
25+
arguments: ['argument', false]
26+
};
27+
const codeLens = new CodeLens(range, command);
28+
codeLenses.push(codeLens);
29+
}
30+
}
31+
return codeLenses;
2532
}
2633
}
2734

extensions/ql-vscode/src/extension.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,6 @@ export async function activate(ctx: ExtensionContext): Promise<CodeQLExtensionIn
161161

162162
languages.registerCodeLensProvider('*', codelensProvider);
163163

164-
// commands.registerCommand('codelens-sample.enableCodeLens', () => {
165-
// void workspace.getConfiguration('codelens-sample').update('enableCodeLens', true, true);
166-
// });
167-
168-
// commands.registerCommand('codelens-sample.disableCodeLens', () => {
169-
// void workspace.getConfiguration('codelens-sample').update('enableCodeLens', false, true);
170-
// });
171-
172-
// commands.registerCommand('codelens-sample.codelensAction', (args: any) => {
173-
// void window.showInformationMessage(`CodeLens action clicked with args=${args}`);
174-
// });
175-
176164
void logger.log(`Starting ${extensionId} extension`);
177165
if (extension === undefined) {
178166
throw new Error(`Can't find extension ${extensionId}`);
@@ -489,7 +477,9 @@ async function activateWithInstalledDistribution(
489477
progress: ProgressCallback,
490478
token: CancellationToken,
491479
databaseItem: DatabaseItem | undefined,
480+
args?: any
492481
): Promise<void> {
482+
console.log(args);
493483
if (qs !== undefined) {
494484
// If no databaseItem is specified, use the database currently selected in the Databases UI
495485
databaseItem = databaseItem || await databaseUI.getDatabaseItem(progress, token);
@@ -503,7 +493,8 @@ async function activateWithInstalledDistribution(
503493
quickEval,
504494
selectedQuery,
505495
progress,
506-
token
496+
token,
497+
args
507498
);
508499
const item = qhm.buildCompletedQuery(info);
509500
await showResultsForCompletedQuery(item, WebviewReveal.NotForced);
@@ -725,6 +716,15 @@ async function activateWithInstalledDistribution(
725716
cancellable: true
726717
})
727718
);
719+
720+
commands.registerCommand('codeQL.codeLensQuickEval', async (
721+
progress: ProgressCallback,
722+
token: CancellationToken,
723+
uri: Uri | undefined,
724+
args: any) => {
725+
await compileAndRunQuery(true, uri, progress, token, undefined, args );
726+
});
727+
728728
ctx.subscriptions.push(
729729
commandRunnerWithProgress('codeQL.quickQuery', async (
730730
progress: ProgressCallback,

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,17 @@ async function convertToQlPath(filePath: string): Promise<string> {
327327

328328

329329
/** Gets the selected position within the given editor. */
330-
async function getSelectedPosition(editor: TextEditor): Promise<messages.Position> {
331-
const pos = editor.selection.start;
332-
const posEnd = editor.selection.end;
333-
// Convert from 0-based to 1-based line and column numbers.
334-
return {
335-
fileName: await convertToQlPath(editor.document.fileName),
336-
line: pos.line + 1,
337-
column: pos.character + 1,
338-
endLine: posEnd.line + 1,
339-
endColumn: posEnd.character + 1
340-
};
330+
async function getSelectedPosition(editor: TextEditor, args: any | undefined): Promise<messages.Position> {
331+
const pos = editor.selection.start;
332+
const posEnd = editor.selection.end;
333+
// Convert from 0-based to 1-based line and column numbers.
334+
return {
335+
fileName: await convertToQlPath(editor.document.fileName),
336+
line: pos.line + 1,
337+
column: pos.character + 1,
338+
endLine: posEnd.line + 1,
339+
endColumn: posEnd.character + 1
340+
};
341341
}
342342

343343
/**
@@ -485,7 +485,7 @@ type SelectedQuery = {
485485
* @param selectedResourceUri The selected resource when the command was run.
486486
* @param quickEval Whether the command being run is `Quick Evaluation`.
487487
*/
488-
export async function determineSelectedQuery(selectedResourceUri: Uri | undefined, quickEval: boolean): Promise<SelectedQuery> {
488+
export async function determineSelectedQuery(selectedResourceUri: Uri | undefined, quickEval: boolean, args: any | undefined): Promise<SelectedQuery> {
489489
const editor = window.activeTextEditor;
490490

491491
// Choose which QL file to use.
@@ -539,7 +539,7 @@ export async function determineSelectedQuery(selectedResourceUri: Uri | undefine
539539
// Report an error if we end up in this (hopefully unlikely) situation.
540540
throw new Error('The selected resource for quick evaluation should match the active editor.');
541541
}
542-
quickEvalPosition = await getSelectedPosition(editor);
542+
quickEvalPosition = await getSelectedPosition(editor, args);
543543
quickEvalText = editor.document.getText(editor.selection);
544544
}
545545

@@ -554,14 +554,15 @@ export async function compileAndRunQueryAgainstDatabase(
554554
selectedQueryUri: Uri | undefined,
555555
progress: ProgressCallback,
556556
token: CancellationToken,
557+
args: any | undefined,
557558
templates?: messages.TemplateDefinitions,
558559
): Promise<QueryWithResults> {
559560
if (!db.contents || !db.contents.dbSchemeUri) {
560561
throw new Error(`Database ${db.databaseUri} does not have a CodeQL database scheme.`);
561562
}
562563

563564
// Determine which query to run, based on the selection and the active editor.
564-
const { queryPath, quickEvalPosition, quickEvalText } = await determineSelectedQuery(selectedQueryUri, quickEval);
565+
const { queryPath, quickEvalPosition, quickEvalText } = await determineSelectedQuery(selectedQueryUri, quickEval, args);
565566

566567
const historyItemOptions: QueryHistoryItemOptions = {};
567568
historyItemOptions.isQuickQuery === isQuickQueryPath(queryPath);

0 commit comments

Comments
 (0)