Skip to content

Commit

Permalink
fix(substitute): correctly substitute text with unicode
Browse files Browse the repository at this point in the history
Fixes: #73
  • Loading branch information
gbprod committed Oct 10, 2023
1 parent 523047f commit bdf0924
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lua/substitute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ end
function substitute.operator_callback(vmode)
local marks = utils.get_marks(0, vmode)

print(vim.inspect(marks))

local substitued_text = utils.text(0, marks.start, marks.finish, vmode)

local regcontents = vim.fn.getreg(substitute.state.register)
Expand Down
25 changes: 20 additions & 5 deletions lua/substitute/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function utils.substitute_text(bufnr, start, finish, regtype, replacement, repla
row - 1,
start.col,
row - 1,
current_row_len > finish.col and finish.col + 1 or current_row_len,
current_row_len > finish.col and utils.get_next_char_bytecol(finish.row, finish.col) or current_row_len,
{ last_replacement }
)

Expand All @@ -75,7 +75,7 @@ function utils.substitute_text(bufnr, start, finish, regtype, replacement, repla
row - 1,
current_row_len > start.col and start.col or current_row_len,
row - 1,
current_row_len > finish.col and finish.col + 1 or current_row_len,
current_row_len > finish.col and utils.get_next_char_bytecol(finish.row, finish.col) or current_row_len,
replacement
)

Expand All @@ -102,7 +102,7 @@ function utils.substitute_text(bufnr, start, finish, regtype, replacement, repla
start.row - 1,
start.col,
finish.row - 1,
current_row_len > finish.col and finish.col + 1 or current_row_len,
current_row_len > finish.col and utils.get_next_char_bytecol(finish.row, finish.col) or current_row_len,
replacement
)
end
Expand Down Expand Up @@ -131,7 +131,8 @@ function utils.text(bufnr, start, finish, vmode)
for row = start.row, finish.row, 1 do
local current_row_len = vim.fn.getline(row):len()

local end_col = current_row_len > finish.col and finish.col + 1 or current_row_len
local end_col = current_row_len > finish.col and utils.get_next_char_bytecol(finish.row, finish.col)
or current_row_len
if start.col > end_col then
end_col = start.col
end
Expand All @@ -146,7 +147,14 @@ function utils.text(bufnr, start, finish, vmode)
return text
end

return vim.api.nvim_buf_get_text(0, start.row - 1, start.col, finish.row - 1, finish.col + 1, {})
return vim.api.nvim_buf_get_text(
0,
start.row - 1,
start.col,
finish.row - 1,
utils.get_next_char_bytecol(finish.row, finish.col),
{}
)
end

function utils.get_default_register()
Expand Down Expand Up @@ -238,4 +246,11 @@ function utils.compare_regions(origin, target)
return "="
end

utils.get_next_char_bytecol = function(linenr, colnr)
local line = vim.fn.getline(linenr)
local utf_index = vim.str_utfindex(line, math.min(line:len(), colnr + 1))

return vim.str_byteindex(line, utf_index)
end

return utils

0 comments on commit bdf0924

Please sign in to comment.