Skip to content

Commit ee4234f

Browse files
committed
refactor(utils): cache pathsep
1 parent ace288f commit ee4234f

3 files changed

Lines changed: 16 additions & 19 deletions

File tree

lua/telescope/make_entry.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ function make_entry.gen_from_ctags(opts)
11011101
end
11021102
local kind = string.match(extension_fields or "", "kind:(%S+)")
11031103

1104-
if utils.get_separator() == "\\" then
1104+
if utils.pathsep == "\\" then
11051105
file = string.gsub(file, "/", "\\")
11061106
end
11071107

lua/telescope/sorters.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ end
177177
sorters.Sorter = Sorter
178178

179179
local make_cached_tail = function()
180-
local os_sep = utils.get_separator()
181-
local match_string = "[^" .. os_sep .. "]*$"
180+
local match_string = "[^" .. utils.pathsep .. "]*$"
182181
return setmetatable({}, {
183182
__index = function(t, k)
184183
local tail = string.match(k, match_string)

lua/telescope/utils.lua

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local get_status = require("telescope.state").get_status
1212
local utils = {}
1313

1414
utils.iswin = vim.fn.has "win32" == 1
15+
utils.pathsep = utils.iswin and "\\" or "/"
1516

1617
utils.if_nil = vim.nonnil or vim.F.if_nil -- TODO: remove with nvim 0.12 drop
1718

@@ -70,10 +71,6 @@ utils.path_expand = function(path)
7071
return (path:gsub("(.)/$", "%1"))
7172
end
7273

73-
utils.get_separator = function()
74-
return utils.iswin and "\\" or "/"
75-
end
76-
7774
utils.cycle = function(i, n)
7875
return i % n == 0 and n or i % n
7976
end
@@ -167,7 +164,8 @@ utils.filter_symbols = function(results, opts, post_filter)
167164
end
168165

169166
local path_filename_first = function(path, reverse_directories)
170-
local dirs = vim.split(path, utils.get_separator())
167+
local pathsep = utils.pathsep
168+
local dirs = vim.split(path, pathsep)
171169
local filename
172170

173171
if reverse_directories then
@@ -177,7 +175,7 @@ local path_filename_first = function(path, reverse_directories)
177175
filename = table.remove(dirs, #dirs)
178176
end
179177

180-
local tail = table.concat(dirs, utils.get_separator())
178+
local tail = table.concat(dirs, pathsep)
181179
-- Trim prevents a top-level filename to have a trailing white space
182180
local transformed_path = vim.trim(filename .. " " .. tail)
183181
local path_style = { { { #filename, #transformed_path }, "TelescopeResultsComment" } }
@@ -205,7 +203,7 @@ local path_shorten = function(filename, len, exclude)
205203
len = len or 1
206204
exclude = exclude or { -1 }
207205
local exc = {}
208-
local pathsep = utils.get_separator()
206+
local pathsep = utils.pathsep
209207

210208
-- get parts in a table
211209
local parts = {}
@@ -267,15 +265,15 @@ end
267265
-- local we would potential break consumers of this method.
268266
utils.path_smart = (function()
269267
local paths = {}
270-
local os_sep = utils.get_separator()
268+
local pathsep = utils.pathsep
271269
return function(filepath)
272270
local final = filepath
273271
if #paths ~= 0 then
274-
local dirs = vim.split(filepath, os_sep)
272+
local dirs = vim.split(filepath, pathsep)
275273
local max = 1
276274
for _, p in pairs(paths) do
277275
if #p > 0 and p ~= filepath then
278-
local _dirs = vim.split(p, os_sep)
276+
local _dirs = vim.split(p, pathsep)
279277
for i = 1, math.min(#dirs, #_dirs) do
280278
if (dirs[i] ~= _dirs[i]) and i > max then
281279
max = i
@@ -291,7 +289,7 @@ utils.path_smart = (function()
291289
final = ""
292290
for k, v in pairs(dirs) do
293291
if k >= max - 1 then
294-
final = final .. (#final > 0 and os_sep or "") .. v
292+
final = final .. (#final > 0 and pathsep or "") .. v
295293
end
296294
end
297295
end
@@ -301,20 +299,20 @@ utils.path_smart = (function()
301299
table.insert(paths, filepath)
302300
end
303301
if final and final ~= filepath then
304-
return ".." .. os_sep .. final
302+
return ".." .. pathsep .. final
305303
else
306304
return filepath
307305
end
308306
end
309307
end)()
310308

311309
utils.path_tail = (function()
312-
local os_sep = utils.get_separator()
310+
local pathsep = utils.pathsep
313311

314-
if os_sep == "/" then
312+
if pathsep == "/" then
315313
return function(path)
316314
for i = #path, 1, -1 do
317-
if path:sub(i, i) == os_sep then
315+
if path:sub(i, i) == pathsep then
318316
return path:sub(i + 1, -1)
319317
end
320318
end
@@ -324,7 +322,7 @@ utils.path_tail = (function()
324322
return function(path)
325323
for i = #path, 1, -1 do
326324
local c = path:sub(i, i)
327-
if c == os_sep or c == "/" then
325+
if c == pathsep or c == "/" then
328326
return path:sub(i + 1, -1)
329327
end
330328
end

0 commit comments

Comments
 (0)