Skip to content

Commit

Permalink
refactor: remove floating preview from path source
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Feb 28, 2024
1 parent cf4fcd2 commit cc3b26c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 106 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1405,10 +1405,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

Expand Down
3 changes: 0 additions & 3 deletions lua/dropbar/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
113 changes: 14 additions & 99 deletions lua/dropbar/sources/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -61,117 +60,43 @@ 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,
callback = function()
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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cc3b26c

Please sign in to comment.