Refactor action open_selected function

This commit is contained in:
Marty Oehme 2020-11-06 16:42:09 +01:00
parent 162057f149
commit 30e0519a1f
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -19,18 +19,20 @@ end
-- superseding the one set in options. -- superseding the one set in options.
function A.open_selected(style) function A.open_selected(style)
local style = style or o.link().following local style = style or o.link().following
local curpos = vim.api.nvim_win_get_cursor(0)[2]
local links = link.extract_all(vim.api.nvim_get_current_line())
if style == 'line' then if style == 'line' then
A.open(A.get_next_link_on_line()) A.open(A.get_next_link_on_line(links, curpos))
elseif style == 'cursor' then elseif style == 'cursor' then
A.open(A.get_link_under_cursor()) A.open(A.get_link_under_cursor(links, curpos))
end end
end end
-- Returns the link currently under cursor, roughly the vim equivalent of yiW. -- Returns the link currently under cursor, roughly the vim equivalent of yiW.
-- Works for links containing spaces in their text or reference link. -- Works for links containing spaces in their text or reference link.
function A.get_link_under_cursor() function A.get_link_under_cursor(links, curpos)
local curpos = vim.api.nvim_win_get_cursor(0)[2]
local links = link.extract_all(vim.api.nvim_get_current_line())
for _, link in pairs(links) do for _, link in pairs(links) do
if link.startpos <= curpos + 1 and link.endpos > curpos then if link.startpos <= curpos + 1 and link.endpos > curpos then
return link return link
@ -40,9 +42,7 @@ function A.get_link_under_cursor()
end end
-- Returns the next link of the current line from the cursor onwards. -- Returns the next link of the current line from the cursor onwards.
function A.get_next_link_on_line() function A.get_next_link_on_line(links, curpos)
local curpos = vim.api.nvim_win_get_cursor(0)[2]
local links = link.extract_all(vim.api.nvim_get_current_line())
local nearestpos = BIGNUMBER local nearestpos = BIGNUMBER
local nearestlink local nearestlink
for k, link in pairs(links) do for k, link in pairs(links) do