~yerinalexey/dotfiles

8ae09a8208a2166b3286b12992e862c747d11d1a — Alexey Yerin 3 months ago 7c75c03
vis: add long-awaited quick import
1 files changed, 57 insertions(+), 0 deletions(-)

M vis/visrc.lua
M vis/visrc.lua => vis/visrc.lua +57 -0
@@ 79,6 79,63 @@ vis:map(vis.modes.VISUAL, ",c", function(keys)
	vis:redraw()
end)

vis:map(vis.modes.NORMAL, ",i", function(keys)
	local win = vis.win
	local vp = win.viewport

	local sel = win.selection
	local s_line = sel.line
	local s_col = sel.col

	local last_import = 0
	local line_nr = 1
	for line in win.file:lines_iterator() do
		if line:match("^import .*$") then
			last_import = line_nr
		end
		line_nr = line_nr + 1
	end

	if last_import == 0 then
		vis:info("Not a Java/Kotlin file?")
		return
	end

	local pos = sel.pos
	if not pos then return end
	local range = win.file:text_object_word(pos)
	if not range then return end
	if range.start == range.finish then return end
	local word = win.file:content(range)
	if not word then return end

	local cmd = io.popen(string.format("jimport %s | fzf -0 -1 -i --height=25%%", word))
	local selected = cmd:read("*all")
	local success, msg, status = cmd:close()
	vis:redraw()
	if not success then
		return
	end

	selected = string.gsub(selected, "\n", "")
	if string.len(selected) == 0 then
		vis:info("No candidates for import")
		return
	end

	sel:to(last_import + 1, 0)
	local line_ending = (win.syntax == "kotlin") and "" or ";"
	win.file:insert(sel.pos, string.format("import %s%s\n", selected, line_ending))

	if s_line > last_import then
		sel:to(s_line + 1, s_col)
	else
		sel:to(s_line, s_col)
	end
	vis.win.viewport = vp
	vis:redraw()
end)

vis:map(vis.modes.INSERT, "<S-Tab>", function(keys)
	vis.mode = vis.modes.NORMAL
	vis:feedkeys("<<")