Skip to content

Commit 9214e62

Browse files
suanbugthesystem
authored andcommitted
Enable multi-line selection (#56)
Hi @suan Thanks 👍
1 parent 0b02d18 commit 9214e62

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/extension.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var findParentDir = require('find-parent-dir');
1717

1818
const gitProvider = require('./gitProvider');
1919

20-
function getGitProviderLink(cb, fileFsPath, line, pr) {
20+
function getGitProviderLink(cb, fileFsPath, lines, pr) {
2121
var cwd = workspace.rootPath;
2222
var repoDir = findParentDir.sync(workspace.rootPath, '.git') || cwd;
2323

@@ -48,9 +48,14 @@ function getGitProviderLink(cb, fileFsPath, line, pr) {
4848
cb(provider.prUrl(branch));
4949
}
5050
else {
51-
cb(provider.webUrl(branch, subdir, line));
51+
if (lines[0] == lines[1]) {
52+
cb(provider.webUrl(branch, subdir, lines[0]));
53+
}
54+
else {
55+
cb(provider.webUrl(branch, subdir, lines[0], lines[1]));
56+
}
5257
}
53-
58+
5459
});
5560
});
5661
}
@@ -61,15 +66,21 @@ function getGitProviderLinkForFile(fileFsPath, cb) {
6166
getGitProviderLink(cb, fileFsPath);
6267
}
6368

64-
function getGitProviderLinkForCurrentEditorLine(cb) {
69+
function getGitProviderLinkForCurrentEditorLines(cb) {
6570
var editor = Window.activeTextEditor;
6671
if (editor) {
67-
var lineIndex = editor.selection.active.line + 1;
6872
var fileFsPath = editor.document.uri.fsPath;
69-
getGitProviderLink(cb, fileFsPath, lineIndex);
73+
getGitProviderLink(cb, fileFsPath, getSelectedLines(editor));
7074
}
7175
}
7276

77+
function getSelectedLines(editor) {
78+
var anchorLineIndex = editor.selection.anchor.line + 1;
79+
var activeLineIndex = editor.selection.active.line + 1;
80+
81+
return [anchorLineIndex, activeLineIndex].sort();
82+
}
83+
7384
function getGitProviderLinkForRepo(cb) {
7485
getGitProviderLink(cb);
7586
}
@@ -87,7 +98,7 @@ function branchOnCallingContext(args, cb, pr) {
8798
getGitProviderLinkForFile(args.fsPath, cb);
8899
}
89100
else if (Window.activeTextEditor) {
90-
getGitProviderLinkForCurrentEditorLine(cb);
101+
getGitProviderLinkForCurrentEditorLines(cb);
91102
}
92103
else {
93104
getGitProviderLinkForRepo(cb);

src/gitProvider.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,31 @@ class BaseProvider {
1919
* @param {string} branch
2020
* @param {string} filePath The file path relative to repository root, beginning with '/'.
2121
* @param {number} line
22+
* @param {number} endLine The last line in a multi-line selection
2223
* @return {string} The URL to be opened with the browser.
2324
*/
24-
webUrl(branch, filePath, line) {
25+
webUrl(branch, filePath, line, endLine) {
2526
return '';
2627
}
2728
prUrl(branch) {
2829
return '';
29-
}
30+
}
3031
}
3132

3233
class GitHub extends BaseProvider {
33-
webUrl(branch, filePath, line) {
34+
webUrl(branch, filePath, line, endLine) {
3435
if (filePath) {
35-
return `${this.baseUrl}/blob/${branch}${filePath}` + (line ? '#L' + line : '');
36+
return `${this.baseUrl}/blob/${branch}${filePath}` + (line ? '#L' + line : '') + (endLine ? '-L' + endLine : '');
3637
}
3738
return `${this.baseUrl}/tree/${branch}`;
3839
}
3940
prUrl(branch){
4041
return `${this.baseUrl}/pull/new/${branch}`;
41-
}
42+
}
4243
}
4344

4445
class Bitbucket extends BaseProvider {
45-
webUrl(branch, filePath, line) {
46+
webUrl(branch, filePath, line, endLine) {
4647
return `${this.baseUrl}/src/${branch}` + (filePath ? `${filePath}` : '') + (line ? `#cl-${line}` : '');
4748
}
4849
prUrl(branch){
@@ -58,7 +59,7 @@ class VisualStudio extends BaseProvider {
5859
return `https://${this.gitUrl.resource}${this.gitUrl.pathname}`.replace(/\.git/, '');
5960
}
6061

61-
webUrl(branch, filePath, line) {
62+
webUrl(branch, filePath, line, endLine) {
6263
let query = {
6364
version: `GB${branch}`,
6465
};

0 commit comments

Comments
 (0)