summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-03-10 00:41:02 -0700
committerJosh Rahm <joshuarahm@gmail.com>2024-03-10 00:41:02 -0700
commit92ca3c6dcfa4f6d9d3c729e3c11ca1e62a51e4d6 (patch)
tree38185a950659fc4327f28b7fe5ff8ac5a0dc84ad /lua
parent894306991ff8bf2313da5df3c7782c2d1719e72e (diff)
downloadnvim-warp-92ca3c6dcfa4f6d9d3c729e3c11ca1e62a51e4d6.tar.gz
nvim-warp-92ca3c6dcfa4f6d9d3c729e3c11ca1e62a51e4d6.tar.bz2
nvim-warp-92ca3c6dcfa4f6d9d3c729e3c11ca1e62a51e4d6.zip
Plugin working better
Diffstat (limited to 'lua')
-rw-r--r--lua/warp.lua105
1 files changed, 67 insertions, 38 deletions
diff --git a/lua/warp.lua b/lua/warp.lua
index b123fdb..5c50c25 100644
--- a/lua/warp.lua
+++ b/lua/warp.lua
@@ -16,32 +16,34 @@ M.open_vertical = function()
local current_win = vim.api.nvim_get_current_win()
local height = vim.api.nvim_win_get_height(current_win)
local topline = vim.fn.line('w0')
+ local width_of_garbage = vim.fn.getwininfo(current_win)[1].textoff
M.row_map = {}
- local bufnr = vim.api.nvim_create_buf(0, 1)
+ M.vert_buf = vim.api.nvim_create_buf(0, 1)
local line = 0
- local text = {}
+ M.vert_lines = {}
+ local real_width = math.max(3, width_of_garbage)
while line < height do
local t = char_at(cons, line) .. char_at(vowel, line)
- table.insert(text, ' ' .. t)
+ table.insert(M.vert_lines, (' '):rep(real_width-3) .. t .. ' ')
M.row_map[t] = topline + line
line = line + 1
end
- vim.api.nvim_buf_set_lines(bufnr, 0, -1, 0, text)
+ vim.api.nvim_buf_set_lines(M.vert_buf, 0, -1, 0, M.vert_lines)
local vert_win = vim.api.nvim_open_win(
- bufnr,
+ M.vert_buf,
false,
{
relative = 'win',
row = 0,
col = 0,
- width = 4,
+ width = real_width,
height = height
})
- vim.api.nvim_win_set_option(vert_win, "winhighlight", "Normal:Salmon")
+ vim.api.nvim_win_set_option(vert_win, "winhighlight", "Normal:WarpNormal")
vim.api.nvim_win_set_option(vert_win, "number", false) -- Hide line numbers
vim.api.nvim_win_set_option(vert_win, "relativenumber", false) -- Hide relative numbers
vim.api.nvim_win_set_option(vert_win, "wrap", false) -- Disable wrapping
@@ -55,8 +57,8 @@ M.open_horiz = function()
local curpos = vim.api.nvim_win_get_cursor(0)
local topline = vim.fn.line('w0')
- local width = vim.api.nvim_win_get_width(current_win)
local width_of_garbage = vim.fn.getwininfo(current_win)[1].textoff
+ local width = vim.api.nvim_win_get_width(current_win) - width_of_garbage
M.horiz_bufnr = vim.api.nvim_create_buf(0, 1)
local col = 0
@@ -67,8 +69,10 @@ M.open_horiz = function()
local v = ''
local c = nil
local i = 0
+ local line_at = vim.fn.getline(curpos[1])
M.col_map = {}
- while col < width do
+ local max_width = math.min(width, #line_at + 1)
+ while col < max_width do
v = char_at(hsel1, i)
if c then
if M.col_map [c .. v] then
@@ -80,12 +84,17 @@ M.open_horiz = function()
if M.col_map [v .. c] then
break
end
- M.col_map[v .. c] = col
- line = line .. v .. c
- col = col + 2
- i = i + 1
+ line = line .. v
+ col = col + 1
+ if col < max_width then
+ line = line .. c
+ M.col_map[v .. c] = col
+ col = col + 1
+ i = i + 1
+ end
end
- vim.api.nvim_buf_set_lines(M.horiz_bufnr, 0, -1, 0, { line })
+
+ vim.api.nvim_buf_set_lines(M.horiz_bufnr, 0, -1, 0, { ' ' .. line })
M.horiz_line = line
local horiz_win = vim.api.nvim_open_win(
@@ -94,11 +103,11 @@ M.open_horiz = function()
{
relative = 'win',
row = curpos[1] - topline + 1,
- col = width_of_garbage, -- -curpos[2],
- width = width - width_of_garbage,
+ col = width_of_garbage - 1, -- -curpos[2],
+ width = max_width + 2,
height = 1
})
- vim.api.nvim_win_set_option(horiz_win, "winhighlight", "Normal:Salmon")
+ vim.api.nvim_win_set_option(horiz_win, "winhighlight", "Normal:WarpNormal")
vim.api.nvim_win_set_option(horiz_win, "number", false) -- Hide line numbers
vim.api.nvim_win_set_option(horiz_win, "relativenumber", false) -- Hide relative numbers
@@ -111,6 +120,7 @@ end
M.filter_horiz_string = function(ch)
local new_horiz_line = ''
local i = 1
+
while i <= #M.horiz_line do
local curch = char_at(M.horiz_line, i - 1)
if curch == ch then
@@ -120,7 +130,24 @@ M.filter_horiz_string = function(ch)
new_horiz_line = new_horiz_line .. ' '
i = i + 1
end
- vim.api.nvim_buf_set_lines(M.horiz_bufnr, 0, -1, 0, { new_horiz_line })
+ vim.api.nvim_buf_set_lines(M.horiz_bufnr, 0, -1, 0, { ' ' .. new_horiz_line })
+end
+
+M.filter_vert_text = function (ch)
+ local new_vert_text = {}
+ for _, t in pairs(M.vert_lines) do
+ local str = t:gsub("^%s*" .. ch, function (m)
+ return (' '):rep(#m)
+ end)
+
+ if t:match('^%s*' .. ch) then
+ table.insert(new_vert_text, str)
+ else
+ table.insert(new_vert_text, "")
+ end
+ end
+ M.vert_lines = new_vert_text
+ vim.api.nvim_buf_set_lines(M.vert_buf, 0, -1, 0, M.vert_lines)
end
local function next_char(callback)
@@ -136,26 +163,28 @@ M.run = function()
local current_pos = vim.api.nvim_win_get_cursor(0)
next_char(function(vch1)
- next_char(function(vch2)
- vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(w1), { force = true })
- -- vim.api.nvim_win_close(w1, 1)
- local r = M.row_map[vch1 .. vch2]
- if r then
- vim.api.nvim_win_set_cursor(0, {r, current_pos[2]})
- local w2 = M.open_horiz()
- next_char(function (hch1)
- M.filter_horiz_string(hch1)
- vim.cmd("redraw!")
- local hch2 = vim.fn.nr2char(vim.fn.getchar())
- local c = M.col_map[hch1 .. hch2]
- vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(w2), { force = true })
- -- vim.api.nvim_win_close(w2, 1)
- if c then
- vim.api.nvim_win_set_cursor(0, {r, c})
- end
- end)
- end
- end)
+ M.filter_vert_text(vch1)
+ vim.cmd("redraw!")
+ local vch2 = vim.fn.nr2char(vim.fn.getchar())
+ vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(w1), { force = true })
+ -- vim.api.nvim_win_close(w1, 1)
+ local r = M.row_map[vch1 .. vch2]
+ print("ROW: " .. vch1 .. vch2)
+ if r then
+ vim.api.nvim_win_set_cursor(0, {r, current_pos[2]})
+ local w2 = M.open_horiz()
+ next_char(function (hch1)
+ M.filter_horiz_string(hch1)
+ vim.cmd("redraw!")
+ local hch2 = vim.fn.nr2char(vim.fn.getchar())
+ local c = M.col_map[hch1 .. hch2]
+ vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(w2), { force = true })
+ -- vim.api.nvim_win_close(w2, 1)
+ if c then
+ vim.api.nvim_win_set_cursor(0, {r, c})
+ end
+ end)
+ end
end)
end