Skip to content

Commit 9f26f0b

Browse files
authored
🐛 trailing spaces capable in execute() (#163)
1 parent 08b9d76 commit 9f26f0b

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

denops_std/helper/execute.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import type {
66
/**
77
* Execute Vim script directly
88
*/
9-
export async function execute(
9+
export function execute(
1010
denops: Denops,
1111
script: string | string[],
1212
ctx: Context = {},
1313
): Promise<void> {
14-
if (Array.isArray(script)) {
15-
ctx = {
16-
...ctx,
17-
__denops_internal_command: script
18-
.map((x) => x.replace(/^\s+|\s+$/g, ""))
19-
.filter((x) => !!x),
20-
};
21-
await denops.cmd("call execute(l:__denops_internal_command, '')", ctx);
22-
return;
14+
if (!Array.isArray(script)) {
15+
// join line-continuation
16+
script = script.replace(/\r?\n\s*\\/g, "");
17+
// convert to array
18+
script = script.split(/\r?\n/g);
2319
}
24-
script = script.replace(/\r?\n\s*\\/g, "");
25-
await execute(denops, script.split(/\r?\n/g), ctx);
20+
script = script.map((x) => x.trimStart()).filter((x) => !!x);
21+
ctx = {
22+
...ctx,
23+
__denops_internal_command: script,
24+
};
25+
return denops.cmd("call execute(l:__denops_internal_command, '')", ctx);
2626
}

denops_std/helper/execute_test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
assertEquals,
3+
assertInstanceOf,
34
assertRejects,
45
} from "https://deno.land/std@0.167.0/testing/asserts.ts";
56
import { test } from "https://deno.land/x/denops_core@v3.2.2/test/mod.ts";
@@ -75,3 +76,41 @@ test({
7576
assertEquals(await denops.eval("g:denops_std_execute_test") as number, 25);
7677
},
7778
});
79+
80+
test({
81+
mode: "any",
82+
name: "execute() executes Vim script with line-continuation",
83+
fn: async (denops) => {
84+
await execute(
85+
denops,
86+
`
87+
let g:denops_std_execute_test = 1
88+
\\ + 1
89+
`,
90+
);
91+
assertEquals(await denops.eval("g:denops_std_execute_test") as number, 2);
92+
},
93+
});
94+
95+
test({
96+
mode: "any",
97+
name: "execute() executes Vim script with trailing spaces",
98+
fn: async (denops) => {
99+
try {
100+
await execute(denops, "setlocal path=foo\\\\\\ ");
101+
assertEquals(await denops.eval("&l:path") as string, "foo\\ ");
102+
} finally {
103+
await denops.cmd("setlocal path&");
104+
}
105+
},
106+
});
107+
108+
test({
109+
mode: "any",
110+
name: "execute() returns Promise<void>",
111+
fn: async (denops) => {
112+
const actual = execute(denops, "let g:denops_std_execute_test = 1");
113+
assertInstanceOf(actual, Promise);
114+
assertEquals(await actual, undefined);
115+
},
116+
});

0 commit comments

Comments
 (0)