Add get_line api wrapper method
Should enable somewhat easier testing and reasoning about my code.
This commit is contained in:
parent
f67c22bfa7
commit
a4aa3f825b
3 changed files with 49 additions and 5 deletions
|
@ -112,6 +112,7 @@ describe("create_link", function()
|
||||||
-- getpos = function() return {0, 0, 0, 0} end,
|
-- getpos = function() return {0, 0, 0, 0} end,
|
||||||
-- getline = function() return "testline" end
|
-- getline = function() return "testline" end
|
||||||
-- }
|
-- }
|
||||||
|
pending()
|
||||||
vim.cmd = function() end
|
vim.cmd = function() end
|
||||||
action.create_link("my selection", 1, 1, 37)
|
action.create_link("my selection", 1, 1, 37)
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -17,18 +17,18 @@ end
|
||||||
-- delimited word, otherwise the vim specified wordboundary word.
|
-- delimited word, otherwise the vim specified wordboundary word.
|
||||||
function T.get_current_word(big)
|
function T.get_current_word(big)
|
||||||
local pattern = [[\k]]
|
local pattern = [[\k]]
|
||||||
if not big then
|
if not big then pattern = [[\S]] end
|
||||||
pattern = [[\S]]
|
|
||||||
end
|
|
||||||
|
|
||||||
local cur_col = vim.api.nvim_win_get_cursor(0)[2]
|
local cur_col = vim.api.nvim_win_get_cursor(0)[2]
|
||||||
local line = vim.api.nvim_get_current_line()
|
local line = vim.api.nvim_get_current_line()
|
||||||
|
|
||||||
local word_before_cur = vim.fn.matchstrpos(line:sub(1, cur_col + 1), pattern .. "*$")
|
local word_before_cur = vim.fn.matchstrpos(line:sub(1, cur_col + 1),
|
||||||
|
pattern .. "*$")
|
||||||
local word_start_col = word_before_cur[2] + 1
|
local word_start_col = word_before_cur[2] + 1
|
||||||
word_before_cur = word_before_cur[1]
|
word_before_cur = word_before_cur[1]
|
||||||
|
|
||||||
local word_after_cur = vim.fn.matchstr(line:sub(cur_col + 1), "^" .. pattern .."*"):sub(2)
|
local word_after_cur = vim.fn.matchstr(line:sub(cur_col + 1),
|
||||||
|
"^" .. pattern .. "*"):sub(2)
|
||||||
|
|
||||||
return word_before_cur .. word_after_cur, word_start_col
|
return word_before_cur .. word_after_cur, word_start_col
|
||||||
end
|
end
|
||||||
|
@ -83,4 +83,15 @@ function T.replace_text(text, new_text, start_col)
|
||||||
return line_edited
|
return line_edited
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Return editor line contents.
|
||||||
|
-- Returns the content of the line number passed in or the currently active
|
||||||
|
-- line if no number passed in. Lines are, as per neovim function,
|
||||||
|
-- *zero-indexed* compared to what you see in e.g. the editor sidebar.
|
||||||
|
--- @param linenr number
|
||||||
|
--- @return string
|
||||||
|
function T.get_line(linenr)
|
||||||
|
if linenr then return vim.api.nvim_buf_get_lines(0, linenr, linenr + 1) end
|
||||||
|
return vim.api.nvim_get_current_line()
|
||||||
|
end
|
||||||
|
|
||||||
return T
|
return T
|
||||||
|
|
32
lua/zettelkasten/text_spec.lua
Normal file
32
lua/zettelkasten/text_spec.lua
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
local t = require 'zettelkasten.text'
|
||||||
|
|
||||||
|
before_each(function() _G.vim = {g = {}, b = {}} end)
|
||||||
|
after_each(function() _G.vim = nil end)
|
||||||
|
|
||||||
|
describe("get_line", function()
|
||||||
|
it("returns current line contents if no line nr passed", function()
|
||||||
|
vim.api = {
|
||||||
|
nvim_get_current_line = function()
|
||||||
|
return "hello my old friend"
|
||||||
|
end
|
||||||
|
}
|
||||||
|
assert.same("hello my old friend", t.get_line())
|
||||||
|
end)
|
||||||
|
it("returns zero-indexed line contents", function()
|
||||||
|
vim.api = {
|
||||||
|
nvim_buf_get_lines = function(...)
|
||||||
|
local args = table.pack(...)
|
||||||
|
if args[1] == 0 and args[2] + 1 == args[3] then
|
||||||
|
return "hello my new enemy"
|
||||||
|
end
|
||||||
|
return "wrong arguments"
|
||||||
|
end
|
||||||
|
}
|
||||||
|
assert.same("hello my new enemy", t.get_line(1))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- describe("Zettelkasten", function()
|
||||||
|
-- it("should create an anchor for the current datetime",
|
||||||
|
-- function() assert.same(os.date('%y%m%d%H%M'), ZK.create_anchor()) end)
|
||||||
|
-- end)
|
Loading…
Reference in a new issue