MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / registerCommandPaletteEx

Function registerCommandPaletteEx

packages/app-core/src/components/Editor.tsx:976–1028  ·  view source on GitHub ↗

* Bridge every command from the palette registry into the `:` ex line so * the keyboard-first experience is comprehensive — any action the palette * exposes can be invoked directly by typing its kebab-cased id. Plus a * catch-all `:cmd ` that fuzzy-matches against title/keywords and * run

()

Source from the content-addressed store, hash-verified

974 * runs the top match (opens the full palette when the query is empty).
975 */
976function registerCommandPaletteEx(): void {
977 const runCommand = (cmd: Command): void => {
978 // Re-check `when` at invocation time so `:note-save` doesn't silently
979 // fire when nothing is selected, for example.
980 if (cmd.when && !cmd.when()) return
981 void cmd.run()
982 }
983
984 const names = new Set<string>(MANUAL_EX_NAMES)
985 for (const cmd of buildCommands()) {
986 const name = commandIdToExName(cmd.id)
987 if (names.has(name)) continue
988 names.add(name)
989 try {
990 Vim.defineEx(name, name, () => runCommand(cmd))
991 } catch {
992 /* ignore duplicate registrations across HMR cycles */
993 }
994 }
995
996 // `:cmd` — fuzzy fallback. With a query, runs the best match directly.
997 // Without, opens the command palette so the user can browse.
998 Vim.defineEx(
999 'cmd',
1000 'cmd',
1001 (_cm: unknown, params: { argString?: string } | undefined) => {
1002 const query = (params?.argString ?? '').trim()
1003 if (!query) {
1004 useStore.getState().setCommandPaletteOpen(true)
1005 return
1006 }
1007 const commands = buildCommands()
1008 const ranked = rankItems(commands, query, [
1009 { get: (c) => c.title, weight: 1 },
1010 { get: (c) => c.keywords ?? '', weight: 0.6 },
1011 { get: (c) => c.category, weight: 0.4 }
1012 ])
1013 const first = ranked.find((c) => !c.when || c.when())
1014 if (first) runCommand(first)
1015 }
1016 )
1017 names.add('cmd')
1018
1019 // `:commands` — alias that always opens the palette (no implicit run).
1020 Vim.defineEx('commands', 'commands', () => {
1021 useStore.getState().setCommandPaletteOpen(true)
1022 })
1023 names.add('commands')
1024
1025 registeredExNames.splice(0, registeredExNames.length, ...names)
1026 registeredExNames.sort()
1027 installExTabCompletion()
1028}
1029
1030let exTabListenerInstalled = false
1031

Callers 1

registerVimCommandsFunction · 0.85

Calls 5

buildCommandsFunction · 0.90
rankItemsFunction · 0.90
commandIdToExNameFunction · 0.85
installExTabCompletionFunction · 0.85
runCommandFunction · 0.70

Tested by

no test coverage detected