neovim

Personal neovim configuration files
git clone git://gtms.dev/neovim
Log | Files | Refs

line.lua (1423B)


      1 local M = {}
      2 
      3 local function get_line_starts(winid, skip_range)
      4   local wininfo = vim.fn.getwininfo(winid)[1]
      5   local cur_line = vim.fn.line('.')
      6   -- Skip lines close to the cursor.
      7   local skip_range = skip_range or 2
      8 
      9   -- Get targets.
     10   local targets = {}
     11   local lnum = wininfo.topline
     12   while lnum <= wininfo.botline do
     13     local fold_end = vim.fn.foldclosedend(lnum)
     14     -- Skip folded ranges.
     15     if fold_end ~= -1 then
     16       lnum = fold_end + 1
     17     else
     18       if (lnum < cur_line - skip_range) or (lnum > cur_line + skip_range) then
     19         table.insert(targets, { pos = { lnum, 1 } })
     20       end
     21       lnum = lnum + 1
     22     end
     23   end
     24 
     25   -- Sort them by vertical screen distance from cursor.
     26   local cur_screen_row = vim.fn.screenpos(winid, cur_line, 1)['row']
     27   local function screen_rows_from_cur(t)
     28     local t_screen_row = vim.fn.screenpos(winid, t.pos[1], t.pos[2])['row']
     29     return math.abs(cur_screen_row - t_screen_row)
     30   end
     31   table.sort(targets, function(t1, t2)
     32     return screen_rows_from_cur(t1) < screen_rows_from_cur(t2)
     33   end)
     34 
     35   if #targets >= 1 then
     36     return targets
     37   end
     38 end
     39 
     40 -- You can pass an argument to specify a range to be skipped
     41 -- before/after the cursor (default is +/-2).
     42 function M.leap_line_start(skip_range)
     43   local winid = vim.api.nvim_get_current_win()
     44   require('leap').leap {
     45     target_windows = { winid },
     46     targets = get_line_starts(winid, skip_range),
     47   }
     48 end
     49 
     50 return M