Skip to content

Commit

Permalink
fix(input): refactor for win changes and ensure modified=false. Fixes #…
Browse files Browse the repository at this point in the history
…403. Fixes #402
  • Loading branch information
folke committed Dec 31, 2024
1 parent cc0b528 commit 8930630
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lua/snacks/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ Snacks.config.style("input", {
completion = false, -- disable blink completions in input
},
keys = {
n_esc = { "<esc>", { "cmp_close", "cancel" }, mode = "n" },
i_esc = { "<esc>", { "cmp_close", "stopinsert" }, mode = "i" },
i_cr = { "<cr>", { "cmp_accept", "confirm" }, mode = "i" },
i_tab = { "<tab>", { "cmp_select_next", "cmp" }, mode = "i" },
n_esc = { "<esc>", { "cmp_close", "cancel" }, mode = "n", expr = true },
i_esc = { "<esc>", { "cmp_close", "stopinsert" }, mode = "i", expr = true },
i_cr = { "<cr>", { "cmp_accept", "confirm" }, mode = "i", expr = true },
i_tab = { "<tab>", { "cmp_select_next", "cmp" }, mode = "i", expr = true },
q = "cancel",
},
})

local ui_input = vim.ui.input

---@class snacks.input.Opts: snacks.input.Config|{}
---@class snacks.input.Opts: snacks.input.Config,{}
---@field prompt? string
---@field default? string
---@field completion? string
Expand Down Expand Up @@ -188,7 +188,6 @@ function M.input(opts, on_confirm)
vim.cmd.startinsert()
if opts.default then
vim.api.nvim_buf_set_lines(win.buf, 0, -1, false, { opts.default })
vim.bo[win.buf].modified = false
vim.api.nvim_win_set_cursor(win.win, { 1, #opts.default + 1 })
end
vim.fn.prompt_setcallback(win.buf, function(text)
Expand All @@ -200,23 +199,22 @@ function M.input(opts, on_confirm)
win:close()
end)

if opts.expand then
vim.api.nvim_create_autocmd("TextChangedI", {
buffer = win.buf,
callback = function()
vim.bo[win.buf].modified = false
if vim.api.nvim_win_is_valid(parent_win) then
vim.api.nvim_win_call(parent_win, function()
win:update()
end)
end
vim.api.nvim_win_call(win.win, function()
vim.fn.winrestview({ leftcol = 0 })
win:on({ "TextChangedI", "TextChanged" }, function()
if not win:valid() then
return
end
vim.bo[win.buf].modified = false
if opts.expand then
if vim.api.nvim_win_is_valid(parent_win) then
vim.api.nvim_win_call(parent_win, function()
win:update()
end)
end,
})
end

end
vim.api.nvim_win_call(win.win, function()
vim.fn.winrestview({ leftcol = 0 })
end)
end
end, { buf = true })
return win
end

Expand Down

0 comments on commit 8930630

Please sign in to comment.