---@module 'snacks' local data = require("javelin.data") local log = require("javelin.log") local UI = {} ---@type JavelinConfig local options local function open_buf(file) -- nvim 0.11 only command if vim.fn.exists("*isabsolutepath") == 1 then if vim.fn.isabsolutepath(file) == 0 then file = data.get_root() .. "/" .. file end else if file:sub(1, 1) ~= "/" and file:sub(1, 1) ~= "~" then file = data.get_root() .. "/" .. file end end local bufnr = vim.fn.bufadd(vim.fn.expand(file)) vim.fn.bufload(bufnr) vim.api.nvim_set_option_value("buflisted", true, { buf = bufnr, }) vim.api.nvim_set_current_buf(bufnr) end ---@param index number function UI.select(index) local file = data.get_current()[index] if file ~= nil then open_buf(file) end end function UI.open() local root = data.get_root() local initial_text = data.get_list(root) Snacks.win.new({ text = initial_text, title = root, title_pos = options.menu.title_pos, width = options.menu.width, height = options.menu.height, border = options.menu.border, keys = options.menu.keys, fixbuf = true, wo = { number = true, }, bo = { modifiable = true, readonly = false, buftype = "nofile", bufhidden = "wipe", }, actions = { ["goto"] = function(self) local file = vim.api.nvim_get_current_line() self:close() open_buf(file) end, }, on_buf = function(self) vim.api.nvim_create_autocmd("BufLeave", { buffer = self.buf, callback = function() if not self.closed then self:close() end return true end, }) end, on_close = function(self) if self.buf then local lines = self:lines() if #lines == 1 and lines[1] == "" then lines = {} end if not vim.deep_equal(initial_text, lines) then data.update(root, self:lines()) log.info("Javelin updated.") end else log.error("No buf on win") end end, }) end function UI.setup(opt) options = opt end return UI