Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support nested repos and add extra placeholders #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ Adds a button at the top of files in both Sourcegraph app and code hosts like Gi
- `sublime` (Sublime Text, requires a URL handler installed such as [this one for macOS](https://github.com/inopinatus/sublime_url))
- `custom` (requires also setting `openineditor.customUrlPattern`): `"openineditor.editor": "custom"`
- `openineditor.basePath`: The absolute path on your computer where your git repositories live. This extension requires all git repos to be already cloned under this path with their original names. `"/Users/yourusername/src"` is a valid absolute path, while `"~/src"` is not.
- `openineditor.customUrlPattern`: If you set `openineditor.editor` to `custom`, this must be set. Use placeholders `%file`, `%line`, and `%col` to mark where the file path, line number, and column number must be replaced. Example URL for IntelliJ IDEA: `idea://open?file=%file&line=%line&column=%col`
- `openineditor.customUrlPattern`: If you set `openineditor.editor` to `custom`, this must be set. Use placeholders `%host`, `%repo`, `%filePath`, `%file`, `%line`, and `%col` to mark where the file path, line number, and column number must be replaced. Example URL for IntelliJ IDEA: `idea://open?file=%file&line=%line&column=%col`

| Placeholder | Example |
| ----------- | ---------------------------------------------------------- |
| %host | github.com |
| %repo | sourcegraph/open-in-editor-extension |
| %filePath | src/open-in-editor.ts |
| %file | sourcegraph/open-in-editor-extension/src/open-in-editor.ts |
| %line | 1 |
| %col | 1 |
13 changes: 7 additions & 6 deletions src/open-in-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ function getOpenUrl(textDocumentUri: URL): URL {
)
}

const rawRepoName = decodeURIComponent(textDocumentUri.hostname + textDocumentUri.pathname)
// TODO support different folder layouts, e.g. repo nested under owner name
const repoBaseName = rawRepoName.split('/').pop() ?? ''
const relativePath = decodeURIComponent(textDocumentUri.hash.slice('#'.length))
const absolutePath = path.join(basePath, repoBaseName, relativePath)
const codeHost = textDocumentUri.host;
const repoName = textDocumentUri.pathname.replace(/^\//, '').replace(/\/$/, '') ?? ''
const filePath = decodeURIComponent(textDocumentUri.hash.slice('#'.length))
let line = 1;
let column = 1;

Expand All @@ -69,7 +67,10 @@ function getOpenUrl(textDocumentUri: URL): URL {
}

const urlPattern = getEditorUrlPattern(editor, customUrlPattern as string)
.replace('%file', absolutePath)
.replace('%host', codeHost)
.replace('%repo', repoName)
.replace('%filePath', filePath)
.replace('%file', path.join(basePath, repoName, filePath))
.replace('%line', `${line}`)
.replace('%col', `${column}`)

Expand Down