goto-ref.lua (1499B)
1 local M = {} 2 3 local focus_file = function(name) 4 local realpath = io.popen("realpath " .. name):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.generate_iterators = function(file_index_table) 15 local current_index = 1; 16 17 local iterate = function(inc) 18 local file, line, col = table.unpack(file_index_table[current_index]) 19 focus_file(file) 20 vis.win.selection:to(line, type(col) == 'number' and col or 1) 21 current_index = current_index + inc 22 if current_index > #file_index_table then 23 current_index = 1 24 end 25 if current_index < 1 then 26 current_index = #file_index_table 27 end 28 end 29 30 local forward = function() iterate(1) end 31 local backward = function() iterate(-1) end 32 return forward, backward 33 end 34 35 M.generate_line_indices = function(data, filter) 36 local ret = {} 37 for s in data:gmatch("[^\n]*") do 38 local skip = filter and filter(s) 39 if not skip then 40 local found, _, file, line, col = s:find('^([^:]+):([%d]+):([%d]*):?') 41 if found then table.insert(ret, {file, line, col}) end 42 end 43 end 44 return ret 45 end 46 47 M.setup_iterators_from_text = function(text, filter) 48 if text == nil or #text == 0 then return end 49 local filepairs = M.generate_line_indices(text, filter) 50 if #filepairs then 51 local forward, backward = M.generate_iterators(filepairs) 52 vis:map(vis.modes.NORMAL, "gn", forward) 53 vis:map(vis.modes.NORMAL, "gp", backward) 54 end 55 end 56 57 return M