Skip to content

Commit a564852

Browse files
authored
Implementation
Implement open-in-sublime
2 parents 7c2184b + 11cdf3f commit a564852

File tree

10 files changed

+6865
-1
lines changed

10 files changed

+6865
-1
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[*]
2+
insert_final_newline = true
3+
end_of_line = lf
4+
charset = utf-8
5+
trim_trailing_whitespace = true
6+
indent_style = space
7+
indent_size = 4
8+
9+
[*.{json,js,yml}]
10+
indent_size = 2
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.eslintrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": [
3+
"@sourcegraph/eslint-config"
4+
],
5+
"parserOptions": {
6+
"project": "tsconfig.json"
7+
}
8+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
node_modules/
3+
.cache/

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# open-in-sublime (Sourcegraph extension)
1+
# Open in Sublime Sourcegraph extension
22

33
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.
44

@@ -11,3 +11,5 @@ Adds a button at the top of files in both Sourcegraph app and code hosts like Gi
1111
## Settings
1212

1313
- `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+
15+
Sublime Text requires a URL handler installed such as [this one for macOS](https://github.com/inopinatus/sublime_url).

package.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/sourcegraph/sourcegraph/main/client/shared/src/schema/extension.schema.json",
3+
"name": "open-in-sublime",
4+
"description": "Open the current file in Sublime Text",
5+
"publisher": "sourcegraph",
6+
"activationEvents": [
7+
"*"
8+
],
9+
"wip": true,
10+
"categories": [
11+
"Code editors"
12+
],
13+
"tags": [
14+
"sublime",
15+
"editor",
16+
"open"
17+
],
18+
"contributes": {
19+
"actions": [
20+
{
21+
"id": "openInSublime.open.file",
22+
"command": "openInSublime.open.file",
23+
"commandArguments": [
24+
"${resource.uri}"
25+
],
26+
"title": "Open file in Sublime Text",
27+
"category": "Open in Sublime Text",
28+
"actionItem": {
29+
"description": "Open file in Sublime Text",
30+
"iconURL": "data:image/svg+xml,%3Csvg viewBox='26.6 26.6 446.8 446.7' xmlns='http://www.w3.org/2000/svg' width='64' height='64'%3E%3ClinearGradient id='a' gradientUnits='userSpaceOnUse' x1='136.178' x2='372.681' y1='369.638' y2='287.81'%3E%3Cstop offset='.233' stop-color='%23f89822'/%3E%3Cstop offset='1' stop-color='%23c27818'/%3E%3C/linearGradient%3E%3Cpath d='M456.1 473.3H43.9c-9.5 0-17.3-7.8-17.3-17.3V43.9c0-9.5 7.8-17.3 17.3-17.3h412.2c9.5 0 17.3 7.8 17.3 17.3v412.2c-.1 9.5-7.8 17.2-17.3 17.2z' fill='%234d4d4e'/%3E%3Cpath d='M129.6 161.5l233.8-74.2s16.6-8.7 12.6 7.4l.6 71.5s2.9 10.5-11.5 13.1l-95 29.7z' fill='%23f89820'/%3E%3Cpath d='M129.6 161.5s-9.2 2.2-6.5 17.9l-.5 68.9s-.8 8.7 15.7 12.2L370 335.1s7.8 3.1 6.9-6.5l.1-76.8s2.2-7.9-12.2-13.1L270.1 209z' fill='%23f89820'/%3E%3Cpath d='M231.7 290.5l-98.6 30.1s-11.9.4-10.5 22.7-.1 67.2-.1 67.2 1 8.3 12.4 3.5l233.8-74.6s8.3-2.1 1.3-4.4c-7-2.2-138.3-44.5-138.3-44.5z' fill='url(%23a)'/%3E%3C/svg%3E"
31+
}
32+
}
33+
],
34+
"menus": {
35+
"editor/title": [
36+
{
37+
"action": "openInSublime.open.file",
38+
"when": "resource.type === 'textDocument'"
39+
}
40+
],
41+
"commandPalette": [
42+
{
43+
"action": "openInSublime.open.file",
44+
"when": "resource.type === 'textDocument'"
45+
}
46+
]
47+
},
48+
"configuration": {
49+
"properties": {
50+
"openInSublime.basePath": {
51+
"description": "The absolute path on the machine to the folder that is expected to contain all repositories.",
52+
"type": "string",
53+
"format": "regex",
54+
"pattern": "^[^~]+"
55+
}
56+
}
57+
}
58+
},
59+
"version": "0.0.0-DEVELOPMENT",
60+
"repository": {
61+
"type": "git",
62+
"url": "https://github.com/sourcegraph/sourcegraph-open-in-sublime"
63+
},
64+
"license": "Apache-2.0",
65+
"main": "dist/open-in-sublime.js",
66+
"scripts": {
67+
"eslint": "eslint 'src/**/*.ts'",
68+
"typecheck": "tsc -p tsconfig.json",
69+
"build": "parcel build --out-file dist/open-in-sublime.js src/open-in-sublime.ts",
70+
"symlink-package": "mkdirp dist && lnfs ./package.json ./dist/package.json",
71+
"serve": "yarn run symlink-package && parcel serve --no-hmr --out-file dist/open-in-sublime.js src/open-in-sublime.ts",
72+
"watch:typecheck": "tsc -p tsconfig.json -w",
73+
"watch:build": "tsc -p tsconfig.dist.json -w",
74+
"sourcegraph:prepublish": "yarn run typecheck && yarn run build"
75+
},
76+
"browserslist": [
77+
"last 1 Chrome versions",
78+
"last 1 Firefox versions",
79+
"last 1 Edge versions",
80+
"last 1 Safari versions"
81+
],
82+
"devDependencies": {
83+
"@sourcegraph/eslint-config": "^0.20.0",
84+
"@sourcegraph/tsconfig": "^4.0.1",
85+
"@types/node": "14.0.27",
86+
"eslint": "^7.6.0",
87+
"lnfs-cli": "^2.1.0",
88+
"mkdirp": "^1.0.4",
89+
"parcel-bundler": "^1.12.4",
90+
"sourcegraph": "^24.7.0",
91+
"typescript": "^3.9.7"
92+
},
93+
"icon": "data:image/svg+xml,%3Csvg viewBox='26.6 26.6 446.8 446.7' xmlns='http://www.w3.org/2000/svg' width='64' height='64'%3E%3ClinearGradient id='a' gradientUnits='userSpaceOnUse' x1='136.178' x2='372.681' y1='369.638' y2='287.81'%3E%3Cstop offset='.233' stop-color='%23f89822'/%3E%3Cstop offset='1' stop-color='%23c27818'/%3E%3C/linearGradient%3E%3Cpath d='M456.1 473.3H43.9c-9.5 0-17.3-7.8-17.3-17.3V43.9c0-9.5 7.8-17.3 17.3-17.3h412.2c9.5 0 17.3 7.8 17.3 17.3v412.2c-.1 9.5-7.8 17.2-17.3 17.2z' fill='%234d4d4e'/%3E%3Cpath d='M129.6 161.5l233.8-74.2s16.6-8.7 12.6 7.4l.6 71.5s2.9 10.5-11.5 13.1l-95 29.7z' fill='%23f89820'/%3E%3Cpath d='M129.6 161.5s-9.2 2.2-6.5 17.9l-.5 68.9s-.8 8.7 15.7 12.2L370 335.1s7.8 3.1 6.9-6.5l.1-76.8s2.2-7.9-12.2-13.1L270.1 209z' fill='%23f89820'/%3E%3Cpath d='M231.7 290.5l-98.6 30.1s-11.9.4-10.5 22.7-.1 67.2-.1 67.2 1 8.3 12.4 3.5l233.8-74.6s8.3-2.1 1.3-4.4c-7-2.2-138.3-44.5-138.3-44.5z' fill='url(%23a)'/%3E%3C/svg%3E"
94+
}

prettier.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@sourcegraph/prettierrc')

renovate.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["github>sourcegraph/renovate-config"]
3+
}

src/open-in-sublime.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as sourcegraph from 'sourcegraph'
2+
import * as path from 'path'
3+
4+
interface Settings {
5+
'openInSublime.basePath'?: string
6+
}
7+
8+
function getOpenUrl(textDocumentUri: URL): URL {
9+
const basePath = sourcegraph.configuration.get<Settings>().value['openInSublime.basePath']
10+
const learnMorePath = new URL('/extensions/sourcegraph/open-in-sublime', sourcegraph.internal.sourcegraphURL.href)
11+
.href
12+
const userSettingsPath = new URL('/user/settings', sourcegraph.internal.sourcegraphURL.href).href
13+
14+
if (typeof basePath !== 'string') {
15+
throw new TypeError(
16+
`Add \`openInSublime.basePath\` to your [user settings](${userSettingsPath}) to open files in the editor. [Learn more](${learnMorePath})`
17+
)
18+
}
19+
if (!path.isAbsolute(basePath)) {
20+
throw new Error(
21+
`\`openInSublime.basePath\` value \`${basePath}\` is not an absolute path. Please correct the error in your [user settings](${userSettingsPath}).`
22+
)
23+
}
24+
25+
const rawRepoName = decodeURIComponent(textDocumentUri.hostname + textDocumentUri.pathname)
26+
const repoBaseName = rawRepoName.split('/').pop() ?? ''
27+
const relativePath = decodeURIComponent(textDocumentUri.hash.slice('#'.length))
28+
const absolutePath = path.join(basePath, repoBaseName, relativePath)
29+
const openUrl = new URL('subl://open?url=' + absolutePath)
30+
31+
if (sourcegraph.app.activeWindow?.activeViewComponent?.type === 'CodeEditor') {
32+
const selection = sourcegraph.app.activeWindow?.activeViewComponent?.selection
33+
if (selection) {
34+
openUrl.searchParams.set('line', (selection.start.line + 1).toString())
35+
36+
if (selection && selection.start.character !== 0) {
37+
openUrl.searchParams.set('column', (selection.start.character + 1).toString())
38+
}
39+
}
40+
}
41+
42+
return openUrl
43+
}
44+
45+
export function activate(context: sourcegraph.ExtensionContext): void {
46+
context.subscriptions.add(
47+
sourcegraph.commands.registerCommand('openInSublime.open.file', async (uri?: string) => {
48+
if (!uri) {
49+
const viewer = sourcegraph.app.activeWindow?.activeViewComponent
50+
uri = viewerUri(viewer)
51+
}
52+
if (!uri) {
53+
throw new Error('No file currently open')
54+
}
55+
const openUrl = getOpenUrl(new URL(uri))
56+
await sourcegraph.commands.executeCommand('open', openUrl.href)
57+
})
58+
)
59+
}
60+
61+
function viewerUri(viewer: sourcegraph.ViewComponent | undefined): string | undefined {
62+
switch (viewer?.type) {
63+
case 'CodeEditor':
64+
return viewer.document.uri
65+
case 'DirectoryViewer':
66+
return viewer.directory.uri.href
67+
default:
68+
return undefined
69+
}
70+
}

tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "@sourcegraph/tsconfig",
3+
"compilerOptions": {
4+
"target": "ES2019",
5+
"module": "ESNext",
6+
"moduleResolution": "Node",
7+
"sourceMap": true,
8+
"declaration": true,
9+
"outDir": "dist",
10+
"rootDir": "src",
11+
"esModuleInterop": true,
12+
"allowSyntheticDefaultImports": true
13+
}
14+
}

0 commit comments

Comments
 (0)