|
| 1 | +-- Shared test utilities for smart-motion.nvim integration tests |
| 2 | + |
| 3 | +local M = {} |
| 4 | + |
| 5 | +--- Default test config that disables timing-dependent features |
| 6 | +M.test_config = { |
| 7 | + keys = "fjdksleirughtynm", |
| 8 | + flow_state_timeout_ms = 0, |
| 9 | + native_search = false, |
| 10 | + auto_select_target = false, |
| 11 | + dim_background = false, |
| 12 | + presets = {}, |
| 13 | +} |
| 14 | + |
| 15 | +--- Setup the plugin with optional config overrides |
| 16 | +---@param overrides? table |
| 17 | +function M.setup_plugin(overrides) |
| 18 | + local config = vim.tbl_deep_extend("force", vim.deepcopy(M.test_config), overrides or {}) |
| 19 | + require("smart-motion").setup(config) |
| 20 | +end |
| 21 | + |
| 22 | +--- Create a scratch buffer with the given lines and set it as current |
| 23 | +---@param lines string[] |
| 24 | +---@return integer bufnr |
| 25 | +function M.create_buf(lines) |
| 26 | + local bufnr = vim.api.nvim_create_buf(false, true) |
| 27 | + vim.api.nvim_set_current_buf(bufnr) |
| 28 | + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) |
| 29 | + vim.bo[bufnr].filetype = "text" |
| 30 | + return bufnr |
| 31 | +end |
| 32 | + |
| 33 | +--- Set cursor position (1-indexed row, 0-indexed col) |
| 34 | +---@param row integer |
| 35 | +---@param col integer |
| 36 | +function M.set_cursor(row, col) |
| 37 | + vim.api.nvim_win_set_cursor(0, { row, col }) |
| 38 | +end |
| 39 | + |
| 40 | +--- Get cursor position (1-indexed row, 0-indexed col) |
| 41 | +---@return integer row, integer col |
| 42 | +function M.get_cursor() |
| 43 | + local pos = vim.api.nvim_win_get_cursor(0) |
| 44 | + return pos[1], pos[2] |
| 45 | +end |
| 46 | + |
| 47 | +--- Get the contents of a register |
| 48 | +---@param reg string |
| 49 | +---@return string |
| 50 | +function M.get_register(reg) |
| 51 | + return vim.fn.getreg(reg) |
| 52 | +end |
| 53 | + |
| 54 | +--- Get all lines from the current buffer |
| 55 | +---@return string[] |
| 56 | +function M.get_buf_lines() |
| 57 | + return vim.api.nvim_buf_get_lines(0, 0, -1, false) |
| 58 | +end |
| 59 | + |
| 60 | +--- Build a minimal SmartMotionContext |
| 61 | +---@param overrides? table |
| 62 | +---@return SmartMotionContext |
| 63 | +function M.build_ctx(overrides) |
| 64 | + local bufnr = vim.api.nvim_get_current_buf() |
| 65 | + local winid = vim.api.nvim_get_current_win() |
| 66 | + local cursor = vim.api.nvim_win_get_cursor(winid) |
| 67 | + local last_line = vim.api.nvim_buf_line_count(bufnr) |
| 68 | + |
| 69 | + local ctx = { |
| 70 | + bufnr = bufnr, |
| 71 | + winid = winid, |
| 72 | + cursor_line = cursor[1] - 1, -- 0-indexed |
| 73 | + cursor_col = cursor[2], |
| 74 | + last_line = last_line, |
| 75 | + } |
| 76 | + |
| 77 | + if overrides then |
| 78 | + ctx = vim.tbl_extend("force", ctx, overrides) |
| 79 | + end |
| 80 | + |
| 81 | + return ctx |
| 82 | +end |
| 83 | + |
| 84 | +--- Clean up all scratch buffers and reset plugin state |
| 85 | +function M.cleanup() |
| 86 | + -- Delete all scratch buffers |
| 87 | + for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do |
| 88 | + if vim.api.nvim_buf_is_valid(bufnr) then |
| 89 | + pcall(vim.api.nvim_buf_delete, bufnr, { force = true }) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + -- Clear relevant package.loaded entries for fresh state between test files |
| 94 | + for key, _ in pairs(package.loaded) do |
| 95 | + if key:match("^smart%-motion") then |
| 96 | + package.loaded[key] = nil |
| 97 | + end |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +return M |
0 commit comments