summaryrefslogtreecommitdiff
path: root/lua/warp.lua
blob: 874fded465ddfa859afe72767823cc6ebee6d1d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local vim = assert(vim)
local M = {}

M.run = function(win_selector, row_selector, col_selector)
  if not row_selector then
    row_selector = require('warp.row_selectors.grid')
  end

  if not col_selector then
    col_selector = require('warp.col_selectors.grid')
  end

  if not win_selector then
    win_selector = require('warp.win_selectors.null')
  end

  local current_pos = vim.api.nvim_win_get_cursor(0)
  local old_scroll = vim.o.scrolloff
  vim.o.scrolloff = 0

  local f = function()
    local jump_to_win = win_selector.select_win()
    if not jump_to_win then
      return
    end
    vim.cmd([[redraw!]])
    if jump_to_win ~= 0 then
      vim.api.nvim_set_current_win(jump_to_win)
    end

    local r = row_selector.read_row()
    if not r then return end
    if r == '.' then
      r = current_pos[1]
    end

    vim.cmd("normal! m'")
    vim.api.nvim_win_set_cursor(0, {r, current_pos[2]})

    col_selector.run()
  end
  f ()

  vim.o.scrolloff = old_scroll
end

return M