Skip to content

Commit

Permalink
Merge pull request #251 from vim-denops/key
Browse files Browse the repository at this point in the history
🐛 Fix `helper/input` in Vim
  • Loading branch information
lambdalisue committed Aug 2, 2024
2 parents 38ddb1f + 3b05c69 commit f3be1cd
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 45 deletions.
15 changes: 11 additions & 4 deletions helper/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fn from "../function/mod.ts";
import * as lambda from "../lambda/mod.ts";
import { execute } from "./execute.ts";

const cacheKey = "denops_std/helper/input@1";
const cacheKey = "denops_std/helper/input@2";

async function ensurePrerequisites(denops: Denops): Promise<string> {
if (typeof denops.context[cacheKey] === "string") {
Expand Down Expand Up @@ -67,8 +67,10 @@ async function ensurePrerequisites(denops: Denops): Promise<string> {
endfunction
else
function! s:input_${suffix}(prompt, text, completion) abort
let original = maparg('<Esc>', 'n')
let originalEsc = maparg('<Esc>', 'c', 0, 1)
let originalInt = maparg('<C-c>', 'c', 0, 1)
execute printf('cnoremap <nowait><buffer> <Esc> <C-u>%s<CR>', s:escape_token_${suffix})
execute printf('cnoremap <nowait><buffer> <C-c> <C-u>%s<CR>', s:escape_token_${suffix})
try
let result = a:completion is# v:null
\\ ? input(a:prompt, a:text)
Expand All @@ -81,10 +83,15 @@ async function ensurePrerequisites(denops: Denops): Promise<string> {
catch /^Vim:Interrupt$/
return v:null
finally
if empty(original)
if get(originalEsc, 'buffer')
call mapset(originalEsc)
else
cunmap <buffer> <Esc>
endif
if get(originalInt, 'buffer')
call mapset(originalInt)
else
execute printf('cmap <buffer> %s', original)
cunmap <buffer> <C-c>
endif
endtry
endfunction
Expand Down
152 changes: 112 additions & 40 deletions helper/input_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test({
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("Hello world!\\<CR>", "t")`,
`call feedkeys("Hello world!\\<CR>", "it")`,
);
});
const result = await input(denops);
Expand All @@ -28,7 +28,7 @@ test({
fn: async () => {
await autocmd.group(denops, "denops_std_helper_input", (helper) => {
helper.remove("*");
helper.define("CmdlineEnter", "*", `call feedkeys("\\<CR>", "t")`);
helper.define("CmdlineEnter", "*", `call feedkeys("\\<CR>", "it")`);
});
const result = await input(denops, {
text: "Hello world!",
Expand All @@ -44,7 +44,7 @@ test({
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("\\<Tab>\\<CR>", "t")`,
`call feedkeys("\\<Tab>\\<CR>", "it")`,
);
});
const result = await input(denops, {
Expand All @@ -61,7 +61,7 @@ test({
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("\\<Tab>\\<CR>", "t")`,
`call feedkeys("\\<Tab>\\<CR>", "it")`,
);
});
const result = await input(denops, {
Expand All @@ -88,7 +88,7 @@ test({
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("\\<Tab>\\<CR>", "t")`,
`call feedkeys("\\<Tab>\\<CR>", "it")`,
);
});
const result = await input(denops, {
Expand All @@ -105,7 +105,7 @@ test({
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("\\<Tab>\\<CR>", "t")`,
`call feedkeys("\\<Tab>\\<CR>", "it")`,
);
});
const result = await input(denops, {
Expand All @@ -127,41 +127,113 @@ test({
);
},
});
},
});

test({
// XXX: This test does not work properly on Vim
mode: "nvim",
name: "returns `null` when <Esc> is pressed",
fn: async (denops) => {
await autocmd.group(denops, "denops_std_helper_input", (helper) => {
helper.remove("*");
helper.define(
"CmdlineEnter",
"*",
`call timer_start(0, { -> feedkeys("Hello world!\\<Esc>", "t") })`,
);
await t.step({
name: "returns `null` when <Esc> is pressed",
fn: async () => {
await autocmd.group(denops, "denops_std_helper_input", (helper) => {
helper.remove("*");
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("Hello world!\\<Esc>", "it")`,
);
});
const result = await input(denops);
assertEquals(result, null);
},
});
const result = await input(denops);
assertEquals(result, null);
},
});

