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

feat: sided region selection #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions lua/tsht.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ end
local function region(opts)
api.nvim_buf_clear_namespace(0, ns, 0, -1)
opts = opts or {}
local region_start = opts.side == nil or opts.side == 'start'
local region_end = opts.side == nil or opts.side == 'end'
local nodes = get_nodes(opts)
local iter = keys_iter()
local hints = {}
Expand All @@ -230,14 +232,18 @@ local function region(opts)
local start_col = node[2]
local end_row = node[3]
local end_col = node[4]
api.nvim_buf_set_extmark(0, ns, start_row, start_col, {
virt_text = {{key, 'TSNodeKey'}},
virt_text_pos = 'overlay'
})
api.nvim_buf_set_extmark(0, ns, end_row, end_col, {
virt_text = {{key, 'TSNodeKey'}},
virt_text_pos = 'overlay'
})
if region_start then
api.nvim_buf_set_extmark(0, ns, start_row, start_col, {
virt_text = {{key, 'TSNodeKey'}},
virt_text_pos = 'overlay'
})
end
if region_end then
api.nvim_buf_set_extmark(0, ns, end_row, end_col, {
virt_text = {{key, 'TSNodeKey'}},
virt_text_pos = 'overlay'
})
end
hints[key] = node
end
vim.cmd('redraw')
Expand All @@ -252,21 +258,28 @@ local function region(opts)
local node = hints[key]
if node then
local start_row, start_col, end_row, end_col = unpack(node)
api.nvim_win_set_cursor(0, { start_row + 1, start_col })
vim.cmd('normal! v')
local max_row = api.nvim_buf_line_count(0)
if max_row == end_row then
end_row = end_row - 1
end_col = #(api.nvim_buf_get_lines(0, end_row, end_row + 1, true)[1])
elseif end_col == 0 then
-- If the end points to the start of the next line, move it to the
-- end of the previous line.
-- Otherwise operations include the first character of the next line
local end_line = api.nvim_buf_get_lines(0, end_row - 1, end_row, true)[1]
end_row = end_row - 1
end_col = #end_line
if not region_end then
vim.cmd('normal! v')
end
if region_start then
api.nvim_win_set_cursor(0, { start_row + 1, start_col })
end
if region_end then
vim.cmd('normal! v')
local max_row = api.nvim_buf_line_count(0)
if max_row == end_row then
end_row = end_row - 1
end_col = #(api.nvim_buf_get_lines(0, end_row, end_row + 1, true)[1])
elseif end_col == 0 then
-- If the end points to the start of the next line, move it to the
-- end of the previous line.
-- Otherwise operations include the first character of the next line
local end_line = api.nvim_buf_get_lines(0, end_row - 1, end_row, true)[1]
end_row = end_row - 1
end_col = #end_line
end
api.nvim_win_set_cursor(0, { end_row + 1, math.max(0, end_col - 1) })
end
api.nvim_win_set_cursor(0, { end_row + 1, math.max(0, end_col - 1) })
api.nvim_buf_clear_namespace(0, ns, 0, -1)
break
else
Expand Down