Skip to content

Commit

Permalink
refactor(devicons): custom version of nvim-web-devicons
Browse files Browse the repository at this point in the history
In order to supprot multi-part extensions with minimal
performance impact fzf-lua will now use its  own version
of the devcions lookup tables built from the original plugin
tables.

For more info, see discussion in #1053.
  • Loading branch information
ibhagwan committed Mar 17, 2024
1 parent 2ba4a51 commit e4952d6
Show file tree
Hide file tree
Showing 20 changed files with 658 additions and 363 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ PLENARY-REPO=https://github.com/nvim-lua/plenary.nvim.git

.PHONY: test
test:
nvim --headless --noplugin -u tests/minimal_init.vim -c "lua require('plenary.test_harness').test_directory('tests/', { minimal_init = 'tests/minimal_init.vim', sequential = true, timeout = 120000 })"
nvim --headless --noplugin -u tests/minimal_init.lua -c "lua require('plenary.test_harness').test_directory('tests/', { minimal_init = 'tests/minimal_init.lua', sequential = true, timeout = 120000 })"

# run with `make FILE=tests/path_spec.lua test-file`
.PHONY: test-file
test-file:
nvim --headless --noplugin -u tests/minimal_init.vim -c "lua require('plenary.busted').run(vim.loop.cwd()..'/'..[[$(FILE)]])"
nvim --headless --noplugin -u tests/minimal_init.lua -c "lua require('plenary.busted').run(vim.loop.cwd()..'/'..[[$(FILE)]])"


