vscode-sublime-keybindings/convert.js

85 строки
2.1 KiB
JavaScript
Исходник Обычный вид История

2016-03-03 18:00:06 +03:00
// Converts sublime key bindings to VS Code key bindings
// TODO this is not complete!! More work to do..
2016-03-03 18:00:06 +03:00
const jsonfile = require('jsonfile');
const lodash = require('lodash');
const vscodeToSublimeMap = require('vscode-sublime-mapping');
2016-03-03 18:00:06 +03:00
const SUBLIME_PATH = 'sublime-mac-keybindings.json';
const PACKAGE_PATH = 'package.json';
2016-03-03 18:00:06 +03:00
class VsCodeKeyBindings {
constructor(vscodeToSublime) {
this.vscodeToSublime = vscodeToSublime;
this.bindings = [];
}
bind(sublimeBinding) {
// {
// "key": "cmd+l",
// "command": "commandId",
// "when": "editorTextFocus"
// }
let binding = {};
// look up sublime key binding
let sublimeKeys = sublime.keys;
// match sublime command to vscode command
// look up vscode to see if it has a "when" context
this.bindings.append(binding);
}
2016-03-03 18:00:06 +03:00
getCommand(target) {
// could optimize here be building a reverse lookup table
for (let command in this.vscodeToSublime) {
let sublimeCommands = this.vscodeToSublime[command];
for (let sublime in sublimeCommands) {
if (equals(sublime, target)) {
return command;
}
}
}
}
}
2016-03-03 18:00:06 +03:00
function equals(sublimeX, sublimeY) {
if (sublimeX.command != sublimeY.command) {
return false;
}
if (!('args' in sublimeX) && !('args' in sublimeY)) {
return true;
}
return lodash.isEqual(sublimeX.args, sublimeY.args);
}
2016-03-03 18:00:06 +03:00
function writeKeyBindings(bindings) {
var path = PACKAGE_PATH;
jsonfile.readFile(path, (err, data) => {
data.contributions = {
"keybindings": bindings
};
jsonfile.writeFile(path, data, { spaces: 2 }, (err) => {
console.error(err);
});
});
}
}
2016-03-03 18:00:06 +03:00
const bindings = jsonfile.readFileSync(SUBLIME_PATH);
const vscode = VsCodeKeyBindings(vscodeToSublimeMap);
2016-03-03 18:00:06 +03:00
sublime.bindings.forEach(vscode.bind);
writeKeyBindings(vscode.bindings);
2016-03-03 18:00:06 +03:00