From 3d910b2ba0b8536bb708467690b6685f1783cb19 Mon Sep 17 00:00:00 2001 From: Micah Halter Date: Mon, 15 Jul 2024 19:09:29 -0400 Subject: [PATCH] feat: add support for `mini.icons` (#383) --- README.md | 4 ++-- doc/aerial.txt | 4 ++-- lua/aerial/config.lua | 43 ++++++++++++++++++++++++++++++++++++------- tests/config_spec.lua | 4 ++-- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d456ce7..69fc1fe 100644 --- a/README.md +++ b/README.md @@ -342,7 +342,7 @@ require("aerial").setup({ -- icon when the tree is collapsed at that symbol, or "Collapsed" to specify a -- default collapsed icon. The default icon set is determined by the -- "nerd_font" option below. - -- If you have lspkind-nvim installed, it will be the default icon set. + -- If you have mini.icons or lspkind-nvim installed, it will be the default icon set. -- This can be a filetype map (see :help aerial-filetype-map) icons = {}, @@ -395,7 +395,7 @@ require("aerial").setup({ link_tree_to_folds = true, -- Set default symbol icons to use patched font icons (see https://www.nerdfonts.com/) - -- "auto" will set it to true if nvim-web-devicons or lspkind-nvim is installed. + -- "auto" will set it to true if nvim-web-devicons, lspkind-nvim, or mini.icons is installed. nerd_font = "auto", -- Call this function when aerial attaches to a buffer. diff --git a/doc/aerial.txt b/doc/aerial.txt index 890c376..744adcc 100644 --- a/doc/aerial.txt +++ b/doc/aerial.txt @@ -150,7 +150,7 @@ OPTIONS *aerial-option -- icon when the tree is collapsed at that symbol, or "Collapsed" to specify a -- default collapsed icon. The default icon set is determined by the -- "nerd_font" option below. - -- If you have lspkind-nvim installed, it will be the default icon set. + -- If you have mini.icons or lspkind-nvim installed, it will be the default icon set. -- This can be a filetype map (see :help aerial-filetype-map) icons = {}, @@ -203,7 +203,7 @@ OPTIONS *aerial-option link_tree_to_folds = true, -- Set default symbol icons to use patched font icons (see https://www.nerdfonts.com/) - -- "auto" will set it to true if nvim-web-devicons or lspkind-nvim is installed. + -- "auto" will set it to true if nvim-web-devicons, lspkind-nvim, or mini.icons is installed. nerd_font = "auto", -- Call this function when aerial attaches to a buffer. diff --git a/lua/aerial/config.lua b/lua/aerial/config.lua index 7f033ed..02e1ef8 100644 --- a/lua/aerial/config.lua +++ b/lua/aerial/config.lua @@ -504,7 +504,9 @@ M.setup = function(opts) if newconf.nerd_font == "auto" then local has_devicons = pcall(require, "nvim-web-devicons") local has_lspkind = pcall(require, "lspkind") - newconf.nerd_font = has_devicons or has_lspkind + pcall(require, "mini.icons") + local has_miniicons = vim.tbl_get(_G, "MiniIcons", "config", "style") == "glyph" + newconf.nerd_font = has_devicons or has_lspkind or has_miniicons end -- Add lookup to close_automatic_events @@ -512,10 +514,10 @@ M.setup = function(opts) newconf.close_automatic_events[v] = i end - -- Undocumented use_lspkind option for tests. End users can simply provide + -- Undocumented use_icon_provider option for tests. End users can simply provide -- their own icons - if newconf.use_lspkind == nil then - newconf.use_lspkind = true + if newconf.use_icon_provider == nil then + newconf.use_icon_provider = true end newconf.default_icons = newconf.nerd_font and nerd_icons or plain_icons @@ -580,6 +582,31 @@ local function get_icon(kind, filetypes) return M.icons[kind] end +local function get_icon_provider() + if not M.use_icon_provider then -- skip if icon provider not used + return false + end + -- prefer mini.icons + local _, mini_icons = pcall(require, "mini.icons") + ---@diagnostic disable-next-line: undefined-field + if _G.MiniIcons then -- `_G.MiniIcons` is a better check to see if the module is setup + return function(kind) + return mini_icons.get("lsp", kind) + end + end + + -- fallback to `lspkind` + local has_lspkind, lspkind = pcall(require, "lspkind") + if has_lspkind then + return function(kind) + return lspkind.symbolic(kind, { mode = "symbolic " }) + end + end + + return false -- no icon provider +end + +local icon_provider M.get_icon = function(bufnr, kind, collapsed) if collapsed then kind = kind .. "Collapsed" @@ -598,9 +625,11 @@ M.get_icon = function(bufnr, kind, collapsed) end end - local has_lspkind, lspkind = pcall(require, "lspkind") - if has_lspkind and M.use_lspkind and not collapsed then - icon = lspkind.symbolic(kind, { with_text = false }) + if icon_provider == nil then + icon_provider = get_icon_provider() + end + if icon_provider and not collapsed then + icon = icon_provider(kind) if icon and icon ~= "" then return icon end diff --git a/tests/config_spec.lua b/tests/config_spec.lua index 6080137..d9b5f69 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -86,7 +86,7 @@ describe("config", function() -- Icons it("reads icons from the default table", function() - config.setup({ nerd_font = true, use_lspkind = false }) + config.setup({ nerd_font = true, use_icon_provider = false }) assert.equals("󰊕 ", config.get_icon(0, "Function", false)) end) it("reads icons from setup var", function() @@ -95,7 +95,7 @@ describe("config", function() icons = { Function = "*", }, - use_lspkind = false, + use_icon_provider = false, }) assert.equals("*", config.get_icon(0, "Function", false)) assert.equals("󰊕 ", config.get_icon(0, "Method", false))