Skip to content

Commit

Permalink
feat: add options to automatically close Chosen buffer after saving c…
Browse files Browse the repository at this point in the history
…urrent file
  • Loading branch information
dangooddd committed Jan 27, 2025
1 parent e57eae8 commit 032814a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ require("chosen").setup({
-- h and l -- horizontal scroll
-- j and k -- PageUp / PageDown
bind_hjkl = true,
-- Exit on save / delete of current file
exit_on_save = false,
-- Chosen ui options
ui_options = {
max_height = 10,
Expand All @@ -98,7 +100,7 @@ require("chosen").setup({
keymap = {
-- Reset mode or exit
revert = "<Esc>",
-- Save current file
-- Save / delete current file
save = "c",
-- Toggle delete mode
delete = "d",
Expand Down
10 changes: 6 additions & 4 deletions doc/chosen.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ require("chosen").setup({
-- h and l -- horizontal scroll
-- j and k -- PageUp / PageDown
bind_hjkl = true,
-- Exit on save / delete of current file
exit_on_save = false,
-- Chosen ui options
ui_options = {
max_height = 10,
Expand All @@ -44,7 +46,7 @@ require("chosen").setup({
keymap = {
-- Reset mode or exit
revert = "<Esc>",
-- Save current file
-- Save / delete current file
save = "c",
-- Toggle delete mode
delete = "d",
Expand Down Expand Up @@ -113,8 +115,8 @@ require("chosen").dump_index(store_path, index)
| ChosenDelete | `{ link = "DiagnosticError" }` | Key in delete mode
| ChosenSwap | `{ link = "DiagnosticWarning" }` | Key in swap mode
| ChosenPlaceholder | `{ link = "DiagnosticHint" }` | Placeholder on empty buffer
| ChosenSplit | `{ link = "Special" }` | Key in split and vsplit modes
| ChosenCurrentFile | `{ link = "Type" }` | Current file hightlights
| ChosenSplit | `{ link = "DiagnosticInfo" }` | Key in split and vsplit modes
| ChosenCurrentFile | `{ link = "Special" }` | Current file hightlights
| ChosenCursor | `{ nocombine = true, blend = 100}` | Cursor in Chosen buffers

# Tips
Expand All @@ -135,7 +137,7 @@ require("chosen").setup({
})
```

If you have issues because of cursor change:
If you have issues because of cursor changes:
```lua
vim.api.nvim_set_hl(0, "ChosenCursor", { link = "Cursor" })
```
37 changes: 24 additions & 13 deletions lua/chosen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ M.config = {
-- h and l -- horizontal scroll
-- j and k -- PageUp / PageDown
bind_hjkl = true,
-- Exit on save / delete of current file
exit_on_save = false,
-- Chosen ui options
ui_options = {
max_height = 10,
Expand All @@ -36,7 +38,7 @@ M.config = {
keymap = {
-- Reset mode or exit
revert = "<Esc>",
-- Save current file
-- Save / delete current file
save = "c",
-- Toggle delete mode
delete = "d",
Expand Down Expand Up @@ -150,8 +152,8 @@ function M.setup(opts)
vim.api.nvim_set_hl(0, "ChosenDelete", { link = "DiagnosticError", default = true })
vim.api.nvim_set_hl(0, "ChosenSwap", { link = "DiagnosticWarn", default = true })
vim.api.nvim_set_hl(0, "ChosenPlaceholder", { link = "DiagnosticHint", default = true })
vim.api.nvim_set_hl(0, "ChosenSplit", { link = "Special", default = true })
vim.api.nvim_set_hl(0, "ChosenCurrentFile", { link = "Type", default = true })
vim.api.nvim_set_hl(0, "ChosenSplit", { link = "DiagnosticInfo", default = true })
vim.api.nvim_set_hl(0, "ChosenCurrentFile", { link = "Special", default = true })
vim.api.nvim_set_hl(0, "ChosenCursor", { nocombine = true, blend = 100, default = true })

-- autocmds
Expand All @@ -176,24 +178,25 @@ function M.setup(opts)
end

---Delete file from index entry for given cwd
---Returns true if found and deleted
---@param cwd string?
---@param fname string File name to delete
---@return boolean?
function H.delete_from_index(cwd, fname)
cwd = cwd or H.get_resolved_cwd()
if not cwd or not M.index[cwd] then return end

fname = vim.fn.fnamemodify(fname, ":p")

for i, file in ipairs(M.index[cwd] or {}) do
for i, file in ipairs(M.index[cwd]) do
if file == fname then
table.remove(M.index[cwd], i)
break
if #M.index[cwd] == 0 then
M.index[cwd] = nil
end
return true
end
end

if #M.index[cwd] == 0 then
M.index[cwd] = nil
end
end

---Swap two files in index entry for given cwd
Expand Down Expand Up @@ -269,7 +272,7 @@ end
---Callbacks to call on buffer actions
---@type table<string, function>
H.keymap_callbacks = {
---Clear swap|delete mode or close window
---Clear mode or close window
---@param buf chosen.Buf
revert = function(buf)
if vim.b[buf].chosen_mode == "" then
Expand All @@ -280,11 +283,19 @@ H.keymap_callbacks = {
end
end,

---Save current buffer and re-render window
---Save current file to index
---If already saved, delete it
---@param buf chosen.Buf
save = function(buf)
H.save_to_index(nil, vim.b[buf].chosen_fname)
H.refresh_win(buf)
if not H.delete_from_index(nil, vim.b[buf].chosen_fname) then
H.save_to_index(nil, vim.b[buf].chosen_fname)
end

if M.config.exit_on_save then
pcall(vim.api.nvim_win_close, vim.fn.bufwinid(buf), false)
else
H.refresh_win(buf)
end
end,

---@param buf chosen.Buf
Expand Down

0 comments on commit 032814a

Please sign in to comment.