Skip to content

Commit 07f80ad

Browse files
authored
fix: support natural ordering for numbers with >12 digits (#652)
* fix: support natural ordering for numbers with >12 digits Changes the column ordering code when `view_options.natural_order` is enabled, so that it can support larger numbers. The previous 12-digit padding approach breaks for numbers above 12 digits. This length-prefixed approach can scale to much higher numbers. I picked %03 (padding 3 digits) because most filesystems don't allow more than 255 bytes in a path segment, and "255" is 3 digits long. * add memoization to natural order sorting * remove call to unpack
1 parent bbad9a7 commit 07f80ad

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lua/oil/columns.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ M.register("type", {
228228
end,
229229
})
230230

231-
local function pad_number(int)
232-
return string.format("%012d", int)
231+
local function adjust_number(int)
232+
return string.format("%03d%s", #int, int)
233233
end
234234

235235
M.register("name", {
@@ -256,14 +256,16 @@ M.register("name", {
256256
end
257257
end
258258
else
259-
if config.view_options.case_insensitive then
260-
return function(entry)
261-
return entry[FIELD_NAME]:gsub("%d+", pad_number):lower()
262-
end
263-
else
264-
return function(entry)
265-
return entry[FIELD_NAME]:gsub("%d+", pad_number)
259+
local memo = {}
260+
return function(entry)
261+
if memo[entry] == nil then
262+
local name = entry[FIELD_NAME]:gsub("0*(%d+)", adjust_number)
263+
if config.view_options.case_insensitive then
264+
name = name:lower()
265+
end
266+
memo[entry] = name
266267
end
268+
return memo[entry]
267269
end
268270
end
269271
end,

lua/oil/view.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ local function get_sort_function(adapter, num_entries)
587587
end
588588
return function(a, b)
589589
for _, sort_fn in ipairs(idx_funs) do
590-
local get_sort_value, order = unpack(sort_fn)
590+
local get_sort_value, order = sort_fn[1], sort_fn[2]
591591
local a_val = get_sort_value(a)
592592
local b_val = get_sort_value(b)
593593
if a_val ~= b_val then

0 commit comments

Comments
 (0)