dotfiles

personal dotfiles
git clone anongit@rnpnr.xyz:dotfiles.git
Log | Files | Refs | Feed | Submodules

goto-ref.lua (1593B)


      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 = 1;
     24 
     25 	local iterate = function(inc)
     26 		M.focus_file_at(table.unpack(file_index_table[current_index]))
     27 		current_index = current_index + inc
     28 		if current_index > #file_index_table then
     29 			current_index = 1
     30 		end
     31 		if current_index < 1 then
     32 			current_index = #file_index_table
     33 		end
     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, filter)
     42 	local ret = {}
     43 	for s in data:gmatch("[^\n]*") do
     44 		local skip = filter and filter(s)
     45 		if not skip 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, filter)
     54 	if text == nil or #text == 0 then return end
     55 	local filepairs = M.generate_line_indices(text, filter)
     56 	if #filepairs then
     57 		local forward, backward = M.generate_iterators(filepairs)
     58 		vis:map(vis.modes.NORMAL, "gn", forward)
     59 		vis:map(vis.modes.NORMAL, "gp", backward)
     60 	end
     61 end
     62 
     63 return M