feat: icons can be defined as per-filetype map (#108)

This commit is contained in:
Steven Arcangeli 2022-05-29 13:30:19 -07:00
parent c003aca643
commit ae9787240c
7 changed files with 96 additions and 44 deletions

View file

@ -334,7 +334,7 @@ NOTES *aerial-notes
*aerial-filetype-map*
Certain options can be configured per-filetype by passing in a table. "_" will
be used as the default if the filetype is not present. >
be used as the default if the filetype is not present.
>
backends = {
'_' = {"lsp", "treesitter"},

View file

@ -69,7 +69,8 @@ local default_options = {
-- 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, aerial will use it for icons.
-- If you have lspkind-nvim installed, it will be the default icon set.
-- This can be a filetype map (see :help aerial-filetype-map)
icons = {},
-- Control which windows and buffers aerial should ignore.
@ -312,17 +313,17 @@ end
M.setup = function(opts)
opts = opts or {}
if HAS_LSPKIND and opts.icons then
vim.notify(
"Aerial: use lspkind to configure symbols instead of config value 'icons'",
vim.log.levels.WARN
)
end
local newconf = vim.tbl_deep_extend("force", default_options, opts)
if newconf.nerd_font == "auto" then
newconf.nerd_font = HAS_DEVICONS or HAS_LSPKIND
end
-- Undocumented use_lspkind option for tests. End users can simply provide
-- their own icons
if newconf.use_lspkind == nil then
newconf.use_lspkind = true
end
-- If not managing folds, don't link either direction
if newconf.manage_folds == false then
newconf.link_tree_to_folds = false
@ -336,11 +337,7 @@ M.setup = function(opts)
newconf[k] = nil
end
end
newconf.icons = vim.tbl_deep_extend(
"keep",
newconf.icons or {},
newconf.nerd_font and nerd_icons or plain_icons
)
newconf.default_icons = newconf.nerd_font and nerd_icons or plain_icons
-- Much of this logic is for backwards compatibility and can be removed in the
-- future
@ -492,29 +489,46 @@ setmetatable(M, {
end,
})
-- Exposed for tests
M._get_icon = function(kind, collapsed)
if collapsed then
kind = kind .. "Collapsed"
end
local ret = M.icons[kind]
if ret ~= nil then
return ret
end
if collapsed then
ret = M.icons["Collapsed"]
end
return ret or " "
end
M.get_icon = function(kind, collapsed)
if HAS_LSPKIND and not collapsed then
local icon = lspkind.symbolic(kind, { with_text = false })
local function get_icon(kind, filetypes)
for _, ft in ipairs(filetypes) do
local icon_map = M.icons[ft]
local icon = icon_map and icon_map[kind]
if icon then
return icon
end
end
return M._get_icon(kind, collapsed)
return M.icons[kind]
end
M.get_icon = function(bufnr, kind, collapsed)
if collapsed then
kind = kind .. "Collapsed"
end
local icon
-- Slight optimization for users that don't specify icons
if not vim.tbl_isempty(M.icons) then
local filetypes = M.get_filetypes(bufnr)
table.insert(filetypes, "_")
icon = get_icon(kind, filetypes)
if not icon and collapsed then
icon = get_icon("Collapsed", filetypes)
end
if icon then
return icon
end
end
if HAS_LSPKIND and M.use_lspkind and not collapsed then
icon = lspkind.symbolic(kind, { with_text = false })
if icon and icon ~= "" then
return icon
end
end
icon = M.default_icons[kind]
if not icon and collapsed then
icon = M.default_icons.Collapsed
end
return icon or " "
end
return M

View file

@ -148,7 +148,7 @@ M.get_location = function(exact)
while item do
table.insert(ret, 1, {
kind = item.kind,
icon = config.get_icon(item.kind),
icon = config.get_icon(0, item.kind),
name = item.name,
})
item = item.parent

View file

@ -93,7 +93,7 @@ M.update_aerial_buffer = function(buf)
end,
})
data[bufnr]:visit(function(item, conf)
local kind = config.get_icon(item.kind, conf.collapsed)
local kind = config.get_icon(bufnr, item.kind, conf.collapsed)
local spacing
if config.show_guides then
local last_spacing = 0

View file

@ -32,7 +32,7 @@ local function aerial_picker(opts)
local function make_display(entry)
local item = entry.value
local icon = config.get_icon(item.kind)
local icon = config.get_icon(bufnr, item.kind)
local text = vim.api.nvim_buf_get_lines(bufnr, item.lnum - 1, item.lnum, false)[1] or ""
text = vim.trim(text)
local columns = {

View file

@ -3,3 +3,4 @@ set -e
nvim --headless --noplugin -u tests/init.lua \
-c "PlenaryBustedDirectory ${1-tests} { minimal_init = './tests/init.lua' }"
echo "Success"

View file

@ -114,20 +114,57 @@ describe("config", function()
-- Icons
it("reads icons from the default table", function()
vim.g.aerial = {
nerd_font = true,
}
assert.equals("", config._get_icon("Function", false))
config.setup({ nerd_font = true, use_lspkind = false })
assert.equals("", config.get_icon(0, "Function", false))
end)
it("reads icons from g:aerial dict var", function()
vim.g.aerial = {
it("reads icons from setup var", function()
config.setup({
nerd_font = true,
icons = {
Function = "*",
},
}
assert.equals("*", config._get_icon("Function", false))
assert.equals("", config._get_icon("Method", false))
use_lspkind = false,
})
assert.equals("*", config.get_icon(0, "Function", false))
assert.equals("", config.get_icon(0, "Method", false))
end)
it("fetches the collapsed version of icon", function()
config.setup({
icons = {
FunctionCollapsed = "a",
},
})
assert.equals("a", config.get_icon(0, "Function", true))
end)
it("defaults to 'Collapsed' for collapsed icons", function()
config.setup({
icons = {
Collapsed = "a",
},
})
assert.equals("a", config.get_icon(0, "Function", true))
end)
it("uses filetype overrides for icons", function()
config.setup({
icons = {
foo = {
Function = "a",
},
},
})
vim.api.nvim_buf_set_option(0, "filetype", "foo")
assert.equals("a", config.get_icon(0, "Function", false))
end)
it("falls back to '_' filetype if no match", function()
config.setup({
icons = {
["_"] = {
Function = "a",
},
},
})
vim.api.nvim_buf_set_option(0, "filetype", "foo")
assert.equals("a", config.get_icon(0, "Function", false))
end)
-- This is for backwards compatibility with lsp options that used to be in the