martanne / vis

A vi-like editor based on Plan 9's structural regular expressions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Complete file name and file path swapped in doc

lobre opened this issue · comments

Doing :help gives me this:

<C-x><C-f>            Complete file path                                                                                                                                                                                             
<C-x><C-o>            Complete file name

Trying myself, it seems it is the opposite in fact:

<C-x><C-f>            Complete file name                                                                                                                                                                                             
<C-x><C-o>            Complete file path

I don't know if it is the implementation or doc that is wrong.

It is all in the script lua/plugins/complete-filename.lua (which badly cries for the refactoring love), and I wonder what’s the actual difference between two commands. Documentation has a lot to be desired.

The difference isn't that big. Assuming $PWD = /usr:

<C-x><C-f>: bin/ --> bin/awk
<C-x><C-o>: bin/ --> /usr/bin/awk

Here is my first attempt at cleaning it up, but it could probably be better:

local complete_filename = function(expand)
	local win = vis.win
	local file = win.file
	local pos = win.selection.pos
	if not pos then return end

	-- TODO do something clever here
	local range = file:text_object_longword(pos > 0 and pos-1 or pos);
	if not range then return end
	if range.finish > pos then range.finish = pos end
	if not expand and range.start == range.finish then return end

	local prefix = file:content(range)
	if not prefix then return end

	-- Strip leading delimiters for some progamming languages
	local _, j = prefix:find("[[(<'\"]+")
	if not expand and j then prefix = prefix:sub(j + 1) end
	if expand and prefix:match("^%s*$") then
		prefix = ""
		range.start = pos
		range.finish = pos
	end

	local cmdfmt = "vis-complete --file '%s'"
	if expand then cmdfmt = "vis-open -- '%s'*" end
	local status, out, err = vis:pipe(cmdfmt:format(prefix:gsub("'", "'\\''")))
	if status ~= 0 or not out then
		if err then vis:info(err) end
		return
	end
	out = out:gsub("\n$", "")

	if expand then
		file:delete(range)
		file:insert(range.start, out)
		win.selection.pos = range.start + #out
	else
		file:insert(pos, out)
		win.selection.pos = pos + #out
	end
end

-- complete file path at primary selection location using vis-complete(1)
vis:map(vis.modes.INSERT, "<C-x><C-f>", function()
	complete_filename(false);
end, "Complete file name")

-- complete file path at primary selection location using vis-open(1)
vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
	complete_filename(true);
end, "Complete file name (expands path)")

Perhaps PR would be better?

Done: #1148. I didn't use yours because your repo has some commit adding expansion of ~ to $HOME. Its probably fine but should be added as a separate commit.