From 0c1bc129da3f684b04d72530dddaedb5255f12ef Mon Sep 17 00:00:00 2001 From: JINNOUCHI Yasushi Date: Tue, 15 Jun 2021 03:47:33 +0900 Subject: [PATCH] chore: use plenary.strings and remove strings functions from utils (#690) Co-authored-by: Simon Hauser --- lua/telescope/builtin/git.lua | 3 +- lua/telescope/builtin/lsp.lua | 3 +- lua/telescope/config.lua | 28 +------ lua/telescope/make_entry.lua | 3 +- lua/telescope/pickers/entry_display.lua | 29 +------- lua/telescope/utils.lua | 98 +++---------------------- 6 files changed, 25 insertions(+), 139 deletions(-) diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index 0a2d5dc..418379d 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -6,6 +6,7 @@ local pickers = require('telescope.pickers') local previewers = require('telescope.previewers') local utils = require('telescope.utils') local entry_display = require('telescope.pickers.entry_display') +local strings = require('plenary.strings') local conf = require('telescope.config').values @@ -138,7 +139,7 @@ git.branches = function(opts) entry.name = string.sub(entry.refname, string.len(prefix)+1) 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 if string.len(entry.upstream) > 0 then widths.upstream_indicator = 2 diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 532f221..8bda217 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -5,6 +5,7 @@ local make_entry = require('telescope.make_entry') local pickers = require('telescope.pickers') local entry_display = require('telescope.pickers.entry_display') local utils = require('telescope.utils') +local strings = require('plenary.strings') local a = require('plenary.async_lib') local async, await = a.async, a.await local channel = a.util.channel @@ -172,7 +173,7 @@ lsp.code_actions = function(opts) } 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 table.insert(results, entry) diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 2c1b463..4ca3d0a 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -1,3 +1,5 @@ +local strings = require('plenary.strings') + -- Keep the values around between reloads _TelescopeConfigurationValues = _TelescopeConfigurationValues or {} _TelescopeConfigurationPickers = _TelescopeConfigurationPickers or {} @@ -13,30 +15,6 @@ local function first_non_null(...) 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') -- TODO: Add other major configuration points here. @@ -69,7 +47,7 @@ function config.set_defaults(defaults) config.values[name] = get(name, default_val) if description then - config.descriptions[name] = dedent(description) + config.descriptions[name] = strings.dedent(description) end end diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 4fee48d..9c3ab14 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -1,6 +1,7 @@ local entry_display = require('telescope.pickers.entry_display') local path = require('telescope.path') local utils = require('telescope.utils') +local strings = require('plenary.strings') local Path = require('plenary.path') @@ -444,7 +445,7 @@ function make_entry.gen_from_buffer(opts) local icon_width = 0 if not disable_devicons then local icon, _ = utils.get_devicons('fname', disable_devicons) - icon_width = utils.strdisplaywidth(icon) + icon_width = strings.strdisplaywidth(icon) end local displayer = entry_display.create { diff --git a/lua/telescope/pickers/entry_display.lua b/lua/telescope/pickers/entry_display.lua index f64f693..e2be8c0 100644 --- a/lua/telescope/pickers/entry_display.lua +++ b/lua/telescope/pickers/entry_display.lua @@ -1,28 +1,7 @@ -local utils = require('telescope.utils') +local strings = require('plenary.strings') local entry_display = {} - -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.truncate = strings.truncate entry_display.create = function(configuration) local generator = {} @@ -31,9 +10,9 @@ entry_display.create = function(configuration) local justify = v.right_justify table.insert(generator, function(item) 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 - 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) else diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index d9e4ec0..3938bae 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -377,94 +377,20 @@ function utils.get_os_command_output(cmd, cwd) return stdout, ret, stderr end -utils.strdisplaywidth = (function() - if jit and pathlib.separator ~= '\\' then - 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 - - local len = 0 - if charlen then - 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 - - if nbyte < 0 then - len = len + nbyte - nbyte = 0 - elseif nbyte > #str then - nbyte = #str - end - if len < 0 then - len = 0 - elseif nbyte + len > #str then - len = #str - nbyte - end - - return str:sub(nbyte + 1, nbyte + len) +utils.strdisplaywidth = function() + error("strdisplaywidth deprecated. please use plenary.strings.strdisplaywidth") end -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) +utils.utf_ptr2len = function() + error("utf_ptr2len deprecated. please use plenary.strings.utf_ptr2len") +end + +utils.strcharpart = function() + error("strcharpart deprecated. please use plenary.strings.strcharpart") +end + +utils.align_str = function() + error("align_str deprecated. please use plenary.strings.align_str") end utils.transform_devicons = (function()