diff --git a/lua/brokencode/ui.lua b/lua/brokencode/ui.lua index b53bad6..75af1bc 100644 --- a/lua/brokencode/ui.lua +++ b/lua/brokencode/ui.lua @@ -164,6 +164,7 @@ function M.input(callback) local function close(send) if send then + vim.cmd("stopinsert") local text = table.concat(vim.api.nvim_buf_get_lines(buffer, 0, -1, false), "\n") if vim.trim(text) ~= "" then callback(text) diff --git a/tests/ui_spec.lua b/tests/ui_spec.lua index 4d3991e..da03465 100644 --- a/tests/ui_spec.lua +++ b/tests/ui_spec.lua @@ -60,3 +60,26 @@ callbacks[3](nil, { { type = "user", text = "old session" } }) assert(ui.current_session == "ses_new", "old polling replaced the active session") client.messages = messages + +local submitted +ui.input(function(text) submitted = text end) +local input_buf = vim.api.nvim_get_current_buf() +vim.api.nvim_buf_set_lines(input_buf, 0, -1, false, { "send me" }) + +local commands = {} +local original_cmd = vim.cmd +vim.cmd = function(command) + table.insert(commands, command) + original_cmd(command) +end + +for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(input_buf, "i")) do + if mapping.lhs == "" then + mapping.callback() + break + end +end +vim.cmd = original_cmd + +assert(submitted == "send me", "Ctrl-S did not submit the message") +assert(vim.tbl_contains(commands, "stopinsert"), "Ctrl-S did not return to normal mode")