See More

local picker = require("quickbuffer.picker") local window = require("quickbuffer.window") local utils = require("quickbuffer.utils") local M = {} local win = nil M.buf = nil M.keybinds = { gotopicker = "", closebuffer = "" } local function get_keybinds(opts) local kbs = nil if opts.keybinds then kbs = opts.keybinds end if not kbs or kbs.gotopicker == "" then M.keybinds.gotopicker = "p" else M.keybinds.gotopicker = kbs.gotopicker end if not kbs or kbs.closebuffer == "" then M.keybinds.closebuffer = "" else M.keybinds.closebuffer = kbs.closebuffer end end local function set_buf_keymaps(buf, opts) -- close buffer vim.api.nvim_buf_set_keymap(buf, "n", M.keybinds.closebuffer, "", { noremap = true, silent = true, callback = function() if vim.api.nvim_get_option_value("modified", { buf = buf }) then vim.cmd("w") end vim.api.nvim_win_close(0, true) win = nil if picker.buf ~= nil and vim.api.nvim_buf_is_valid(picker.buf) and picker.win ~= nil then vim.api.nvim_win_call(picker.win, function () vim.api.nvim_win_close(0, true) picker.win = nil end) end end, }) -- go to picker vim.api.nvim_buf_set_keymap(buf, "n", M.keybinds.gotopicker, "", { noremap = true, silent = true, callback = function() picker.open(opts, function(path) M.switchBufFile(path, opts) end, M.buf) end, }) end function M.switchBufFile(path, opts) if vim.api.nvim_get_option_value("modified", {buf = M.buf}) then vim.api.nvim_buf_call(M.buf, function() vim.cmd("w") end) end path = utils.Expand_path(path) if win == nil or not vim.api.nvim_win_is_valid(win) then return end local buf = vim.fn.bufnr(path, true) if not vim.api.nvim_buf_is_loaded(buf) then vim.fn.bufload(buf) end vim.api.nvim_win_set_buf(win, buf) M.buf = buf set_buf_keymaps(M.buf, opts) -- Trigger markview refresh if it's loaded local ok = pcall(require, "markview") if ok then vim.api.nvim_exec_autocmds("BufWinEnter", { buffer = buf }) end end function M.open_floating_file(opts) if win ~= nil and vim.api.nvim_win_is_valid(win) then vim.api.nvim_set_current_win(win) return end local expanded_path = utils.Expand_path(opts.target_file) -- See if we are in a project folder local cwd = utils.Expand_path(vim.fn.getcwd()) -- We need to go through all folders just in case we encounter something like "/projects/code" at the start but actually there is "/projects/code/anotherFolder" and if we are in that second folder we want that one even though we are still technicallt in /projects/code local best_match = nil local best_len = 0 if opts.project_folders then for _, project in ipairs(opts.project_folders) do local dir = utils.Expand_path(project.path) -- normalize (remove trailing slash) dir = dir:gsub("/+$", "") if cwd == dir or cwd:sub(1, #dir + 1) == dir .. "/" then if #dir > best_len then best_match = project best_len = #dir end end end end if best_match then expanded_path = utils.Expand_path(best_match.target_file) end if vim.fn.filereadable(expanded_path) == 0 then vim.notify("todo file does not exist at directory: " .. expanded_path, vim.log.levels.ERROR) return end M.buf = vim.fn.bufnr(expanded_path, true) if M.buf == -1 then M.buf = vim.api.nvim_create_buf(false, false) vim.api.nvim_buf_set_name(M.buf, expanded_path) end vim.bo[M.buf].swapfile = false win = vim.api.nvim_open_win(M.buf, true, window.Win_config(opts.width, opts.height, opts.bufferposition, opts.border)) get_keybinds(opts) set_buf_keymaps(M.buf, opts) end return M