Refactor link options into their own category

This commit is contained in:
Marty Oehme 2020-11-06 16:01:51 +01:00
parent 44a985cce6
commit 101a15c8c9
Signed by: Marty
GPG key ID: B7538B8F50A1C800
5 changed files with 55 additions and 14 deletions

View file

@ -12,6 +12,7 @@ next up:
* [x] probably stop hardcoding anchor regex, make an option
* [ ] implement custom anchor creation function to go with custom regex
* [ ] opening zettel should use generated link table for full filename anchor search
* [ ] need a default zettel directory to look in
* [ ] implement fallback to filename
## TODO: needed functionality

View file

@ -18,7 +18,7 @@ end
-- Takes an optional style of link following to use,
-- superseding the one set in options.
function A.open_selected(style)
local style = style or o.zettel().link_following
local style = style or o.link().following
if style == 'line' then
A.open(A.get_next_link_on_line())
elseif style == 'cursor' then
@ -55,3 +55,39 @@ function A.get_next_link_on_line()
end
return {open = A.open, open_selected = A.open_selected}
--- local function get_selection()
--- s_start = vim.fn.line("'<") - 1
--- s_end = vim.fn.line("'>")
--- return vim.api.nvim_buf_get_lines(0, s_start, s_end, true)
--- end
--
--- -- UGLY HACKS ABOUND
--- function ZK.create_zettel()
--- -- get line and its number
--- local selection
--- local line = vim.api.nvim_get_current_line()
--- local linenr = vim.api.nvim_win_get_cursor(0)[1]
--
--- -- get words under cursor / selected
--- local mode = vim.api.nvim_get_mode()['mode']
--- if mode == "n" then
--- print(vim.fn.line("'<'") - 1)
--- selection = vim.fn.expand("<cWORD>")
--- -- NOT WORKING YET
--- elseif mode == "v" then
--- selection = get_selection()
--- else
--- return
--- end
--
--- -- get valid link
--- local link = l.create(nil, selection)
--
--- -- create new line with selection replaced in middle
--- local st, en = line:find(selection, 0, true)
--- local repl_line = line:sub(1, st - 1) .. link .. line:sub(en + 1)
--
--- -- replace existing line in favor of new one
--- vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, true, {repl_line})
--- end

View file

@ -48,7 +48,7 @@ end
-- Takes an optional link text which will be added to the link.
-- Takes an optional style according to which the link will be transformed.
function L.create(anchor, text, style)
style = style or o.zettel().link_style
style = style or o.link().style
return parsers[style].style_func(anchor, text, o.zettel().extension)
end

View file

@ -2,13 +2,15 @@ local Opt = {}
-- vim setting names and defaults
local zettel_defaults = {
extension = {vimname = "zettel_extension", default = ".md"},
link_style = {
extension = {vimname = "zettel_extension", default = ".md"}
}
local link_defaults = {
style = {
vimname = "zettel_link_style",
default = "markdown",
valid = {markdown = true, wiki = true}
},
link_following = {
following = {
vimname = "zettel_link_following",
default = "cursor",
valid = {cursor = true, line = true}
@ -54,7 +56,7 @@ local function get_options(defaults)
end
function Opt.zettel() return get_options(zettel_defaults) end
function Opt.link() return get_options(link_defaults) end
function Opt.anchor() return get_options(anchor_defaults) end
return Opt

View file

@ -15,35 +15,37 @@ describe("zettel options", function()
end)
it("should return the default zettel extension if not set in vim",
function() assert.same(".md", opt.zettel().extension) end)
end)
describe("link options", function()
it("should return the global link style if set in vim", function()
_G.vim.g.zettel_link_style = "wiki"
assert.same("wiki", opt.zettel().link_style)
assert.same("wiki", opt.link().style)
end)
it("should return the buffer link style if set in vim", function()
_G.vim.b.zettel_link_style = "wiki"
assert.same("wiki", opt.zettel().link_style)
assert.same("wiki", opt.link().style)
end)
it("should return the default link style if not set in vim",
function() assert.same("markdown", opt.zettel().link_style) end)
function() assert.same("markdown", opt.link().style) end)
it("should error on entries other than markdown/wiki", function()
_G.vim.g.zettel_link_style = "idontbelong"
assert.is_error(function() opt.zettel() end)
assert.is_error(function() opt.link() end)
end)
it("should return the global link following if set in vim", function()
_G.vim.g.zettel_link_following = "line"
assert.same("line", opt.zettel().link_following)
assert.same("line", opt.link().following)
end)
it("should return the buffer link following if set in vim", function()
_G.vim.b.zettel_link_following = "line"
assert.same("line", opt.zettel().link_following)
assert.same("line", opt.link().following)
end)
it("should return the default link following if not set in vim",
function() assert.same("cursor", opt.zettel().link_following) end)
function() assert.same("cursor", opt.link().following) end)
it("should error on entries other than markdown/wiki", function()
_G.vim.g.zettel_link_following = "idontbelong"
assert.is_error(function() opt.zettel() end)
assert.is_error(function() opt.link() end)
end)
end)