|
| 1 | +import * as path from "path"; |
| 2 | +import { |
| 3 | + DocumentLink, |
| 4 | + DocumentLinkProvider, |
| 5 | + Position, |
| 6 | + Range, |
| 7 | + Selection, |
| 8 | + TextDocument, |
| 9 | + Uri, |
| 10 | + window, |
| 11 | + workspace, |
| 12 | +} from "vscode"; |
| 13 | + |
| 14 | +type SourceLink = { |
| 15 | + sourcePath: string; |
| 16 | + range: Range; |
| 17 | + line?: number; |
| 18 | +}; |
| 19 | + |
| 20 | +export const SOURCE_COMMAND = "mitsuhiko.insta.open-source-location"; |
| 21 | + |
| 22 | +function extractSourceLink(document: TextDocument): SourceLink | undefined { |
| 23 | + let sourcePath: string | undefined; |
| 24 | + let lineNumber: number | undefined; |
| 25 | + let sourceLineIndex = -1; |
| 26 | + |
| 27 | + // Simple pass through all lines |
| 28 | + for (let i = 0; i < document.lineCount; i++) { |
| 29 | + const text = document.lineAt(i).text; |
| 30 | + |
| 31 | + // Find source path |
| 32 | + if (text.startsWith("source:")) { |
| 33 | + sourcePath = text.slice(7).trim(); // "source:" is 7 characters |
| 34 | + sourceLineIndex = i; |
| 35 | + } |
| 36 | + |
| 37 | + // Find assertion line (optional) |
| 38 | + if (text.startsWith("assertion_line:")) { |
| 39 | + const lineStr = text.slice(15).trim(); // "assertion_line:" is 15 characters |
| 40 | + lineNumber = parseInt(lineStr, 10); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (sourcePath && sourceLineIndex >= 0) { |
| 45 | + // Create a simple range for the entire source line |
| 46 | + const line = document.lineAt(sourceLineIndex); |
| 47 | + const range = new Range( |
| 48 | + new Position(sourceLineIndex, 0), |
| 49 | + new Position(sourceLineIndex, line.text.length) |
| 50 | + ); |
| 51 | + |
| 52 | + return { |
| 53 | + sourcePath, |
| 54 | + range, |
| 55 | + line: lineNumber, |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + return undefined; |
| 60 | +} |
| 61 | + |
| 62 | +async function resolveSourceUri( |
| 63 | + document: TextDocument, |
| 64 | + sourcePath: string |
| 65 | +): Promise<Uri | undefined> { |
| 66 | + // Handle absolute paths |
| 67 | + if (path.isAbsolute(sourcePath)) { |
| 68 | + return Uri.file(sourcePath); |
| 69 | + } |
| 70 | + |
| 71 | + // For relative paths, resolve against workspace root |
| 72 | + // Insta snapshots are always workspace-relative |
| 73 | + const workspaceFolder = workspace.getWorkspaceFolder(document.uri); |
| 74 | + if (workspaceFolder) { |
| 75 | + return Uri.file(path.join(workspaceFolder.uri.fsPath, sourcePath)); |
| 76 | + } |
| 77 | + |
| 78 | + return undefined; |
| 79 | +} |
| 80 | + |
| 81 | +export class SnapshotDocumentLinkProvider implements DocumentLinkProvider { |
| 82 | + async provideDocumentLinks( |
| 83 | + document: TextDocument |
| 84 | + ): Promise<DocumentLink[]> { |
| 85 | + const info = extractSourceLink(document); |
| 86 | + if (!info) { |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + const target = await resolveSourceUri(document, info.sourcePath); |
| 91 | + if (!target) { |
| 92 | + return []; |
| 93 | + } |
| 94 | + |
| 95 | + const payload = encodeURIComponent( |
| 96 | + JSON.stringify({ |
| 97 | + target: target.toString(true), |
| 98 | + line: info.line, |
| 99 | + }) |
| 100 | + ); |
| 101 | + |
| 102 | + const link = new DocumentLink( |
| 103 | + info.range, |
| 104 | + Uri.parse(`command:${SOURCE_COMMAND}?${payload}`) |
| 105 | + ); |
| 106 | + link.tooltip = "Open snapshot source"; |
| 107 | + return [link]; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export async function openSourceDocument(payload?: { |
| 112 | + target?: string; |
| 113 | + line?: number; |
| 114 | +}) { |
| 115 | + if (!payload?.target) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const uri = Uri.parse(payload.target); |
| 120 | + try { |
| 121 | + const document = await workspace.openTextDocument(uri); |
| 122 | + const editor = await window.showTextDocument(document, { preview: true }); |
| 123 | + if (payload.line !== undefined) { |
| 124 | + const zeroBasedLine = Math.max(payload.line - 1, 0); |
| 125 | + const position = new Position(zeroBasedLine, 0); |
| 126 | + editor.selection = new Selection(position, position); |
| 127 | + editor.revealRange(new Range(position, position)); |
| 128 | + } |
| 129 | + } catch (error) { |
| 130 | + const message = |
| 131 | + error instanceof Error ? error.message : "Unknown error opening file"; |
| 132 | + window.showErrorMessage(`Could not open source file: ${message}`); |
| 133 | + } |
| 134 | +} |
0 commit comments