feat(lsp): use symbol icons and highlight using CmpItemKindXXX hls

main
bhagwan 2 years ago
parent 7817c4205e
commit ea287de481

@ -750,6 +750,12 @@ require'fzf-lua'.setup {
git_icons = false,
lsp_icons = true,
ui_select = true, -- use 'vim.ui.select' for code actions
symbol_style = 1, -- style for document/workspace symbols
-- colorize using CmpItemKindXXX hls
-- false: disable, 1: icon+kind
-- 2: icon only, 3: kind only
-- additional symbol formatting, works with or without style
symbol_fmt = function(s) return "["..s.."]" end,
severity = "hint",
icons = {
["Error"] = { icon = "", color = "red" }, -- error

@ -795,6 +795,12 @@ Consult the list below for available settings:
git_icons = false,
lsp_icons = true,
ui_select = true, -- use 'vim.ui.select' for code actions
symbol_style = 1, -- style for document/workspace symbols
-- colorize using CmpItemKindXXX hls
-- false: disable, 1: icon+kind
-- 2: icon only, 3: kind only
-- additional symbol formatting, works with or without style
symbol_fmt = function(s) return "["..s.."]" end,
severity = "hint",
icons = {
["Error"] = { icon = "", color = "red" }, -- error

@ -484,6 +484,8 @@ M.globals.lsp = {
lsp_icons = true,
cwd_only = false,
ui_select = true,
symbol_style = 1,
symbol_fmt = function(s) return "["..s.."]" end,
async_or_timeout = 5000,
_actions = function() return M.globals.actions.files end,
icons = {

@ -119,7 +119,7 @@ local function gen_buffer_entry(opts, buf, hl_curbuf)
if opts.filename_only then
bufname = path.basename(bufname)
end
-- replace $HOME with '~' for paths ourside of cwd
-- replace $HOME with '~' for paths outside of cwd
bufname = bufname:gsub("^"..vim.env.HOME, "~")
-- add line number
bufname = ("%s:%s"):format(bufname, buf.info.lnum>0 and buf.info.lnum or "")

@ -81,6 +81,12 @@ local function symbol_handler(opts, cb, _, result, _, _)
end
if not opts.current_buffer_only or
vim.api.nvim_buf_get_name(opts.bufnr) == entry.filename then
if M._sym2style then
local kind = entry.text:match("%[(.-)%]")
if kind and M._sym2style[kind] then
entry.text = entry.text:gsub("%[.-%]", M._sym2style[kind])
end
end
entry = core.make_entry_lcol(opts, entry)
entry = core.make_entry_file(opts, entry)
if entry then
@ -373,12 +379,60 @@ M.outgoing_calls = function(opts)
return M.call_hierarchy(opts)
end
local function gen_sym2style_map(opts)
assert(M._sym2style == nil)
assert(opts.symbol_style ~= nil)
M._sym2style = {}
local colormap = vim.api.nvim_get_color_map()
for k, v in pairs(vim.lsp.protocol.CompletionItemKind) do
if type(k) == 'string' then
local icon = vim.lsp.protocol.CompletionItemKind[v]
-- style==1: "<icon> <kind>"
-- style==2: "<icon>"
-- style==3: "<kind>"
local s = nil
if opts.symbol_style == 1 then
s = ("%s %s"):format(icon, k)
elseif opts.symbol_style == 2 then
s = icon
elseif opts.symbol_style == 3 then
s = k
end
if s then
M._sym2style[k] = utils.ansi_from_hl("CmpItemKind" .. k, s, colormap)
else
-- can get here when only 'opts.symbol_fmt' was set
M._sym2style[k] = k
end
end
end
if type(opts.symbol_fmt) == 'function' then
for k, v in pairs(M._sym2style) do
M._sym2style[k] = opts.symbol_fmt(v) or v
end
end
end
M.document_symbols = function(opts)
opts = set_async_default(opts, true)
-- TODO: filename hiding
-- since single document
opts = normalize_lsp_opts(opts, config.globals.lsp)
if not opts then return end
opts = core.set_header(opts, 2)
opts = core.set_fzf_field_index(opts)
if opts.force_uri == nil then opts.force_uri = true end
if not opts.fzf_opts or opts.fzf_opts['--with-nth'] == nil then
opts.fzf_opts = opts.fzf_opts or {}
opts.fzf_opts["--with-nth"] = '2..'
opts.fzf_opts["--tiebreak"] = 'index'
end
opts.ignore_filename = true
return fzf_lsp_locations(opts)
opts = set_lsp_fzf_fn(opts)
if not opts.fzf_fn then return end
if opts.symbol_style or opts.symbol_fmt then
gen_sym2style_map(opts)
opts.fn_post_fzf = function() M._sym2style = nil end
end
return core.fzf_files(opts)
end
M.workspace_symbols = function(opts)
@ -386,10 +440,15 @@ M.workspace_symbols = function(opts)
opts = normalize_lsp_opts(opts, config.globals.lsp)
if not opts then return end
opts.lsp_params = {query = opts.query or ''}
opts = core.set_header(opts, 2)
opts = core.set_fzf_field_index(opts)
if opts.force_uri == nil then opts.force_uri = true end
opts = set_lsp_fzf_fn(opts)
if not opts.fzf_fn then return end
if opts.symbol_style or opts.symbol_fmt then
gen_sym2style_map(opts)
opts.fn_post_fzf = function() M._sym2style = nil end
end
return core.fzf_files(opts)
end

Loading…
Cancel
Save