|
| 1 | +import { CancellationToken, ExtensionContext, Uri, window } from "vscode"; |
| 2 | +import { commandRunnerWithProgress, ProgressCallback } from "./commandRunner"; |
| 3 | +import { AstViewer } from "./astViewer"; |
| 4 | +import { |
| 5 | + TemplatePrintAstProvider, |
| 6 | + TemplatePrintCfgProvider, |
| 7 | +} from "./contextual/templateProvider"; |
| 8 | +import { compileAndRunQuery } from "./local-queries"; |
| 9 | +import { QueryRunner } from "./queryRunner"; |
| 10 | +import { QueryHistoryManager } from "./query-history/query-history-manager"; |
| 11 | +import { DatabaseUI } from "./local-databases-ui"; |
| 12 | +import { ResultsView } from "./interface"; |
| 13 | + |
| 14 | +type AstCfgOptions = { |
| 15 | + queryRunner: QueryRunner; |
| 16 | + queryHistoryManager: QueryHistoryManager; |
| 17 | + databaseUI: DatabaseUI; |
| 18 | + localQueryResultsView: ResultsView; |
| 19 | + queryStorageDir: string; |
| 20 | + |
| 21 | + astViewer: AstViewer; |
| 22 | + astTemplateProvider: TemplatePrintAstProvider; |
| 23 | + cfgTemplateProvider: TemplatePrintCfgProvider; |
| 24 | +}; |
| 25 | + |
| 26 | +export function registerAstCfgCommands( |
| 27 | + ctx: ExtensionContext, |
| 28 | + { |
| 29 | + queryRunner, |
| 30 | + queryHistoryManager, |
| 31 | + databaseUI, |
| 32 | + localQueryResultsView, |
| 33 | + queryStorageDir, |
| 34 | + astViewer, |
| 35 | + astTemplateProvider, |
| 36 | + cfgTemplateProvider, |
| 37 | + }: AstCfgOptions, |
| 38 | +) { |
| 39 | + ctx.subscriptions.push( |
| 40 | + commandRunnerWithProgress( |
| 41 | + "codeQL.viewAst", |
| 42 | + async ( |
| 43 | + progress: ProgressCallback, |
| 44 | + token: CancellationToken, |
| 45 | + selectedFile: Uri, |
| 46 | + ) => |
| 47 | + await viewAst( |
| 48 | + astViewer, |
| 49 | + astTemplateProvider, |
| 50 | + progress, |
| 51 | + token, |
| 52 | + selectedFile, |
| 53 | + ), |
| 54 | + { |
| 55 | + cancellable: true, |
| 56 | + title: "Calculate AST", |
| 57 | + }, |
| 58 | + ), |
| 59 | + ); |
| 60 | + |
| 61 | + // Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command |
| 62 | + ctx.subscriptions.push( |
| 63 | + commandRunnerWithProgress( |
| 64 | + "codeQL.viewAstContextExplorer", |
| 65 | + async ( |
| 66 | + progress: ProgressCallback, |
| 67 | + token: CancellationToken, |
| 68 | + selectedFile: Uri, |
| 69 | + ) => |
| 70 | + await viewAst( |
| 71 | + astViewer, |
| 72 | + astTemplateProvider, |
| 73 | + progress, |
| 74 | + token, |
| 75 | + selectedFile, |
| 76 | + ), |
| 77 | + { |
| 78 | + cancellable: true, |
| 79 | + title: "Calculate AST", |
| 80 | + }, |
| 81 | + ), |
| 82 | + ); |
| 83 | + |
| 84 | + // Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewAst" command |
| 85 | + ctx.subscriptions.push( |
| 86 | + commandRunnerWithProgress( |
| 87 | + "codeQL.viewAstContextEditor", |
| 88 | + async ( |
| 89 | + progress: ProgressCallback, |
| 90 | + token: CancellationToken, |
| 91 | + selectedFile: Uri, |
| 92 | + ) => |
| 93 | + await viewAst( |
| 94 | + astViewer, |
| 95 | + astTemplateProvider, |
| 96 | + progress, |
| 97 | + token, |
| 98 | + selectedFile, |
| 99 | + ), |
| 100 | + { |
| 101 | + cancellable: true, |
| 102 | + title: "Calculate AST", |
| 103 | + }, |
| 104 | + ), |
| 105 | + ); |
| 106 | + |
| 107 | + ctx.subscriptions.push( |
| 108 | + commandRunnerWithProgress( |
| 109 | + "codeQL.viewCfg", |
| 110 | + async (progress: ProgressCallback, token: CancellationToken) => { |
| 111 | + const res = await cfgTemplateProvider.provideCfgUri( |
| 112 | + window.activeTextEditor?.document, |
| 113 | + ); |
| 114 | + if (res) { |
| 115 | + await compileAndRunQuery( |
| 116 | + queryRunner, |
| 117 | + queryHistoryManager, |
| 118 | + databaseUI, |
| 119 | + localQueryResultsView, |
| 120 | + queryStorageDir, |
| 121 | + false, |
| 122 | + res[0], |
| 123 | + progress, |
| 124 | + token, |
| 125 | + undefined, |
| 126 | + ); |
| 127 | + } |
| 128 | + }, |
| 129 | + { |
| 130 | + title: "Calculating Control Flow Graph", |
| 131 | + cancellable: true, |
| 132 | + }, |
| 133 | + ), |
| 134 | + ); |
| 135 | + |
| 136 | + // Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command |
| 137 | + ctx.subscriptions.push( |
| 138 | + commandRunnerWithProgress( |
| 139 | + "codeQL.viewCfgContextExplorer", |
| 140 | + async (progress: ProgressCallback, token: CancellationToken) => { |
| 141 | + const res = await cfgTemplateProvider.provideCfgUri( |
| 142 | + window.activeTextEditor?.document, |
| 143 | + ); |
| 144 | + if (res) { |
| 145 | + await compileAndRunQuery( |
| 146 | + queryRunner, |
| 147 | + queryHistoryManager, |
| 148 | + databaseUI, |
| 149 | + localQueryResultsView, |
| 150 | + queryStorageDir, |
| 151 | + false, |
| 152 | + res[0], |
| 153 | + progress, |
| 154 | + token, |
| 155 | + undefined, |
| 156 | + ); |
| 157 | + } |
| 158 | + }, |
| 159 | + { |
| 160 | + title: "Calculating Control Flow Graph", |
| 161 | + cancellable: true, |
| 162 | + }, |
| 163 | + ), |
| 164 | + ); |
| 165 | + |
| 166 | + // Since we are tracking extension usage through commands, this command mirrors the "codeQL.viewCfg" command |
| 167 | + ctx.subscriptions.push( |
| 168 | + commandRunnerWithProgress( |
| 169 | + "codeQL.viewCfgContextEditor", |
| 170 | + async (progress: ProgressCallback, token: CancellationToken) => { |
| 171 | + const res = await cfgTemplateProvider.provideCfgUri( |
| 172 | + window.activeTextEditor?.document, |
| 173 | + ); |
| 174 | + if (res) { |
| 175 | + await compileAndRunQuery( |
| 176 | + queryRunner, |
| 177 | + queryHistoryManager, |
| 178 | + databaseUI, |
| 179 | + localQueryResultsView, |
| 180 | + queryStorageDir, |
| 181 | + false, |
| 182 | + res[0], |
| 183 | + progress, |
| 184 | + token, |
| 185 | + undefined, |
| 186 | + ); |
| 187 | + } |
| 188 | + }, |
| 189 | + { |
| 190 | + title: "Calculating Control Flow Graph", |
| 191 | + cancellable: true, |
| 192 | + }, |
| 193 | + ), |
| 194 | + ); |
| 195 | +} |
| 196 | + |
| 197 | +async function viewAst( |
| 198 | + astViewer: AstViewer, |
| 199 | + printAstTemplateProvider: TemplatePrintAstProvider, |
| 200 | + progress: ProgressCallback, |
| 201 | + token: CancellationToken, |
| 202 | + selectedFile: Uri, |
| 203 | +): Promise<void> { |
| 204 | + const ast = await printAstTemplateProvider.provideAst( |
| 205 | + progress, |
| 206 | + token, |
| 207 | + selectedFile ?? window.activeTextEditor?.document.uri, |
| 208 | + ); |
| 209 | + if (ast) { |
| 210 | + astViewer.updateRoots(await ast.getRoots(), ast.db, ast.fileName); |
| 211 | + } |
| 212 | +} |
0 commit comments