Skip to content

Commit

Permalink
lua: refactor complete-filename plugin
Browse files Browse the repository at this point in the history
There are probably more things to simplify but at least this makes
it easier to see what exactly is different between `<C-x><C-f>` and
`<C-x><C-o>`.

closes martanne#1146
  • Loading branch information
rnpnr committed Oct 13, 2023
1 parent aa18162 commit 2681bf9
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions lua/plugins/complete-filename.lua
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
-- complete file path at primary selection location using vis-complete(1)

vis:map(vis.modes.INSERT, "<C-x><C-f>", function()
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 range.start == range.finish then return end
local prefix = file:content(range)
if not prefix then return end
-- Strip leading delimiters for some languages
local _, j = string.find(prefix, "[[(<'\"]+")
if j then prefix = prefix:sub(j + 1) end
local cmd = string.format("vis-complete --file '%s'", prefix:gsub("'", "'\\''"))
local status, out, err = vis:pipe(file, { start = 0, finish = 0 }, cmd)
if status ~= 0 or not out then
if err then vis:info(err) end
return
end
file:insert(pos, out)
win.selection.pos = pos + #out
end, "Complete file path")

-- complete file path at primary selection location using vis-open(1)

vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
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
if prefix:match("^%s*$") then

-- 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 cmd = string.format("vis-open -- '%s'*", prefix:gsub("'", "'\\''"))
local status, out, err = vis:pipe(file, { start = 0, finish = 0 }, cmd)

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$", "")
file:delete(range)
file:insert(range.start, out)
win.selection.pos = range.start + #out

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)")

0 comments on commit 2681bf9

Please sign in to comment.