From a4aa3f825b921f23937af40c4b6e563206171e92 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 4 May 2021 14:50:56 +0200 Subject: [PATCH] Add get_line api wrapper method Should enable somewhat easier testing and reasoning about my code. --- lua/zettelkasten/action_spec.lua | 1 + lua/zettelkasten/text.lua | 21 ++++++++++++++++----- lua/zettelkasten/text_spec.lua | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 lua/zettelkasten/text_spec.lua diff --git a/lua/zettelkasten/action_spec.lua b/lua/zettelkasten/action_spec.lua index f87ca55..1d2cec6 100644 --- a/lua/zettelkasten/action_spec.lua +++ b/lua/zettelkasten/action_spec.lua @@ -112,6 +112,7 @@ describe("create_link", function() -- getpos = function() return {0, 0, 0, 0} end, -- getline = function() return "testline" end -- } + pending() vim.cmd = function() end action.create_link("my selection", 1, 1, 37) end) diff --git a/lua/zettelkasten/text.lua b/lua/zettelkasten/text.lua index 9bb75e8..a3ea7e1 100644 --- a/lua/zettelkasten/text.lua +++ b/lua/zettelkasten/text.lua @@ -17,18 +17,18 @@ end -- delimited word, otherwise the vim specified wordboundary word. function T.get_current_word(big) local pattern = [[\k]] - if not big then - pattern = [[\S]] - end + if not big then pattern = [[\S]] end local cur_col = vim.api.nvim_win_get_cursor(0)[2] 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 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 end @@ -83,4 +83,15 @@ function T.replace_text(text, new_text, start_col) return line_edited 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 diff --git a/lua/zettelkasten/text_spec.lua b/lua/zettelkasten/text_spec.lua new file mode 100644 index 0000000..2436402 --- /dev/null +++ b/lua/zettelkasten/text_spec.lua @@ -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)