From 07f80ad645895af849a597d1cac897059d89b686 Mon Sep 17 00:00:00 2001 From: XeroOl Date: Wed, 20 Aug 2025 21:22:30 -0400 Subject: [PATCH] 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 --- lua/oil/columns.lua | 20 +++++++++++--------- lua/oil/view.lua | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lua/oil/columns.lua b/lua/oil/columns.lua index cc3b445..d531a7c 100644 --- a/lua/oil/columns.lua +++ b/lua/oil/columns.lua @@ -228,8 +228,8 @@ M.register("type", { end, }) -local function pad_number(int) - return string.format("%012d", int) +local function adjust_number(int) + return string.format("%03d%s", #int, int) end M.register("name", { @@ -256,14 +256,16 @@ M.register("name", { end end else - if config.view_options.case_insensitive then - return function(entry) - return entry[FIELD_NAME]:gsub("%d+", pad_number):lower() - end - else - return function(entry) - return entry[FIELD_NAME]:gsub("%d+", pad_number) + local memo = {} + return function(entry) + if memo[entry] == nil then + local name = entry[FIELD_NAME]:gsub("0*(%d+)", adjust_number) + if config.view_options.case_insensitive then + name = name:lower() + end + memo[entry] = name end + return memo[entry] end end end, diff --git a/lua/oil/view.lua b/lua/oil/view.lua index c2cb076..e4145ad 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -587,7 +587,7 @@ local function get_sort_function(adapter, num_entries) end return function(a, b) for _, sort_fn in ipairs(idx_funs) do - local get_sort_value, order = unpack(sort_fn) + local get_sort_value, order = sort_fn[1], sort_fn[2] local a_val = get_sort_value(a) local b_val = get_sort_value(b) if a_val ~= b_val then