Skip to content

Commit 48fef0c

Browse files
authored
Share link from File Menu shouldn't include active file (microsoft#153911)
Fixes microsoft#153537
1 parent efbf613 commit 48fef0c

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

extensions/github/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
{
3838
"command": "github.copyVscodeDevLink",
3939
"title": "Copy vscode.dev Link"
40+
},
41+
{
42+
"command": "github.copyVscodeDevLinkFile",
43+
"title": "Copy vscode.dev Link"
4044
}
4145
],
4246
"menus": {
@@ -48,11 +52,15 @@
4852
{
4953
"command": "github.copyVscodeDevLink",
5054
"when": "false"
55+
},
56+
{
57+
"command": "github.copyVscodeDevLinkFile",
58+
"when": "false"
5159
}
5260
],
5361
"file/share": [
5462
{
55-
"command": "github.copyVscodeDevLink",
63+
"command": "github.copyVscodeDevLinkFile",
5664
"when": "github.hasGitHubRepo"
5765
}
5866
],

extensions/github/src/commands.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import { publishRepository } from './publish';
99
import { DisposableStore } from './util';
1010
import { getPermalink } from './links';
1111

12+
async function copyVscodeDevLink(gitAPI: GitAPI, useSelection: boolean) {
13+
try {
14+
const permalink = getPermalink(gitAPI, useSelection, 'https://vscode.dev/github');
15+
if (permalink) {
16+
return vscode.env.clipboard.writeText(permalink);
17+
}
18+
} catch (err) {
19+
vscode.window.showErrorMessage(err.message);
20+
}
21+
}
22+
1223
export function registerCommands(gitAPI: GitAPI): vscode.Disposable {
1324
const disposables = new DisposableStore();
1425

@@ -21,14 +32,11 @@ export function registerCommands(gitAPI: GitAPI): vscode.Disposable {
2132
}));
2233

2334
disposables.add(vscode.commands.registerCommand('github.copyVscodeDevLink', async () => {
24-
try {
25-
const permalink = getPermalink(gitAPI, 'https://vscode.dev/github');
26-
if (permalink) {
27-
vscode.env.clipboard.writeText(permalink);
28-
}
29-
} catch (err) {
30-
vscode.window.showErrorMessage(err.message);
31-
}
35+
return copyVscodeDevLink(gitAPI, true);
36+
}));
37+
38+
disposables.add(vscode.commands.registerCommand('github.copyVscodeDevLinkFile', async () => {
39+
return copyVscodeDevLink(gitAPI, false);
3240
}));
3341

3442
return disposables;

extensions/github/src/links.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ function rangeString(range: vscode.Range | undefined) {
4343
return hash;
4444
}
4545

46-
export function getPermalink(gitAPI: GitAPI, hostPrefix?: string): string | undefined {
46+
export function getPermalink(gitAPI: GitAPI, useSelection: boolean, hostPrefix?: string): string | undefined {
4747
hostPrefix = hostPrefix ?? 'https://github.com';
4848
const { uri, range } = getFileAndPosition();
49-
if (!uri) {
50-
return;
51-
}
52-
const gitRepo = getRepositoryForFile(gitAPI, uri);
49+
// Use the first repo if we cannot determine a repo from the uri.
50+
const gitRepo = (uri ? getRepositoryForFile(gitAPI, uri) : gitAPI.repositories[0]) ?? gitAPI.repositories[0];
5351
if (!gitRepo) {
5452
return;
5553
}
@@ -71,8 +69,8 @@ export function getPermalink(gitAPI: GitAPI, hostPrefix?: string): string | unde
7169
}
7270

7371
const commitHash = gitRepo.state.HEAD?.commit;
74-
const pathSegment = uri.path.substring(gitRepo.rootUri.path.length);
72+
const fileSegments = (useSelection && uri) ? `${uri.path.substring(gitRepo.rootUri.path.length)}${rangeString(range)}` : '';
7573

7674
return `${hostPrefix}/${repo.owner}/${repo.repo}/blob/${commitHash
77-
}${pathSegment}${rangeString(range)}`;
75+
}${fileSegments}`;
7876
}

0 commit comments

Comments
 (0)