Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.
Closed
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
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"activationEvents": [
"onCommand:extension.open",
"onCommand:extension.copyUrl",
"onCommand:extension.search.selection",
"onCommand:extension.search"
],
Expand All @@ -28,6 +29,10 @@
"command": "extension.open",
"title": "Sourcegraph: Open"
},
{
"command": "extension.copyUrl",
"title": "Sourcegraph: Copy Url"
},
{
"command": "extension.search.selection",
"title": "Sourcegraph: Search Selection"
Expand Down Expand Up @@ -74,6 +79,13 @@
"default": {},
"description": "For each item in this object, replace key with value in the remote url."
},
"sourcegraph.remoteRegexUrlReplacements": {
"type": [
"object"
],
"default": {},
"description": "For each item in this object, replace RegEx key with value in the remote url."
},
"sourcegraph.defaultBranch": {
"type": [
"string"
Expand Down
79 changes: 53 additions & 26 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import open from 'open'
import * as vscode from 'vscode'
import { getSourcegraphUrl } from './config'
import { repoInfo } from './git'
import { repoInfo, RepositoryInfo } from './git'

// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const { version } = require('../package.json')
Expand All @@ -23,6 +23,23 @@ const handleCommandErrors = <P extends unknown[], R>(command: (...args: P) => Pr
}
}

/**
* Returns the Sourcegraph URL for a cursor selection.
*/
const getSelectionSourcegraphUrl = (editor: vscode.TextEditor, repositoryInfo: RepositoryInfo): string => {
const { remoteURL, branch, fileRelative } = repositoryInfo
return `${getSourcegraphUrl()}/-/editor` +
`?remote_url=${encodeURIComponent(remoteURL)}` +
`&branch=${encodeURIComponent(branch)}` +
`&file=${encodeURIComponent(fileRelative)}` +
`&editor=${encodeURIComponent('VSCode')}` +
`&version=${encodeURIComponent(version)}` +
`&start_row=${encodeURIComponent(String(editor.selection.start.line))}` +
`&start_col=${encodeURIComponent(String(editor.selection.start.character))}` +
`&end_row=${encodeURIComponent(String(editor.selection.end.line))}` +
`&end_col=${encodeURIComponent(String(editor.selection.end.character))}`
}

/**
* The command implementation for opening a cursor selection on Sourcegraph.
*/
Expand All @@ -35,21 +52,24 @@ async function openCommand(): Promise<void> {
if (!repositoryInfo) {
return
}
const { remoteURL, branch, fileRelative } = repositoryInfo

// Open in browser.
await open(
`${getSourcegraphUrl()}/-/editor` +
`?remote_url=${encodeURIComponent(remoteURL)}` +
`&branch=${encodeURIComponent(branch)}` +
`&file=${encodeURIComponent(fileRelative)}` +
`&editor=${encodeURIComponent('VSCode')}` +
`&version=${encodeURIComponent(version)}` +
`&start_row=${encodeURIComponent(String(editor.selection.start.line))}` +
`&start_col=${encodeURIComponent(String(editor.selection.start.character))}` +
`&end_row=${encodeURIComponent(String(editor.selection.end.line))}` +
`&end_col=${encodeURIComponent(String(editor.selection.end.character))}`
)
await open(getSelectionSourcegraphUrl(editor, repositoryInfo))
}

/**
* The command implementation for copying the Sourcegraph URL of a cursor selection.
*/
async function copyUrlCommand(): Promise<void> {
const editor = vscode.window.activeTextEditor
if (!editor) {
throw new Error('No active editor')
}
const repositoryInfo = await repoInfo(editor.document.uri.fsPath)
if (!repositoryInfo) {
return
}
// Copy to clipboard.
await vscode.env.clipboard.writeText(getSelectionSourcegraphUrl(editor, repositoryInfo))
}

/**
Expand All @@ -74,12 +94,12 @@ async function searchSelectionCommand(): Promise<void> {
// Search in browser.
await open(
`${getSourcegraphUrl()}/-/editor` +
`?remote_url=${encodeURIComponent(remoteURL)}` +
`&branch=${encodeURIComponent(branch)}` +
`&file=${encodeURIComponent(fileRelative)}` +
`&editor=${encodeURIComponent('VSCode')}` +
`&version=${encodeURIComponent(version)}` +
`&search=${encodeURIComponent(query)}`
`?remote_url=${encodeURIComponent(remoteURL)}` +
`&branch=${encodeURIComponent(branch)}` +
`&file=${encodeURIComponent(fileRelative)}` +
`&editor=${encodeURIComponent('VSCode')}` +
`&version=${encodeURIComponent(version)}` +
`&search=${encodeURIComponent(query)}`
)
}

Expand All @@ -101,11 +121,18 @@ async function searchCommand(): Promise<void> {
*/
export function activate(context: vscode.ExtensionContext): void {
// Register our extension commands (see package.json).
context.subscriptions.push(vscode.commands.registerCommand('extension.open', handleCommandErrors(openCommand)))
context.subscriptions.push(
vscode.commands.registerCommand('extension.search.selection', handleCommandErrors(searchSelectionCommand))
)
context.subscriptions.push(vscode.commands.registerCommand('extension.search', handleCommandErrors(searchCommand)))
context.subscriptions.push(vscode.commands.registerCommand(
'extension.open',
handleCommandErrors(openCommand)))
context.subscriptions.push(vscode.commands.registerCommand(
'extension.copyUrl',
handleCommandErrors(copyUrlCommand)))
context.subscriptions.push(vscode.commands.registerCommand(
'extension.search.selection',
handleCommandErrors(searchSelectionCommand)))
context.subscriptions.push(vscode.commands.registerCommand(
'extension.search',
handleCommandErrors(searchCommand)))
}

export function deactivate(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { gitHelpers } from './helpers'
import { Branch, gitRemoteNameAndBranch } from './remoteNameAndBranch'
import { gitRemoteUrlWithReplacements } from './remoteUrl'

interface RepositoryInfo extends Branch {
export interface RepositoryInfo extends Branch {
/** Git repository remote URL */
remoteURL: string

Expand Down