From 4d6f8e65fbca18837e87c459746d5ce7c1e0b29e Mon Sep 17 00:00:00 2001 From: Christopher Brewin Date: Sat, 14 Dec 2024 00:54:16 +1000 Subject: [PATCH] feat (sidebar) nvim-web-devicons support files with multiple dots and use default for unknown file types. (#943) --- lua/avante/sidebar.lua | 21 +-------------------- lua/avante/utils/file.lua | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index c07d206..4f8745f 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -755,25 +755,6 @@ function Sidebar:render_result() ) 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 function Sidebar:render_input(ask) if ask == nil then ask = true end @@ -2050,7 +2031,7 @@ function Sidebar:create_selected_files_container() local selected_filepaths_with_icon = {} 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)) end diff --git a/lua/avante/utils/file.lua b/lua/avante/utils/file.lua index 5e3a196..30df171 100644 --- a/lua/avante/utils/file.lua +++ b/lua/avante/utils/file.lua @@ -1,4 +1,5 @@ local LRUCache = require("avante.utils.lru_cache") +local Filetype = require("plenary.filetype") ---@class avante.utils.file local M = {} @@ -38,4 +39,27 @@ function M.exists(filepath) return stat ~= nil 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