|
| 1 | +describe("cursorless tests", function() |
| 2 | + local cursorless = require("cursorless.cursorless") |
| 3 | + |
| 4 | + local get_selected_text = function() |
| 5 | + local _, ls, cs = unpack(vim.fn.getpos("v")) |
| 6 | + local _, le, ce = unpack(vim.fn.getpos(".")) |
| 7 | + return vim.api.nvim_buf_get_text(0, ls - 1, cs - 1, le - 1, ce, {}) |
| 8 | + end |
| 9 | + |
| 10 | + local function convert_table_entries(tbl, func) |
| 11 | + local mapped = {} |
| 12 | + for k, v in pairs(tbl) do |
| 13 | + mapped[k] = func(v) |
| 14 | + end |
| 15 | + return mapped |
| 16 | + end |
| 17 | + |
| 18 | + describe("window_get_visible_lines()", function() |
| 19 | + before_each(function() end) |
| 20 | + |
| 21 | + it("Can read one visible line", function() |
| 22 | + local pos = vim.api.nvim_win_get_cursor(0)[2] |
| 23 | + local line = vim.api.nvim_get_current_line() |
| 24 | + local nline = line:sub(0, pos) .. "hello" .. line:sub(pos + 1) |
| 25 | + vim.api.nvim_set_current_line(nline) |
| 26 | + |
| 27 | + local visible = cursorless.window_get_visible_lines() |
| 28 | + assert(table.concat(visible) == table.concat({ 1, 1 })) |
| 29 | + end) |
| 30 | + |
| 31 | + it("Can read all lines visible on the window", function() |
| 32 | + local maxlines = vim.api.nvim_win_get_height(0) |
| 33 | + local lines = {} |
| 34 | + for _ = 1, (maxlines + 1) do |
| 35 | + table.insert(lines, "hello ") |
| 36 | + end |
| 37 | + vim.api.nvim_buf_set_lines(0, 0, -1, false, lines) |
| 38 | + local visible = cursorless.window_get_visible_lines() |
| 39 | + assert(table.concat(visible) == table.concat({ 1, maxlines })) |
| 40 | + end) |
| 41 | + end) |
| 42 | + describe("select_range()", function() |
| 43 | + it("Selects the specified range", function() |
| 44 | + local lines = "hello world" |
| 45 | + vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n")) |
| 46 | + cursorless.select_range(1, 2, 1, 4) |
| 47 | + |
| 48 | + assert(table.concat(get_selected_text()) == "llo") |
| 49 | + end) |
| 50 | + end) |
| 51 | + describe("buffer_get_selection()", function() |
| 52 | + it( |
| 53 | + "Can get the forward selection in a format expected by cursorless", |
| 54 | + function() |
| 55 | + local lines = "hello world" |
| 56 | + vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n")) |
| 57 | + cursorless.select_range(1, 2, 1, 4) |
| 58 | + assert( |
| 59 | + table.concat( |
| 60 | + convert_table_entries(cursorless.buffer_get_selection(), tostring), |
| 61 | + ", " |
| 62 | + ) |
| 63 | + == table.concat( |
| 64 | + convert_table_entries({ 1, 3, 1, 5, false }, tostring), |
| 65 | + ", " |
| 66 | + ) |
| 67 | + ) |
| 68 | + end |
| 69 | + ) |
| 70 | + it( |
| 71 | + "Can get the backward selection in a format expected by cursorless", |
| 72 | + function() |
| 73 | + local lines = "hello world" |
| 74 | + vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n")) |
| 75 | + cursorless.select_range(1, 4, 1, 2) |
| 76 | + print( |
| 77 | + table.concat( |
| 78 | + convert_table_entries(cursorless.buffer_get_selection(), tostring), |
| 79 | + ", " |
| 80 | + ) |
| 81 | + ) |
| 82 | + assert( |
| 83 | + table.concat( |
| 84 | + convert_table_entries(cursorless.buffer_get_selection(), tostring), |
| 85 | + ", " |
| 86 | + ) |
| 87 | + == table.concat( |
| 88 | + convert_table_entries({ 1, 3, 1, 5, true }, tostring), |
| 89 | + ", " |
| 90 | + ) |
| 91 | + ) |
| 92 | + end |
| 93 | + ) |
| 94 | + end) |
| 95 | +end) |
0 commit comments