Skip to content

Commit

Permalink
Make helpers.move_cursor always ensure we're really moving the cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Oct 22, 2023
1 parent 186a664 commit a72d656
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lua/lean/_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ function M.load_mappings(mappings, buffer)
end
end

--- Build a single-line string out a multiline one, replacing \n with spaces.
function M.s(str)
return M.dedent(str):gsub('\n', ' ')
end

-- from mfussenegger/nvim-lsp-compl@29a81f3
function M.mk_handler(fn)
return function(...)
Expand Down
13 changes: 12 additions & 1 deletion lua/tests/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ end
---
---@param opts MoveCursorOpts
function helpers.move_cursor(opts)
vim.api.nvim_win_set_cursor(opts.window or 0, opts.to)
local window = opts.window or 0

if not vim.deep_equal(opts.to, vim.api.nvim_win_get_cursor(window)) then
local message = util.s[[
Cursor is already at %s.
If you just want to ensure the cursor is at this location,
use nvim_win_set_cursor directly.
]]
error(message:format(vim.inspect(opts.to)))
end

vim.api.nvim_win_set_cursor(window, opts.to)
vim.cmd.doautocmd('CursorMoved')
end

Expand Down
3 changes: 0 additions & 3 deletions lua/tests/infoview/contents_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ describe('infoview content (auto-)update', function()
end)

it('updates when the cursor moves', function()
assert.are_not.same(vim.api.nvim_win_get_cursor(0), {3, 0})

helpers.move_cursor{ to = {3, 0} }
-- FIXME: Trailing extra newline.
assert.infoview_contents.are[[
Expand Down Expand Up @@ -101,7 +99,6 @@ describe('infoview content (auto-)update', function()

it('does not error while closed and continues updating when reopened', function()
assert.windows.are(lean_window, infoview.get_current_infoview().window)
assert.are_not.same(vim.api.nvim_win_get_cursor(0), {1, 0})

infoview.close()

Expand Down
4 changes: 2 additions & 2 deletions lua/tests/infoview/widgets_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('infoview widgets', helpers.clean_buffer('#check Nat', function()
]]

vim.api.nvim_set_current_win(current_infoview.window)
helpers.move_cursor{ to = {2, 4} } -- `Type`
helpers.move_cursor{ to = {2, 5} } -- `Type`

local known_windows = { lean_window, current_infoview.window }
assert.windows.are(known_windows)
Expand All @@ -47,7 +47,7 @@ describe('infoview widgets', helpers.clean_buffer('#check Nat', function()
helpers.move_cursor{ to = {1, 8} }
helpers.wait_for_loading_pins()
vim.api.nvim_set_current_win(tab2_infoview.window)
helpers.move_cursor{ to = {2, 4} } -- `Type`
helpers.move_cursor{ to = {2, 5} } -- `Type`
helpers.feed('<CR>')

helpers.wait_for_new_window({ tab2_window, tab2_infoview.window })
Expand Down

0 comments on commit a72d656

Please sign in to comment.