Skip to content

Commit

Permalink
Generate head links for Continue Edit Session (#159833)
Browse files Browse the repository at this point in the history
  • Loading branch information
joyceerhl committed Sep 2, 2022
1 parent c4722f1 commit 9b99454
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions extensions/github/src/commands.ts
Expand Up @@ -7,15 +7,15 @@ import * as vscode from 'vscode';
import { API as GitAPI } from './typings/git';
import { publishRepository } from './publish';
import { DisposableStore } from './util';
import { getPermalink } from './links';
import { getLink } from './links';

function getVscodeDevHost(): string {
return `https://${vscode.env.appName.toLowerCase().includes('insiders') ? 'insiders.' : ''}vscode.dev/github`;
}

async function copyVscodeDevLink(gitAPI: GitAPI, useSelection: boolean) {
try {
const permalink = getPermalink(gitAPI, useSelection, getVscodeDevHost());
const permalink = getLink(gitAPI, useSelection, getVscodeDevHost());
if (permalink) {
return vscode.env.clipboard.writeText(permalink);
}
Expand All @@ -26,8 +26,8 @@ async function copyVscodeDevLink(gitAPI: GitAPI, useSelection: boolean) {

async function openVscodeDevLink(gitAPI: GitAPI): Promise<vscode.Uri | undefined> {
try {
const permalink = getPermalink(gitAPI, true, getVscodeDevHost());
return permalink ? vscode.Uri.parse(permalink) : undefined;
const headlink = getLink(gitAPI, true, getVscodeDevHost(), 'headlink');
return headlink ? vscode.Uri.parse(headlink) : undefined;
} catch (err) {
vscode.window.showErrorMessage(err.message);
return undefined;
Expand Down
6 changes: 3 additions & 3 deletions extensions/github/src/links.ts
Expand Up @@ -95,7 +95,7 @@ export function notebookCellRangeString(index: number | undefined, range: vscode
return hash;
}

export function getPermalink(gitAPI: GitAPI, useSelection: boolean, hostPrefix?: string): string | undefined {
export function getLink(gitAPI: GitAPI, useSelection: boolean, hostPrefix?: string, linkType: 'permalink' | 'headlink' = 'permalink'): string | undefined {
hostPrefix = hostPrefix ?? 'https://github.com';
const fileAndPosition = getFileAndPosition();
if (!fileAndPosition) {
Expand Down Expand Up @@ -125,11 +125,11 @@ export function getPermalink(gitAPI: GitAPI, useSelection: boolean, hostPrefix?:
return;
}

const commitHash = (gitRepo.state.HEAD?.ahead === 0) ? `/blob/${gitRepo.state.HEAD?.commit}` : '';
const blobSegment = (gitRepo.state.HEAD?.ahead === 0) ? `/blob/${linkType === 'headlink' ? gitRepo.state.HEAD.name : gitRepo.state.HEAD?.commit}` : '';
const fileSegments = fileAndPosition.type === LinkType.File
? (useSelection ? `${uri.path.substring(gitRepo.rootUri.path.length)}${rangeString(fileAndPosition.range)}` : '')
: (useSelection ? `${uri.path.substring(gitRepo.rootUri.path.length)}${notebookCellRangeString(fileAndPosition.cellIndex, fileAndPosition.range)}` : '');

return `${hostPrefix}/${repo.owner}/${repo.repo}${commitHash
return `${hostPrefix}/${repo.owner}/${repo.repo}${blobSegment
}${fileSegments}`;
}

0 comments on commit 9b99454

Please sign in to comment.