Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete all selections with <C-n> #950

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions lua/plugins/complete-word.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
-- complete word at primary selection location using vis-complete(1)
-- all non-primary selections will be completed aswell if their cursor
-- is located at the end of the same word

local get_prefix = function (file, pos)
if not pos then return "", false end

local range = file:text_object_word(pos > 0 and pos-1 or pos);
if not range then return "", false end
if range.finish > pos then range.finish = pos end
if range.start == range.finish then return "", false end
local prefix = file:content(range)
if not prefix then return "", false end
return prefix, true
end

vis:map(vis.modes.INSERT, "<C-n>", function()
local win = vis.win
local file = win.file
local pos = win.selection.pos
if not pos then return end
local range = file:text_object_word(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
prefix, ok = get_prefix(file, pos)
if not ok then return end

local cmd = string.format("vis-complete --word '%s'", prefix:gsub("'", "'\\''"))
local status, out, err = vis:pipe(file, { start = 0, finish = file.size }, 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
for selection in win:selections_iterator() do
local pos = selection.pos
localprefix, ok = get_prefix(file, pos)
if not ok then goto continue end
if localprefix ~= prefix then goto continue end

file:insert(pos, out)
selection.pos = pos + #out
::continue::
end
end, "Complete word in file")