Share link from File Menu shouldn't include active file (#153911)

Fixes #153537
This commit is contained in:
Alex Ross 2022-07-01 15:04:12 +02:00 committed by GitHub
parent efbf613639
commit 48fef0c1da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 16 deletions

View file

@ -37,6 +37,10 @@
{
"command": "github.copyVscodeDevLink",
"title": "Copy vscode.dev Link"
},
{
"command": "github.copyVscodeDevLinkFile",
"title": "Copy vscode.dev Link"
}
],
"menus": {
@ -48,11 +52,15 @@
{
"command": "github.copyVscodeDevLink",
"when": "false"
},
{
"command": "github.copyVscodeDevLinkFile",
"when": "false"
}
],
"file/share": [
{
"command": "github.copyVscodeDevLink",
"command": "github.copyVscodeDevLinkFile",
"when": "github.hasGitHubRepo"
}
],

View file

@ -9,6 +9,17 @@ import { publishRepository } from './publish';
import { DisposableStore } from './util';
import { getPermalink } from './links';
async function copyVscodeDevLink(gitAPI: GitAPI, useSelection: boolean) {
try {
const permalink = getPermalink(gitAPI, useSelection, 'https://vscode.dev/github');
if (permalink) {
return vscode.env.clipboard.writeText(permalink);
}
} catch (err) {
vscode.window.showErrorMessage(err.message);
}
}
export function registerCommands(gitAPI: GitAPI): vscode.Disposable {
const disposables = new DisposableStore();
@ -21,14 +32,11 @@ export function registerCommands(gitAPI: GitAPI): vscode.Disposable {
}));
disposables.add(vscode.commands.registerCommand('github.copyVscodeDevLink', async () => {
try {
const permalink = getPermalink(gitAPI, 'https://vscode.dev/github');
if (permalink) {
vscode.env.clipboard.writeText(permalink);
}
} catch (err) {
vscode.window.showErrorMessage(err.message);
}
return copyVscodeDevLink(gitAPI, true);
}));
disposables.add(vscode.commands.registerCommand('github.copyVscodeDevLinkFile', async () => {
return copyVscodeDevLink(gitAPI, false);
}));
return disposables;

View file

@ -43,13 +43,11 @@ function rangeString(range: vscode.Range | undefined) {
return hash;
}
export function getPermalink(gitAPI: GitAPI, hostPrefix?: string): string | undefined {
export function getPermalink(gitAPI: GitAPI, useSelection: boolean, hostPrefix?: string): string | undefined {
hostPrefix = hostPrefix ?? 'https://github.com';
const { uri, range } = getFileAndPosition();
if (!uri) {
return;
}
const gitRepo = getRepositoryForFile(gitAPI, uri);
// Use the first repo if we cannot determine a repo from the uri.
const gitRepo = (uri ? getRepositoryForFile(gitAPI, uri) : gitAPI.repositories[0]) ?? gitAPI.repositories[0];
if (!gitRepo) {
return;
}
@ -71,8 +69,8 @@ export function getPermalink(gitAPI: GitAPI, hostPrefix?: string): string | unde
}
const commitHash = gitRepo.state.HEAD?.commit;
const pathSegment = uri.path.substring(gitRepo.rootUri.path.length);
const fileSegments = (useSelection && uri) ? `${uri.path.substring(gitRepo.rootUri.path.length)}${rangeString(range)}` : '';
return `${hostPrefix}/${repo.owner}/${repo.repo}/blob/${commitHash
}${pathSegment}${rangeString(range)}`;
}${fileSegments}`;
}