feat (sidebar) nvim-web-devicons support files with multiple dots and use default for unknown file types. (#943)

This commit is contained in:
Christopher Brewin 2024-12-14 00:54:16 +10:00 committed by GitHub
parent 78dd9b0a6d
commit 4d6f8e65fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 20 deletions

View File

@ -755,25 +755,6 @@ function Sidebar:render_result()
) )
end end
function Sidebar:get_file_icon(filepath)
local filetype = vim.filetype.match({ filename = filepath }) or "unknown"
---@type string
local icon
---@diagnostic disable-next-line: undefined-field
if _G.MiniIcons ~= nil then
---@diagnostic disable-next-line: undefined-global
icon, _, _ = MiniIcons.get("filetype", filetype) -- luacheck: ignore
else
local ok, devicons = pcall(require, "nvim-web-devicons")
if ok then
icon = devicons.get_icon_by_filetype(filetype, {})
else
icon = ""
end
end
return icon
end
---@param ask? boolean ---@param ask? boolean
function Sidebar:render_input(ask) function Sidebar:render_input(ask)
if ask == nil then ask = true end if ask == nil then ask = true end
@ -2050,7 +2031,7 @@ function Sidebar:create_selected_files_container()
local selected_filepaths_with_icon = {} local selected_filepaths_with_icon = {}
for _, filepath in ipairs(selected_filepaths_) do for _, filepath in ipairs(selected_filepaths_) do
local icon = self:get_file_icon(filepath) local icon = Utils.file.get_file_icon(filepath)
table.insert(selected_filepaths_with_icon, string.format("%s %s", icon, filepath)) table.insert(selected_filepaths_with_icon, string.format("%s %s", icon, filepath))
end end

View File

@ -1,4 +1,5 @@
local LRUCache = require("avante.utils.lru_cache") local LRUCache = require("avante.utils.lru_cache")
local Filetype = require("plenary.filetype")
---@class avante.utils.file ---@class avante.utils.file
local M = {} local M = {}
@ -38,4 +39,27 @@ function M.exists(filepath)
return stat ~= nil return stat ~= nil
end end
function M.get_file_icon(filepath)
local filetype = Filetype.detect(filepath, {}) or "unknown"
---@type string
local icon
---@diagnostic disable-next-line: undefined-field
if _G.MiniIcons ~= nil then
---@diagnostic disable-next-line: undefined-global
icon, _, _ = MiniIcons.get("filetype", filetype) -- luacheck: ignore
else
local ok, devicons = pcall(require, "nvim-web-devicons")
if ok then
icon = devicons.get_icon(filepath, filetype, { default = false })
if not icon then
icon = devicons.get_icon(filepath, nil, { default = true })
icon = icon or " "
end
else
icon = ""
end
end
return icon
end
return M return M