Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 18 additions & 13 deletions lua/tirenvi/core/tir_vim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ end

---@param line string
---@return integer[]
function M.get_pipe_byte_position(line)
local function get_pipe_byte_position(line)
local indexes = {}
local index = 1
while index <= #line do
Expand Down Expand Up @@ -140,11 +140,6 @@ function M.get_block_bottom_nrow(lines, irow)
return find_block_edge(lines, irow, 1)
end

---@param count integer
---@return Range|nil
function M.get_block_range(count)
end

---@param line string
---@return string[]
function M.get_cells(line)
Expand All @@ -162,29 +157,39 @@ end
---@param lines string[]
---@param count integer
---@param is_around boolean
---@param allow_plain boolean
---@return Range|nil
function M.get_select(lines, count, is_around)
function M.get_block_range(lines, count, is_around, allow_plain)
-- local mode = vim.fn.mode()
local irow, icol0 = unpack(vim.api.nvim_win_get_cursor(0))
local icol = icol0 + 1
local cline = vim.api.nvim_get_current_line()
local cbyte_pos = M.get_pipe_byte_position(cline)
local cbyte_pos = get_pipe_byte_position(cline)
if #cbyte_pos == 0 then
return nil
end
local colIndex = M.get_current_col_index(cbyte_pos, icol)
if not colIndex then
return nil
end
local trow = M.get_block_top_nrow(lines, irow)
local brow = M.get_block_bottom_nrow(lines, irow)
local tbyte_pos = M.get_pipe_byte_position(lines[trow])
local bbyte_pos = M.get_pipe_byte_position(lines[brow])
local trow
local brow
if allow_plain then
trow = M.get_block_top_nrow(lines, irow)
brow = M.get_block_bottom_nrow(lines, irow)
else
trow = 1
brow = #lines
end
local tbyte_pos = get_pipe_byte_position(lines[trow])
local bbyte_pos = get_pipe_byte_position(lines[brow])
local end_index = colIndex + count
end_index = math.min(end_index, #bbyte_pos)
return {
start_row = trow,
end_row = brow,
start_col = tbyte_pos[colIndex] + (is_around and 0 or plen),
end_col = bbyte_pos[colIndex + 1] - 1
end_col = bbyte_pos[end_index] - 1
}
end

Expand Down
25 changes: 19 additions & 6 deletions lua/tirenvi/editor/motion.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local config = require("tirenvi.config")
local buf_state = require("tirenvi.state.buf_state")
local tir_vim = require("tirenvi.core.tir_vim")
local util = require("tirenvi.util.util")

local M = {}

Expand Down Expand Up @@ -29,21 +30,33 @@ M.F = build_motion("F")
M.t = build_motion("t")
M.T = build_motion("T")

function M.g()
function M.block_top()
local bufnr = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local top = tir_vim.get_block_top_nrow(lines, row)
local top
local parser = util.get_parser(bufnr)
if not parser or not parser.allow_plain then
top = 1
else
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
top = tir_vim.get_block_top_nrow(lines, row)
end
vim.api.nvim_win_set_cursor(0, { top, col })
end

function M.G()
function M.block_bottom()
local bufnr = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local bottom = tir_vim.get_block_bottom_nrow(lines, row)
local bottom
local parser = util.get_parser(bufnr)
if not parser or not parser.allow_plain then
bottom = vim.api.nvim_buf_line_count(bufnr)
else
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
bottom = tir_vim.get_block_bottom_nrow(lines, row)
end
vim.api.nvim_win_set_cursor(0, { bottom, col })
end

Expand Down
14 changes: 6 additions & 8 deletions lua/tirenvi/editor/textobj.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
local tir_vim = require("tirenvi.core.tir_vim")
local buffer = require("tirenvi.state.buffer")
local config = require("tirenvi.config")
local util = require("tirenvi.util.util")
local log = require("tirenvi.util.log")

local M = {}

---@param is_around boolean|nil
local function setup_vl(is_around)
if vim.fn.mode() == "n" then
return
end
is_around = is_around or false
local count = vim.v.count1
local lines = buffer.get_lines(0, 0, -1, false)
local pos = tir_vim.get_select(lines, count, is_around)
local parser = util.get_parser()
local pos = tir_vim.get_block_range(lines, count, is_around, parser.allow_plain)
if not pos then
return
end
Expand All @@ -31,13 +30,12 @@ function M.setup_val()
setup_vl(true)
end

-- setup
function M.setup(opts)
vim.keymap.set({ "o", "x" }, "i" .. config.textobj.column, M.setup_vil, {
function M.setup()
vim.keymap.set({ "x" }, "i" .. config.textobj.column, M.setup_vil, {
desc = "Inner column",
})

vim.keymap.set({ "o", "x" }, "a" .. config.textobj.column, M.setup_val, {
vim.keymap.set({ "x" }, "a" .. config.textobj.column, M.setup_val, {
desc = "Around column",
})
end
Expand Down
2 changes: 1 addition & 1 deletion lua/tirenvi/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function M.setup(opts)
config.setup(opts)
require("tirenvi.editor.autocmd").setup()
require("tirenvi.editor.commands").setup()
require("tirenvi.editor.textobj").setup(config)
require("tirenvi.editor.textobj").setup()
require("tirenvi.ui").setup()
end

Expand Down
3 changes: 2 additions & 1 deletion lua/tirenvi/util/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ function M.assert_no_reserved_marks(fl_lines)
end
end

---@param bufnr number
---@param bufnr number|nil
---@return Parser
function M.get_parser(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local parser = get_parser_for_file(bufnr)
if parser == nil then
error(errors.new_domain_error(""))
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/motion/ftg/run.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ vim.keymap.set({ 'n', 'o', 'x' }, 'gtf', require('tirenvi').motion.f, { expr = t
vim.keymap.set({ 'n', 'o', 'x' }, 'gtF', require('tirenvi').motion.F, { expr = true, desc = '[T]irEnvi: F pipe' })
vim.keymap.set({ 'n', 'o', 'x' }, 'gtt', require('tirenvi').motion.t, { expr = true, desc = '[T]irEnvi: t pipe' })
vim.keymap.set({ 'n', 'o', 'x' }, 'gtT', require('tirenvi').motion.T, { expr = true, desc = '[T]irEnvi: T pipe' })
vim.keymap.set('n', 'gtg', require('tirenvi').motion.g, { desc = '[T]irEnvi: block top' })
vim.keymap.set('n', 'gtG', require('tirenvi').motion.G, { desc = '[T]irEnvi: block bottom' })
vim.keymap.set('n', 'gtg', require('tirenvi').motion.block_top, { desc = '[T]irEnvi: block top' })
vim.keymap.set('n', 'gtG', require('tirenvi').motion.block_bottom, { desc = '[T]irEnvi: block bottom' })
EOF

edit $TIRENVI_ROOT/tests/data/complex.md
Expand Down
Loading
Loading