diff --git a/rplugin/node/magenta/src/magenta.ts b/rplugin/node/magenta/src/magenta.ts index 396b568..9cc43b9 100644 --- a/rplugin/node/magenta/src/magenta.ts +++ b/rplugin/node/magenta/src/magenta.ts @@ -4,7 +4,7 @@ import * as Chat from "./chat/chat.ts"; import { Logger } from "./logger.ts"; import { App, createApp, MountedApp } from "./tea/tea.ts"; import { setContext, context } from "./context.ts"; -import { BindingKey } from "./tea/bindings.ts"; +import { BINDING_KEYS, BindingKey } from "./tea/bindings.ts"; import { pos } from "./tea/view.ts"; class Magenta { @@ -71,9 +71,14 @@ class Magenta { } } - onKey(key: BindingKey) { + onKey(args: string[]) { + const key = args[0]; if (this.mountedChatApp) { - this.mountedChatApp.onKey(key); + if (BINDING_KEYS[key as BindingKey]) { + this.mountedChatApp.onKey(key as BindingKey); + } else { + context.logger.error(`Unexpected MagentaKey ${key}`); + } } } @@ -123,6 +128,21 @@ module.exports = (plugin: NvimPlugin) => { }, ); + plugin.registerCommand( + "MagentaKey", + (args: string[]) => { + try { + const magenta = init!.magenta; + magenta.onKey(args); + } catch (err) { + init!.logger.error(err as Error); + } + }, + { + nargs: "1", + }, + ); + plugin.registerAutocmd( "WinClosed", () => { @@ -134,8 +154,4 @@ module.exports = (plugin: NvimPlugin) => { pattern: "*", }, ); - - context.plugin.registerFunction("MagentaOnEnter", () => { - init?.magenta.onKey("Enter"); - }); }; diff --git a/rplugin/node/magenta/src/tea/bindings.ts b/rplugin/node/magenta/src/tea/bindings.ts index 65880fb..52d2cc9 100644 --- a/rplugin/node/magenta/src/tea/bindings.ts +++ b/rplugin/node/magenta/src/tea/bindings.ts @@ -2,7 +2,11 @@ import { MountedVDOM, Position } from "./view.ts"; import { assertUnreachable } from "../utils/assertUnreachable.ts"; import { context } from "../context.ts"; -export type BindingKey = "Enter"; +export const BINDING_KEYS = { + Enter: "", +} as const; + +export type BindingKey = keyof typeof BINDING_KEYS; export type Bindings = Partial<{ [key in BindingKey]: () => void; }>; diff --git a/rplugin/node/magenta/src/tea/tea.ts b/rplugin/node/magenta/src/tea/tea.ts index f2ddbcc..5e07b49 100644 --- a/rplugin/node/magenta/src/tea/tea.ts +++ b/rplugin/node/magenta/src/tea/tea.ts @@ -9,7 +9,7 @@ import { VDOMNode, } from "./view.ts"; import { context } from "../context.ts"; -import { BindingKey, getBindings } from "./bindings.ts"; +import { BINDING_KEYS, BindingKey, getBindings } from "./bindings.ts"; export type Dispatch = (msg: Msg) => void; @@ -204,13 +204,16 @@ export function createApp({ props: { currentState, dispatch }, }); - await context.nvim.call("nvim_buf_set_keymap", [ - mount.buffer.id, - "n", - "", - ":call MagentaOnEnter()", - { noremap: true, silent: true }, - ]); + for (const key in BINDING_KEYS) { + const vimKey = BINDING_KEYS[key as BindingKey]; + await context.nvim.call("nvim_buf_set_keymap", [ + mount.buffer.id, + "n", + vimKey, + ":MagentaKey Enter", + { noremap: true, silent: true }, + ]); + } return { getMountedNode() {