Skip to content

Commit a7856ac

Browse files
committed
fix: also make sure we do sub() calls in Unicode-aware way
1 parent 7d65f21 commit a7856ac

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lua/wincent/commandt/private/match_listing.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,22 @@ function MatchListing:icon_getter()
5959
end
6060
end
6161

62+
-- Unicode-aware `len` implementation.
6263
local function len(str)
6364
return vim.str_utfindex(str, 'utf-32')
6465
end
6566

67+
-- Unicode-aware `sub` implementation.
68+
local function sub(str, start_char, end_char)
69+
local start_byte = vim.str_byteindex(str, 'utf-32', start_char, false)
70+
if end_char == nil then
71+
return str:sub(start_byte)
72+
else
73+
local end_byte = vim.str_byteindex(str, 'utf-32', end_char, false)
74+
return str:sub(start_byte, end_byte)
75+
end
76+
end
77+
6678
local format_line = function(line, width, selected, truncate, get_icon)
6779
local prefix = selected and '> ' or ' '
6880

@@ -89,11 +101,11 @@ local format_line = function(line, width, selected, truncate, get_icon)
89101
-- Line is so short that adding an ellipsis is not practical.
90102
elseif truncate == true or truncate == 'true' or truncate == 'middle' then
91103
local half = math.floor((width - 2) / 2)
92-
local left = line:sub(1, half - 2 + width % 2)
93-
local right = line:sub(2 - half)
104+
local left = sub(line, 1, half - 2 + width % 2)
105+
local right = sub(line, 2 - half)
94106
line = left .. '...' .. right
95107
elseif truncate == 'beginning' then
96-
line = '...' .. line:sub(-width + len(prefix) + 3)
108+
line = '...' .. sub(line, -width + len(prefix) + 3)
97109
elseif truncate == false or truncate == 'false' or truncate == 'end' then
98110
-- Fall through; truncation will happen before the final `return`.
99111
end
@@ -111,7 +123,7 @@ local format_line = function(line, width, selected, truncate, get_icon)
111123
end
112124

113125
-- Trim to make sure we never wrap.
114-
return line:sub(1, vim.str_byteindex(line, 'utf-32', width, false))
126+
return sub(line, 1, width)
115127
end
116128

117129
function MatchListing:select(selected)

0 commit comments

Comments
 (0)