1
1
import { deferred , Denops } from "../deps.ts" ;
2
2
import * as anonymous from "../anonymous/mod.ts" ;
3
+ import { batch } from "../batch/mod.ts" ;
3
4
import { load } from "./load.ts" ;
4
5
5
6
/**
@@ -23,6 +24,44 @@ export function echo(denops: Denops, message: string): Promise<void> {
23
24
}
24
25
}
25
26
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
+
26
65
async function echoVim ( denops : Denops , message : string ) : Promise < void > {
27
66
await load ( denops , new URL ( "./echo.vim" , import . meta. url ) ) ;
28
67
const waiter = deferred < void > ( ) ;
0 commit comments