Skip to content

Commit 6bb4c0c

Browse files
committed
notify nvim on unexpected errors
1 parent bef1a4a commit 6bb4c0c

File tree

6 files changed

+48
-28
lines changed

6 files changed

+48
-28
lines changed

bun/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { attach, type LogLevel } from "bunvim";
22
import { Magenta } from "./magenta.ts";
3+
import { notifyErr } from "./nvim/nvim.ts";
34

45
// These values are set by neovim when starting the bun process
56
const ENV = {
@@ -16,6 +17,8 @@ const nvim = await attach({
1617
});
1718

1819
process.on("uncaughtException", (error) => {
20+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
21+
notifyErr(nvim, error);
1922
nvim.logger?.error(error);
2023
process.exit(1);
2124
});

bun/magenta.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { pos } from "./tea/view.ts";
66
import type { Nvim } from "bunvim";
77
import { Lsp } from "./lsp.ts";
88
import { PROVIDER_NAMES, type ProviderName } from "./providers/provider.ts";
9-
import { getCurrentBuffer, getcwd, getpos } from "./nvim/nvim.ts";
9+
import { getCurrentBuffer, getcwd, getpos, notifyErr } from "./nvim/nvim.ts";
1010
import path from "node:path";
1111
import type { Line } from "./nvim/buffer.ts";
1212
import { pos1to0, type ByteIdx } from "./nvim/window.ts";
@@ -63,6 +63,8 @@ export class Magenta {
6363
await this.sidebar.updateProvider(provider as ProviderName);
6464
} else {
6565
this.nvim.logger?.error(`Provider ${provider} is not supported.`);
66+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
67+
notifyErr(this.nvim, `Provider ${provider} is not supported.`);
6668
}
6769
break;
6870
}
@@ -187,6 +189,8 @@ ${lines.join("\n")}
187189

188190
default:
189191
this.nvim.logger?.error(`Unrecognized command ${command}\n`);
192+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
193+
notifyErr(this.nvim, `Unrecognized command ${command}\n`);
190194
}
191195
}
192196

@@ -197,6 +201,8 @@ ${lines.join("\n")}
197201
this.mountedChatApp.onKey(key as BindingKey);
198202
} else {
199203
this.nvim.logger?.error(`Unexpected MagentaKey ${JSON.stringify(key)}`);
204+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
205+
notifyErr(this.nvim, `Unexpected MagentaKey ${JSON.stringify(key)}`);
200206
}
201207
}
202208
}
@@ -224,6 +230,8 @@ ${lines.join("\n")}
224230
? `Error executing command ${args[0] as string}: ${err.message}\n${err.stack}`
225231
: JSON.stringify(err),
226232
);
233+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
234+
notifyErr(nvim, err);
227235
}
228236
});
229237

@@ -247,7 +255,6 @@ ${lines.join("\n")}
247255
try {
248256
lsp.onLspResponse(args);
249257
} catch (err) {
250-
console.error(err);
251258
nvim.logger?.error(JSON.stringify(err));
252259
}
253260
});

bun/nvim/nvim.ts

+8
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ export async function getpos(
7373
export function diffthis(nvim: Nvim) {
7474
return nvim.call("nvim_command", ["diffthis"]);
7575
}
76+
77+
export function notifyErr(nvim: Nvim, err: unknown) {
78+
return nvim.call("nvim_notify", [
79+
`Thunk execution error: ${err instanceof Error ? err.message : JSON.stringify(err)}`,
80+
3,
81+
{},
82+
]);
83+
}

bun/tea/tea.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type VDOMNode,
88
} from "./view.ts";
99
import { BINDING_KEYS, type BindingKey, getBindings } from "./bindings.ts";
10-
import { getCurrentWindow } from "../nvim/nvim.ts";
10+
import { getCurrentWindow, notifyErr } from "../nvim/nvim.ts";
1111
import type { Row0Indexed } from "../nvim/window.ts";
1212
import type { Nvim } from "bunvim";
1313

