Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/libs/code_intelligence/code_intelligence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { filter, map, mergeMap, observeOn, withLatestFrom } from 'rxjs/operators

import { TextDocumentItem } from 'sourcegraph/module/client/types/textDocument'
import { Disposable } from 'vscode-jsonrpc'
import { createJumpURLFetcher, createLSPFromExtensions } from '../../shared/backend/lsp'
import { createJumpURLFetcher, createLSPFromExtensions, JumpURLLocation } from '../../shared/backend/lsp'
import { lspViaAPIXlang, toTextDocumentIdentifier } from '../../shared/backend/lsp'
import { ButtonProps, CodeViewToolbar } from '../../shared/components/CodeViewToolbar'
import { AbsoluteRepoFile } from '../../shared/repo'
Expand Down Expand Up @@ -135,6 +135,9 @@ export interface CodeHost {
* Get the DOM element where we'll mount the command palette for extensions.
*/
getCommandPaletteMount?: MountGetter

/** Build the J2D url from the location. */
buildJumpURLLocation?: (def: JumpURLLocation) => string
}

export interface FileInfo {
Expand Down Expand Up @@ -226,7 +229,10 @@ function initCodeIntelligence(

const relativeElement = document.body

const fetchJumpURL = createJumpURLFetcher(simpleProviderFns.fetchDefinition, toPrettyBlobURL)
const fetchJumpURL = createJumpURLFetcher(
simpleProviderFns.fetchDefinition,
codeHost.buildJumpURLLocation || toPrettyBlobURL
)

const containerComponentUpdates = new Subject<void>()

Expand Down
30 changes: 29 additions & 1 deletion src/libs/github/code_intelligence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AdjustmentDirection, PositionAdjuster } from '@sourcegraph/codeintellify'
import { trimStart } from 'lodash'
import { map } from 'rxjs/operators'
import { JumpURLLocation } from '../../shared/backend/lsp'
import { fetchBlobContentLines } from '../../shared/repo/backend'
import { CodeHost, CodeView, CodeViewResolver, CodeViewWithOutSelector } from '../code_intelligence'
import {
Expand All @@ -12,7 +13,7 @@ import {
} from './dom_functions'
import { getCommandPaletteMount } from './extensions'
import { resolveDiffFileInfo, resolveFileInfo, resolveSnippetFileInfo } from './file_info'
import { createCodeViewToolbarMount, parseURL } from './util'
import { createCodeViewToolbarMount, getFileContainers, parseURL } from './util'

const toolbarButtonProps = {
className: 'btn btn-sm tooltipped tooltipped-n',
Expand Down Expand Up @@ -118,4 +119,31 @@ export const githubCodeHost: CodeHost = {
codeViewResolver,
check: checkIsGithub,
getCommandPaletteMount,
buildJumpURLLocation: (def: JumpURLLocation) => {
const rev = def.rev
// If we're provided options, we can make the j2d URL more specific.
const { repoPath } = parseURL()

const sameRepo = repoPath === def.repoPath
// Stay on same page in PR if possible.
if (sameRepo && def.part) {
const containers = getFileContainers()
for (const container of containers) {
const header = container.querySelector('.file-header') as HTMLElement
const anchorPath = header.dataset.path
if (anchorPath === def.filePath) {
const anchorUrl = header.dataset.anchor
const url = `${window.location.origin}${window.location.pathname}#${anchorUrl}${
def.part === 'base' ? 'L' : 'R'
}${def.position.line}`

return url
}
}
}

return `https://${def.repoPath}/blob/${rev}/${def.filePath}#L${def.position.line}${
def.position.character ? ':' + def.position.character : ''
}`
},
}