From d01b530e7560a8fbe35c1e074e226069e8b95a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E9=85=94=E9=96=91=E5=90=9F?= <50296129+Jint-lzxy@users.noreply.github.com> Date: Wed, 4 Jan 2023 13:56:14 +0800 Subject: [PATCH] feat(lspsaga & lualine): Show symbols in lualine. --- lua/modules/completion/config.lua | 9 +++-- lua/modules/completion/plugins.lua | 2 +- lua/modules/tools/config.lua | 3 +- lua/modules/ui/config.lua | 48 ++++++++++++++++++++++++-- lua/modules/ui/icons.lua | 2 +- lua/modules/ui/plugins.lua | 2 +- lua/modules/utils/init.lua | 55 ++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 lua/modules/utils/init.lua diff --git a/lua/modules/completion/config.lua b/lua/modules/completion/config.lua index 9ddd8eb55e..00f5a181b2 100644 --- a/lua/modules/completion/config.lua +++ b/lua/modules/completion/config.lua @@ -99,12 +99,11 @@ function config.lspsaga() virtual_text = true, }, symbol_in_winbar = { + in_custom = true, enable = true, - in_custom = false, separator = " " .. icons.ui.Separator, - show_file = true, + show_file = false, click_support = function(node, clicks, button, modifiers) - -- To see all avaiable details: vim.pretty_print(node) local st = node.range.start local en = node.range["end"] if button == "l" then @@ -115,8 +114,8 @@ function config.lspsaga() end elseif button == "r" then if modifiers == "s" then - print("lspsaga") -- shift right click to print "lspsaga" - end -- jump to node's ending line+char + print("symbol_winbar") + end vim.fn.cursor(en.line + 1, en.character + 1) elseif button == "m" then -- middle click to visual select node diff --git a/lua/modules/completion/plugins.lua b/lua/modules/completion/plugins.lua index f2d4ddecba..3110d8d0f4 100644 --- a/lua/modules/completion/plugins.lua +++ b/lua/modules/completion/plugins.lua @@ -21,7 +21,7 @@ completion["williamboman/mason.nvim"] = { } completion["glepnir/lspsaga.nvim"] = { opt = true, - event = "LspAttach", + after = "nvim-lspconfig", config = conf.lspsaga, } completion["ray-x/lsp_signature.nvim"] = { opt = true, after = "nvim-lspconfig" } diff --git a/lua/modules/tools/config.lua b/lua/modules/tools/config.lua index 091536c42b..2c5a93db96 100644 --- a/lua/modules/tools/config.lua +++ b/lua/modules/tools/config.lua @@ -225,8 +225,7 @@ function config.wilder() ), }) - local string_fg = vim.api.nvim_get_hl_by_name("String", true).foreground - local match_hl = string_fg ~= nil and string.format("#%06x", string_fg) or "#ABE9B3" + local match_hl = require("modules.utils").hlToRgb("String", false, "#ABE9B3") local popupmenu_renderer = wilder.popupmenu_renderer(wilder.popupmenu_border_theme({ border = "rounded", diff --git a/lua/modules/ui/config.lua b/lua/modules/ui/config.lua index 7b47332a95..039a727510 100644 --- a/lua/modules/ui/config.lua +++ b/lua/modules/ui/config.lua @@ -361,8 +361,7 @@ end function config.neodim() vim.api.nvim_command([[packadd nvim-treesitter]]) - local normal_background = vim.api.nvim_get_hl_by_name("Normal", true).background - local blend_color = normal_background ~= nil and string.format("#%06x", normal_background) or "#000000" + local blend_color = require("modules.utils").hlToRgb("Normal", true, "#000000") require("neodim").setup({ alpha = 0.45, @@ -430,6 +429,36 @@ function config.lualine() return ok and m.waiting and icons.misc.EscapeST or "" end + local function lspsaga_symbols() + local exclude = { + ["terminal"] = true, + ["toggleterm"] = true, + ["prompt"] = true, + ["NvimTree"] = true, + ["help"] = true, + } + if vim.api.nvim_win_get_config(0).zindex or exclude[vim.bo.filetype] then + return "" -- Excluded filetypes + else + local ok, lspsaga = pcall(require, "lspsaga.symbolwinbar") + if ok then + if lspsaga.get_symbol_node() ~= nil then + return lspsaga.get_symbol_node() + else + return "" -- Cannot get node + end + end + end + end + + local function code_context() + if lspsaga_symbols() ~= "" then + return lspsaga_symbols() + else + return "" + end + end + local function diff_source() local gitsigns = vim.b.gitsigns_status_dict if gitsigns then @@ -450,6 +479,12 @@ function config.lualine() return icons.ui.RootFolderOpened .. cwd end + local conditions = { + check_code_context = function() + return lspsaga_symbols() ~= "" + end, + } + local mini_sections = { lualine_a = { "filetype" }, lualine_b = {}, @@ -503,7 +538,7 @@ function config.lualine() sections = { lualine_a = { { "mode" } }, lualine_b = { { "branch" }, { "diff", source = diff_source } }, - lualine_c = { { get_cwd } }, + lualine_c = { { get_cwd }, { code_context, cond = conditions.check_code_context } }, lualine_x = { { escape_status }, { @@ -551,6 +586,13 @@ function config.lualine() diffview, }, }) + + -- Properly set background color for lspsaga + local winbar_bg = require("modules.utils").hlToRgb("StatusLine", true, "#000000") + require("modules.utils").extend_hl("LspSagaWinbarSep", { bg = winbar_bg }) + for _, hlGroup in pairs(require("lspsaga.lspkind")) do + require("modules.utils").extend_hl("LspSagaWinbar" .. hlGroup[1], { bg = winbar_bg }) + end end function config.nvim_tree() diff --git a/lua/modules/ui/icons.lua b/lua/modules/ui/icons.lua index 7e69429e68..e1008d3bc4 100644 --- a/lua/modules/ui/icons.lua +++ b/lua/modules/ui/icons.lua @@ -29,7 +29,7 @@ local data = { TypeParameter = "", Unit = "", Value = "", - Variable = "", + Variable = "", -- ccls-specific icons. TypeAlias = "", Parameter = "", diff --git a/lua/modules/ui/plugins.lua b/lua/modules/ui/plugins.lua index a5177410d2..78c5aafce7 100644 --- a/lua/modules/ui/plugins.lua +++ b/lua/modules/ui/plugins.lua @@ -21,7 +21,7 @@ ui["rcarriga/nvim-notify"] = { } ui["hoob3rt/lualine.nvim"] = { opt = true, - after = "nvim-lspconfig", + after = { "nvim-lspconfig", "lspsaga.nvim" }, config = conf.lualine, } ui["goolord/alpha-nvim"] = { diff --git a/lua/modules/utils/init.lua b/lua/modules/utils/init.lua new file mode 100644 index 0000000000..28f85488fb --- /dev/null +++ b/lua/modules/utils/init.lua @@ -0,0 +1,55 @@ +local M = {} + +---@param c string @The color in hexadecimal. +local function hexToRgb(c) + c = string.lower(c) + return { tonumber(c:sub(2, 3), 16), tonumber(c:sub(4, 5), 16), tonumber(c:sub(6, 7), 16) } +end + +---@param foreground string @The foreground color +---@param background string @The background color to blend with +---@param alpha number|string @Number between 0 and 1 for blending amount. +function M.blend(foreground, background, alpha) + alpha = type(alpha) == "string" and (tonumber(alpha, 16) / 0xff) or alpha + local bg = hexToRgb(background) + local fg = hexToRgb(foreground) + + local blendChannel = function(i) + local ret = (alpha * fg[i] + ((1 - alpha) * bg[i])) + return math.floor(math.min(math.max(0, ret), 255) + 0.5) + end + + return string.format("#%02x%02x%02x", blendChannel(1), blendChannel(2), blendChannel(3)) +end + +--- Get RGB highlight by highlight group +---@param hl_group string @Highlight group name +---@param use_bg boolean @Returns background or not +---@param fallback_hl? string @Fallback value if the hl group is not defined +---@return string +function M.hlToRgb(hl_group, use_bg, fallback_hl) + fallback_hl = fallback_hl or "#D9E0ED" + + if use_bg == true then + local color = vim.api.nvim_get_hl_by_name(hl_group, true).background + local hex = color ~= nil and string.format("#%06x", color) or fallback_hl + return hex + else + local color = vim.api.nvim_get_hl_by_name(hl_group, true).foreground + local hex = color ~= nil and string.format("#%06x", color) or fallback_hl + return hex + end +end + +function M.extend_hl(name, def) + local current_def = vim.api.nvim_get_hl_by_name(name, true) + if current_def == nil then + -- Do nothing if highlight group not found + return + end + local combined_def = vim.tbl_extend("force", current_def, def) + + vim.api.nvim_set_hl(0, name, combined_def) +end + +return M