Skip to content

Commit

Permalink
feat(lspsaga & lualine): Show symbols in lualine.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jint-lzxy committed Jan 4, 2023
1 parent 642b1db commit d01b530
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 13 deletions.
9 changes: 4 additions & 5 deletions lua/modules/completion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/modules/completion/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 1 addition & 2 deletions lua/modules/tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 45 additions & 3 deletions lua/modules/ui/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 = {},
Expand Down Expand Up @@ -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 },
{
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lua/modules/ui/icons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local data = {
TypeParameter = "",
Unit = "",
Value = "",
Variable = "",
Variable = "",
-- ccls-specific icons.
TypeAlias = "",
Parameter = "",
Expand Down
2 changes: 1 addition & 1 deletion lua/modules/ui/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = {
Expand Down
55 changes: 55 additions & 0 deletions lua/modules/utils/init.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d01b530

Please sign in to comment.