Skip to content

Commit

Permalink
open from url (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
chdsbd authored Jun 3, 2024
1 parent ae2595c commit 6c674e3
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## 3.2.0 - 2024-06-02

### Added

- added "Open from URL" command to open a workspace file from a GitHub repository URL. (#64)

## 3.1.0 - 2024-06-02

### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ With `vsce` installed from NPM (`yarn global add vsce`), clone [this repo](https
| `Githubinator: Repository` ||| open repo | N/A |
| `Githubinator: History` ||| open history | N/A |
| `Githubinator: Open Pull Request` ||| open pull request | N/A |
| `Githubinator: Open from URL` | N/A | N/A | open file | N/A |
| `Githubinator: Compare` ||| compare branch | N/A |

The "main" branch is configured via `githubinator.mainBranches` (see "Extension Settings" below).
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "githubinator",
"displayName": "Githubinator",
"description": "Quickly open files on Github and other providers. View blame information, copy permalinks and more. See the \"commands\" section of the README for more details.",
"version": "3.1.0",
"version": "3.2.0",
"publisher": "chdsbd",
"license": "SEE LICENSE IN LICENSE",
"icon": "images/logo256.png",
Expand Down Expand Up @@ -58,6 +58,11 @@
"title": "Copy",
"category": "Githubinator"
},
{
"command": "extension.githubinatorOpenFromUrl",
"title": "Open from URL",
"category": "Githubinator"
},
{
"command": "extension.githubinatorCopyMaster",
"title": "Copy Main",
Expand Down
6 changes: 6 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode"
import * as git from "./git"
import { providers, IUrlInfo, createSha, createBranch } from "./providers"
import { getRelativeFilePath } from "./utils"
import { openFileFromGitHubUrl } from "./openfromUrl"

const COMMANDS: [string, IGithubinator][] = [
[
Expand Down Expand Up @@ -84,6 +85,11 @@ export function activate(context: vscode.ExtensionContext) {
)
context.subscriptions.push(disposable)
})
vscode.commands.registerCommand(
"extension.githubinatorOpenFromUrl",
openFileFromGitHubUrl,
)

console.log("githubinator.active.complete")
}

Expand Down
97 changes: 97 additions & 0 deletions src/openfromUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as vscode from "vscode"

function strToLocation(
x: string | null,
): { line: number; character: number } | null {
if (!x) {
return null
}
const line = Number(x.split("C")[0].split("L")[1]) - 1 || null
const character = Number(x.split("C")[1]) - 1 || 0

if (line == null) {
return null
}

return { line, character }
}

function fragmentToSelection(fragment: string): vscode.Selection | null {
const [start, end] = fragment.split("-")
if (!start) {
return null
}
// start: L1C1 or L1
const startLocation = strToLocation(start)
if (!startLocation) {
return null
}
const endLocation = strToLocation(end)
if (!endLocation) {
return new vscode.Selection(
startLocation.line,
startLocation.character,
startLocation.line,
startLocation.character,
)
}

return new vscode.Selection(
startLocation.line,
startLocation.character,
endLocation.line,
endLocation.character,
)
}

/**
* We only support GitHub for now.
*/
export async function openFileFromGitHubUrl() {
const input = await vscode.window.showInputBox({
title: "Paste URL to open",
placeHolder:
"https://github.com/owner/repo/blob/branch/file.js#L1C1-L10C10",
})
if (!input) {
return
}
const url = vscode.Uri.parse(input)

// we only support simple blob/branch/ paths.
// if a branch name has a / in it, this won't work.
//
// /org/repo/blob/branch/my/file.js -> /my/file.js
const path =
"/" +
url.path
.split("/")
.slice(5)
.join("/")
if (!path) {
return
}
const selection = fragmentToSelection(url.fragment)
const results = await Promise.allSettled(
vscode.workspace.workspaceFolders?.map(async folder => {
const doc = await vscode.workspace.openTextDocument(
folder.uri.fsPath + path,
)

const editor = await vscode.window.showTextDocument(doc)
if (selection) {
editor.selections = [selection]
}
}) ?? [],
)

for (const result of results) {
// we were able to open the file in at least one workspace.
if (result.status === "fulfilled") {
return
}
}
// don't wait for error message so we can trigger command.
vscode.window.showErrorMessage("Could not open file from URL.")
await vscode.commands.executeCommand("extension.githubinatorOpenFromUrl")
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"target": "es2020",
"outDir": "out",
"lib": ["es6"],
"lib": ["es2020"],
"sourceMap": true,
"rootDir": "src",
"esModuleInterop": true,
Expand Down

0 comments on commit 6c674e3

Please sign in to comment.