Fix local variable overwrites

This commit is contained in:
Marty Oehme 2021-04-30 13:26:40 +02:00
parent 9a58deafb6
commit 5a0a258658
Signed by: Marty
GPG key ID: B7538B8F50A1C800
2 changed files with 10 additions and 12 deletions

View file

@ -8,9 +8,9 @@ local BIGNUMBER = 10000000
-- Opens the link passed in in the editor's current buffer. -- Opens the link passed in in the editor's current buffer.
-- Requires a link object passed in. -- Requires a link object passed in.
function A.open(link) function A.open(zlink)
if not link or not link.ref then return end if not zlink or not zlink.ref then return end
local fname = list.get_zettel(link.anchor) or link.ref local fname = list.get_zettel(zlink.anchor) or zlink.ref
vim.api.nvim_command(string.format("edit %s", fname)) vim.api.nvim_command(string.format("edit %s", fname))
end end
@ -19,14 +19,14 @@ end
-- Takes an optional style of link following to use, -- Takes an optional style of link following to use,
-- 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 st = style or o.link().following
local curpos = vim.api.nvim_win_get_cursor(0)[2] local curpos = vim.api.nvim_win_get_cursor(0)[2]
local links = link.extract_all(vim.api.nvim_get_current_line()) local links = link.extract_all(vim.api.nvim_get_current_line())
if style == 'line' then if st == 'line' then
A.open(A.get_next_link_on_line(links, curpos)) A.open(A.get_next_link_on_line(links, curpos))
elseif style == 'cursor' then elseif st == 'cursor' then
A.open(A.get_link_under_cursor(links, curpos)) A.open(A.get_link_under_cursor(links, curpos))
end end
end end
@ -34,10 +34,8 @@ 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(links, curpos) function A.get_link_under_cursor(links, curpos)
for _, link in pairs(links) do for _, l in pairs(links) do
if link.startpos <= curpos + 1 and link.endpos > curpos then if l.startpos <= curpos + 1 and l.endpos > curpos then return l end
return link
end
end end
return nil return nil
end end

View file

@ -1,4 +1,4 @@
action = require 'zettelkasten.action' local action = require 'zettelkasten.action'
before_each(function() before_each(function()
_G.vim = {g = {}, b = {}, loop = {fs_scandir = function() end}} _G.vim = {g = {}, b = {}, loop = {fs_scandir = function() end}}
@ -19,7 +19,7 @@ describe("open", function()
end) end)
it("should use the anchor to open the corresponding zettel", function() it("should use the anchor to open the corresponding zettel", function()
vim.api = {nvim_command = mock(function() end)} vim.api = {nvim_command = mock(function() end)}
ls = stub(require 'zettelkasten.list', "get_zettel") local ls = stub(require 'zettelkasten.list', "get_zettel")
action.open({ action.open({
ref = "1910271456_link-to-my-file.md", ref = "1910271456_link-to-my-file.md",