From e9ade0ed220d0ec961d5cfe673f7eeeff2354790 Mon Sep 17 00:00:00 2001 From: hedy Date: Sat, 11 Nov 2023 16:01:05 +0800 Subject: [PATCH] feat: Custom icon sources --- README.md | 57 +++++++++++++++++++++++++++++++++ lua/symbols-outline/config.lua | 2 ++ lua/symbols-outline/symbols.lua | 36 +++++++++++++++------ 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9dcc524..321a6eb 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,9 @@ Features/Changes: - Option to blend cursor with cursorline (`outline_window.hide_cursor`) +- Option to use lspkind for icons, and use your own fetcher function. See +[config](#configuration) and [tips](#tips) + Screen recordings/shots of some of the features is shown at the [bottom of the readme](#recipes). @@ -602,6 +605,17 @@ Default values are shown: -- Symbols to ignore. -- Possible values are the Keys in the icons table below. blacklist = {}, + -- Added in this fork: + -- You can use a custom function that returns the icon for each symbol kind. + -- This function takes a kind (string) as parameter and should return an + -- icon. + icon_fetcher = nil, + -- 3rd party source for fetching icons. Fallback if icon_fetcher returned + -- empty string. Currently supported values: 'lspkind' + icon_source = nil, + -- The next fall back if both icon_fetcher and icon_source has failed, is + -- the custom mapping of icons specified below. The icons table is also + -- needed for specifying hl group. -- Changed in this fork to fix deprecated icons not showing. icons = { File = { icon = "󰈔", hl = "@text.uri" }, @@ -645,6 +659,15 @@ Default values are shown: To find out exactly what some of the options do, please see the [recipes](#recipes) section of the readme at the bottom for screen-recordings. +The order in which the sources for icons are checked is: + +1. Icon fetcher function +2. Icon source (only `lspkind` is supported for this option as of now) +3. Icons table + +A fallback is always used if the previous candidate returned either an empty +string or a falsey value. + ## Commands - **:SymbolsOutline[!]** @@ -744,6 +767,9 @@ Possible highlight groups provided by symbols-outline to customize: You can customize any other highlight groups using `winhl` too, this option is passed directly to the `winhl` vim option unprocessed. +To customize colors of the symbol icons, use the `symbols.icons` table. See +[config](#configuration). + ### Preview window ```lua @@ -825,6 +851,20 @@ require'symbols-outline' `outline_window.winhl` for the outline window or `preview_window.winhl` for the preview floating window. See [highlights](#highlights). +- To fix symbol icon related issues, there are several options. If you use + `lspkind`, you can set `symbols.icon_source = 'lspkind'` to use lspkind for + fetching icons. You can also use your own function `symbols.icon_fetcher` that + takes a string and should return an icon. Otherwise, you can edit the + `symbols.icons` table for specifying icons. + + The order in which the sources of icons are checked is: + + 1. Icon fetcher function + 2. Icon source + 3. Icons table + + A fallback is always used if the previous candidate returned either an empty + string or a falsey value. ## Recipes @@ -920,6 +960,23 @@ outline becomes more readable this way, hence this is an option. This feature is newly added in this fork, and is currently experimental (may be unstable). +- **Custom icons** + +You can write your own function for fetching icons. Here is one such example +that simply returns in plain text, the first letter of the given kind. + +```lua +symbols = { + icon_fetcher = function(kind) return kind:sub(1,1) end +} +``` + +The fetcher function, if provided, is checked first before using `icon_source` +and `icons` as fallback. + +outline with plain text icons + + --- diff --git a/lua/symbols-outline/config.lua b/lua/symbols-outline/config.lua index 98b4595..1e472e5 100644 --- a/lua/symbols-outline/config.lua +++ b/lua/symbols-outline/config.lua @@ -74,6 +74,8 @@ M.defaults = { }, symbols = { blacklist = {}, + icon_source = nil, + icon_fetcher = nil, icons = { File = { icon = '󰈔', hl = '@text.uri' }, Module = { icon = '󰆧', hl = '@namespace' }, diff --git a/lua/symbols-outline/symbols.lua b/lua/symbols-outline/symbols.lua index fee46b4..b8e6ed2 100644 --- a/lua/symbols-outline/symbols.lua +++ b/lua/symbols-outline/symbols.lua @@ -1,4 +1,4 @@ -local config = require 'symbols-outline.config' +local cfg = require 'symbols-outline.config' local M = {} @@ -39,18 +39,36 @@ M.kinds = { [255] = 'Macro', } +---@param kind string|integer function M.icon_from_kind(kind) - local symbols = config.o.symbols.icons - - if type(kind) == 'string' then - return symbols[kind].icon + local kindstr = kind + if type(kind) ~= 'string' then + kindstr = M.kinds[kind] + end + if not kindstr then + kindstr = 'Object' end - -- If the kind index is not available then default to 'Object' - if M.kinds[kind] == nil then - kind = 19 + if type(cfg.o.symbols.icon_fetcher) == 'function' then + local icon = cfg.o.symbols.icon_fetcher(kindstr) + if icon and icon ~= "" then + return icon + end end - return symbols[M.kinds[kind]].icon + + if cfg.o.symbols.icon_source == 'lspkind' then + local has_lspkind, lspkind = pcall(require, 'lspkind') + if not has_lspkind then + vim.notify("[symbols-outline]: icon_source set to lspkind but failed to require lspkind!", vim.log.levels.ERROR) + else + local icon = lspkind.symbolic(kindstr, { with_text = false }) + if icon and icon ~= "" then + return icon + end + end + end + + return cfg.o.symbols.icons[kindstr].icon end return M