diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 39106e0427..9408718b0a 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -83,7 +83,7 @@ do local hl_group local display = path.make_relative(entry.value, cwd) if shorten_path then - display = utils.path_shorten(display) + display = utils.path_shorten(display,shorten_path) end display, hl_group = utils.transform_devicons(entry.value, display, disable_devicons) @@ -196,7 +196,7 @@ do display = function(entry) local display_filename if shorten_path then - display_filename = utils.path_shorten(entry.filename) + display_filename = utils.path_shorten(entry.filename,shorten_path) else display_filename = entry.filename end @@ -312,7 +312,7 @@ function make_entry.gen_from_quickfix(opts) if opts.tail_path then filename = utils.path_tail(filename) elseif opts.shorten_path then - filename = utils.path_shorten(filename) + filename = utils.path_shorten(filename,opts.shorten_path) end end @@ -391,7 +391,7 @@ function make_entry.gen_from_lsp_symbols(opts) if opts.tail_path then filename = utils.path_tail(filename) elseif opts.shorten_path then - filename = utils.path_shorten(filename) + filename = utils.path_shorten(filename,opts.shorten_path) end end @@ -469,7 +469,7 @@ function make_entry.gen_from_buffer(opts) local make_display = function(entry) local display_bufname if opts.shorten_path then - display_bufname = path.shorten(entry.filename) + display_bufname = utils.path_shorten(entry.filename,opts.shorten_path) else display_bufname = entry.filename end @@ -875,7 +875,7 @@ function make_entry.gen_from_ctags(opts) local filename if not opts.hide_filename then if opts.shorten_path then - filename = path.shorten(entry.filename) + filename = utils.path_shorten(entry.filename,opts.shorten_path) else filename = entry.filename end @@ -969,7 +969,7 @@ function make_entry.gen_from_lsp_diagnostics(opts) if opts.tail_path then filename = utils.path_tail(filename) elseif opts.shorten_path then - filename = utils.path_shorten(filename) + filename = utils.path_shorten(filename,opts.shorten_path) end end @@ -1173,7 +1173,7 @@ function make_entry.gen_from_jumplist(opts) if opts.tail_path then filename = utils.path_tail(filename) elseif opts.shorten_path then - filename = utils.path_shorten(filename) + filename = utils.path_shorten(filename,opts.shorten_path) end end diff --git a/lua/telescope/path.lua b/lua/telescope/path.lua index 0d17c1a336..fbb4442d64 100644 --- a/lua/telescope/path.lua +++ b/lua/telescope/path.lua @@ -20,29 +20,12 @@ path.make_relative = function(filepath, cwd) return filepath end -path.shorten = (function() - if jit then - local ffi = require('ffi') - ffi.cdef [[ - typedef unsigned char char_u; - char_u *shorten_dir(char_u *str); - ]] - - return function(filepath) - if not filepath then - return filepath - end - - local c_str = ffi.new("char[?]", #filepath + 1) - ffi.copy(c_str, filepath) - return ffi.string(ffi.C.shorten_dir(c_str)) - end - else - return function(filepath) - return filepath - end - end -end)() +-- In most cases it is better to use `utils.path_shorten` +-- as it handles cases for `len` being things other than +-- a positive integer. +path.shorten = function(filepath,len) + return require'plenary.path'.new(filepath):shorten(len) +end path.normalize = function(filepath, cwd) filepath = path.make_relative(filepath, cwd) diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 85e8723f3e..56e20a14ca 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -136,7 +136,17 @@ end -- return result -- end or nil) -utils.path_shorten = pathlib.shorten +utils.path_shorten = function(filename,len) + if len == nil then + return pathlib.shorten(filename) + elseif len == true then + return pathlib.shorten(filename,1) + elseif tonumber(len) > 0 then + return pathlib.shorten(filename,tonumber(len)) + else + error('Invalid value for `len`. Acceptable values are `nil`, `true` and positive integers.') + end +end utils.path_tail = (function() local os_sep = utils.get_separator() @@ -352,7 +362,7 @@ utils.transform_devicons = (function() end local icon, icon_highlight = devicons.get_icon(filename, string.match(filename, '%a+$'), { default = true }) - local icon_display = (icon or ' ') .. ' ' .. display + local icon_display = (icon or ' ') .. ' ' .. (display or '') if conf.color_devicons then return icon_display, icon_highlight