Skip to content

Commit c8ed351

Browse files
authored
feat: support per-platform paths (#21)
* Add support to multi-platform paths
1 parent bd7a17e commit c8ed351

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ Adds a button to the Sourcegraph's extension panel and at the top of files in co
44

55
## Settings
66

7-
**This extension requires all git repos to be already cloned under the base path with their original names.**
7+
****This extension requires all git repos to be cloned and available on your local machine.**
88

99
- Add `openineditor.editor` to your user settings to open files in the editor. Copy one of the following lines depending on the editor you would like to use. This extension only supports opening in one editor at a time. Supported editors:
1010
- `vscode` (Visual Studio Code): `"openineditor.editor": "vscode"`
1111
- `idea` (JetBrains IntelliJ IDEA): `"openineditor.editor": "idea"`
1212
- `sublime` (Sublime Text, requires a URL handler installed such as [this one for macOS](https://github.com/inopinatus/sublime_url))
1313
- `custom` (requires also setting `openineditor.customUrlPattern`): `"openineditor.editor": "custom"`
14+
1415
- `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.
16+
1517
- `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`
18+
1619
- `openineditor.replacements`: Takes object, where each key is replaced by value in the final url. The key can be a string or a RegExp, and the value must be a string. For example, `"openineditor.replacements": {"(?<=Documents\/)(.*[\\\/])": "sourcegraph-$1"},` will add `sourcegraph-` in front of the string that matches the `(?<=Documents\/)(.*[\\\/])` RegExp pattern, which is the string after `Documents/` and before the final slash: `vscode://file//Users/USERNAME/Documents/REPO-NAME/package.json` => `vscode://file//Users/USERNAME/Documents/sourcegraph-REPO-NAME/package.json`
1720

1821
## Examples
@@ -110,6 +113,26 @@ To open repository files in your Documents directory:
110113
}
111114
```
112115

116+
### VS Code with different base paths configured for different platforms
117+
118+
Uses the assigned path for the detected Operating System when available:
119+
120+
```json
121+
{
122+
"extensions": {
123+
"sourcegraph/open-in-editor": true
124+
},
125+
"openineditor.osPaths": {
126+
"windows": "/C:/Users/USERNAME/folder/",
127+
"mac": "/Users/USERNAME/folder/",
128+
"linux": "/home/USERNAME/folder/"
129+
},
130+
"openineditor.editor" : "vscode",
131+
// basePath is required as the default path when no Operating System is detected
132+
"openineditor.basePath": "/Users/USERNAME/Documents/",
133+
}
134+
```
135+
113136
### Replacement Example 1: Open Remote folders with VS Code on Mac by removing file names
114137

115138
**This requires VS Code extension [Remote Development by Microsoft](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) to work.**

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
"openineditor.basePath": {
6565
"description": "The file path on the machine to the folder that is expected to contain all repositories.",
6666
"type": "string"
67+
},
68+
"openineditor.osPaths": {
69+
"description": "The file path on each platform to the folder that is expected to contain all repositories.",
70+
"type": "object"
71+
},
72+
"openineditor.replacements": {
73+
"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.",
74+
"type": "object"
6775
}
6876
}
6977
}

src/open-in-editor.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@ const supportedEditors: {[editor: string]: {urlPattern: string}} = {
1717
}
1818

1919
function getOpenUrl(textDocumentUri: URL): URL {
20-
const basePath: unknown = sourcegraph.configuration.get().value['openineditor.basePath'] as unknown
20+
let basePath: unknown = sourcegraph.configuration.get().value['openineditor.basePath'] as unknown
2121
const editor: unknown = sourcegraph.configuration.get().value['openineditor.editor'] as unknown
22+
const osPaths: Record<string, string> = sourcegraph.configuration.get().value['openineditor.osPaths'] as Record<string, string>
2223
const customUrlPattern: unknown = sourcegraph.configuration.get().value['openineditor.customUrlPattern'] as unknown
2324
const replacements: Record<string, string> = sourcegraph.configuration.get().value['openineditor.replacements'] as Record<string, string>
2425
const learnMorePath = new URL('/extensions/sourcegraph/open-in-editor', sourcegraph.internal.sourcegraphURL.href).href
2526

27+
// check platform and use assigned path when available;
28+
if(osPaths){
29+
if (navigator.userAgent.includes('Win') && osPaths.windows) {
30+
basePath = osPaths.windows;
31+
} else if (navigator.userAgent.includes('Mac') && osPaths.mac) {
32+
basePath = osPaths.mac;
33+
} else if (navigator.userAgent.includes('Linux') && osPaths.linux) {
34+
basePath = osPaths.linux;
35+
}
36+
}
2637
if (typeof basePath !== 'string') {
2738
throw new TypeError(
2839
`Add \`openineditor.basePath\` to your user settings to open files in the editor. [Learn more](${learnMorePath})`

0 commit comments

Comments
 (0)