Skip to content

Commit

Permalink
fixes martanne#949
Browse files Browse the repository at this point in the history
Autocomplete in INSERT mode by <C-n> completes all selections based on
the suggestions generated by the input at the primary selection's cursor.
  • Loading branch information
sauterp authored and erf committed Jun 8, 2023
1 parent 010ae32 commit 4fa7ab9
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lua/plugins/complete-word.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-- 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

vis:map(vis.modes.INSERT, "<C-n>", function()
local win = vis.win
Expand All @@ -17,6 +19,20 @@ vis:map(vis.modes.INSERT, "<C-n>", function()
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
if not pos then goto continue end

local range = file:text_object_word(pos > 0 and pos-1 or pos);
if not range then goto continue end
if range.finish > pos then range.finish = pos end
if range.start == range.finish then goto continue end
local localprefix = file:content(range)
if not localprefix 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")

0 comments on commit 4fa7ab9

Please sign in to comment.