Skip to content

Commit

Permalink
feature: smart move cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
obaland committed Apr 25, 2023
1 parent 97f2ab3 commit 97e63dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 27 additions & 0 deletions lua/vfiler/extensions/bookmark/action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ local function select(extension, layout)
extension:select(item.path, layout)
end

local function next_cursor(extension, step, limited)
local current = vim.fn.line('.')
local pos = current
repeat
pos = limited(extension, pos + step)
local item = extension:get_item(pos)
if (item.type ~= 'category') or not item.opened then
break
end
until pos == current
return pos
end

function action.change_category(extension)
local item = extension:get_item()
if item.type == 'category' then
Expand Down Expand Up @@ -101,4 +114,18 @@ function action.close_tree(extension)
core.cursor.winmove(extension:winid(), extension:indexof(category))
end

function action.smart_cursor_down(extension)
local pos = next_cursor(extension, 1, function(ext, pos)
return (pos > ext:num_lines()) and 1 or pos
end)
core.cursor.winmove(extension:winid(), pos)
end

function action.smart_cursor_up(extension)
local pos = next_cursor(extension, -1, function(ext, pos)
return (pos < 1) and ext:num_lines() or pos
end)
core.cursor.winmove(extension:winid(), pos)
end

return action
4 changes: 2 additions & 2 deletions lua/vfiler/extensions/bookmark/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ M.configs = {
['c'] = action.change_category,
['dd'] = action.delete,
['h'] = action.close_tree,
['j'] = action.loop_cursor_down,
['k'] = action.loop_cursor_up,
['j'] = action.smart_cursor_down,
['k'] = action.smart_cursor_up,
['l'] = action.open_tree,
['q'] = action.quit,
['r'] = action.rename,
Expand Down

0 comments on commit 97e63dc

Please sign in to comment.