summaryrefslogtreecommitdiff
path: root/lua/warp/strategy/words.lua
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-03-10 23:53:04 -0600
committerJosh Rahm <joshuarahm@gmail.com>2024-03-10 23:53:04 -0600
commit2aff69b0bb55f398d199db687617ed35d0335d5b (patch)
tree135b46b6a689b1cb59a49fbbebb8c4a095429ba1 /lua/warp/strategy/words.lua
parent64fb2114f35889eff0a082200471ce3ca33e43bf (diff)
downloadnvim-warp-2aff69b0bb55f398d199db687617ed35d0335d5b.tar.gz
nvim-warp-2aff69b0bb55f398d199db687617ed35d0335d5b.tar.bz2
nvim-warp-2aff69b0bb55f398d199db687617ed35d0335d5b.zip
Added multiple strategies for deteriming the column.
Doing a full grid select can be intimidating. A word-wise strat is probably a good thing to have.
Diffstat (limited to 'lua/warp/strategy/words.lua')
-rw-r--r--lua/warp/strategy/words.lua84
1 files changed, 84 insertions, 0 deletions
diff --git a/lua/warp/strategy/words.lua b/lua/warp/strategy/words.lua
new file mode 100644
index 0000000..d255203
--- /dev/null
+++ b/lua/warp/strategy/words.lua
@@ -0,0 +1,84 @@
+local vim = assert(vim)
+
+local M = {}
+local alphabet = "etansihrdlocumwfgypkbvjxqz"
+
+local function split_lines(line)
+ local i
+ local j = 1
+
+ local chars_to_col = {}
+ local col_to_chars = {}
+
+ local run_regex = function(rx, off)
+ if #alphabet < j then
+ return
+ end
+
+ if not off then
+ off = 0
+ end
+
+ i = 0
+ while true do
+ i = string.find(line, rx, i)
+ if i == nil then break end
+ i = i + 1
+
+ if #alphabet < j then
+ return
+ end
+
+ local nextalph = alphabet:sub(j, j)
+ j = j + 1
+ chars_to_col[nextalph] = i + off
+ col_to_chars[i + off] = nextalph
+ end
+ end
+
+ run_regex("%W%w")
+ run_regex("%s%S")
+ run_regex("%w%W", - 1)
+
+ local winstr = ""
+ i = 1
+ while i <= #line do
+ if col_to_chars[i] then
+ winstr = winstr .. col_to_chars[i]
+ else
+ winstr = winstr .. " "
+ end
+ i = i + 1
+ end
+
+ return winstr, chars_to_col
+end
+
+M.words_strategy = function ()
+ local chartab
+
+ return {
+ display = function ()
+ local curpos = vim.api.nvim_win_get_cursor(0)
+ local line_at = vim.fn.getline(curpos[1])
+ local str, tab = split_lines(line_at)
+ chartab = tab
+ return str
+ end,
+
+ on_char = function (ch)
+ if ch == '$' or ch == '^' then
+ vim.cmd("normal! " .. ch)
+ elseif not ch:match('[a-z]') then
+ vim.cmd("normal! f" .. ch)
+ else
+ if chartab[ch] then
+ vim.cmd("norma! " .. chartab[ch] .. "|")
+ end
+ end
+ return false
+ end
+ }
+end
+
+return M