refactor: move transform_devicons and get_devicons to utils (#580)
So extension developers can access them
This commit is contained in:
@@ -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 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')
|
||||||
@@ -32,50 +29,6 @@ local lsp_type_highlight = {
|
|||||||
|
|
||||||
local make_entry = {}
|
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
|
do
|
||||||
local lookup_keys = {
|
local lookup_keys = {
|
||||||
display = 1,
|
display = 1,
|
||||||
@@ -124,7 +77,7 @@ do
|
|||||||
display = utils.path_shorten(display)
|
display = utils.path_shorten(display)
|
||||||
end
|
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
|
if hl_group then
|
||||||
return display, { { {1, 3}, hl_group } }
|
return display, { { {1, 3}, hl_group } }
|
||||||
@@ -240,7 +193,7 @@ do
|
|||||||
coordinates = string.format("%s:%s:", entry.lnum, entry.col)
|
coordinates = string.format("%s:%s:", entry.lnum, entry.col)
|
||||||
end
|
end
|
||||||
|
|
||||||
local display, hl_group = transform_devicons(
|
local display, hl_group = utils.transform_devicons(
|
||||||
entry.filename,
|
entry.filename,
|
||||||
string.format(display_string, display_filename, coordinates, entry.text),
|
string.format(display_string, display_filename, coordinates, entry.text),
|
||||||
disable_devicons
|
disable_devicons
|
||||||
@@ -468,7 +421,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, _ = get_devicons('fname', disable_devicons)
|
local icon, _ = utils.get_devicons('fname', disable_devicons)
|
||||||
icon_width = utils.strdisplaywidth(icon)
|
icon_width = utils.strdisplaywidth(icon)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -492,7 +445,7 @@ function make_entry.gen_from_buffer(opts)
|
|||||||
display_bufname = entry.filename
|
display_bufname = entry.filename
|
||||||
end
|
end
|
||||||
|
|
||||||
local icon, hl_group = get_devicons(entry.filename, disable_devicons)
|
local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons)
|
||||||
|
|
||||||
return displayer {
|
return displayer {
|
||||||
{entry.bufnr, "TelescopeResultsNumber"},
|
{entry.bufnr, "TelescopeResultsNumber"},
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
local has_devicons, devicons = pcall(require, 'nvim-web-devicons')
|
||||||
|
|
||||||
local pathlib = require('telescope.path')
|
local pathlib = require('telescope.path')
|
||||||
local Job = require('plenary.job')
|
local Job = require('plenary.job')
|
||||||
|
|
||||||
@@ -295,4 +297,50 @@ utils.align_str = function(string, width, right_justify)
|
|||||||
or string..string.rep(" ", width - str_len)
|
or string..string.rep(" ", width - str_len)
|
||||||
end
|
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
|
return utils
|
||||||
|
|||||||
Reference in New Issue
Block a user