|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Atom++ — Hackability: keymap presets |
| 3 | + * |
| 4 | + * Apply an Atom or Notepad++ keybinding preset so muscle memory transfers. A preset is |
| 5 | + * written into the user's keybindings.json (backed up first); VS Code live-reloads it, so |
| 6 | + * no restart is needed. We only override the bindings that DIFFER from the defaults — the |
| 7 | + * many shortcuts Atom/NPP already share with VS Code are left untouched. |
| 8 | + *--------------------------------------------------------------------------------------------*/ |
| 9 | +// @ts-check |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const vscode = require('vscode'); |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | + |
| 16 | +const EDIT = 'editorTextFocus && !editorReadonly'; |
| 17 | + |
| 18 | +// Atom keymap — only the bindings that differ from VS Code's mac defaults. |
| 19 | +const ATOM = [ |
| 20 | + { key: 'cmd+t', command: 'workbench.action.quickOpen' }, |
| 21 | + { key: 'cmd+\\', command: 'workbench.action.toggleSidebarVisibility' }, |
| 22 | + { key: 'cmd+k cmd+up', command: 'workbench.action.splitEditorUp' }, |
| 23 | + { key: 'cmd+k cmd+down', command: 'workbench.action.splitEditorDown' }, |
| 24 | + { key: 'cmd+k cmd+left', command: 'workbench.action.splitEditorLeft' }, |
| 25 | + { key: 'cmd+k cmd+right', command: 'workbench.action.splitEditorRight' }, |
| 26 | + { key: 'cmd+r', command: 'workbench.action.gotoSymbol' }, |
| 27 | + { key: 'cmd+shift+r', command: 'workbench.action.showAllSymbols' }, |
| 28 | + { key: 'ctrl+cmd+up', command: 'editor.action.moveLinesUpAction', when: EDIT }, |
| 29 | + { key: 'ctrl+cmd+down', command: 'editor.action.moveLinesDownAction', when: EDIT }, |
| 30 | + { key: 'cmd+shift+d', command: 'editor.action.copyLinesDownAction', when: EDIT } |
| 31 | +]; |
| 32 | + |
| 33 | +// Notepad++ keymap — Windows-style Ctrl bindings (scoped to the editor so they don't |
| 34 | +// fight macOS text navigation outside it). |
| 35 | +const NPP = [ |
| 36 | + { key: 'ctrl+d', command: 'editor.action.copyLinesDownAction', when: EDIT }, |
| 37 | + { key: 'ctrl+shift+up', command: 'editor.action.moveLinesUpAction', when: EDIT }, |
| 38 | + { key: 'ctrl+shift+down', command: 'editor.action.moveLinesDownAction', when: EDIT }, |
| 39 | + { key: 'ctrl+g', command: 'workbench.action.gotoLine' }, |
| 40 | + { key: 'ctrl+q', command: 'editor.action.commentLine', when: EDIT }, |
| 41 | + { key: 'ctrl+shift+q', command: 'editor.action.blockComment', when: EDIT }, |
| 42 | + { key: 'ctrl+j', command: 'editor.action.joinLines', when: EDIT }, |
| 43 | + { key: 'ctrl+h', command: 'editor.action.startFindReplaceAction', when: 'editorFocus' }, |
| 44 | + { key: 'ctrl+shift+f', command: 'workbench.action.findInFiles' }, |
| 45 | + { key: 'f3', command: 'editor.action.nextMatchFindAction', when: 'editorFocus' }, |
| 46 | + { key: 'shift+f3', command: 'editor.action.previousMatchFindAction', when: 'editorFocus' }, |
| 47 | + { key: 'ctrl+w', command: 'workbench.action.closeActiveEditor' } |
| 48 | +]; |
| 49 | + |
| 50 | +const PRESETS = { |
| 51 | + atom: { label: 'Atom', bindings: ATOM, note: '' }, |
| 52 | + npp: { label: 'Notepad++', bindings: NPP, note: 'Uses Windows-style Ctrl bindings — they apply inside the editor, so macOS Ctrl text-navigation still works elsewhere.' }, |
| 53 | + default: { label: 'VS Code default', bindings: [], note: 'Cleared the preset — your keybindings.json is now empty (defaults apply).' } |
| 54 | +}; |
| 55 | + |
| 56 | +/** keybindings.json lives next to globalStorage, two levels up: <userData>/User/keybindings.json */ |
| 57 | +function keybindingsPath(context) { |
| 58 | + const userDir = path.dirname(path.dirname(context.globalStorageUri.fsPath)); |
| 59 | + return { userDir, file: path.join(userDir, 'keybindings.json') }; |
| 60 | +} |
| 61 | + |
| 62 | +/** @param {vscode.ExtensionContext} context */ |
| 63 | +async function applyKeymap(context) { |
| 64 | + /** @type {any[]} */ |
| 65 | + const items = [ |
| 66 | + { label: '$(star) Atom', description: '⌘T finder · ⌘\\ tree · ⌃⌘↑↓ move lines · ⌘R symbols · ⌘⇧D duplicate', _key: 'atom' }, |
| 67 | + { label: '$(keyboard) Notepad++', description: 'Windows-style Ctrl bindings (Ctrl+D, Ctrl+G, Ctrl+Q, F3, …)', _key: 'npp' }, |
| 68 | + { label: '$(discard) VS Code default', description: 'Clear the preset (empty keybindings.json)', _key: 'default' } |
| 69 | + ]; |
| 70 | + const pick = await vscode.window.showQuickPick(items, { |
| 71 | + placeHolder: 'Apply a keymap preset — overwrites keybindings.json (a backup is saved first)' |
| 72 | + }); |
| 73 | + if (!pick) { return; } |
| 74 | + const preset = PRESETS[pick._key]; |
| 75 | + |
| 76 | + const proceed = await vscode.window.showWarningMessage( |
| 77 | + `Apply the ${preset.label} keymap? This replaces your keybindings.json. A timestamped backup is saved first.`, |
| 78 | + { modal: true }, 'Apply' |
| 79 | + ); |
| 80 | + if (proceed !== 'Apply') { return; } |
| 81 | + |
| 82 | + const { userDir, file } = keybindingsPath(context); |
| 83 | + try { |
| 84 | + fs.mkdirSync(userDir, { recursive: true }); |
| 85 | + // Back up any existing non-empty keybindings. |
| 86 | + if (fs.existsSync(file)) { |
| 87 | + const cur = fs.readFileSync(file, 'utf8').trim(); |
| 88 | + if (cur && cur !== '[]') { |
| 89 | + fs.copyFileSync(file, path.join(userDir, 'keybindings.backup-' + Date.now() + '.json')); |
| 90 | + } |
| 91 | + } |
| 92 | + const header = '// Atom++ — ' + preset.label + ' keymap preset.\n' + |
| 93 | + '// Applied via "Atom++: Apply Keymap Preset". Edit freely; re-apply any time.\n'; |
| 94 | + fs.writeFileSync(file, header + JSON.stringify(preset.bindings, null, 2) + '\n', 'utf8'); |
| 95 | + } catch (e) { |
| 96 | + vscode.window.showErrorMessage('Atom++: could not write keybindings.json: ' + ((e && e.message) || e)); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const open = 'Open Keybindings'; |
| 101 | + const msg = `Atom++: applied the ${preset.label} keymap.` + (preset.note ? ' ' + preset.note : ''); |
| 102 | + vscode.window.showInformationMessage(msg, open).then((c) => { |
| 103 | + if (c === open) { vscode.commands.executeCommand('workbench.action.openGlobalKeybindingsFile'); } |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +function registerKeymaps(context) { |
| 108 | + context.subscriptions.push( |
| 109 | + vscode.commands.registerCommand('atompp.keymap.apply', () => applyKeymap(context)) |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +module.exports = { registerKeymaps }; |
0 commit comments