From 678494e503d3c717df8295f55a6f3c7d3309b7fa Mon Sep 17 00:00:00 2001 From: ndavid <74260683+ndavidq0@users.noreply.github.com> Date: Mon, 1 Mar 2021 22:59:17 +0000 Subject: [PATCH] refactor: move transform_devicons and get_devicons to utils (#580) So extension developers can access them --- lua/telescope/make_entry.lua | 55 +++--------------------------------- lua/telescope/utils.lua | 48 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 51 deletions(-) diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index bd1f622..8c793d7 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -1,6 +1,3 @@ -local has_devicons, devicons = pcall(require, 'nvim-web-devicons') - -local conf = require('telescope.config').values local entry_display = require('telescope.pickers.entry_display') local path = require('telescope.path') local utils = require('telescope.utils') @@ -32,50 +29,6 @@ local lsp_type_highlight = { local make_entry = {} -local transform_devicons -local get_devicons -if has_devicons then - if not devicons.has_loaded() then - devicons.setup() - end - - transform_devicons = function(filename, display, disable_devicons) - if disable_devicons or not filename then - return display - end - - local icon, icon_highlight = devicons.get_icon(filename, string.match(filename, '%a+$'), { default = true }) - local icon_display = (icon or ' ') .. ' ' .. display - - if conf.color_devicons then - return icon_display, icon_highlight - else - return icon_display - end - end - - get_devicons = function(filename, disable_devicons) - if disable_devicons or not filename then - return '' - end - - local icon, icon_highlight = devicons.get_icon(filename, string.match(filename, '%a+$'), { default = true }) - if conf.color_devicons then - return icon, icon_highlight - else - return icon - end - end -else - transform_devicons = function(_, display, _) - return display - end - - get_devicons = function(_, _) - return '' - end -end - do local lookup_keys = { display = 1, @@ -124,7 +77,7 @@ do display = utils.path_shorten(display) end - display, hl_group = transform_devicons(entry.value, display, disable_devicons) + display, hl_group = utils.transform_devicons(entry.value, display, disable_devicons) if hl_group then return display, { { {1, 3}, hl_group } } @@ -240,7 +193,7 @@ do coordinates = string.format("%s:%s:", entry.lnum, entry.col) end - local display, hl_group = transform_devicons( + local display, hl_group = utils.transform_devicons( entry.filename, string.format(display_string, display_filename, coordinates, entry.text), disable_devicons @@ -468,7 +421,7 @@ function make_entry.gen_from_buffer(opts) local icon_width = 0 if not disable_devicons then - local icon, _ = get_devicons('fname', disable_devicons) + local icon, _ = utils.get_devicons('fname', disable_devicons) icon_width = utils.strdisplaywidth(icon) end @@ -492,7 +445,7 @@ function make_entry.gen_from_buffer(opts) display_bufname = entry.filename end - local icon, hl_group = get_devicons(entry.filename, disable_devicons) + local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons) return displayer { {entry.bufnr, "TelescopeResultsNumber"}, diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 4470c56..4d3de20 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -1,3 +1,5 @@ +local has_devicons, devicons = pcall(require, 'nvim-web-devicons') + local pathlib = require('telescope.path') local Job = require('plenary.job') @@ -295,4 +297,50 @@ utils.align_str = function(string, width, right_justify) or string..string.rep(" ", width - str_len) end +utils.transform_devicons = (function() + if has_devicons then + return function(filename, display, disable_devicons) + local conf = require('telescope.config').values + if disable_devicons or not filename then + return display + end + + local icon, icon_highlight = devicons.get_icon(filename, string.match(filename, '%a+$'), { default = true }) + local icon_display = (icon or ' ') .. ' ' .. display + + if conf.color_devicons then + return icon_display, icon_highlight + else + return icon_display + end + end + else + return function(_, display, _) + return display + end + end +end)() + +utils.get_devicons = (function() + if has_devicons then + return function(filename, disable_devicons) + local conf = require('telescope.config').values + if disable_devicons or not filename then + return '' + end + + local icon, icon_highlight = devicons.get_icon(filename, string.match(filename, '%a+$'), { default = true }) + if conf.color_devicons then + return icon, icon_highlight + else + return icon + end + end + else + return function(_, _) + return '' + end + end +end)() + return utils