diff --git a/fixtures/settings.json b/fixtures/settings.json index 40493ba..90aa1dd 100644 --- a/fixtures/settings.json +++ b/fixtures/settings.json @@ -6,7 +6,7 @@ "leader" ], "commands": [ - "whichkeyConfigGen.show" + "whichKeyConfigGen.show" ] }, { @@ -15,6 +15,7 @@ "c", "p" ], + "names": ["Changes...", "Previous Changes..."], "commands": [ "workbench.action.editor.previousChange" ] @@ -25,6 +26,7 @@ "c", "N" ], + "names": ["Changes...", "Previous Changes..."], "commands": [ "workbench.action.editor.previousChange" ] @@ -35,6 +37,7 @@ "c", "n" ], + "names": ["Changes...", "Next Changes..."], "commands": [ "workbench.action.editor.nextChange" ] @@ -45,6 +48,7 @@ "e", "n" ], + "names": ["Errors...", "Next Errors..."], "commands": [ "go-to-next-error.next.error" ] @@ -55,6 +59,7 @@ "e", "N" ], + "names": ["Errors...", "Previous Errors..."], "commands": [ "go-to-next-error.prev.error" ] @@ -65,6 +70,7 @@ "e", "p" ], + "names": ["Errors...", "Previous Errors..."], "commands": [ "go-to-next-error.prev.error" ] @@ -75,6 +81,7 @@ "w", "n" ], + "names": ["Warnings...", "Next Warnings..."], "commands": [ "go-to-next-error.next.warning" ] @@ -85,6 +92,7 @@ "w", "N" ], + "names": ["Warnings...", "Previous Warnings..."], "commands": [ "go-to-next-error.prev.warning" ] @@ -95,6 +103,7 @@ "w", "p" ], + "names": ["Warnings...", "Previous Warnings..."], "commands": [ "go-to-next-error.prev.warning" ] diff --git a/package.json b/package.json index fbb7820..ce094e7 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,10 @@ { "title": "Show Generated Which Key", "command": "whichKeyConfigGen.gen" + }, + { + "title": "Update Which Key Config", + "command": "whichKeyConfigGen.updateConfig" } ], "configuration": { diff --git a/src/index.ts b/src/index.ts index edb0320..ff2fe75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,26 +6,38 @@ import { trans } from './utils' import { scopedConfigs } from './generated/meta' import * as Meta from './generated/meta' -export const { activate, deactivate } = defineExtension(() => { - logger.show() +function updateConfig() { const vimConfigs = workspace.getConfiguration('vim') const nnoremaps = vimConfigs.inspect('normalModeKeyBindingsNonRecursive')?.globalValue if (nnoremaps) { const bindings = trans(nnoremaps) - workspace + return workspace .getConfiguration(scopedConfigs.scope) .update('bindings', bindings, ConfigurationTarget.Global) .then(() => { - logger.info(JSON.stringify(bindings, null, 2)) - - commands.executeCommand('whichkey.register', { - bindings: [Meta.scopedConfigs.scope, 'bindings'], - overrides: [Meta.scopedConfigs.scope, 'bindingOverrides'], - title: 'Genrated whichkey config', - }) - - useCommand(Meta.commands.show, () => commands.executeCommand('whichkey.show', Meta.configs.bindings.key)) + return bindings }) } + else { + return Promise.reject(new Error('no nnoremaps')) + } +} + +export const { activate, deactivate } = defineExtension(async () => { + logger.show() + + const bindings = await updateConfig() + logger.info(JSON.stringify(bindings, null, 2)) + commands.executeCommand('whichkey.register', { + bindings: [Meta.scopedConfigs.scope, 'bindings'], + overrides: [Meta.scopedConfigs.scope, 'bindingOverrides'], + title: 'Genrated whichkey config', + }) + + useCommand(Meta.commands.show, () => commands.executeCommand('whichkey.show', Meta.configs.bindings.key)) + useCommand(Meta.commands.updateConfig, async () => { + const bindings = await updateConfig() + logger.info(JSON.stringify(bindings, null, 2)) + }) }) diff --git a/src/types.ts b/src/types.ts index 9fbed24..0cca7b9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,4 +17,6 @@ export type WhichKeyItem = WhichKeyBinding | WhichKeyCommand export interface VimKeybinding { before: string[] commands: [string] + // custom attr + names?: string[] } diff --git a/src/utils.ts b/src/utils.ts index 8dee072..2fc5d5d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,47 +7,50 @@ export function findParent( nodes: WhichKeyItem[], keys: string[], maybeParent: WhichKeyBinding | 'root' = 'root', -): { parent: WhichKeyBinding | 'root', restKeys: string[] } { + names: string[] = [], +): { parent: WhichKeyBinding | 'root', restKeys: string[], restNames: string[] } { const [key, ...restKeys] = keys + const [_, ...restNames] = names const target = nodes.find(i => i.key === key) if (!target) - return { parent: maybeParent, restKeys: keys } + return { parent: maybeParent, restKeys: keys, restNames: names } // target: sibling if (isCommand(target)) - return { parent: maybeParent, restKeys: keys } + return { parent: maybeParent, restKeys: keys, restNames: names } if (isBinding(target)) { if (restKeys.length === 0) - return { parent: maybeParent, restKeys: [] } + return { parent: maybeParent, restKeys: [], restNames: [] } // target: parent if (restKeys.length === 1) - return { parent: target, restKeys } + return { parent: target, restKeys, restNames } // target: ancestor else - return findParent(target.bindings, restKeys, target) + return findParent(target.bindings, restKeys, target, restNames) } - return { parent: 'root', restKeys: keys } + return { parent: 'root', restKeys: keys, restNames: names } } -export function genWhichKeyTree(keys: string[], command: string): WhichKeyItem { - if (keys.length === 1) { - return { - key: keys[0], - type: 'command', - command, - } as WhichKeyCommand - } - +export function genWhichKeyTree(keys: string[], command: string, names: string[] = []): WhichKeyItem { const [key, ...restKey] = keys + const [name, ...restName] = names - return { - key, - type: 'bindings', - bindings: [genWhichKeyTree(restKey, command)], - } as WhichKeyBinding + return restKey.length === 0 + ? { + key, + type: 'command', + name, + command, + } + : { + key, + type: 'bindings', + name, + bindings: [genWhichKeyTree(restKey, command, restName)], + } } export function trans(vimKeybindings: VimKeybinding[]): WhichKeyItem[] { @@ -58,16 +61,17 @@ export function trans(vimKeybindings: VimKeybinding[]): WhichKeyItem[] { && keybinding.before[0] === 'leader', ) .reduce((whichKeyBindings, vimKeybinding) => { - const [_, ...keys2bind] = vimKeybinding.before - const [command] = vimKeybinding.commands + const { before: keys, commands, names } = vimKeybinding + const [_, ...keys4bind] = keys + const [command] = commands - const { parent, restKeys } = findParent(whichKeyBindings, keys2bind) + const { parent, restKeys, restNames } = findParent(whichKeyBindings, keys4bind, 'root', names) if (parent === 'root') { - return [...whichKeyBindings, genWhichKeyTree(restKeys, command)] + return [...whichKeyBindings, genWhichKeyTree(restKeys, command, restNames)] } else { - parent.bindings.push(genWhichKeyTree(restKeys, command)) + parent.bindings.push(genWhichKeyTree(restKeys, command, restNames)) return whichKeyBindings } }, [] as WhichKeyItem[]) diff --git a/test/.output/settings.json b/test/.output/settings.json index d099b2c..8a030a2 100644 --- a/test/.output/settings.json +++ b/test/.output/settings.json @@ -2,20 +2,24 @@ { "key": "c", "type": "bindings", + "name": "Changes...", "bindings": [ { "key": "p", "type": "command", + "name": "Previous Changes...", "command": "workbench.action.editor.previousChange" }, { "key": "N", "type": "command", + "name": "Previous Changes...", "command": "workbench.action.editor.previousChange" }, { "key": "n", "type": "command", + "name": "Next Changes...", "command": "workbench.action.editor.nextChange" } ] @@ -23,20 +27,24 @@ { "key": "e", "type": "bindings", + "name": "Errors...", "bindings": [ { "key": "n", "type": "command", + "name": "Next Errors...", "command": "go-to-next-error.next.error" }, { "key": "N", "type": "command", + "name": "Previous Errors...", "command": "go-to-next-error.prev.error" }, { "key": "p", "type": "command", + "name": "Previous Errors...", "command": "go-to-next-error.prev.error" } ] @@ -44,20 +52,24 @@ { "key": "w", "type": "bindings", + "name": "Warnings...", "bindings": [ { "key": "n", "type": "command", + "name": "Next Warnings...", "command": "go-to-next-error.next.warning" }, { "key": "N", "type": "command", + "name": "Previous Warnings...", "command": "go-to-next-error.prev.warning" }, { "key": "p", "type": "command", + "name": "Previous Warnings...", "command": "go-to-next-error.prev.warning" } ]