Skip to content

Commit

Permalink
perf(menu): make preview smoother; reduce unnecessary cursor jumps
Browse files Browse the repository at this point in the history
  • Loading branch information
bekaboo committed Dec 14, 2023
1 parent 9405df5 commit 4f22910
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ basic element of [`dropbar_t`](#dropbar_t) and [`dropbar_menu_entry_t`](#dropbar
| `dropbar_symbol_t:displaywidth(): integer` | returns the display width of the symbol |
| `dropbar_symbol_t:bytewidth(): integer` | returns the byte width of the symbol |
| `dropbar_symbol_t:jump()` | jump to the start of the range of the dropbar symbol |
| `dropbar_symbol_t:preview()` | preview the symbol in the source window |
| `dropbar_symbol_t:preview(orig_view: table?)` | preview the symbol in the source window, use `orig_table` as the original view of the source window (to restore win view after preview ends) |
| `dropbar_symbol_t:preview_restore_hl()` | clear the preview highlights in the source window |
| `dropbar_symbol_t:preview_restore_view()` | restore the view in the source window after previewing the symbol |
| `dropbar_symbol_t:swap_field(field: string, new_val: any)` | temporarily change the content of a dropbar symbol |
Expand Down
8 changes: 7 additions & 1 deletion doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,13 @@ dropbar_symbol_t:jump() *dropbar_symbol_t:jump()*

Jump to the start of the symbol associated with the winbar symbol

dropbar_symbol_t:preview() *dropbar_symbol_t:preview()*
dropbar_symbol_t:preview([{orig_view}]) *dropbar_symbol_t:preview()*

Parameters ~
• {orig_view} (table?): Use this win view as the original view of
the source window, see |winsaveview()| and
|winrestview()|. Default to the current
view of the source window

Preview the symbol in the source window

Expand Down
10 changes: 4 additions & 6 deletions lua/dropbar/bar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,13 @@ function dropbar_symbol_t:jump()
end

---Preview the symbol in the source window
---@param orig_view table? use this view as original view
---@return nil
function dropbar_symbol_t:preview()
if not self.range then
function dropbar_symbol_t:preview(orig_view)
if not self.range or not self.win or not self.buf then
return
end
if not self.win or not self.buf then
return
end
self.view = vim.api.nvim_win_call(self.win, vim.fn.winsaveview)
self.view = orig_view or vim.api.nvim_win_call(self.win, vim.fn.winsaveview)
utils.hl.range_single(self.buf, 'DropBarPreview', self.range)
vim.api.nvim_win_set_cursor(self.win, {
self.range.start.line + 1,
Expand Down
14 changes: 6 additions & 8 deletions lua/dropbar/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -726,17 +726,15 @@ end
---@param look_ahead boolean? whether to look ahead for a component
---@return nil
function dropbar_menu_t:preview_symbol_at(pos, look_ahead)
if self.prev_cursor then
local prev_component = self:get_component_at(self.prev_cursor, look_ahead)
if prev_component then
prev_component:preview_restore_view()
end
if not pos then
return
end
local component = self:get_component_at(pos, look_ahead)
if component then
self.symbol_previewed = component
component:preview()
if not component or component == self.symbol_previewed then
return
end
component:preview(self.symbol_previewed and self.symbol_previewed.view)
self.symbol_previewed = component
end

---Finish the preview in current menu
Expand Down

0 comments on commit 4f22910

Please sign in to comment.