@@ -102,6 +102,9 @@ export function createApp<Model, Msg>({
102102
? `Error: ${err.message}\n${err.stack}`
103103
: JSON.stringify(err);
104104
nvim.logger?.error(`Thunk execution error: ${message}`);
105+
106+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
107+
notifyErr(nvim, err);
105108
});
106109
}
107110
}
@@ -128,6 +131,8 @@ export function createApp<Model, Msg>({
128131
.render({ currentState, dispatch })
129132
.catch((err) => {
130133
nvim.logger?.error(err as Error);
134+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
135+
notifyErr(nvim, err);
131136
throw err;
132137
})
133138
.finally(() => {

bun/tools/listDirectory.ts

+19-24
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const update: Update<Msg, Model> = (msg, model) => {
5656
async function listDirectoryBFS(
5757
startPath: string,
5858
cwd: string,
59-
context: { nvim: Nvim },
6059
): Promise<string[]> {
6160
const ig = await readGitignore(cwd);
6261
const queue: string[] = [startPath];
@@ -66,35 +65,31 @@ async function listDirectoryBFS(
6665
while (queue.length > 0 && results.length < 100) {
6766
const currentPath = queue.shift()!;
6867

69-
try {
70-
const entries = await fs.promises.readdir(currentPath, {
71-
withFileTypes: true,
72-
});
68+
const entries = await fs.promises.readdir(currentPath, {
69+
withFileTypes: true,
70+
});
7371

74-
for (const entry of entries) {
75-
const fullPath = path.join(currentPath, entry.name);
76-
const relativePath = path.relative(cwd, fullPath);
72+
for (const entry of entries) {
73+
const fullPath = path.join(currentPath, entry.name);
74+
const relativePath = path.relative(cwd, fullPath);
7775

78-
// Skip hidden files and respected gitignored files
79-
if (entry.name.startsWith(".") || ig.ignores(relativePath)) {
80-
continue;
81-
}
76+
// Skip hidden files and respected gitignored files
77+
if (entry.name.startsWith(".") || ig.ignores(relativePath)) {
78+
continue;
79+
}
8280

83-
if (!fullPath.startsWith(cwd)) {
84-
continue;
85-
}
81+
if (!fullPath.startsWith(cwd)) {
82+
continue;
83+
}
8684

87-
if (!seen.has(fullPath)) {
88-
seen.add(fullPath);
89-
results.push(relativePath);
85+
if (!seen.has(fullPath)) {
86+
seen.add(fullPath);
87+
results.push(relativePath);
9088

91-
if (entry.isDirectory()) {
92-
queue.push(fullPath);
93-
}
89+
if (entry.isDirectory()) {
90+
queue.push(fullPath);
9491
}
9592
}
96-
} catch (error) {
97-
context.nvim.logger?.error(error as Error);
9893
}
9994
}
10095

@@ -133,7 +128,7 @@ export function initModel(
133128
return;
134129
}
135130

136-
const files = await listDirectoryBFS(absolutePath, cwd, context);
131+
const files = await listDirectoryBFS(absolutePath, cwd);
137132
context.nvim.logger?.debug(`files: ${files.join("\n")}`);
138133
dispatch({
139134
type: "finish",

bun/tools/toolManager.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ ${result.result.value}
231231
) {
232232
return withBindings(
233233
d`${renderToolContents(model.model, dispatch)}${
234-
model.showRequest ? d`\nid: ${model.model.request.id}\n${displayRequestInput(model.model)}` : ""
234+
model.showRequest
235+
? d`\nid: ${model.model.request.id}\n${displayRequestInput(model.model)}`
236+
: ""
235237
}${model.showResult ? displayResult(model.model) : ""}`,
236238
{
237239
"<CR>": () =>

0 commit comments

Comments
 (0)