diff --git a/.luarc.json b/.luarc.json index 17a5072..3c99bde 100644 --- a/.luarc.json +++ b/.luarc.json @@ -2,5 +2,6 @@ "diagnostics.globals": [ "describe", "it", + "before_each", ] } diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 60b9795..a0f1922 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -386,6 +386,9 @@ local state = { extmark = function() return extmark end, + gutter_group = function() + return gutter_group + end, ns = function() return ns end, diff --git a/tests/precognition/e2e_spec.lua b/tests/precognition/e2e_spec.lua index b7eda5c..70b6493 100644 --- a/tests/precognition/e2e_spec.lua +++ b/tests/precognition/e2e_spec.lua @@ -2,6 +2,20 @@ local precognition = require("precognition") ---@diagnostic disable-next-line: undefined-field local eq = assert.are.same +local function get_gutter_extmarks(buffer) + local gutter_extmarks = {} + for _, extmark in + pairs(vim.api.nvim_buf_get_extmarks(buffer, -1, 0, -1, { + details = true, + })) + do + if extmark[4] and extmark[4].sign_name and extmark[4].sign_name:match(precognition.gutter_group) then + table.insert(gutter_extmarks, extmark) + end + end + return gutter_extmarks +end + describe("e2e tests", function() before_each(function() precognition.setup({}) @@ -26,7 +40,13 @@ describe("e2e tests", function() it("virtual line is displayed and updated", function() local buffer = vim.api.nvim_create_buf(true, false) vim.api.nvim_set_current_buf(buffer) - vim.api.nvim_buf_set_lines(buffer, 0, -1, false, { "Hello World this is a test", "line 2" }) + vim.api.nvim_buf_set_lines( + buffer, + 0, + -1, + false, + { "Hello World this is a test", "line 2", "", "line 4", "", "line 6" } + ) vim.api.nvim_win_set_cursor(0, { 1, 1 }) precognition.on_cursor_moved() @@ -35,6 +55,22 @@ describe("e2e tests", function() details = true, }) + local gutter_extmarks = get_gutter_extmarks(buffer) + + for _, extmark in pairs(gutter_extmarks) do + if extmark[4].sign_text == "G " then + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq(2, extmark[2]) + else + assert(false, "unexpected sign text") + end + end + eq(vim.api.nvim_win_get_cursor(0)[1] - 1, extmarks[1]) eq("b e w $", extmarks[3].virt_lines[1][1][1]) @@ -71,11 +107,45 @@ describe("e2e tests", function() vim.api.nvim_win_set_cursor(0, { 2, 1 }) precognition.on_cursor_moved() + gutter_extmarks = get_gutter_extmarks(buffer) + extmarks = vim.api.nvim_buf_get_extmark_by_id(buffer, precognition.ns, precognition.extmark, { details = true, }) + for _, extmark in pairs(gutter_extmarks) do + if extmark[4].sign_text == "G " then + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq(2, extmark[2]) + else + assert(false, "unexpected sign text") + end + end + eq(vim.api.nvim_win_get_cursor(0)[1] - 1, extmarks[1]) eq("b e w", extmarks[3].virt_lines[1][1][1]) + + vim.api.nvim_win_set_cursor(0, { 4, 1 }) + precognition.on_cursor_moved() + gutter_extmarks = get_gutter_extmarks(buffer) + + for _, extmark in pairs(gutter_extmarks) do + if extmark[4].sign_text == "G " then + eq(5, extmark[2]) + elseif extmark[4].sign_text == "gg" then + eq(0, extmark[2]) + elseif extmark[4].sign_text == "{ " then + eq(2, extmark[2]) + elseif extmark[4].sign_text == "} " then + eq(4, extmark[2]) + else + assert(false, "unexpected sign text") + end + end end) end)