test({
// XXX: This test does not work properly on Vim
mode: "nvim",
name: "returns `null` when <C-c> is pressed",
fn: async (denops) => {
await autocmd.group(denops, "denops_std_helper_input", (helper) => {
helper.remove("*");
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("Hello world!\\<C-c>", "t")`,
);
await t.step({
name: "returns `null` when <C-c> is pressed",
fn: async () => {
await autocmd.group(denops, "denops_std_helper_input", (helper) => {
helper.remove("*");
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("Hello world!\\<C-c>", "it")`,
);
});
const result = await input(denops);
assertEquals(result, null);
},
});
await t.step({
name: "should have global mapping restored",
fn: async () => {
await denops.cmd("cnoremap <Esc> foo");
await denops.cmd("cmap <silent> <C-c> bar");
const globalEsc = await denops.call("maparg", "<Esc>", "c", 0, 1);
const globalInt = await denops.call("maparg", "<C-c>", "c", 0, 1);
try {
await autocmd.group(denops, "denops_std_helper_input", (helper) => {
helper.remove("*");
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("Hello world!\\<CR>", "it")`,
);
});
await input(denops);
assertEquals(
await denops.call("maparg", "<Esc>", "c", 0, 1),
globalEsc,
);
assertEquals(
await denops.call("maparg", "<C-c>", "c", 0, 1),
globalInt,
);
} finally {
await denops.cmd("silent! cunmap <Esc>");
await denops.cmd("silent! cunmap <C-c>");
}
},
});
await t.step({
name: "should have buffer local mapping restored",
fn: async () => {
await denops.cmd("cnoremap <Esc> foo");
await denops.cmd("cmap <silent> <C-c> bar");
const globalEsc = await denops.call("maparg", "<Esc>", "c", 0, 1);
const globalInt = await denops.call("maparg", "<C-c>", "c", 0, 1);
await denops.cmd("cnoremap <expr><buffer> <Esc> eval('')");
await denops.cmd("cnoremap <nowait><buffer> <C-c> baz");
const bufferEsc = await denops.call("maparg", "<Esc>", "c", 0, 1);
const bufferInt = await denops.call("maparg", "<C-c>", "c", 0, 1);
try {
await autocmd.group(denops, "denops_std_helper_input", (helper) => {
helper.remove("*");
helper.define(
"CmdlineEnter",
"*",
`call feedkeys("Hello world!\\<CR>", "it")`,
);
});
await input(denops);
assertEquals(
await denops.call("maparg", "<Esc>", "c", 0, 1),
bufferEsc,
);
assertEquals(
await denops.call("maparg", "<C-c>", "c", 0, 1),
bufferInt,
);
await denops.cmd("cunmap <buffer> <Esc>");
await denops.cmd("cunmap <buffer> <C-c>");
assertEquals(
await denops.call("maparg", "<Esc>", "c", 0, 1),
globalEsc,
);
assertEquals(
await denops.call("maparg", "<C-c>", "c", 0, 1),
globalInt,
);
} finally {
await denops.cmd("silent! cunmap <buffer> <Esc>");
await denops.cmd("silent! cunmap <buffer> <C-c>");
await denops.cmd("silent! cunmap <Esc>");
await denops.cmd("silent! cunmap <C-c>");
}
},
});
const result = await input(denops);
assertEquals(result, null);
},
});
2 changes: 1 addition & 1 deletion helper/keymap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Spec = {
};

test({
mode: "nvim",
mode: "all",
name: "send()",
fn: async (denops, t) => {
const specs: Spec[] = [
Expand Down

0 comments on commit f3be1cd

Please sign in to comment.