Files
outline.nvim/lua/outline/symbols.lua
Oskar Tołkacz fdaa883d9d Show lspkind error message just once
Instead of showing an error message for every symbol in every outline
window, show just one error on opening nvim.
2024-08-12 10:43:26 -04:00

97 lines
1.9 KiB
Lua

local cfg = require('outline.config')
local M = {}
M.kinds = {
[1] = 'File',
[2] = 'Module',
[3] = 'Namespace',
[4] = 'Package',
[5] = 'Class',
[6] = 'Method',
[7] = 'Property',
[8] = 'Field',
[9] = 'Constructor',
[10] = 'Enum',
[11] = 'Interface',
[12] = 'Function',
[13] = 'Variable',
[14] = 'Constant',
[15] = 'String',
[16] = 'Number',
[17] = 'Boolean',
[18] = 'Array',
[19] = 'Object',
[20] = 'Key',
[21] = 'Null',
[22] = 'EnumMember',
[23] = 'Struct',
[24] = 'Event',
[25] = 'Operator',
[26] = 'TypeParameter',
[27] = 'Component',
[28] = 'Fragment',
-- ccls
[252] = 'TypeAlias',
[253] = 'Parameter',
[254] = 'StaticMethod',
[255] = 'Macro',
}
-- inverse indexing of symbols.kind
M.str_to_kind = {}
for k, v in pairs(M.kinds) do
M.str_to_kind[v] = k
end
---@return table
local function get_lspkind()
local has_lspkind, lspkind = pcall(require, 'lspkind')
if has_lspkind then
return lspkind
end
vim.notify(
'[outline]: icon_source set to lspkind but failed to require lspkind!',
vim.log.levels.ERROR
)
-- return lspkind stub
return {
symbolic = function(kind, opts) return '' end
}
end
local lspkind = get_lspkind()
---@param kind string|integer
---@param bufnr integer
---@return string icon
function M.icon_from_kind(kind, bufnr)
local kindstr = kind
if type(kind) ~= 'string' then
kindstr = M.kinds[kind]
end
if not kindstr then
kindstr = 'Object'
end
if type(cfg.o.symbols.icon_fetcher) == 'function' then
local icon = cfg.o.symbols.icon_fetcher(kindstr, bufnr)
-- Allow returning empty string
if icon then
return icon
end
end
if cfg.o.symbols.icon_source == 'lspkind' then
local icon = lspkind.symbolic(kindstr, { with_text = false })
if icon and icon ~= '' then
return icon
end
end
return cfg.o.symbols.icons[kindstr].icon
end
return M