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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sauterp
Copy link

@sauterp sauterp commented May 20, 2021

fixes #949

Autocomplete in INSERT mode by completes all selections based on
the suggestions generated by the input at the primary selection's cursor.

Autocomplete in INSERT mode by <C-n> completes all selections based on
the suggestions generated by the input at the primary selection's cursor.
@Shugyousha
Copy link
Contributor

Refactoring out the common parts like this may be nicer:

diff --git a/lua/plugins/complete-word.lua b/lua/plugins/complete-word.lua
index f0a6ff5..737cabe 100644
--- a/lua/plugins/complete-word.lua
+++ b/lua/plugins/complete-word.lua
@@ -2,17 +2,25 @@
 -- 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
@@ -21,14 +29,8 @@ vis:map(vis.modes.INSERT, "<C-n>", function()
 	end
 	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
+		localprefix, ok = get_prefix(file, pos)
+		if not ok then goto continue end
 		if localprefix ~= prefix then goto continue end
 
 		file:insert(pos, out)

Other than that it looks good to me (with very basic testing)!

Copy link
Contributor

@Shugyousha Shugyousha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope the way to define the helper function is somewhat Lua-style conform (I am not very familiar with the language). This change looks good to me in any case!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

INSERT mode autocomplete by <C-n> only completes the primary selection
2 participants