Check if nvim-window-picker is installed before registering auto command

This commit is contained in:
Aietes 2024-12-01 22:52:09 +01:00
parent 4b80c3c300
commit 53a71c0e86
No known key found for this signature in database

View file

@ -108,38 +108,41 @@ return {
end, end,
}) })
--- Opens the file in a picked window. --- Allow to open the file in a picked window if nvim-window-picker is installed.
--- @param close boolean Whether to close the mini.files window after opening the file. if pcall(require, "window-picker") then
local open_in_window = function(close) --- Opens the file in a picked window.
close = close or false --- @param close boolean Whether to close the mini.files window after opening the file.
local mini_files = require("mini.files") local open_in_window = function(close)
local picked_window_id = require("window-picker").pick_window({ close = close or false
filter_rules = { local mini_files = require("mini.files")
autoselect_one = true, local picked_window_id = require("window-picker").pick_window({
bo = { filter_rules = {
filetype = { "notify", "minifiles" }, autoselect_one = true,
buftype = { "terminal", "quickfix", "nofile" }, bo = {
filetype = { "notify", "minifiles" },
buftype = { "terminal", "quickfix", "nofile" },
},
}, },
}, })
}) if picked_window_id then
if picked_window_id then mini_files.set_target_window(picked_window_id)
mini_files.set_target_window(picked_window_id) mini_files.go_in({ close_on_file = close })
mini_files.go_in({ close_on_file = close }) else
else vim.notify("Window selection canceled", vim.log.levels.INFO, { title = "mini.files" })
vim.notify("Window selection canceled", vim.log.levels.INFO, { title = "mini.files" }) end
end end
vim.api.nvim_create_autocmd("User", {
pattern = "MiniFilesBufferCreate",
callback = function(args)
local buf_id = args.data.buf_id
vim.keymap.set("n", "<M-l>", open_in_window, { buffer = buf_id, desc = "Open file in window" })
vim.keymap.set("n", "<M-L>", function()
open_in_window(true)
end, { buffer = buf_id, desc = "Open file in window and close" })
end,
})
end end
vim.api.nvim_create_autocmd("User", {
pattern = "MiniFilesBufferCreate",
callback = function(args)
local buf_id = args.data.buf_id
vim.keymap.set("n", "<M-l>", open_in_window, { buffer = buf_id, desc = "Open file in window" })
vim.keymap.set("n", "<M-L>", function()
open_in_window(true)
end, { buffer = buf_id, desc = "Open file in window and close" })
end,
})
end, end,
} }