Skip to content

Commit

Permalink
rework onKey to allow for more binding keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dlants committed Dec 21, 2024
1 parent 679aa8a commit 88a27cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
30 changes: 23 additions & 7 deletions rplugin/node/magenta/src/magenta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}`);
}
}
}

Expand Down Expand Up @@ -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",
() => {
Expand All @@ -134,8 +154,4 @@ module.exports = (plugin: NvimPlugin) => {
pattern: "*",
},
);

context.plugin.registerFunction("MagentaOnEnter", () => {
init?.magenta.onKey("Enter");
});
};
6 changes: 5 additions & 1 deletion rplugin/node/magenta/src/tea/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<CR>",
} as const;

export type BindingKey = keyof typeof BINDING_KEYS;
export type Bindings = Partial<{
[key in BindingKey]: () => void;
}>;
Expand Down
19 changes: 11 additions & 8 deletions rplugin/node/magenta/src/tea/tea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: Msg) => void;

Expand Down Expand Up @@ -204,13 +204,16 @@ export function createApp<Model, Msg, SubscriptionType extends string>({
props: { currentState, dispatch },
});

await context.nvim.call("nvim_buf_set_keymap", [
mount.buffer.id,
"n",
"<CR>",
":call MagentaOnEnter()<CR>",
{ 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<CR>",
{ noremap: true, silent: true },
]);
}

return {
getMountedNode() {
Expand Down

0 comments on commit 88a27cf

Please sign in to comment.