Skip to content

Commit 356b0fa

Browse files
authored
Merge pull request #176 from vim-denops/fix-echoerr
🐛 Fix `echoerr`
2 parents 0b3c937 + 0515eb5 commit 356b0fa

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

denops_std/helper/echo.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Denops } from "https://deno.land/x/denops_core@v4.0.0/mod.ts";
22
import { execute } from "./execute.ts";
3-
import { batch } from "../batch/mod.ts";
43
import { generateUniqueString } from "../util.ts";
54

65
const cacheKey = Symbol("denops_std/helper/echo");
@@ -16,13 +15,15 @@ async function ensurePrerequisites(denops: Denops): Promise<string> {
1615
let g:loaded_denops_std_helper_echo_${suffix} = 1
1716
let s:denops_std_helper_echo_timer = 0
1817
19-
function! DenopsStdHelperEcho_${suffix}(message) abort
18+
function! DenopsStdHelperEcho_${suffix}(message, highlight) abort
2019
call timer_stop(s:denops_std_helper_echo_timer)
21-
let s:denops_std_helper_echo_timer = timer_start(0, { -> s:DenopsStdHelperEchoInternal_${suffix}(a:message) })
20+
let s:denops_std_helper_echo_timer = timer_start(0, { -> s:DenopsStdHelperEchoInternal_${suffix}(a:message, a:highlight) })
2221
endfunction
2322
24-
function! s:DenopsStdHelperEchoInternal_${suffix}(message) abort
23+
function! s:DenopsStdHelperEchoInternal_${suffix}(message, highlight) abort
24+
if a:highlight !=# '' | execute 'echohl' a:highlight | endif
2525
redraw | echo a:message
26+
if a:highlight !=# '' | echohl None | endif
2627
endfunction
2728
`;
2829
await execute(denops, script);
@@ -201,11 +202,7 @@ export async function echoerr(
201202
if (denops.meta.mode === "test") {
202203
return Promise.resolve();
203204
} else {
204-
await batch(denops, async (denops) => {
205-
await denops.cmd("echohl ErrorMsg");
206-
await echoInternal(denops, message);
207-
await denops.cmd("echohl None");
208-
});
205+
await echoInternal(denops, message, "ErrorMsg");
209206
}
210207
}
211208

@@ -250,7 +247,11 @@ export async function friendlyCall(
250247
}
251248
}
252249

253-
async function echoInternal(denops: Denops, message: string): Promise<void> {
250+
async function echoInternal(
251+
denops: Denops,
252+
message: string,
253+
highlight?: string,
254+
): Promise<void> {
254255
const suffix = await ensurePrerequisites(denops);
255-
await denops.call(`DenopsStdHelperEcho_${suffix}`, message);
256+
await denops.call(`DenopsStdHelperEcho_${suffix}`, message, highlight ?? "");
256257
}

denops_std/helper/echo_test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from "https://deno.land/x/denops_test@v1.0.1/mod.ts";
2-
import { echo } from "./echo.ts";
2+
import { echo, echoerr } from "./echo.ts";
33

44
test({
55
mode: "all",
@@ -22,3 +22,25 @@ test({
2222
});
2323
},
2424
});
25+
26+
test({
27+
mode: "all",
28+
name: "echoerr()",
29+
fn: async (denops, t) => {
30+
await t.step({
31+
name: "shows short message",
32+
fn: async () => {
33+
await echoerr(denops, "Hello");
34+
// No way to test if `echoerr` success
35+
},
36+
});
37+
await t.step({
38+
name: "shows multiline message",
39+
fn: async () => {
40+
await denops.cmd("set cmdheight=5");
41+
await echoerr(denops, "A\nB\nC\nD\nE");
42+
// No way to test if `echoerr` success
43+
},
44+
});
45+
},
46+
});

0 commit comments

Comments
 (0)