Skip to content

Commit c8e2795

Browse files
committed
fix: disable newlines in single-line inputs
1 parent 16ebf11 commit c8e2795

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

lua/input-form/form.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,13 @@ function M:_install_keymaps(input)
327327
-- Block insert mode on the select display buffer.
328328
vim.keymap.set("n", "i", "<Nop>", { buffer = buf, nowait = true, silent = true })
329329
vim.keymap.set("n", "a", "<Nop>", { buffer = buf, nowait = true, silent = true })
330+
elseif input.type == "text" then
331+
-- Single-line text inputs must never contain newlines. <CR> in insert
332+
-- mode just exits insert mode (accepting the value) rather than inserting
333+
-- a line break. Multiline inputs intentionally keep <CR> for newline entry.
334+
map("i", "<CR>", function()
335+
vim.cmd("stopinsert")
336+
end)
330337
end
331338
end
332339

tests/test_form.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,40 @@ T["form"]["invalid spec raises"] = function()
153153
eq(ok_empty, false)
154154
end
155155

156+
T["form"]["text inputs stop insert on <CR> (no newlines)"] = function()
157+
child.lua([[
158+
_G.f = _G.make_form()
159+
_G.f:show()
160+
vim.api.nvim_set_current_win(_G.f._inputs[1].win)
161+
-- Feed `i` to enter insert, then `<CR>` which should now invoke stopinsert
162+
-- instead of inserting a newline.
163+
vim.api.nvim_feedkeys(
164+
vim.api.nvim_replace_termcodes('i<CR>', true, false, true),
165+
'x',
166+
false
167+
)
168+
]])
169+
-- Exactly one line (no newline inserted) and we're back in normal mode.
170+
eq(child.lua_get([[#vim.api.nvim_buf_get_lines(_G.f._inputs[1].buf, 0, -1, false)]]), 1)
171+
eq(child.lua_get([[vim.api.nvim_get_mode().mode]]), "n")
172+
-- Form still visible.
173+
eq(child.lua_get([[_G.f._visible]]), true)
174+
end
175+
176+
T["form"]["multiline inputs do not rebind <CR>"] = function()
177+
child.lua([[_G.f = _G.make_form(); _G.f:show()]])
178+
local has_cr = child.lua_get([[
179+
(function()
180+
local maps = vim.api.nvim_buf_get_keymap(_G.f._inputs[3].buf, 'i')
181+
for _, m in ipairs(maps) do
182+
if m.lhs == '<CR>' then return true end
183+
end
184+
return false
185+
end)()
186+
]])
187+
eq(has_cr, false)
188+
end
189+
156190
T["form"]["keymaps are installed on each input buffer"] = function()
157191
child.lua([[_G.f = _G.make_form(); _G.f:show()]])
158192
-- <Tab> should be mapped in normal mode on the first input's buffer.

0 commit comments

Comments
 (0)