Skip to content

Commit ce85e6c

Browse files
authored
feat: support url replacements & multi-platform paths (#10)
* Add support for url replacements * Add support to multi-platform paths
1 parent a564852 commit ce85e6c

File tree

4 files changed

+111
-15
lines changed

4 files changed

+111
-15
lines changed

README.md

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,76 @@
11
# Open in Sublime Sourcegraph extension
22

3-
Adds a button at the top of files in both Sourcegraph app and code hosts like GitHub (when the Sourcegraph browser extension is installed) that will open the current file in Sublime Text.
3+
Adds a button to the Sourcegraph's extension panel and at the top of files in code hosts like GitHub (when the Sourcegraph browser extension is installed) that will open the current file in Sublime Text.
4+
5+
**This extension requires all git repos to be cloned and available on your local machine.**
46

57
<picture>
6-
<source srcset="https://user-images.githubusercontent.com/37420160/96816775-e9760400-13eb-11eb-812f-85046bd4db6d.png" media="(prefers-color-scheme: dark)" />
7-
<source srcset="https://user-images.githubusercontent.com/37420160/96816847-0ad6f000-13ec-11eb-9ef8-b1c3b2009a4d.png" media="(prefers-color-scheme: light)" />
8-
<img src="https://user-images.githubusercontent.com/37420160/96816847-0ad6f000-13ec-11eb-9ef8-b1c3b2009a4d.png" alt="Screenshot" />
8+
<source srcset="https://user-images.githubusercontent.com/37420160/96816775-e9760400-13eb-11eb-812f-85046bd4db6d.png" width="100%" media="(prefers-color-scheme: dark)" />
9+
<source srcset="https://user-images.githubusercontent.com/37420160/96816847-0ad6f000-13ec-11eb-9ef8-b1c3b2009a4d.png" width="100%" media="(prefers-color-scheme: light)" />
10+
<img src="https://user-images.githubusercontent.com/37420160/96816847-0ad6f000-13ec-11eb-9ef8-b1c3b2009a4d.png" width="100%" alt="Screenshot" />
911
</picture>
1012

11-
## Settings
1213

13-
- `openInSublime.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.
14+
## Prerequisites
15+
16+
- This extension requires all git repos to be cloned and available on your local machine.
17+
18+
- Sublime Text requires a URL handler installed such as [this one for macOS](https://github.com/inopinatus/sublime_url).
19+
20+
## Configurations
21+
22+
- `openInSublime.basePath`: [REQUIRED] String. The absolute path on your computer where your git repositories live. The extension will try to open the file in a clone named by the last segment of the repository name in that folder.
23+
- This extension requires all git repos to be already cloned under this path with their original names, which the final path can later be altered using the `openInSublime.replacements` option.
24+
- `"/Users/yourusername/src"` is a valid absolute path, while `"~/src"` is not.
25+
26+
- `openInSublime.replacements`: [OPTIONAL] Object. Set to an object that includes pairs of strings, where each key will be replaced by its value in the final url. The key can be a string or a RegExp, and the value must be a string.
27+
- Example: using `"openInSublime.replacements": {"(?<=Documents\/)(.*[\\\/])": "sourcegraph-$1"}` will add `sourcegraph-` in front of the string that matches the `(?<=Documents\/)(.*[\\\/])` RegExp pattern, while `"openInSublime.replacements": {"sourcegraph-": ""}` will remove `sourcegraph-` from the final URL.
28+
29+
- `openInSublime.osPaths`: [OPTIONAL] Object. The extension uses the assigned path for the detected Operating System when available. If no platform is detected then we will keep using the basePath provided with `openInSublime.basePath`.
30+
- Note: Currently support `"windows"`, `"mac"`, and `"linux"` as keys.
31+
32+
## Examples
33+
34+
### Mac
35+
36+
**Requires a URL handler installed such as [this one for macOS](https://github.com/inopinatus/sublime_url).**
37+
38+
Opens repository files in your Documents directory:
39+
40+
```json
41+
{
42+
"extensions": {
43+
"sourcegraph/open-in-sublime": true,
44+
},
45+
// where the cloned git repositories are located
46+
"openInSublime.basePath": "/Users/USERNAME/Documents/"
47+
}
48+
```
49+
50+
### Set basePath for multiple platforms
51+
52+
**Requires a URL handler installed such as [this one for macOS](https://github.com/inopinatus/sublime_url).**
53+
54+
Uses the assigned path for the detected Operating System when available:
55+
56+
```json
57+
{
58+
"extensions": {
59+
"sourcegraph/open-in-sublime": true,
60+
},
61+
"openInSublime.osPaths": {
62+
"windows": "/C:/Users/USERNAME/folder/",
63+
"mac": "/Users/USERNAME/folder/",
64+
"linux": "/home/USERNAME/folder/"
65+
},
66+
// set basePath as fallback path when no operating system is detected
67+
"openInSublime.basePath": "/Users/USERNAME/Documents/"
68+
}
69+
```
70+
71+
## Development
72+
73+
1. Run `yarn && yarn run serve` and keep the Parcel bundler process running
74+
1. [Sideload the extension](https://docs.sourcegraph.com/extensions/authoring/local_development) (at the URL http://localhost:1234 by default) on your Sourcegraph instance or Sourcegraph.com
1475

15-
Sublime Text requires a URL handler installed such as [this one for macOS](https://github.com/inopinatus/sublime_url).
76+
When you edit a source file in your editor, Parcel will recompile the extension. Reload the Sourcegraph web page to use the updated extension.

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
"type": "string",
5353
"format": "regex",
5454
"pattern": "^[^~]+"
55+
},
56+
"openInSublime.replacements": {
57+
"description": "Take key-value pairs where each key is replaced by its value in the final url. The key can be a string or a RegExp pattern, and the value must be a string. The final path must be a valid path on the machine to the folder that is expected to contain all repositories.",
58+
"type": "object",
59+
"format": "{}",
60+
"examples": ["'(?<=Documents\/)(.*[\\\/])': 'string-to-add-$1'", "'string-to-remove': ''"]
61+
},
62+
"openInSublime.osPaths": {
63+
"description": "The absolute path on each platform to the folder that is expected to contain all repositories. Currently supports 'windows', 'mac', and 'linux' as keys.",
64+
"type": "object",
65+
"format": "{}"
5566
}
5667
}
5768
}

src/open-in-sublime.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@ import * as path from 'path'
33

44
interface Settings {
55
'openInSublime.basePath'?: string
6+
'openInSublime.replacements'?: Record<string, string>
7+
'openInAtom.osPaths'?: Record<string, string>
68
}
79

810
function getOpenUrl(textDocumentUri: URL): URL {
9-
const basePath = sourcegraph.configuration.get<Settings>().value['openInSublime.basePath']
11+
let basePath = sourcegraph.configuration.get<Settings>().value['openInSublime.basePath']
12+
const replacements = sourcegraph.configuration.get().value['openInSublime.replacements'] as Record<string, string>
13+
const osPaths: Record<string, string> = sourcegraph.configuration.get().value['openInAtom.osPaths'] as Record<string, string>
1014
const learnMorePath = new URL('/extensions/sourcegraph/open-in-sublime', sourcegraph.internal.sourcegraphURL.href)
1115
.href
1216
const userSettingsPath = new URL('/user/settings', sourcegraph.internal.sourcegraphURL.href).href
1317

18+
// check platform and use assigned path when available;
19+
if(osPaths){
20+
if (navigator.userAgent.includes('Win') && osPaths.windows) {
21+
basePath = osPaths.windows;
22+
} else if (navigator.userAgent.includes('Mac') && osPaths.mac) {
23+
basePath = osPaths.mac;
24+
} else if (navigator.userAgent.includes('Linux') && osPaths.linux) {
25+
basePath = osPaths.linux;
26+
}
27+
}
1428
if (typeof basePath !== 'string') {
1529
throw new TypeError(
1630
`Add \`openInSublime.basePath\` to your [user settings](${userSettingsPath}) to open files in the editor. [Learn more](${learnMorePath})`
@@ -26,20 +40,30 @@ function getOpenUrl(textDocumentUri: URL): URL {
2640
const repoBaseName = rawRepoName.split('/').pop() ?? ''
2741
const relativePath = decodeURIComponent(textDocumentUri.hash.slice('#'.length))
2842
const absolutePath = path.join(basePath, repoBaseName, relativePath)
29-
const openUrl = new URL('subl://open?url=' + absolutePath)
43+
let openUrl = 'subl://open?url=file://' + absolutePath;
3044

3145
if (sourcegraph.app.activeWindow?.activeViewComponent?.type === 'CodeEditor') {
3246
const selection = sourcegraph.app.activeWindow?.activeViewComponent?.selection
3347
if (selection) {
34-
openUrl.searchParams.set('line', (selection.start.line + 1).toString())
48+
openUrl += `:${selection.start.line + 1}`
3549

3650
if (selection && selection.start.character !== 0) {
37-
openUrl.searchParams.set('column', (selection.start.character + 1).toString())
51+
openUrl += `:${selection.start.character + 1}`
52+
}
53+
}
54+
}
55+
56+
// If configured, run replacements before returning final URL
57+
if(replacements) {
58+
for (const replacement in replacements) {
59+
if (typeof replacement === 'string') {
60+
const POST_REGEX = new RegExp(replacement);
61+
openUrl = openUrl.replace(POST_REGEX, replacements[replacement])
3862
}
3963
}
4064
}
4165

42-
return openUrl
66+
return new URL(openUrl)
4367
}
4468

4569
export function activate(context: sourcegraph.ExtensionContext): void {

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,9 +1683,9 @@ caniuse-api@^3.0.0:
16831683
lodash.uniq "^4.5.0"
16841684

16851685
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001093:
1686-
version "1.0.30001111"
1687-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001111.tgz#dd0ce822c70eb6c7c068e4a55c22e19ec1501298"
1688-
integrity sha512-xnDje2wchd/8mlJu8sXvWxOGvMgv+uT3iZ3bkIAynKOzToCssWCmkz/ZIkQBs/2pUB4uwnJKVORWQ31UkbVjOg==
1686+
version "1.0.30001269"
1687+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001269.tgz"
1688+
integrity sha512-UOy8okEVs48MyHYgV+RdW1Oiudl1H6KolybD6ZquD0VcrPSgj25omXO1S7rDydjpqaISCwA8Pyx+jUQKZwWO5w==
16891689

16901690
caseless@~0.12.0:
16911691
version "0.12.0"

0 commit comments

Comments
 (0)