.PHONY: plenary
Expand All @@ -18,5 +19,5 @@ plenary:
git clone $(PLENARY-REPO) $(PLENARY-DIR); \
else \
echo "Updating plenary.nvim..."; \
git -C $(PLENARY-DIR) pull; \
git -C $(PLENARY-DIR) pull --rebase; \
fi
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1290,15 +1290,7 @@ require'fzf-lua'.setup {
-- manpages = { previewer = "man_native" },
-- helptags = { previewer = "help_native" },
--
-- optional override of file extension icon colors
-- available colors (terminal):
-- clear, bold, black, red, green, yellow
-- blue, magenta, cyan, grey, dark_grey, white
file_icon_colors = {
["sh"] = "green",
},
-- padding can help kitty term users with
-- double-width icon rendering
-- padding can help kitty term users with double-width icon rendering
file_icon_padding = '',
-- uncomment if your terminal/font does not support unicode character
-- 'EN SPACE' (U+2002), the below sets it to 'NBSP' (U+00A0) instead
Expand Down
12 changes: 2 additions & 10 deletions doc/fzf-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1382,15 +1382,7 @@ open an issue and I'll be more than happy to help.**
-- manpages = { previewer = "man_native" },
-- helptags = { previewer = "help_native" },
--
-- optional override of file extension icon colors
-- available colors (terminal):
-- clear, bold, black, red, green, yellow
-- blue, magenta, cyan, grey, dark_grey, white
file_icon_colors = {
["sh"] = "green",
},
-- padding can help kitty term users with
-- double-width icon rendering
-- padding can help kitty term users with double-width icon rendering
file_icon_padding = '',
-- uncomment if your terminal/font does not support unicode character
-- 'EN SPACE' (U+2002), the below sets it to 'NBSP' (U+00A0) instead
Expand Down Expand Up @@ -1491,4 +1483,4 @@ I missed your name feel free to contact me and I'll add it below:
as baseline for the builtin previewer and his must have plugin nvim-bqf
<https://github.com/kevinhwang91/nvim-bqf>

vim:tw=78:ts=8:ft=help:norl:
vim:tw=78:ts=8:ft=help:norl:
99 changes: 20 additions & 79 deletions lua/fzf-lua/config.lua
Original file line number Diff line number Diff line change
@@ -1,77 +1,10 @@
local path = require "fzf-lua.path"
local utils = require "fzf-lua.utils"
local actions = require "fzf-lua.actions"
local devicons = require "fzf-lua.devicons"

local M = {}

if utils.__HAS_DEVICONS then
M._has_devicons, M._devicons = pcall(require, "nvim-web-devicons")

-- get the devicons module path
M._devicons_path = M._has_devicons and M._devicons and M._devicons.setup
and path.normalize(debug.getinfo(M._devicons.setup, "S").source:gsub("^@", ""))
end

M._diricon_escseq = function()
local hlgroup = utils.map_get(M, "__resume_data.opts.hls.dir_icon") or M.globals.__HLS.dir_icon
local _, escseq = utils.ansi_from_hl(hlgroup)
return escseq
end

-- get icons proxy for the headless instance
M._devicons_geticons = function()
if not M._has_devicons or not M._devicons or not M._devicons.get_icons then
return
end
-- force refresh if `bg` changed from dark/light (#855)
if M.__DEVICONS and vim.o.bg == M.__DEVICONS_BG then
return M.__DEVICONS
end
-- save the current background
M.__DEVICONS_BG = vim.o.bg
-- rpc request cannot return a table that has mixed elements
-- of both indexed items and key value, it will fail with
-- "Cannot convert given lua table"
-- NOTES:
-- (1) devicons.get_icons() returns the default icon in [1]
-- (2) we cannot rely on having either .name or .color (#817)
local all_devicons = M._devicons.get_icons()
if not all_devicons or vim.tbl_isempty(all_devicons) then
-- something is wrong with devicons
-- can't use `error` due to fast event
print("[Fzf-lua] error: devicons.get_icons() is nil or empty!")
return
end
-- We only need the name, icon and color properties
local default_icon = all_devicons[1] or {}
M.__DEVICONS = {
["<default>"] = {
name = default_icon.name or "Default",
icon = default_icon.icon or "",
color = default_icon.color or "#6d8086",
}
}
for k, v in pairs(all_devicons) do
-- skip all indexed (numeric) entries
if type(k) == "string" then
M.__DEVICONS[k] = {
name = v.name or k,
icon = v.icon or "",
color = v.color or (function()
-- some devicons customizations remove `info.color`
-- retrieve the color from the highlight group (#801)
local hlgroup = "DevIcon" .. (v.name or k)
local hexcol = utils.hexcol_from_hl(hlgroup, "fg")
if hexcol and #hexcol > 0 then
return hexcol
end
end)(),
}
end
end
return M.__DEVICONS
end

-- set this so that make_entry won't get nil err when setting remotely
M.__resume_data = {}

Expand Down Expand Up @@ -283,9 +216,6 @@ function M.normalize_opts(opts, globals, __resume_key)
if v == "" then opts.fzf_opts[k] = true end
end

-- Disable devicons if not available
opts.file_icons = utils.__HAS_DEVICONS and opts.file_icons or nil

-- Execlude file icons from the fuzzy matching (#1080)
if opts.file_icons and opts._fzf_nth_devicons and not opts.fzf_opts["--delimiter"] then
opts.fzf_opts["--nth"] = opts.fzf_opts["--nth"] or "-1.."
Expand Down Expand Up @@ -327,7 +257,13 @@ function M.normalize_opts(opts, globals, __resume_key)
-- also check if we need to override 'opts.prompt' from cli args
-- if we don't override 'opts.prompt' 'FzfWin.save_query' will
-- fail to remove the prompt part from resume saved query (#434)
for _, s in ipairs({ "fzf_args", "fzf_cli_args", "fzf_raw_args" }) do
for _, s in ipairs({
"fzf_args",
"fzf_cli_args",
"fzf_raw_args",
"file_icon_padding",
"dir_icon",
}) do
if opts[s] == nil then
opts[s] = M.globals[s]
end
Expand Down Expand Up @@ -521,17 +457,22 @@ function M.normalize_opts(opts, globals, __resume_key)
opts.winopts.split = nil
end

if devicons.plugin_loaded() then
-- refresh icons, does nothing if "vim.o.bg" didn't change
devicons.load({
icon_padding = opts.file_icon_padding,
dir_icon = { icon = opts.dir_icon, color = utils.hexcol_from_hl(opts.hls.dir_icon, "fg") }
})
elseif opts.file_icons then
-- Disable devicons if not available
utils.warn("nvim-web-devicons isn't available, disabling 'file_icons'.")
opts.file_icons = nil
end

-- libuv.spawn_nvim_fzf_cmd() pid callback
opts._set_pid = M.set_pid
opts._get_pid = M.get_pid

-- setup devicons terminal highlight groups does nothing unless
-- neovim `bg` is changed, call via utils/loadstring to prevent
-- circular require and also make sure "make_entry.lua" isn't
-- loaded before devicons vars are setup by this module
-- TODO: cleanup the background update and devicons load logic
utils.setup_devicon_term_hls()

-- mark as normalized
opts._normalized = true

Expand Down
3 changes: 2 additions & 1 deletion lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local actions = require "fzf-lua.actions"
local win = require "fzf-lua.win"
local libuv = require "fzf-lua.libuv"
local shell = require "fzf-lua.shell"
local devicons = require "fzf-lua.devicons"
local make_entry = require "fzf-lua.make_entry"
local base64 = require "fzf-lua.lib.base64"
local serpent = require "fzf-lua.lib.serpent"
Expand Down Expand Up @@ -635,7 +636,7 @@ M.mt_cmd_wrapper = function(opts)
t.g = {}
for k, v in pairs({
["_fzf_lua_server"] = vim.g.fzf_lua_server,
["_devicons_path"] = config._devicons_path,
["_devicons_path"] = devicons.plugin_path(),
["_devicons_setup"] = config._devicons_setup,
}) do
t.g[k] = v
Expand Down
5 changes: 1 addition & 4 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -967,10 +967,7 @@ M.defaults.complete_line = { complete = true }

M.defaults.file_icon_padding = ""

M.defaults.file_icon_colors = {}

M.defaults.dir_icon = ""
M.defaults.dir_icon_color = "#519aba"
M.defaults.dir_icon = ""

M.defaults.__HLS = {
normal = "FzfLuaNormal",
Expand Down
Loading

0 comments on commit e4952d6

Please sign in to comment.