|
| 1 | +import * as vscode from "vscode" |
| 2 | + |
| 3 | +function strToLocation( |
| 4 | + x: string | null, |
| 5 | +): { line: number; character: number } | null { |
| 6 | + if (!x) { |
| 7 | + return null |
| 8 | + } |
| 9 | + const line = Number(x.split("C")[0].split("L")[1]) - 1 || null |
| 10 | + const character = Number(x.split("C")[1]) - 1 || 0 |
| 11 | + |
| 12 | + if (line == null) { |
| 13 | + return null |
| 14 | + } |
| 15 | + |
| 16 | + return { line, character } |
| 17 | +} |
| 18 | + |
| 19 | +function fragmentToSelection(fragment: string): vscode.Selection | null { |
| 20 | + const [start, end] = fragment.split("-") |
| 21 | + if (!start) { |
| 22 | + return null |
| 23 | + } |
| 24 | + // start: L1C1 or L1 |
| 25 | + const startLocation = strToLocation(start) |
| 26 | + if (!startLocation) { |
| 27 | + return null |
| 28 | + } |
| 29 | + const endLocation = strToLocation(end) |
| 30 | + if (!endLocation) { |
| 31 | + return new vscode.Selection( |
| 32 | + startLocation.line, |
| 33 | + startLocation.character, |
| 34 | + startLocation.line, |
| 35 | + startLocation.character, |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + return new vscode.Selection( |
| 40 | + startLocation.line, |
| 41 | + startLocation.character, |
| 42 | + endLocation.line, |
| 43 | + endLocation.character, |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * We only support GitHub for now. |
| 49 | + */ |
| 50 | +export async function openFileFromGitHubUrl() { |
| 51 | + const input = await vscode.window.showInputBox({ |
| 52 | + title: "Paste URL to open", |
| 53 | + placeHolder: |
| 54 | + "https://github.com/owner/repo/blob/branch/file.js#L1C1-L10C10", |
| 55 | + }) |
| 56 | + if (!input) { |
| 57 | + return |
| 58 | + } |
| 59 | + const url = vscode.Uri.parse(input) |
| 60 | + |
| 61 | + // we only support simple blob/branch/ paths. |
| 62 | + // if a branch name has a / in it, this won't work. |
| 63 | + // |
| 64 | + // /org/repo/blob/branch/my/file.js -> /my/file.js |
| 65 | + const path = |
| 66 | + "/" + |
| 67 | + url.path |
| 68 | + .split("/") |
| 69 | + .slice(5) |
| 70 | + .join("/") |
| 71 | + if (!path) { |
| 72 | + return |
| 73 | + } |
| 74 | + const selection = fragmentToSelection(url.fragment) |
| 75 | + const results = await Promise.allSettled( |
| 76 | + vscode.workspace.workspaceFolders?.map(async folder => { |
| 77 | + const doc = await vscode.workspace.openTextDocument( |
| 78 | + folder.uri.fsPath + path, |
| 79 | + ) |
| 80 | + |
| 81 | + const editor = await vscode.window.showTextDocument(doc) |
| 82 | + if (selection) { |
| 83 | + editor.selections = [selection] |
| 84 | + } |
| 85 | + }) ?? [], |
| 86 | + ) |
| 87 | + |
| 88 | + for (const result of results) { |
| 89 | + // we were able to open the file in at least one workspace. |
| 90 | + if (result.status === "fulfilled") { |
| 91 | + return |
| 92 | + } |
| 93 | + } |
| 94 | + // don't wait for error message so we can trigger command. |
| 95 | + vscode.window.showErrorMessage("Could not open file from URL.") |
| 96 | + await vscode.commands.executeCommand("extension.githubinatorOpenFromUrl") |
| 97 | +} |
0 commit comments