Skip to content

Commit

Permalink
fix: floating-input potential close-bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Jan 19, 2025
1 parent 02f0d50 commit eec361d
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions config/nvim/lua/pde/floating-input.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local M = {}
-- https://github.com/liangxianzhe/floating-input.nvim

---@param winid integer
---@param input_width integer
Expand All @@ -17,12 +17,14 @@ local under_cursor = {
col = 0,
}

local M = {}

function M.input(opts, on_confirm, win_config)
local prompt = opts.prompt or "Input: "
local default = opts.default or ""
on_confirm = on_confirm or function() end

-- Calculate a minimal width with a bit padding
-- Calculate a minimal width with a padding
local default_length = vim.str_utfindex(default, "utf-8")
local default_width = default_length + 10
local prompt_width = vim.str_utfindex(prompt, "utf-8") + 10
Expand Down Expand Up @@ -56,26 +58,24 @@ function M.input(opts, on_confirm, win_config)
vim.cmd("startinsert")
vim.api.nvim_win_set_cursor(window, { 1, default_length + 1 })

local function close()
vim.cmd("stopinsert")
vim.api.nvim_win_close(window, true)
end

-- Enter to confirm
vim.keymap.set({ "n", "i", "v" }, "<cr>", function()
local lines = vim.api.nvim_buf_get_lines(buffer, 0, 1, false)
close()
vim.cmd("stopinsert")
on_confirm(lines[1])
vim.api.nvim_win_close(window, true)
end, { buffer = buffer })

-- Esc or q to close
vim.keymap.set("n", "<esc>", function()
close()
on_confirm(nil)
vim.cmd("stopinsert")
vim.api.nvim_win_close(window, true)
end, { buffer = buffer })
vim.keymap.set("n", "q", function()
close()
on_confirm(nil)
vim.cmd("stopinsert")
vim.api.nvim_win_close(window, true)
end, { buffer = buffer })
end

Expand Down

0 comments on commit eec361d

Please sign in to comment.