Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kvoon3 committed Aug 5, 2024
1 parent e42fba7 commit 65e3491
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 39 deletions.
11 changes: 10 additions & 1 deletion fixtures/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"leader"
],
"commands": [
"whichkeyConfigGen.show"
"whichKeyConfigGen.show"
]
},
{
Expand All @@ -15,6 +15,7 @@
"c",
"p"
],
"names": ["Changes...", "Previous Changes..."],
"commands": [
"workbench.action.editor.previousChange"
]
Expand All @@ -25,6 +26,7 @@
"c",
"N"
],
"names": ["Changes...", "Previous Changes..."],
"commands": [
"workbench.action.editor.previousChange"
]
Expand All @@ -35,6 +37,7 @@
"c",
"n"
],
"names": ["Changes...", "Next Changes..."],
"commands": [
"workbench.action.editor.nextChange"
]
Expand All @@ -45,6 +48,7 @@
"e",
"n"
],
"names": ["Errors...", "Next Errors..."],
"commands": [
"go-to-next-error.next.error"
]
Expand All @@ -55,6 +59,7 @@
"e",
"N"
],
"names": ["Errors...", "Previous Errors..."],
"commands": [
"go-to-next-error.prev.error"
]
Expand All @@ -65,6 +70,7 @@
"e",
"p"
],
"names": ["Errors...", "Previous Errors..."],
"commands": [
"go-to-next-error.prev.error"
]
Expand All @@ -75,6 +81,7 @@
"w",
"n"
],
"names": ["Warnings...", "Next Warnings..."],
"commands": [
"go-to-next-error.next.warning"
]
Expand All @@ -85,6 +92,7 @@
"w",
"N"
],
"names": ["Warnings...", "Previous Warnings..."],
"commands": [
"go-to-next-error.prev.warning"
]
Expand All @@ -95,6 +103,7 @@
"w",
"p"
],
"names": ["Warnings...", "Previous Warnings..."],
"commands": [
"go-to-next-error.prev.warning"
]
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
{
"title": "Show Generated Which Key",
"command": "whichKeyConfigGen.gen"
},
{
"title": "Update Which Key Config",
"command": "whichKeyConfigGen.updateConfig"
}
],
"configuration": {
Expand Down
36 changes: 24 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,38 @@ import { trans } from './utils'
import { scopedConfigs } from './generated/meta'

Check failure on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module './generated/meta' or its corresponding type declarations.
import * as Meta from './generated/meta'

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module './generated/meta' or its corresponding type declarations.

export const { activate, deactivate } = defineExtension(() => {
logger.show()
function updateConfig() {
const vimConfigs = workspace.getConfiguration('vim')
const nnoremaps = vimConfigs.inspect<VimKeybinding[]>('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))
})
})
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export type WhichKeyItem = WhichKeyBinding | WhichKeyCommand
export interface VimKeybinding {
before: string[]
commands: [string]
// custom attr
names?: string[]
}
56 changes: 30 additions & 26 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand All @@ -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[])
Expand Down
12 changes: 12 additions & 0 deletions test/.output/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,74 @@
{
"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"
}
]
},
{
"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"
}
]
},
{
"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"
}
]
Expand Down

0 comments on commit 65e3491

Please sign in to comment.