aboutsummaryrefslogtreecommitdiff
path: root/lua/plugins/complete-filename.lua
diff options
context:
space:
mode:
authorRandy Palamar <randy@rnpnr.xyz>2023-10-13 06:40:13 -0600
committerRandy Palamar <randy@rnpnr.xyz>2023-11-03 09:31:55 -0600
commit1e64b1c13aa0f4b41f564223b6c0529a56959f85 (patch)
treea758e4ab4c59b6f5ed11c01ec5ae0611847ec207 /lua/plugins/complete-filename.lua
parent18c72a152a196920bfd8804b35ac8b278c373437 (diff)
downloadvis-1e64b1c13aa0f4b41f564223b6c0529a56959f85.tar.gz
vis-1e64b1c13aa0f4b41f564223b6c0529a56959f85.tar.xz
lua: refactor complete-filename plugin
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>`. Some differences were removed: * whitespace in range is treated the same for both actions * empty range will expand to files in CWD for both actions closes #1146: Complete file name and file path swapped in doc
Diffstat (limited to 'lua/plugins/complete-filename.lua')
-rw-r--r--lua/plugins/complete-filename.lua61
1 files changed, 28 insertions, 33 deletions
diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua
index e955347..6b842af 100644
--- a/lua/plugins/complete-filename.lua
+++ b/lua/plugins/complete-filename.lua
@@ -1,56 +1,51 @@
--- 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)
+ -- Strip leading delimiters for some progamming languages
+ local _, j = prefix:find("[[(<'\"]+")
+ if not expand and j then prefix = prefix:sub(j + 1) end
-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
- local prefix = file:content(range)
- if not prefix then return end
if 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)
+ pos = range.start
+ end
+
+ file:insert(pos, out)
+ win.selection.pos = pos + #out
+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)")