|
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import { RawSourceMap, SourceMapConsumer } from 'source-map'; |
| 3 | +import { commands, Position, Selection, TextDocument, TextEditor, TextEditorRevealType, TextEditorSelectionChangeEvent, ViewColumn, window, workspace } from 'vscode'; |
| 4 | +import { DisposableObject } from '../pure/disposable-object'; |
| 5 | +import { commandRunner } from '../commandRunner'; |
| 6 | +import { logger } from '../logging'; |
| 7 | +import { getErrorMessage } from '../pure/helpers-pure'; |
| 8 | + |
| 9 | +/** A `Position` within a specified file on disk. */ |
| 10 | +interface PositionInFile { |
| 11 | + filePath: string; |
| 12 | + position: Position; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Opens the specified source location in a text editor. |
| 17 | + * @param position The position (including file path) to show. |
| 18 | + */ |
| 19 | +async function showSourceLocation(position: PositionInFile): Promise<void> { |
| 20 | + const document = await workspace.openTextDocument(position.filePath); |
| 21 | + const editor = await window.showTextDocument(document, ViewColumn.Active); |
| 22 | + editor.selection = new Selection(position.position, position.position); |
| 23 | + editor.revealRange(editor.selection, TextEditorRevealType.InCenterIfOutsideViewport); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Simple language support for human-readable evaluator log summaries. |
| 28 | + * |
| 29 | + * This class implements the `codeQL.gotoQL` command, which jumps from RA code to the corresponding |
| 30 | + * QL code that generated it. It also tracks the current selection and active editor to enable and |
| 31 | + * disable that command based on whether there is a QL mapping for the current selection. |
| 32 | + */ |
| 33 | +export class SummaryLanguageSupport extends DisposableObject { |
| 34 | + /** |
| 35 | + * The last `TextDocument` (with language `ql-summary`) for which we tried to find a sourcemap, or |
| 36 | + * `undefined` if we have not seen such a document yet. |
| 37 | + */ |
| 38 | + private lastDocument : TextDocument | undefined = undefined; |
| 39 | + /** |
| 40 | + * The sourcemap for `lastDocument`, or `undefined` if there was no such sourcemap or document. |
| 41 | + */ |
| 42 | + private sourceMap : SourceMapConsumer | undefined = undefined; |
| 43 | + |
| 44 | + constructor() { |
| 45 | + super(); |
| 46 | + |
| 47 | + this.push(window.onDidChangeActiveTextEditor(this.handleDidChangeActiveTextEditor)); |
| 48 | + this.push(window.onDidChangeTextEditorSelection(this.handleDidChangeTextEditorSelection)); |
| 49 | + this.push(workspace.onDidCloseTextDocument(this.handleDidCloseTextDocument)); |
| 50 | + |
| 51 | + this.push(commandRunner('codeQL.gotoQL', this.handleGotoQL)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets the location of the QL code that generated the RA at the current selection in the active |
| 56 | + * editor, or `undefined` if there is no mapping. |
| 57 | + */ |
| 58 | + private async getQLSourceLocation(): Promise<PositionInFile | undefined> { |
| 59 | + const editor = window.activeTextEditor; |
| 60 | + if (editor === undefined) { |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + |
| 64 | + const document = editor.document; |
| 65 | + if (document.languageId !== 'ql-summary') { |
| 66 | + return undefined; |
| 67 | + } |
| 68 | + |
| 69 | + if (document.uri.scheme !== 'file') { |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + |
| 73 | + if (this.lastDocument !== document) { |
| 74 | + this.clearCache(); |
| 75 | + |
| 76 | + const mapPath = document.uri.fsPath + '.map'; |
| 77 | + |
| 78 | + try { |
| 79 | + const sourceMapText = await fs.readFile(mapPath, 'utf-8'); |
| 80 | + const rawMap: RawSourceMap = JSON.parse(sourceMapText); |
| 81 | + this.sourceMap = await new SourceMapConsumer(rawMap); |
| 82 | + } catch (e: unknown) { |
| 83 | + // Error reading sourcemap. Pretend there was no sourcemap. |
| 84 | + void logger.log(`Error reading sourcemap file '${mapPath}': ${getErrorMessage(e)}`); |
| 85 | + this.sourceMap = undefined; |
| 86 | + } |
| 87 | + this.lastDocument = document; |
| 88 | + } |
| 89 | + |
| 90 | + if (this.sourceMap === undefined) { |
| 91 | + return undefined; |
| 92 | + } |
| 93 | + |
| 94 | + const qlPosition = this.sourceMap.originalPositionFor({ |
| 95 | + line: editor.selection.start.line + 1, |
| 96 | + column: editor.selection.start.character, |
| 97 | + bias: SourceMapConsumer.GREATEST_LOWER_BOUND |
| 98 | + }); |
| 99 | + |
| 100 | + if ((qlPosition.source === null) || (qlPosition.line === null)) { |
| 101 | + // No position found. |
| 102 | + return undefined; |
| 103 | + } |
| 104 | + const line = qlPosition.line - 1; // In `source-map`, lines are 1-based... |
| 105 | + const column = qlPosition.column ?? 0; // ...but columns are 0-based :( |
| 106 | + |
| 107 | + return { |
| 108 | + filePath: qlPosition.source, |
| 109 | + position: new Position(line, column) |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Clears the cached sourcemap and its corresponding `TextDocument`. |
| 115 | + */ |
| 116 | + private clearCache(): void { |
| 117 | + if (this.sourceMap !== undefined) { |
| 118 | + this.sourceMap.destroy(); |
| 119 | + this.sourceMap = undefined; |
| 120 | + this.lastDocument = undefined; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Updates the `codeql.hasQLSource` context variable based on the current selection. This variable |
| 126 | + * controls whether or not the `codeQL.gotoQL` command is enabled. |
| 127 | + */ |
| 128 | + private async updateContext(): Promise<void> { |
| 129 | + const position = await this.getQLSourceLocation(); |
| 130 | + |
| 131 | + await commands.executeCommand('setContext', 'codeql.hasQLSource', position !== undefined); |
| 132 | + } |
| 133 | + |
| 134 | + handleDidChangeActiveTextEditor = async (_editor: TextEditor | undefined): Promise<void> => { |
| 135 | + await this.updateContext(); |
| 136 | + } |
| 137 | + |
| 138 | + handleDidChangeTextEditorSelection = async (_e: TextEditorSelectionChangeEvent): Promise<void> => { |
| 139 | + await this.updateContext(); |
| 140 | + } |
| 141 | + |
| 142 | + handleDidCloseTextDocument = (document: TextDocument): void => { |
| 143 | + if (this.lastDocument === document) { |
| 144 | + this.clearCache(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + handleGotoQL = async (): Promise<void> => { |
| 149 | + const position = await this.getQLSourceLocation(); |
| 150 | + if (position !== undefined) { |
| 151 | + await showSourceLocation(position); |
| 152 | + } |
| 153 | + }; |
| 154 | +} |
0 commit comments