Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit d9abff5

Browse files
authored
feat: stay in github for j2d in new inject method (#202)
* feat: stay on github * chore: prettier
1 parent d2ce8e4 commit d9abff5

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/libs/code_intelligence/code_intelligence.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { filter, map, mergeMap, observeOn, withLatestFrom } from 'rxjs/operators
2020

2121
import { TextDocumentItem } from 'sourcegraph/module/client/types/textDocument'
2222
import { Disposable } from 'vscode-jsonrpc'
23-
import { createJumpURLFetcher, createLSPFromExtensions } from '../../shared/backend/lsp'
23+
import { createJumpURLFetcher, createLSPFromExtensions, JumpURLLocation } from '../../shared/backend/lsp'
2424
import { lspViaAPIXlang, toTextDocumentIdentifier } from '../../shared/backend/lsp'
2525
import { ButtonProps, CodeViewToolbar } from '../../shared/components/CodeViewToolbar'
2626
import { AbsoluteRepoFile } from '../../shared/repo'
@@ -135,6 +135,9 @@ export interface CodeHost {
135135
* Get the DOM element where we'll mount the command palette for extensions.
136136
*/
137137
getCommandPaletteMount?: MountGetter
138+
139+
/** Build the J2D url from the location. */
140+
buildJumpURLLocation?: (def: JumpURLLocation) => string
138141
}
139142

140143
export interface FileInfo {
@@ -226,7 +229,10 @@ function initCodeIntelligence(
226229

227230
const relativeElement = document.body
228231

229-
const fetchJumpURL = createJumpURLFetcher(simpleProviderFns.fetchDefinition, toPrettyBlobURL)
232+
const fetchJumpURL = createJumpURLFetcher(
233+
simpleProviderFns.fetchDefinition,
234+
codeHost.buildJumpURLLocation || toPrettyBlobURL
235+
)
230236

231237
const containerComponentUpdates = new Subject<void>()
232238

src/libs/github/code_intelligence.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AdjustmentDirection, PositionAdjuster } from '@sourcegraph/codeintellify'
22
import { trimStart } from 'lodash'
33
import { map } from 'rxjs/operators'
4+
import { JumpURLLocation } from '../../shared/backend/lsp'
45
import { fetchBlobContentLines } from '../../shared/repo/backend'
56
import { CodeHost, CodeView, CodeViewResolver, CodeViewWithOutSelector } from '../code_intelligence'
67
import {
@@ -12,7 +13,7 @@ import {
1213
} from './dom_functions'
1314
import { getCommandPaletteMount } from './extensions'
1415
import { resolveDiffFileInfo, resolveFileInfo, resolveSnippetFileInfo } from './file_info'
15-
import { createCodeViewToolbarMount, parseURL } from './util'
16+
import { createCodeViewToolbarMount, getFileContainers, parseURL } from './util'
1617

1718
const 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

Comments
 (0)