Skip to content

Commit

Permalink
feat: support multi-part extensions for file icons
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeroelens authored and ibhagwan committed Mar 17, 2024
1 parent 97b455a commit 2ba4a51
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
31 changes: 26 additions & 5 deletions lua/fzf-lua/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ local utils = require "fzf-lua.utils"
local libuv = require "fzf-lua.libuv"
local config = nil

-- Supports multi-part extensions
-- e.g.
-- "file.js" -> "js"
-- "file.test.js" -> "test.js"
-- "file.spec.js" -> "spec.js"
local function get_extension_from_file_name(file_name)
local name, extension = file_name:match("(.+)%.(.+)$")

if name and extension then
local preExtension = name:match(".+%.(.+)$")
if preExtension then
return (preExtension .. "." .. extension):lower()
else
return extension:lower()
end
end
end

-- attempt to load the current config
-- should fail if we're running headless
do
Expand Down Expand Up @@ -206,17 +224,21 @@ if not config then
config = _config
end

M.get_devicon = function(file, ext)
-- by default the extension will be derived from `file`, but you can
-- override it by passing `extensionOverride`
M.get_devicon = function(file, extensionOverride)
local ext = extensionOverride or get_extension_from_file_name(file)

local icon, hl
if path.ends_with_separator(file) then
icon, hl = M.__DIR_ICON, M.__DIR_ICON_HL
elseif M._devicons then
icon, hl = M._devicons.get_icon(file, ext:lower(), { default = true })
icon, hl = M._devicons.get_icon(file, ext, { default = true })
elseif M._devicons_map then
-- Lookup first by name, then by ext (devicons `strict=true`)
-- "<default>" is added by fzf-lua and is thus guaranteed
local info = M._devicons_map[file:lower()]
or M._devicons_map[ext:lower()]
or M._devicons_map[ext]
or M._devicons_map["<default>"]
icon, hl = info.icon, "DevIcon" .. info.name
else
Expand Down Expand Up @@ -521,8 +543,7 @@ M.file = function(x, opts)
end
if opts.file_icons then
local filename = path.tail(origpath)
local ext = path.extension(filename)
icon, hl = M.get_devicon(filename, ext)
icon, hl = M.get_devicon(filename)
if opts.color_icons then
-- extra workaround for issue #119 (or similars)
-- use default if we can't find the highlight ansi
Expand Down
6 changes: 2 additions & 4 deletions lua/fzf-lua/providers/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ local function gen_buffer_entry(opts, buf, max_bufnr, cwd)
buficon, hl = make_entry.get_devicon(buf.info.name, "sh")
else
local filename = path.tail(buf.info.name)
local extension = path.extension(filename)
buficon, hl = make_entry.get_devicon(filename, extension)
buficon, hl = make_entry.get_devicon(filename)
end
if opts.color_icons then
-- fallback to "grey" color (#817)
Expand Down Expand Up @@ -260,8 +259,7 @@ M.buffer_lines = function(opts)
bufname = path.basename(filepath)
if opts.file_icons then
local filename = path.tail(bufname)
local extension = path.extension(filename)
buficon, hl = make_entry.get_devicon(filename, extension)
buficon, hl = make_entry.get_devicon(filename)
if opts.color_icons then
buficon = utils.ansi_codes[hl](buficon)
end
Expand Down
3 changes: 1 addition & 2 deletions lua/fzf-lua/providers/nvim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ M.tagstack = function(opts)
local buficon, hl
if opts.file_icons then
local filename = path.tail(bufname)
local extension = path.extension(filename)
buficon, hl = make_entry.get_devicon(filename, extension)
buficon, hl = make_entry.get_devicon(filename)
if opts.color_icons then
buficon = utils.ansi_codes[hl](buficon)
end
Expand Down

0 comments on commit 2ba4a51

Please sign in to comment.