11import { AdjustmentDirection , PositionAdjuster } from '@sourcegraph/codeintellify'
22import { trimStart } from 'lodash'
33import { map } from 'rxjs/operators'
4+ import { JumpURLLocation } from '../../shared/backend/lsp'
45import { fetchBlobContentLines } from '../../shared/repo/backend'
56import { CodeHost , CodeView , CodeViewResolver , CodeViewWithOutSelector } from '../code_intelligence'
67import {
@@ -12,7 +13,7 @@ import {
1213} from './dom_functions'
1314import { getCommandPaletteMount } from './extensions'
1415import { resolveDiffFileInfo , resolveFileInfo , resolveSnippetFileInfo } from './file_info'
15- import { createCodeViewToolbarMount , parseURL } from './util'
16+ import { createCodeViewToolbarMount , getFileContainers , parseURL } from './util'
1617
1718const toolbarButtonProps = {
1819 className : 'btn btn-sm tooltipped tooltipped-n' ,
@@ -118,4 +119,31 @@ export const githubCodeHost: CodeHost = {
118119 codeViewResolver,
119120 check : checkIsGithub ,
120121 getCommandPaletteMount,
122+ buildJumpURLLocation : ( def : JumpURLLocation ) => {
123+ const rev = def . rev
124+ // If we're provided options, we can make the j2d URL more specific.
125+ const { repoPath } = parseURL ( )
126+
127+ const sameRepo = repoPath === def . repoPath
128+ // Stay on same page in PR if possible.
129+ if ( sameRepo && def . part ) {
130+ const containers = getFileContainers ( )
131+ for ( const container of containers ) {
132+ const header = container . querySelector ( '.file-header' ) as HTMLElement
133+ const anchorPath = header . dataset . path
134+ if ( anchorPath === def . filePath ) {
135+ const anchorUrl = header . dataset . anchor
136+ const url = `${ window . location . origin } ${ window . location . pathname } #${ anchorUrl } ${
137+ def . part === 'base' ? 'L' : 'R'
138+ } ${ def . position . line } `
139+
140+ return url
141+ }
142+ }
143+ }
144+
145+ return `https://${ def . repoPath } /blob/${ rev } /${ def . filePath } #L${ def . position . line } ${
146+ def . position . character ? ':' + def . position . character : ''
147+ } `
148+ } ,
121149}
0 commit comments