goto-ref.lua (1773B)
1 local M = {} 2 3 local focus_or_open = function(file) 4 local realpath = io.popen("realpath " .. file):read("*a"):sub(1, -2) 5 for win in vis:windows() do 6 if win.file and win.file.path == realpath then 7 vis.win = win 8 return 9 end 10 end 11 vis:command(":o " .. realpath) 12 end 13 14 M.focus_file_at = function(file, line, col) 15 if file then 16 focus_or_open(file) 17 vis.mode = vis.modes.NORMAL 18 vis.win.selection:to(line and line or 1, col and col or 1) 19 end 20 end 21 22 M.generate_iterators = function(file_index_table) 23 local current_index 24 local iterate = function(inc) 25 if current_index == nil then 26 if inc < 0 then current_index = ( inc % #file_index_table) + 1 27 else current_index = ((inc - 1) % #file_index_table) + 1 end 28 else 29 current_index = current_index + inc 30 current_index = ((current_index - 1) % #file_index_table) + 1 31 end 32 33 M.focus_file_at(table.unpack(file_index_table[current_index])) 34 end 35 36 local forward = function() iterate(1) end 37 local backward = function() iterate(-1) end 38 return forward, backward 39 end 40 41 M.generate_line_indices = function(data, include) 42 local ret = {} 43 for s in data:gmatch("[^\n]*") do 44 local keep = not include or include(s) 45 if keep then 46 local found, _, file, line, col = s:find('^([^:]+):([%d]+):?([%d]*):?') 47 if found then table.insert(ret, {file, line, col}) end 48 end 49 end 50 return ret 51 end 52 53 M.setup_iterators_from_text = function(text, include) 54 vis:unmap(vis.modes.NORMAL, "gn") 55 vis:unmap(vis.modes.NORMAL, "gp") 56 if not text or #text == 0 then return end 57 local filepairs = M.generate_line_indices(text, include) 58 if #filepairs then 59 local forward, backward = M.generate_iterators(filepairs) 60 vis:map(vis.modes.NORMAL, "gn", forward) 61 vis:map(vis.modes.NORMAL, "gp", backward) 62 end 63 end 64 65 return M