chore: use plenary.strings and remove strings functions from utils (#690)
Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
committed by
GitHub
parent
398a0d391a
commit
0c1bc129da
@@ -6,6 +6,7 @@ local pickers = require('telescope.pickers')
|
|||||||
local previewers = require('telescope.previewers')
|
local previewers = require('telescope.previewers')
|
||||||
local utils = require('telescope.utils')
|
local utils = require('telescope.utils')
|
||||||
local entry_display = require('telescope.pickers.entry_display')
|
local entry_display = require('telescope.pickers.entry_display')
|
||||||
|
local strings = require('plenary.strings')
|
||||||
|
|
||||||
local conf = require('telescope.config').values
|
local conf = require('telescope.config').values
|
||||||
|
|
||||||
@@ -138,7 +139,7 @@ git.branches = function(opts)
|
|||||||
|
|
||||||
entry.name = string.sub(entry.refname, string.len(prefix)+1)
|
entry.name = string.sub(entry.refname, string.len(prefix)+1)
|
||||||
for key, value in pairs(widths) do
|
for key, value in pairs(widths) do
|
||||||
widths[key] = math.max(value, utils.strdisplaywidth(entry[key] or ''))
|
widths[key] = math.max(value, strings.strdisplaywidth(entry[key] or ''))
|
||||||
end
|
end
|
||||||
if string.len(entry.upstream) > 0 then
|
if string.len(entry.upstream) > 0 then
|
||||||
widths.upstream_indicator = 2
|
widths.upstream_indicator = 2
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ local make_entry = require('telescope.make_entry')
|
|||||||
local pickers = require('telescope.pickers')
|
local pickers = require('telescope.pickers')
|
||||||
local entry_display = require('telescope.pickers.entry_display')
|
local entry_display = require('telescope.pickers.entry_display')
|
||||||
local utils = require('telescope.utils')
|
local utils = require('telescope.utils')
|
||||||
|
local strings = require('plenary.strings')
|
||||||
local a = require('plenary.async_lib')
|
local a = require('plenary.async_lib')
|
||||||
local async, await = a.async, a.await
|
local async, await = a.async, a.await
|
||||||
local channel = a.util.channel
|
local channel = a.util.channel
|
||||||
@@ -172,7 +173,7 @@ lsp.code_actions = function(opts)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for key, value in pairs(widths) do
|
for key, value in pairs(widths) do
|
||||||
widths[key] = math.max(value, utils.strdisplaywidth(entry[key]))
|
widths[key] = math.max(value, strings.strdisplaywidth(entry[key]))
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(results, entry)
|
table.insert(results, entry)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
local strings = require('plenary.strings')
|
||||||
|
|
||||||
-- Keep the values around between reloads
|
-- Keep the values around between reloads
|
||||||
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {}
|
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {}
|
||||||
_TelescopeConfigurationPickers = _TelescopeConfigurationPickers or {}
|
_TelescopeConfigurationPickers = _TelescopeConfigurationPickers or {}
|
||||||
@@ -13,30 +15,6 @@ local function first_non_null(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local dedent = function(str, leave_indent)
|
|
||||||
-- find minimum common indent across lines
|
|
||||||
local indent = nil
|
|
||||||
for line in str:gmatch('[^\n]+') do
|
|
||||||
local line_indent = line:match('^%s+') or ''
|
|
||||||
if indent == nil or #line_indent < #indent then
|
|
||||||
indent = line_indent
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if indent == nil or #indent == 0 then
|
|
||||||
-- no minimum common indent
|
|
||||||
return str
|
|
||||||
end
|
|
||||||
local left_indent = (' '):rep(leave_indent or 0)
|
|
||||||
-- create a pattern for the indent
|
|
||||||
indent = indent:gsub('%s', '[ \t]')
|
|
||||||
-- strip it from the first line
|
|
||||||
str = str:gsub('^'..indent, left_indent)
|
|
||||||
-- strip it from the remaining lines
|
|
||||||
str = str:gsub('[\n]'..indent, '\n' .. left_indent)
|
|
||||||
return str
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local sorters = require('telescope.sorters')
|
local sorters = require('telescope.sorters')
|
||||||
|
|
||||||
-- TODO: Add other major configuration points here.
|
-- TODO: Add other major configuration points here.
|
||||||
@@ -69,7 +47,7 @@ function config.set_defaults(defaults)
|
|||||||
|
|
||||||
config.values[name] = get(name, default_val)
|
config.values[name] = get(name, default_val)
|
||||||
if description then
|
if description then
|
||||||
config.descriptions[name] = dedent(description)
|
config.descriptions[name] = strings.dedent(description)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
local entry_display = require('telescope.pickers.entry_display')
|
local entry_display = require('telescope.pickers.entry_display')
|
||||||
local path = require('telescope.path')
|
local path = require('telescope.path')
|
||||||
local utils = require('telescope.utils')
|
local utils = require('telescope.utils')
|
||||||
|
local strings = require('plenary.strings')
|
||||||
|
|
||||||
local Path = require('plenary.path')
|
local Path = require('plenary.path')
|
||||||
|
|
||||||
@@ -444,7 +445,7 @@ function make_entry.gen_from_buffer(opts)
|
|||||||
local icon_width = 0
|
local icon_width = 0
|
||||||
if not disable_devicons then
|
if not disable_devicons then
|
||||||
local icon, _ = utils.get_devicons('fname', disable_devicons)
|
local icon, _ = utils.get_devicons('fname', disable_devicons)
|
||||||
icon_width = utils.strdisplaywidth(icon)
|
icon_width = strings.strdisplaywidth(icon)
|
||||||
end
|
end
|
||||||
|
|
||||||
local displayer = entry_display.create {
|
local displayer = entry_display.create {
|
||||||
|
|||||||
@@ -1,28 +1,7 @@
|
|||||||
local utils = require('telescope.utils')
|
local strings = require('plenary.strings')
|
||||||
|
|
||||||
local entry_display = {}
|
local entry_display = {}
|
||||||
|
entry_display.truncate = strings.truncate
|
||||||
entry_display.truncate = function(str, len)
|
|
||||||
str = tostring(str) -- We need to make sure its an actually a string and not a number
|
|
||||||
if utils.strdisplaywidth(str) <= len then
|
|
||||||
return str
|
|
||||||
end
|
|
||||||
local charlen = 0
|
|
||||||
local cur_len = 0
|
|
||||||
local result = ''
|
|
||||||
local len_of_dots = utils.strdisplaywidth('…')
|
|
||||||
while true do
|
|
||||||
local part = utils.strcharpart(str, charlen, 1)
|
|
||||||
cur_len = cur_len + utils.strdisplaywidth(part)
|
|
||||||
if (cur_len + len_of_dots) > len then
|
|
||||||
result = result .. '…'
|
|
||||||
break
|
|
||||||
end
|
|
||||||
result = result .. part
|
|
||||||
charlen = charlen + 1
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
entry_display.create = function(configuration)
|
entry_display.create = function(configuration)
|
||||||
local generator = {}
|
local generator = {}
|
||||||
@@ -31,9 +10,9 @@ entry_display.create = function(configuration)
|
|||||||
local justify = v.right_justify
|
local justify = v.right_justify
|
||||||
table.insert(generator, function(item)
|
table.insert(generator, function(item)
|
||||||
if type(item) == 'table' then
|
if type(item) == 'table' then
|
||||||
return utils.align_str(entry_display.truncate(item[1], v.width), v.width, justify), item[2]
|
return strings.align_str(entry_display.truncate(item[1], v.width), v.width, justify), item[2]
|
||||||
else
|
else
|
||||||
return utils.align_str(entry_display.truncate(item, v.width), v.width, justify)
|
return strings.align_str(entry_display.truncate(item, v.width), v.width, justify)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -377,94 +377,20 @@ function utils.get_os_command_output(cmd, cwd)
|
|||||||
return stdout, ret, stderr
|
return stdout, ret, stderr
|
||||||
end
|
end
|
||||||
|
|
||||||
utils.strdisplaywidth = (function()
|
utils.strdisplaywidth = function()
|
||||||
if jit and pathlib.separator ~= '\\' then
|
error("strdisplaywidth deprecated. please use plenary.strings.strdisplaywidth")
|
||||||
local ffi = require('ffi')
|
|
||||||
ffi.cdef[[
|
|
||||||
typedef unsigned char char_u;
|
|
||||||
int linetabsize_col(int startcol, char_u *s);
|
|
||||||
]]
|
|
||||||
|
|
||||||
return function(str, col)
|
|
||||||
local startcol = col or 0
|
|
||||||
str = tostring(str)
|
|
||||||
local s = ffi.new('char[?]', #str + 1)
|
|
||||||
ffi.copy(s, str)
|
|
||||||
return ffi.C.linetabsize_col(startcol, s) - startcol
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return function(str, col)
|
|
||||||
return #(tostring(str)) - (col or 0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)()
|
|
||||||
|
|
||||||
utils.utf_ptr2len = (function()
|
|
||||||
if jit and pathlib.separator ~= '\\' then
|
|
||||||
local ffi = require('ffi')
|
|
||||||
ffi.cdef[[
|
|
||||||
typedef unsigned char char_u;
|
|
||||||
int utf_ptr2len(const char_u *const p);
|
|
||||||
]]
|
|
||||||
|
|
||||||
return function(str)
|
|
||||||
local c_str = ffi.new('char[?]', #str + 1)
|
|
||||||
ffi.copy(c_str, str)
|
|
||||||
return ffi.C.utf_ptr2len(c_str)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return function(str)
|
|
||||||
return str == '' and 0 or 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)()
|
|
||||||
|
|
||||||
function utils.strcharpart(str, nchar, charlen)
|
|
||||||
local nbyte = 0
|
|
||||||
if nchar > 0 then
|
|
||||||
while nchar > 0 and nbyte < #str do
|
|
||||||
nbyte = nbyte + utils.utf_ptr2len(str:sub(nbyte + 1))
|
|
||||||
nchar = nchar - 1
|
|
||||||
end
|
|
||||||
else
|
|
||||||
nbyte = nchar
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local len = 0
|
utils.utf_ptr2len = function()
|
||||||
if charlen then
|
error("utf_ptr2len deprecated. please use plenary.strings.utf_ptr2len")
|
||||||
while charlen > 0 and nbyte + len < #str do
|
|
||||||
local off = nbyte + len
|
|
||||||
if off < 0 then
|
|
||||||
len = len + 1
|
|
||||||
else
|
|
||||||
len = len + utils.utf_ptr2len(str:sub(off + 1))
|
|
||||||
end
|
|
||||||
charlen = charlen - 1
|
|
||||||
end
|
|
||||||
else
|
|
||||||
len = #str - nbyte
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if nbyte < 0 then
|
utils.strcharpart = function()
|
||||||
len = len + nbyte
|
error("strcharpart deprecated. please use plenary.strings.strcharpart")
|
||||||
nbyte = 0
|
|
||||||
elseif nbyte > #str then
|
|
||||||
nbyte = #str
|
|
||||||
end
|
|
||||||
if len < 0 then
|
|
||||||
len = 0
|
|
||||||
elseif nbyte + len > #str then
|
|
||||||
len = #str - nbyte
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return str:sub(nbyte + 1, nbyte + len)
|
utils.align_str = function()
|
||||||
end
|
error("align_str deprecated. please use plenary.strings.align_str")
|
||||||
|
|
||||||
utils.align_str = function(string, width, right_justify)
|
|
||||||
local str_len = utils.strdisplaywidth(string)
|
|
||||||
return right_justify
|
|
||||||
and string.rep(" ", width - str_len)..string
|
|
||||||
or string..string.rep(" ", width - str_len)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
utils.transform_devicons = (function()
|
utils.transform_devicons = (function()
|
||||||
|
|||||||
Reference in New Issue
Block a user