-
-
Notifications
You must be signed in to change notification settings - Fork 535
/
extension.js
105 lines (94 loc) · 2.93 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const vscode = require("vscode");
const path = require("path");
const fs = require("fs");
const getCommits = require("./git");
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand(
"extension.git-file-history",
function() {
// The code you place here will be executed every time your command is executed
try {
const currentPath = getCurrentPath();
if (!currentPath) {
vscode.window.showInformationMessage("No active file");
return;
}
const panel = vscode.window.createWebviewPanel(
"gfh",
`${path.basename(currentPath)} (Git History)`,
vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [
vscode.Uri.file(path.join(context.extensionPath, "site"))
]
}
);
const indexPath = path.join(
context.extensionPath,
"site",
"index.html"
);
const index = fs.readFileSync(indexPath, "utf-8");
const newIndex = index
.replace(
"<body>",
`<body><script>/*<!--*/window.vscode=acquireVsCodeApi();window._PATH=${JSON.stringify(
currentPath
)}/*-->*/</script>`
)
.replace(
"<head>",
`<head><base href="${vscode.Uri.file(
path.join(context.extensionPath, "site")
).with({
scheme: "vscode-resource"
})}/"/>`
);
panel.webview.html = newIndex;
panel.webview.onDidReceiveMessage(
message => {
switch (message.command) {
case "commits":
const { path, last = 15, before = null } = message.params;
getCommits(path, last, before)
.then(commits => {
panel.webview.postMessage(commits);
})
.catch(console.error);
}
},
undefined,
context.subscriptions
);
} catch (e) {
console.error(e);
throw e;
}
}
);
context.subscriptions.push(disposable);
}
function getCurrentPath() {
return (
vscode.window.activeTextEditor &&
vscode.window.activeTextEditor.document &&
vscode.window.activeTextEditor.document.fileName
);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
};