diff --git a/src/commands/openFileInRemote.ts b/src/commands/openFileInRemote.ts index 5bf916a..875bb2e 100644 --- a/src/commands/openFileInRemote.ts +++ b/src/commands/openFileInRemote.ts @@ -1,6 +1,6 @@ 'use strict'; import { Arrays } from '../system'; -import { commands, TextEditor, TextEditorEdit, Uri, window } from 'vscode'; +import { commands, Range, TextEditor, TextEditorEdit, Uri, window } from 'vscode'; import { ActiveEditorCommand, Commands } from './commands'; import { GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; @@ -22,7 +22,8 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand { try { const remotes = Arrays.uniqueBy(await this.git.getRemotes(this.repoPath), _ => _.url, _ => !!_.provider); - return commands.executeCommand(Commands.OpenInRemote, uri, remotes, 'file', [gitUri.getRelativePath(), branch.name, gitUri.sha]); + const range = editor && new Range(editor.selection.start.with({ line: editor.selection.start.line + 1 }), editor.selection.end.with({ line: editor.selection.end.line + 1 })); + return commands.executeCommand(Commands.OpenInRemote, uri, remotes, 'file', [gitUri.getRelativePath(), branch.name, gitUri.sha, range]); } catch (ex) { Logger.error(ex, 'OpenFileInRemoteCommand'); diff --git a/src/git/remotes/bitbucket.ts b/src/git/remotes/bitbucket.ts index b880ba2..7877cd7 100644 --- a/src/git/remotes/bitbucket.ts +++ b/src/git/remotes/bitbucket.ts @@ -1,4 +1,5 @@ 'use strict'; +import { Range } from 'vscode'; import { RemoteProvider } from './provider'; export class BitbucketService extends RemoteProvider { @@ -19,9 +20,19 @@ export class BitbucketService extends RemoteProvider { return `${this.baseUrl}/commits/${sha}`; } - protected getUrlForFile(fileName: string, branch?: string, sha?: string): string { - if (sha) return `${this.baseUrl}/src/${sha}/${fileName}`; - if (branch) return `${this.baseUrl}/src/${branch}/${fileName}`; - return `${this.baseUrl}?path=${fileName}`; + protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string { + let line: string = ''; + if (range) { + if (range.start.line === range.end.line) { + line = `#${fileName}-${range.start.line}`; + } + else { + line = `#${fileName}-${range.start.line}:${range.end.line}`; + } + } + + if (sha) return `${this.baseUrl}/src/${sha}/${fileName}${line}`; + if (branch) return `${this.baseUrl}/src/${branch}/${fileName}${line}`; + return `${this.baseUrl}?path=${fileName}${line}`; } } \ No newline at end of file diff --git a/src/git/remotes/github.ts b/src/git/remotes/github.ts index 6c7e99b..3f9fb55 100644 --- a/src/git/remotes/github.ts +++ b/src/git/remotes/github.ts @@ -1,4 +1,5 @@ 'use strict'; +import { Range } from 'vscode'; import { RemoteProvider } from './provider'; export class GitHubService extends RemoteProvider { @@ -19,9 +20,19 @@ export class GitHubService extends RemoteProvider { return `${this.baseUrl}/commit/${sha}`; } - protected getUrlForFile(fileName: string, branch?: string, sha?: string): string { - if (sha) return `${this.baseUrl}/blob/${sha}/${fileName}`; - if (branch) return `${this.baseUrl}/blob/${branch}/${fileName}`; - return `${this.baseUrl}?path=${fileName}`; + protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string { + let line: string = ''; + if (range) { + if (range.start.line === range.end.line) { + line = `#L${range.start.line}`; + } + else { + line = `#L${range.start.line}-L${range.end.line}`; + } + } + + if (sha) return `${this.baseUrl}/blob/${sha}/${fileName}${line}`; + if (branch) return `${this.baseUrl}/blob/${branch}/${fileName}${line}`; + return `${this.baseUrl}?path=${fileName}${line}`; } } \ No newline at end of file diff --git a/src/git/remotes/provider.ts b/src/git/remotes/provider.ts index 0a42947..422101f 100644 --- a/src/git/remotes/provider.ts +++ b/src/git/remotes/provider.ts @@ -1,5 +1,5 @@ 'use strict'; -import { commands, Uri } from 'vscode'; +import { commands, Range, Uri } from 'vscode'; import { BuiltInCommands } from '../../constants'; export type RemoteOpenType = 'branch' | 'commit' | 'file' | 'working-file'; @@ -16,7 +16,7 @@ export abstract class RemoteProvider { protected abstract getUrlForBranch(branch: string): string; protected abstract getUrlForCommit(sha: string): string; - protected abstract getUrlForFile(fileName: string, branch: string, sha?: string): string; + protected abstract getUrlForFile(fileName: string, branch: string, sha?: string, range?: Range): string; private async _openUrl(url: string): Promise<{}> { return url && commands.executeCommand(BuiltInCommands.Open, Uri.parse(url)); @@ -24,9 +24,9 @@ export abstract class RemoteProvider { open(type: 'branch', branch: string): Promise<{}>; open(type: 'commit', sha: string): Promise<{}>; - open(type: 'file', fileName: string, branch?: string, sha?: string): Promise<{}>; - open(type: RemoteOpenType, ...args: string[]): Promise<{}>; - open(type: RemoteOpenType, branchOrShaOrFileName: string, fileBranch?: string, fileSha?: string): Promise<{}> { + open(type: 'file', fileName: string, branch?: string, sha?: string, range?: Range): Promise<{}>; + open(type: RemoteOpenType, ...args: any[]): Promise<{}>; + open(type: RemoteOpenType, branchOrShaOrFileName: string, fileBranch?: string, fileSha?: string, fileRange?: Range): Promise<{}> { switch (type) { case 'branch': return this.openBranch(branchOrShaOrFileName); @@ -34,7 +34,7 @@ export abstract class RemoteProvider { return this.openCommit(branchOrShaOrFileName); case 'file': case 'working-file': - return this.openFile(branchOrShaOrFileName, fileBranch, fileSha); + return this.openFile(branchOrShaOrFileName, fileBranch, fileSha, fileRange); } } @@ -46,7 +46,7 @@ export abstract class RemoteProvider { return this._openUrl(this.getUrlForCommit(sha)); } - openFile(fileName: string, branch?: string, sha?: string) { - return this._openUrl(this.getUrlForFile(fileName, branch, sha)); + openFile(fileName: string, branch?: string, sha?: string, range?: Range) { + return this._openUrl(this.getUrlForFile(fileName, branch, sha, range)); } } \ No newline at end of file diff --git a/src/git/remotes/visualStudio.ts b/src/git/remotes/visualStudio.ts index baecbbb..dcb8997 100644 --- a/src/git/remotes/visualStudio.ts +++ b/src/git/remotes/visualStudio.ts @@ -1,4 +1,5 @@ 'use strict'; +import { Range } from 'vscode'; import { RemoteProvider } from './provider'; export class VisualStudioService extends RemoteProvider { @@ -19,9 +20,19 @@ export class VisualStudioService extends RemoteProvider { return `${this.baseUrl}/commit/${sha}`; } - protected getUrlForFile(fileName: string, branch?: string, sha?: string): string { - if (sha) return `${this.baseUrl}/commit/${sha}/?_a=contents&path=%2F${fileName}`; - if (branch) return `${this.baseUrl}/?path=%2F${fileName}&version=GB${branch}&_a=contents`; - return `${this.baseUrl}?path=%2F${fileName}`; + protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string { + let line: string = ''; + if (range) { + if (range.start.line === range.end.line) { + line = `&line=${range.start.line}`; + } + else { + line = `&line=${range.start.line}&lineEnd=${range.end.line}`; + } + } + + if (sha) return `${this.baseUrl}/commit/${sha}/?_a=contents&path=%2F${fileName}${line}`; + if (branch) return `${this.baseUrl}/?path=%2F${fileName}&version=GB${branch}&_a=contents${line}`; + return `${this.baseUrl}?path=%2F${fileName}${line}`; } } \ No newline at end of file