Skip to content

Commit 0078c49

Browse files
authored
Allow Continue On to vscode.dev with no active editor (microsoft#186459)
1 parent 57cf8c8 commit 0078c49

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

extensions/github/src/links.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as vscode from 'vscode';
77
import { API as GitAPI, RefType, Repository } from './typings/git';
8-
import { getRepositoryFromUrl } from './util';
8+
import { getRepositoryFromUrl, repositoryHasGitHubRemote } from './util';
99

1010
export function isFileInRepo(repository: Repository, file: vscode.Uri): boolean {
1111
return file.path.toLowerCase() === repository.rootUri.path.toLowerCase() ||
@@ -132,19 +132,17 @@ export function encodeURIComponentExceptSlashes(path: string) {
132132
export async function getLink(gitAPI: GitAPI, useSelection: boolean, shouldEnsurePublished: boolean, hostPrefix?: string, linkType: 'permalink' | 'headlink' = 'permalink', context?: LinkContext, useRange?: boolean): Promise<string | undefined> {
133133
hostPrefix = hostPrefix ?? 'https://github.com';
134134
const fileAndPosition = getFileAndPosition(context);
135-
if (!fileAndPosition) {
136-
return;
137-
}
138-
const uri = fileAndPosition.uri;
135+
const fileUri = fileAndPosition?.uri;
139136

140137
// Use the first repo if we cannot determine a repo from the uri.
141-
const gitRepo = (uri ? getRepositoryForFile(gitAPI, uri) : gitAPI.repositories[0]) ?? gitAPI.repositories[0];
138+
const githubRepository = gitAPI.repositories.find(repo => repositoryHasGitHubRemote(repo));
139+
const gitRepo = (fileUri ? getRepositoryForFile(gitAPI, fileUri) : githubRepository) ?? githubRepository;
142140
if (!gitRepo) {
143141
return;
144142
}
145143

146-
if (shouldEnsurePublished) {
147-
await ensurePublished(gitRepo, uri);
144+
if (shouldEnsurePublished && fileUri) {
145+
await ensurePublished(gitRepo, fileUri);
148146
}
149147

150148
let repo: { owner: string; repo: string } | undefined;
@@ -165,13 +163,17 @@ export async function getLink(gitAPI: GitAPI, useSelection: boolean, shouldEnsur
165163
}
166164

167165
const blobSegment = gitRepo.state.HEAD ? (`/blob/${linkType === 'headlink' && gitRepo.state.HEAD.name ? encodeURIComponentExceptSlashes(gitRepo.state.HEAD.name) : gitRepo.state.HEAD?.commit}`) : '';
168-
const encodedFilePath = encodeURIComponentExceptSlashes(uri.path.substring(gitRepo.rootUri.path.length));
166+
const uriWithoutFileSegments = `${hostPrefix}/${repo.owner}/${repo.repo}${blobSegment}`;
167+
if (!fileUri) {
168+
return uriWithoutFileSegments;
169+
}
170+
171+
const encodedFilePath = encodeURIComponentExceptSlashes(fileUri.path.substring(gitRepo.rootUri.path.length));
169172
const fileSegments = fileAndPosition.type === LinkType.File
170173
? (useSelection ? `${encodedFilePath}${useRange ? rangeString(fileAndPosition.range) : ''}` : '')
171174
: (useSelection ? `${encodedFilePath}${useRange ? notebookCellRangeString(fileAndPosition.cellIndex, fileAndPosition.range) : ''}` : '');
172175

173-
return `${hostPrefix}/${repo.owner}/${repo.repo}${blobSegment
174-
}${fileSegments}`;
176+
return `${uriWithoutFileSegments}${fileSegments}`;
175177
}
176178

177179
export function getBranchLink(url: string, branch: string, hostPrefix: string = 'https://github.com') {

0 commit comments

Comments
 (0)