Skip to content

Commit 7be8ff2

Browse files
committed
refactor(win): use vim.str_utfindex/str_byteindex for UTF-8 operations
1 parent e22f2f9 commit 7be8ff2

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

lua/fzf-lua/win.lua

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,9 @@ function PathShortener._apply_line(buf, row, shorten_len)
9797
local component = path_portion:sub(component_start, sep_pos - 1)
9898
local component_len = #component
9999

100-
-- Count UTF-8 characters using path.utf8_char_len
101-
local component_charlen = 0
102-
local byte_i = 1
103-
while byte_i <= component_len do
104-
local c_len = path.utf8_char_len(component, byte_i) or 1
105-
component_charlen = component_charlen + 1
106-
byte_i = byte_i + c_len
107-
end
100+
-- Count UTF-8 characters using vim.str_utfindex
101+
local _, component_charlen = vim.str_utfindex(component, "utf-32")
102+
component_charlen = component_charlen or component_len -- fallback to byte length
108103

109104
-- Only conceal if the component has more characters than shorten_len
110105
if component_charlen > shorten_len then
@@ -118,16 +113,9 @@ function PathShortener._apply_line(buf, row, shorten_len)
118113
-- Bounds check to prevent errors
119114
keep_chars = math.min(keep_chars, component_charlen)
120115

121-
-- Convert character count to byte offset using path.utf8_char_len
122-
local keep_bytes = 0
123-
local char_count = 0
124-
byte_i = 1
125-
while byte_i <= component_len and char_count < keep_chars do
126-
local c_len = path.utf8_char_len(component, byte_i) or 1
127-
keep_bytes = keep_bytes + c_len
128-
char_count = char_count + 1
129-
byte_i = byte_i + c_len
130-
end
116+
-- Convert character count to byte offset using vim.str_byteindex
117+
local keep_bytes = vim.str_byteindex(component, "utf-32", keep_chars, false)
118+
keep_bytes = keep_bytes or keep_chars -- fallback to character count (ASCII approximation)
131119

132120
-- Calculate 0-indexed byte positions in the full line for extmark
133121
-- path_start is 1-indexed, component_start is 1-indexed within path_portion

0 commit comments

Comments
 (0)