add some extmark-edge-case tests from morph.nvim
Some checks failed
ci / ci (push) Failing after 3m20s

This commit is contained in:
2026-04-06 17:37:29 -06:00
parent 43837ac33a
commit 9383f0b3ef
2 changed files with 57 additions and 2 deletions

View File

@@ -360,7 +360,7 @@ function Extmark.from_range(range, nsid)
local r = range:to_charwise() local r = range:to_charwise()
local stop = r.stop or r.start local stop = r.stop or r.start
local end_row = stop.lnum - 1 local end_row = stop.lnum - 1
local end_col = stop.col local end_col = r:is_empty() and (r.start.col - 1) or stop.col
if range.mode == 'V' then if range.mode == 'V' then
end_row = end_row + 1 end_row = end_row + 1
end_col = 0 end_col = 0
@@ -702,7 +702,7 @@ end
function Range:to_charwise() function Range:to_charwise()
local r = self:clone() local r = self:clone()
r.mode = 'v' r.mode = 'v'
if r.stop:is_col_max() then r.stop = r.stop:as_real() end if r.stop and r.stop:is_col_max() then r.stop = r.stop:as_real() end
return r return r
end end

View File

@@ -1370,6 +1370,61 @@ describe('Extmark additional coverage', function()
end) end)
end) end)
describe('Extmark edge cases', function()
it('tracks multi-byte characters correctly', function()
withbuf({ '🚀🌟 hello 你好世界' }, function()
-- The string is 27 bytes: 🚀(4) + 🌟(4) + space(1) + hello(5) + space(1) + 你(3) + 好(3) + 世(3) + 界(3)
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 27), 'v')
local ext = r:save_to_extmark()
local ext_range = ext:range()
assert.are.same('🚀🌟 hello 你好世界', ext_range:text())
end)
end)
it('clamps start position after buffer shrink', function()
withbuf({ 'line 1', 'line 2', 'line 3', '' }, function()
local r = Range.from_buf_text()
local ext = r:save_to_extmark()
-- Delete last line
vim.api.nvim_buf_set_lines(0, 3, 4, true, {})
-- Get range from extmark - should clamp to valid buffer
local ext_range = ext:range()
assert.is_true(ext_range.stop.lnum <= 3)
end)
end)
it('handles zero-width extmark (empty range)', function()
withbuf({ 'hello world' }, function()
local r = Range.new(Pos.new(nil, 1, 1), nil, 'v')
local ext = r:save_to_extmark()
local ext_range = ext:range()
assert.is_true(ext_range:is_empty())
end)
end)
it('handles extmark at buffer start', function()
withbuf({ 'first', 'second', 'third' }, function()
local r = Range.new(Pos.new(nil, 1, 1), Pos.new(nil, 1, 5), 'v')
local ext = r:save_to_extmark()
local ext_range = ext:range()
assert.are.same(1, ext_range.start.lnum)
assert.are.same(1, ext_range.start.col)
end)
end)
it('handles extmark at buffer end', function()
withbuf({ 'first', 'second', 'third' }, function()
local r = Range.new(Pos.new(nil, 3, 1), Pos.new(nil, 3, 5), 'v')
local ext = r:save_to_extmark()
local ext_range = ext:range()
assert.are.same(3, ext_range.stop.lnum)
assert.are.same(5, ext_range.stop.col)
end)
end)
end)
describe('utils', function() describe('utils', function()
local u = require 'u' local u = require 'u'