From 4f27083d09f0fed6cfb90494f3f2f9961d49a202 Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Wed, 1 Sep 2021 16:32:07 -0700 Subject: [PATCH 1/2] feat: add new "extension.copy" command --- package.json | 5 +++++ src/extension.ts | 49 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 704abf38..e17ac3dd 100644 --- a/package.json +++ b/package.json @@ -17,12 +17,17 @@ "Other" ], "activationEvents": [ + "onCommand:extension.copy", "onCommand:extension.open", "onCommand:extension.search" ], "main": "./out/extension", "contributes": { "commands": [ + { + "command": "extension.copy", + "title": "Sourcegraph: Copy link" + }, { "command": "extension.open", "title": "Sourcegraph: Open" diff --git a/src/extension.ts b/src/extension.ts index 9c0361a4..8a3b6447 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,35 +23,51 @@ const handleCommandErrors =

(command: (...args: P) => Pr } } -/** - * The command implementation for opening a cursor selection on Sourcegraph. - */ -async function openCommand(): Promise { +async function getUrlForSelection(): Promise { const editor = vscode.window.activeTextEditor if (!editor) { throw new Error('No active editor') } const repositoryInfo = await repoInfo(editor.document.uri.fsPath) if (!repositoryInfo) { - return + throw new Error('No active repository') } const { remoteURL, branch, fileRelative } = repositoryInfo - // Open in browser. - await open( + 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))}` + `?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 copying a URL for cursor selection to clipboard. + */ +async function copyLinkCommand(): Promise { + const url = await getUrlForSelection() + + // Copy URL to system clipboard. + return vscode.env.clipboard.writeText(url) +} + +/** + * The command implementation for opening a cursor selection on Sourcegraph. + */ +async function openCommand(): Promise { + const url = await getUrlForSelection() + + // Open in browser. + await open(url) +} + /** * The command implementation for searching a cursor selection on Sourcegraph. */ @@ -88,6 +104,7 @@ async function searchCommand(): Promise { */ export function activate(context: vscode.ExtensionContext): void { // Register our extension commands (see package.json). + context.subscriptions.push(vscode.commands.registerCommand('extension.copy', handleCommandErrors(copyLinkCommand))) context.subscriptions.push(vscode.commands.registerCommand('extension.open', handleCommandErrors(openCommand))) context.subscriptions.push(vscode.commands.registerCommand('extension.search', handleCommandErrors(searchCommand))) } From 3989a4b96d747ccfeaa32994f57dd60a538f8e27 Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Wed, 1 Sep 2021 17:36:05 -0700 Subject: [PATCH 2/2] fix: provide feedback to user on successful clipboard copy operation --- src/extension.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 8a3b6447..116880f8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -55,7 +55,9 @@ async function copyLinkCommand(): Promise { const url = await getUrlForSelection() // Copy URL to system clipboard. - return vscode.env.clipboard.writeText(url) + await vscode.env.clipboard.writeText(url) + await vscode.window.showInformationMessage('Sourcegraph URL copied to clipboard.') + return } /**