Skip to content

Commit 758cc3c

Browse files
authored
Merge pull request #119 from vim-denops/feat-helper
Add `echoerr` and `friendlyCall` to `helper` module.
2 parents cee661d + cc04e7b commit 758cc3c

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

denops_std/helper/README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## Usage
88

9-
### echo
9+
### echo / echoerr
1010

1111
Use `echo()` to show messages on the cmdline area. It is required while Vim
1212
won't show messages reported from channel commands and it won't pause multi-line
@@ -18,10 +18,35 @@ properly show multi-line messages with `echomsg` from asynchronous context.
1818

1919
```typescript
2020
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
21-
import { echo } from "https://deno.land/x/denops_std/helper/mod.ts";
21+
import { echo, echoerr } from "https://deno.land/x/denops_std/helper/mod.ts";
2222

2323
export async function main(denops: Denops): Promise<void> {
2424
await echo(denops, "Hello\nWorld!");
25+
await echoerr(denops, "This is error message");
26+
}
27+
```
28+
29+
### friendlyCall
30+
31+
Use `friendlyCall()` to call given function and print a friendly error message
32+
(without stack trace) on failure. It's mainly designed for `dispatcher`
33+
functions.
34+
35+
Note that it prints a stack trace when denops is running in debug mode.
36+
37+
```typescript
38+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
39+
import { friendlyCall } from "https://deno.land/x/denops_std/helper/mod.ts";
40+
41+
export async function main(denops: Denops): Promise<void> {
42+
denops.dispatcher = {
43+
"say": () => {
44+
return friendlyCall(denops, async () => {
45+
// Do whatever you want.
46+
throw new Error("Some error occured");
47+
});
48+
},
49+
};
2550
}
2651
```
2752

denops_std/helper/echo.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { deferred, Denops } from "../deps.ts";
22
import * as anonymous from "../anonymous/mod.ts";
3+
import { batch } from "../batch/mod.ts";
34
import { load } from "./load.ts";
45

56
/**
@@ -23,6 +24,44 @@ export function echo(denops: Denops, message: string): Promise<void> {
2324
}
2425
}
2526

27+
/**
28+
* Echo message as an error message.
29+
*
30+
* Note that this function just use ErrorMsg highlight and is not equivalent
31+
* to `echoerr` command in Vim/Neovim.
32+
*/
33+
export async function echoerr(denops: Denops, message: string): Promise<void> {
34+
await batch(denops, async (denops) => {
35+
await denops.cmd("echohl ErrorMsg");
36+
await echo(denops, message);
37+
await denops.cmd("echohl None");
38+
});
39+
}
40+
41+
/**
42+
* Call given function and print a friendly error message (without stack trace) on failure.
43+
*
44+
* Print a stack trace when denops is running in debug mode.
45+
*/
46+
export async function friendlyCall(
47+
denops: Denops,
48+
fn: () => Promise<unknown>,
49+
): Promise<unknown> {
50+
if (denops.meta.mode === "debug") {
51+
return await fn();
52+
}
53+
try {
54+
return await fn();
55+
} catch (e: unknown) {
56+
if (e instanceof Error) {
57+
const err: Error = e;
58+
await echoerr(denops, `[${denops.name}]: ${err.message}`);
59+
} else {
60+
throw e;
61+
}
62+
}
63+
}
64+
2665
async function echoVim(denops: Denops, message: string): Promise<void> {
2766
await load(denops, new URL("./echo.vim", import.meta.url));
2867
const waiter = deferred<void>();

0 commit comments

Comments
 (0)