diff options
| author | Randy Palamar <randy@rnpnr.xyz> | 2024-02-26 06:41:40 -0700 |
|---|---|---|
| committer | Randy Palamar <randy@rnpnr.xyz> | 2024-03-08 17:51:00 -0700 |
| commit | 004800e348cba1bf5f4536876b4e0eafbcf1ea58 (patch) | |
| tree | 7f7d3fe0dc72f9dd323a84b54624c8b4e76b68b8 | |
| parent | 64c0092d6c17c769b502bc60850f0a5c42792f3a (diff) | |
| download | vis-004800e348cba1bf5f4536876b4e0eafbcf1ea58.tar.gz vis-004800e348cba1bf5f4536876b4e0eafbcf1ea58.tar.xz | |
lua: complete-word: use internal regex for splitting words
Internally vis supports unicode just fine. Instead of relying on
external programs utilize vis' own features.
Thanks to Florian Fischer for the correct regex!
| -rw-r--r-- | lua/plugins/complete-word.lua | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/lua/plugins/complete-word.lua b/lua/plugins/complete-word.lua index 41d112c..7812751 100644 --- a/lua/plugins/complete-word.lua +++ b/lua/plugins/complete-word.lua @@ -5,18 +5,32 @@ vis:map(vis.modes.INSERT, "<C-n>", function() 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 - local cmd = string.format("vis-complete --word '%s'", prefix:gsub("'", "'\\''")) - local status, out, err = vis:pipe(file, { start = 0, finish = file.size }, cmd) + + vis:feedkeys("<vis-selections-save><Escape><Escape>") + -- collect words starting with prefix + vis:command("x/\\b" .. prefix .. "\\w+/") + local candidates = {} + for sel in win:selections_iterator() do + table.insert(candidates, file:content(sel.range)) + end + vis:feedkeys("<Escape><Escape><vis-selections-restore>") + if #candidates == 1 and candidates[1] == "\n" then return end + candidates = table.concat(candidates, "\n") + + local cmd = "printf '" .. candidates .. "' | sort -u | vis-menu" + local status, out, err = vis:pipe(cmd) if status ~= 0 or not out then if err then vis:info(err) end return end + out = out:sub(#prefix + 1, #out - 1) file:insert(pos, out) win.selection.pos = pos + #out end, "Complete word in file") |
