git-history/vscode-ext/extension.js

106 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2019-02-13 23:49:46 +03:00
const vscode = require("vscode");
const path = require("path");
const fs = require("fs");
2019-02-14 07:05:16 +03:00
const getCommits = require("./git");
2019-02-13 23:49:46 +03:00
// 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
2019-02-16 04:24:05 +03:00
try {
const currentPath = getCurrentPath();
if (!currentPath) {
vscode.window.showInformationMessage("No active file");
return;
2019-02-13 23:49:46 +03:00
}
2019-02-16 04:24:05 +03:00
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"
);
2019-02-13 23:49:46 +03:00
2019-02-16 04:24:05 +03:00
const index = fs.readFileSync(indexPath, "utf-8");
2019-02-18 20:53:46 +03:00
const newIndex = index
.replace(
"<body>",
2019-02-21 22:26:02 +03:00
`<body><script>/*<!--*/window.vscode=acquireVsCodeApi();window._PATH=${JSON.stringify(
currentPath
)}/*-->*/</script>`
2019-02-18 20:53:46 +03:00
)
.replace(
"<head>",
`<head><base href="${vscode.Uri.file(
path.join(context.extensionPath, "site")
).with({
scheme: "vscode-resource"
})}/"/>`
);
2019-02-13 23:49:46 +03:00
2019-02-18 20:53:46 +03:00
panel.webview.html = newIndex;
2019-02-13 23:49:46 +03:00
2019-02-18 20:53:46 +03:00
panel.webview.onDidReceiveMessage(
message => {
switch (message.command) {
case "commits":
2019-02-19 05:47:16 +03:00
const { path, last = 15, before = null } = message.params;
2019-02-18 22:06:59 +03:00
getCommits(path, last, before)
2019-02-18 20:53:46 +03:00
.then(commits => {
panel.webview.postMessage(commits);
})
.catch(console.error);
}
},
undefined,
context.subscriptions
);
2019-02-16 04:24:05 +03:00
} catch (e) {
console.error(e);
throw e;
}
2019-02-13 23:49:46 +03:00
}
);
context.subscriptions.push(disposable);
}
2019-02-14 07:05:16 +03:00
function getCurrentPath() {
return (
vscode.window.activeTextEditor &&
vscode.window.activeTextEditor.document &&
vscode.window.activeTextEditor.document.fileName
);
}
2019-02-13 23:49:46 +03:00
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
};