From 85fc827ece3a8f01aac09e077b812a4031c73312 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Wed, 28 Feb 2024 07:18:25 -0800 Subject: [PATCH] refactor: remove floating preview from path source --- README.md | 4 -- lua/dropbar/configs.lua | 3 - lua/dropbar/sources/path.lua | 113 +++++------------------------------ 3 files changed, 14 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 10e9bce3..fb7cd637 100644 --- a/README.md +++ b/README.md @@ -1596,10 +1596,6 @@ each sources. }) end ``` - - `opts.sources.path.preview`: `"floating"` | `"previous"` - - Default: `"previous"` - - `"previous"` will preview the buffer in the menu's previous split window, like TS/LSP symbols. - - `"floating"` will preview the buffer in a popup winow adjacent to the menu. ##### Treesitter diff --git a/lua/dropbar/configs.lua b/lua/dropbar/configs.lua index f658d42c..cc346c0a 100644 --- a/lua/dropbar/configs.lua +++ b/lua/dropbar/configs.lua @@ -536,9 +536,6 @@ M.opts = { modified = function(sym) return sym end, - ---Preview files in the prevous window or a floating window. - ---@type "previous"|"floating" - preview = 'previous', }, treesitter = { -- Lua pattern used to extract a short name from the node text diff --git a/lua/dropbar/sources/path.lua b/lua/dropbar/sources/path.lua index ecf6df1c..4f8574ed 100644 --- a/lua/dropbar/sources/path.lua +++ b/lua/dropbar/sources/path.lua @@ -11,7 +11,7 @@ local function get_icon_and_hl(path) local icon = icon_kind_opts.symbols.File local icon_hl = 'DropBarIconKindFile' local name_hl = 'DropBarKindFile' - local stat = vim.loop.fs_stat(path) + local stat = vim.uv.fs_stat(path) if not stat then return icon, icon_hl elseif stat.type == 'directory' then @@ -38,7 +38,6 @@ end local function preview_prepare_buf(self, path) local buf if vim.uv.fs_stat(path).type == 'directory' then - -- TODO: preview directory entries self:preview_restore_view() return end @@ -61,96 +60,20 @@ local function preview_prepare_buf(self, path) end ---@param self dropbar_symbol_t -local function preview_open_float(self, path, icon, icon_hl) +local function preview_open(self, path) local preview_buf = preview_prepare_buf(self, path) if not preview_buf then return end + local buflisted = vim.bo[preview_buf].buflisted - local function make_title() - local pat = vim.fs.normalize( - configs.eval(configs.opts.sources.path.relative_to, preview_buf) - ) - return { - { icon, icon_hl }, - { - vim.api - .nvim_buf_get_name(preview_buf) - :gsub('' .. pat .. '/', '') - :gsub('^' .. pat, ''), -- ':~' - 'NormalFloat', - }, - } - end - if - self.entry.menu.preview_win == nil - or vim.api.nvim_win_is_valid(self.entry.menu.preview_win) == false - then - self.entry.menu.preview_win = vim.api.nvim_open_win(preview_buf, false, { - -- relative = 'editor', - relative = 'win', - style = 'minimal', - -- focusable = false, - width = math.min(80, math.floor(vim.o.columns / 2)), - height = math.min(25, math.floor(vim.o.lines / 2)), - row = 0, - col = vim.api.nvim_win_get_width(self.entry.menu.win) + 1, - border = 'solid', - title = make_title(), - }) - vim.api.nvim_create_autocmd('BufLeave', { - buffer = self.entry.menu.buf, - callback = function() - self:preview_restore_view() - end, - }) - vim.schedule(function() - vim.api.nvim_exec_autocmds( - 'CursorMoved', - { buffer = self.entry.menu.buf } - ) - end) - else - vim.api.nvim_win_set_buf(self.entry.menu.preview_win, preview_buf) - local config = vim.api.nvim_win_get_config(self.entry.menu.preview_win) - config.title = make_title() - vim.api.nvim_win_set_config(self.entry.menu.preview_win, config) - end - local last_exit = vim.api.nvim_buf_get_mark(preview_buf, '"') - if last_exit[1] ~= 0 then - vim.api.nvim_win_set_cursor(self.entry.menu.preview_win, last_exit) - else - vim.api.nvim_win_set_cursor(self.entry.menu.preview_win, { 1, 0 }) - end - vim.wo[self.entry.menu.preview_win].winbar = '' - vim.wo[self.entry.menu.preview_win].stc = '' - vim.wo[self.entry.menu.preview_win].signcolumn = 'no' - vim.wo[self.entry.menu.preview_win].number = false - vim.wo[self.entry.menu.preview_win].relativenumber = false -end - ----@param self dropbar_symbol_t -local function preview_close_float(self) - if - self.entry.menu.preview_win - and vim.api.nvim_win_is_valid(self.entry.menu.preview_win) - then - vim.api.nvim_win_close(self.entry.menu.preview_win, true) - end - self.entry.menu.preview_win = nil -end - ----@param self dropbar_symbol_t -local function preview_open_previous(self, path) - local preview_buf = preview_prepare_buf(self, path) - if not preview_buf then + local preview_win = self.entry.menu:root_win() + if not preview_win then return end - local buflisted = vim.bo[preview_buf].buflisted - - self.entry.menu.preview_win = self.entry.menu:root_win() + self.entry.menu.preview_win = preview_win self.entry.menu.prev_buf = self.entry.menu.prev_buf - or vim.api.nvim_win_get_buf(self.entry.menu.preview_win) + or vim.api.nvim_win_get_buf(preview_win) vim.api.nvim_create_autocmd('BufLeave', { buffer = self.entry.menu.buf, @@ -158,20 +81,22 @@ local function preview_open_previous(self, path) self:preview_restore_view() end, }) - vim.api.nvim_win_set_buf(self.entry.menu.preview_win, preview_buf) + vim.api.nvim_win_set_buf(preview_win, preview_buf) + -- set cursor to the last exited position in buf (:h '"), if available local last_exit = vim.api.nvim_buf_get_mark(preview_buf, '"') if last_exit[1] ~= 0 then - vim.api.nvim_win_set_cursor(self.entry.menu.preview_win, last_exit) + vim.api.nvim_win_set_cursor(preview_win, last_exit) end vim.bo[preview_buf].buflisted = buflisted + -- ensure dropbar still shows then the preview buffer is opened vim.wo[self.entry.menu.preview_win].winbar = '%{%v:lua.dropbar.get_dropbar_str()%}' end ---@param self dropbar_symbol_t -local function preview_close_previous(self) +local function preview_close(self) if self.win then if self.entry.menu.prev_buf then vim.api.nvim_win_set_buf(self.win, self.entry.menu.prev_buf) @@ -205,19 +130,9 @@ local function convert(path, buf, win) vim.cmd.edit(path) end, preview = vim.schedule_wrap(function(self) - if path_opts.preview == 'previous' then - preview_open_previous(self, path) - else - preview_open_float(self, path, icon, icon_hl) - end + preview_open(self, path) end), - preview_restore_view = function(self) - if path_opts.preview == 'previous' then - preview_close_previous(self) - else - preview_close_float(self) - end - end, + preview_restore_view = preview_close, }, { ---@param self dropbar_symbol_t __index = function(self, k)