Skip to content

Commit 391a72a

Browse files
authored
Merge pull request #31 from gdziadkiewicz/AddContextMenuItem
Add entries to contex menu in explorer
2 parents 2d24031 + 8a30ee8 commit 391a72a

File tree

5 files changed

+106
-47
lines changed

5 files changed

+106
-47
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
Press <kbd>F1</kbd> and narrow down the list commands by typing `extension`. Pick `Extensions: Install Extension`.
1111

12-
![](https://raw.githubusercontent.com/ziyasal/vscode-open-in-github/master/screenshots/install.png)
12+
![](screenshots/install.png?raw=true)
1313

1414
Simply pick the `Open in GitHub / Bitbucket` extension from the list
1515

@@ -37,15 +37,24 @@ npm install
3737

3838
Press <kbd>F1</kbd> and type `Open in GitHub`.
3939

40-
![](https://raw.githubusercontent.com/ziyasal/vscode-open-in-github/master/screenshots/open-in-github.png)
40+
![](screenshots/open-in-github.png?raw=true)
4141

42+
Press <kbd>F1</kbd> and type `Copy GitHub link to clipboard`.
43+
44+
![](screenshots/copy.png?raw=true)
4245

4346
**Keybord Shortcut**
4447

4548
Press <kbd>Ctrl+L G</kbd> to activate.
4649
Press <kbd>Ctrl+L C</kbd> to copy active line link to clipboard.
4750

48-
**Configure custom github domain**
51+
**Context menu**
52+
53+
Right click on explorer item and choose `Open in GitHub` or `Copy GitHub link to clipboard`.
54+
55+
![](screenshots/context-menu.png?raw=true)
56+
57+
**Configure custom github domain**
4958

5059
Add following line into workspace settings;
5160
```json

package.json

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@
4040
"title": "Copy GitHub link to clipboard"
4141
}
4242
],
43-
"configuration": {
44-
"type": "object",
45-
"title": "Open in Github extension configuration",
46-
"properties": {
47-
"openInGitHub.gitHubDomain": {
48-
"type": [
49-
"string"
50-
],
51-
"default": "github.com",
52-
"description": "Configure a custom Github domain. Useful for Github entreprise"
53-
}
54-
}
55-
},
43+
"configuration": {
44+
"type": "object",
45+
"title": "Open in Github extension configuration",
46+
"properties": {
47+
"openInGitHub.gitHubDomain": {
48+
"type": [
49+
"string"
50+
],
51+
"default": "github.com",
52+
"description": "Configure a custom Github domain. Useful for Github entreprise"
53+
}
54+
}
55+
},
5656
"keybindings": [
5757
{
5858
"command": "extension.openInGitHub",
@@ -64,15 +64,25 @@
6464
"key": "ctrl+l c",
6565
"mac": "ctrl+l c"
6666
}
67-
]
67+
],
68+
"menus": {
69+
"explorer/context": [
70+
{
71+
"command": "extension.openInGitHub"
72+
},
73+
{
74+
"command": "extension.copyGitHubLinkToClipboard"
75+
}
76+
]
77+
}
6878
},
6979
"scripts": {
7080
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
7181
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
7282
},
7383
"devDependencies": {
7484
"typescript": "^1.6.2",
75-
"vscode": "0.10.1"
85+
"vscode": "0.11.13"
7686
},
7787
"dependencies": {
7888
"copy-paste": "^1.1.4",

screenshots/context-menu.png

110 KB
Loading

screenshots/copy.png

138 KB
Loading

src/extension.js

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,34 @@ var fs = require('fs');
1212
var git = require('parse-git-config');
1313
var parse = require('github-url-from-git');
1414
var open = require('open');
15-
var copy = require('copy-paste').copy
15+
var copy = require('copy-paste').copy;
1616
var gitRev = require('git-rev-2');
1717

18-
function getGitHubLink(cb) {
18+
function formGitHubLink(parsedUri, branch, subdir, line) {
19+
if (subdir) {
20+
return parsedUri + "/blob/" + branch + subdir + (line ? "#L" + line : "");
21+
}
22+
23+
return parsedUri + "/tree/" + branch;
24+
}
25+
26+
27+
function formBitBucketLink(parsedUri, branch, subdir, line) {
28+
return parsedUri + "/src/" + branch + (subdir ? subdir : "") + (line ? "#cl-" + line : "");
29+
}
30+
31+
function formVisualStudioLink(parsedUri, subdir, branch, line) {
32+
return parsedUri + "#" + (subdir ? "path=" + subdir : "") + "&version=GB" + branch + (line ? "&line=" + line : "");
33+
}
34+
35+
function getGitHubLink(cb, fileFsPath, line) {
1936
var cwd = workspace.rootPath;
2037

2138
git({
2239
cwd: cwd
2340
}, function (err, config) {
24-
var rawUri, parseOpts, lineIndex = 0, parsedUri, branch, editor, selection
25-
, projectName, subdir, gitLink, scUrls, workspaceConfiguration;
41+
var rawUri, parseOpts, parsedUri, branch, projectName,
42+
subdir, gitLink, scUrls, workspaceConfiguration;
2643

2744
workspaceConfiguration = VsCode.workspace.getConfiguration("openInGitHub");
2845
scUrls = {
@@ -49,40 +66,63 @@ function getGitHubLink(cb) {
4966
gitRev.branch( cwd, function (branchErr, branch) {
5067
if (branchErr || !branch)
5168
branch = 'master';
52-
editor = Window.activeTextEditor;
53-
if (editor) {
54-
selection = editor.selection;
55-
56-
lineIndex = selection.active.line + 1;
57-
projectName = parsedUri.substring(parsedUri.lastIndexOf("/") + 1, parsedUri.length);
58-
59-
subdir = editor.document.uri.fsPath.substring(workspace.rootPath.length).replace(/\"/g, "");
60-
61-
if (parsedUri.startsWith(scUrls.github)) {
62-
gitLink = parsedUri + "/blob/" + branch + subdir + "#L" + lineIndex;
63-
} else if (parsedUri.startsWith(scUrls.bitbucket)) {
64-
gitLink = parsedUri + "/src/" + branch + subdir + "#cl-" + lineIndex;
65-
} else if (scUrls.visualstudiocom.test(parsedUri)) {
66-
gitLink = parsedUri + "#path=" + subdir + "&version=GB" + branch;
67-
} else {
68-
Window.showWarningMessage('Unknown Git provider.');
69-
}
69+
70+
projectName = parsedUri.substring(parsedUri.lastIndexOf("/") + 1, parsedUri.length);
71+
72+
subdir = fileFsPath ? fileFsPath.substring(workspace.rootPath.length).replace(/\"/g, "") : undefined;
73+
74+
if (parsedUri.startsWith(scUrls.github)) {
75+
gitLink = formGitHubLink(parsedUri, branch, subdir, line);
76+
} else if (parsedUri.startsWith(scUrls.bitbucket)) {
77+
gitLink = formBitBucketLink(parsedUri, branch, subdir, line);
78+
} else if (scUrls.visualstudiocom.test(parsedUri)) {
79+
gitLink = formVisualStudioLink(parsedUri, subdir, branch, line);
7080
} else {
71-
gitLink = gitLink = parsedUri + "/tree/" + branch;
81+
Window.showWarningMessage('Unknown Git provider.');
7282
}
7383

74-
if (gitLink)
75-
cb(gitLink);
84+
if (gitLink)
85+
cb(gitLink);
7686
});
7787
});
7888
}
7989

80-
function openInGitHub() {
81-
getGitHubLink(open);
90+
function getGitHubLinkForFile(fileFsPath, cb) {
91+
getGitHubLink(cb, fileFsPath);
92+
}
93+
94+
function getGitHubLinkForCurrentEditorLine(cb) {
95+
var editor = Window.activeTextEditor;
96+
if (editor) {
97+
var lineIndex = editor.selection.active.line + 1;
98+
var fileFsPath = editor.document.uri.fsPath;
99+
getGitHubLink(cb, fileFsPath, lineIndex);
100+
}
101+
}
102+
103+
function getGitHubLinkForRepo(cb) {
104+
getGitHubLink(cb);
105+
}
106+
107+
function branchOnCallingContext(contexMenuParam, cb){
108+
if (contexMenuParam !== undefined) {
109+
fileFsPath = contexMenuParam._fsPath;
110+
getGitHubLinkForFile(fileFsPath, cb);
111+
}
112+
else if (Window.activeTextEditor) {
113+
getGitHubLinkForCurrentEditorLine(cb);
114+
}
115+
else {
116+
getGitHubLinkForRepo(cb);
117+
}
118+
}
119+
120+
function openInGitHub(contexMenuParam) {
121+
branchOnCallingContext(contexMenuParam, open);
82122
}
83123

84-
function copyGitHubLinkToClipboard() {
85-
getGitHubLink(copy);
124+
function copyGitHubLinkToClipboard(contexMenuParam) {
125+
branchOnCallingContext(contexMenuParam, copy);
86126
}
87127

88128
function activate(context) {

0 commit comments

Comments
 (0)