From 8d254bb608ffb77c1b5009c65bc4e0a852eb8cf7 Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Sat, 1 Jun 2024 19:38:08 +0200 Subject: [PATCH 1/3] Make DapUI extension work with the control bar --- README.md | 10 +++ lua/lualine/extensions/nvim-dap-ui.lua | 115 +++++++++++++++++++++---- 2 files changed, 107 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index eeb8d42c0..4e0ae0200 100644 --- a/README.md +++ b/README.md @@ -928,6 +928,16 @@ extensions = {'quickfix'} - toggleterm - trouble +#### nvim-dap-ui + +This Extension needs to be loaded with some arguments: +```lua +extensions = {require("lualine.extensions.nvim-dap-ui").setup({ + active_separator = ">", + inactive_separator = "|", +})} +``` + #### Custom extensions You can define your own extensions. If you believe an extension may be useful to others, then please submit a PR. diff --git a/lua/lualine/extensions/nvim-dap-ui.lua b/lua/lualine/extensions/nvim-dap-ui.lua index 42d5265c1..16044aa6f 100644 --- a/lua/lualine/extensions/nvim-dap-ui.lua +++ b/lua/lualine/extensions/nvim-dap-ui.lua @@ -1,18 +1,97 @@ --- MIT license, see LICENSE for more details. --- Extension for nvim-dap-ui -local M = {} - -M.sections = { - lualine_a = { { 'filename', file_status = false } }, -} - -M.filetypes = { - 'dap-repl', - 'dapui_console', - 'dapui_watches', - 'dapui_stacks', - 'dapui_breakpoints', - 'dapui_scopes', -} - -return M +EXTENSION = {} + +local function get_color_codes(name) + local hl = vim.api.nvim_get_hl(0, { name = name }) + local fg = string.format("#%06x", hl.fg and hl.fg or 0) + local bg = string.format("#%06x", hl.bg and hl.bg or 0) + return fg, bg +end + +local function merge_colors(foreground, background) + local new_name = foreground .. background + local fg, _ = get_color_codes(foreground) + local _, bg = get_color_codes(background) + vim.api.nvim_set_hl(0, new_name, { fg = fg, bg = bg }) + return string.format("%%#%s#", new_name) +end +local function inverse_color(name) + local fg, bg = get_color_codes(name) + local new_name = name .. "_inversed" + vim.api.nvim_set_hl(0, new_name, { fg = bg, bg = fg }) + return string.format("%%#%s#", new_name) +end + +local function parse_control_element(element) + local e = element:match("(.*)%%#0#$") + local color, action_element = e:match("^(.-)#%%(.+)$") + color = color:gsub("^%%#", "") + return color, "%" .. action_element +end + +function EXTENSION.setup(config) + local dapui = {} + dapui.filetypes = { + "dap-repl", + "dapui_console", + "dapui_console", + "dapui_watches", + "dapui_stacks", + "dapui_breakpoints", + "dapui_scopes", + } + + local get_mode = require("lualine.highlight").get_mode_suffix + + local lualine_color = "lualine_a" + local lualine_inacitve = "_inactive" + local default_color = lualine_color .. lualine_inacitve + local color_start = "%#" + local color_end = "#" + + local function get_dap_repl_winbar(separator, active) + local background_color = string.format(lualine_color .. "%s", active and get_mode() or lualine_inacitve) + + local controls_string = color_start .. default_color .. color_end .. " " + for control_element in require("dapui.controls").controls():gmatch("%S+") do + local color, action_element = parse_control_element(control_element) + local new_color = merge_colors(color, default_color) + local out = new_color .. action_element + controls_string = controls_string .. " " .. out + end + local separator_color = active and inverse_color(background_color) or color_start .. default_color .. color_end + return "DAP Repl " .. separator_color .. separator .. controls_string + end + + local function get_dapui_winbar(separator, active) + local filetype = vim.bo.filetype + local disabled_filetypes = { "dap-repl" } + if vim.tbl_contains(disabled_filetypes, filetype) then + return get_dap_repl_winbar(separator, active) + else + return vim.fn.expand("%:t") + end + end + + dapui.winbar = { + lualine_a = { + { + function() + return get_dapui_winbar(config.active_separator, true) + end, + }, + }, + } + + dapui.inactive_winbar = { + lualine_a = { + { + function() + return get_dapui_winbar(config.inactive_separator, false) + end, + }, + }, + } + return dapui +end + +return EXTENSION \ No newline at end of file From b484ab286fa6ef43ba3da4fdff858c34c0652699 Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Sat, 1 Jun 2024 19:52:52 +0200 Subject: [PATCH 2/3] Cleanup --- lua/lualine/extensions/nvim-dap-ui.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/lualine/extensions/nvim-dap-ui.lua b/lua/lualine/extensions/nvim-dap-ui.lua index 16044aa6f..03b7f04b5 100644 --- a/lua/lualine/extensions/nvim-dap-ui.lua +++ b/lua/lualine/extensions/nvim-dap-ui.lua @@ -1,4 +1,4 @@ -EXTENSION = {} +local M = {} local function get_color_codes(name) local hl = vim.api.nvim_get_hl(0, { name = name }) @@ -28,7 +28,7 @@ local function parse_control_element(element) return color, "%" .. action_element end -function EXTENSION.setup(config) +function M.setup(config) local dapui = {} dapui.filetypes = { "dap-repl", @@ -94,4 +94,4 @@ function EXTENSION.setup(config) return dapui end -return EXTENSION \ No newline at end of file +return M From 5833da0b7618ebf05c0df0fe061368fd471054c8 Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Sat, 1 Jun 2024 21:31:52 +0200 Subject: [PATCH 3/3] Refactor --- README.md | 10 -- lua/lualine/extensions/nvim-dap-ui.lua | 125 +++++++++---------------- 2 files changed, 43 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 4e0ae0200..eeb8d42c0 100644 --- a/README.md +++ b/README.md @@ -928,16 +928,6 @@ extensions = {'quickfix'} - toggleterm - trouble -#### nvim-dap-ui - -This Extension needs to be loaded with some arguments: -```lua -extensions = {require("lualine.extensions.nvim-dap-ui").setup({ - active_separator = ">", - inactive_separator = "|", -})} -``` - #### Custom extensions You can define your own extensions. If you believe an extension may be useful to others, then please submit a PR. diff --git a/lua/lualine/extensions/nvim-dap-ui.lua b/lua/lualine/extensions/nvim-dap-ui.lua index 03b7f04b5..120f25f3b 100644 --- a/lua/lualine/extensions/nvim-dap-ui.lua +++ b/lua/lualine/extensions/nvim-dap-ui.lua @@ -1,97 +1,58 @@ local M = {} -local function get_color_codes(name) - local hl = vim.api.nvim_get_hl(0, { name = name }) - local fg = string.format("#%06x", hl.fg and hl.fg or 0) - local bg = string.format("#%06x", hl.bg and hl.bg or 0) - return fg, bg -end +M.filetypes = { + "dap-repl", + "dapui_console", + "dapui_console", + "dapui_watches", + "dapui_stacks", + "dapui_breakpoints", + "dapui_scopes", +} local function merge_colors(foreground, background) - local new_name = foreground .. background - local fg, _ = get_color_codes(foreground) - local _, bg = get_color_codes(background) - vim.api.nvim_set_hl(0, new_name, { fg = fg, bg = bg }) - return string.format("%%#%s#", new_name) -end -local function inverse_color(name) - local fg, bg = get_color_codes(name) - local new_name = name .. "_inversed" - vim.api.nvim_set_hl(0, new_name, { fg = bg, bg = fg }) - return string.format("%%#%s#", new_name) -end - -local function parse_control_element(element) - local e = element:match("(.*)%%#0#$") - local color, action_element = e:match("^(.-)#%%(.+)$") - color = color:gsub("^%%#", "") - return color, "%" .. action_element -end + local new_name = foreground .. background -function M.setup(config) - local dapui = {} - dapui.filetypes = { - "dap-repl", - "dapui_console", - "dapui_console", - "dapui_watches", - "dapui_stacks", - "dapui_breakpoints", - "dapui_scopes", - } + local hl_fg = vim.api.nvim_get_hl(0, { name = foreground }) + local hl_bg = vim.api.nvim_get_hl(0, { name = background }) - local get_mode = require("lualine.highlight").get_mode_suffix + local fg = string.format("#%06x", hl_fg.fg and hl_fg.fg or 0) + local bg = string.format("#%06x", hl_bg.bg and hl_bg.bg or 0) - local lualine_color = "lualine_a" - local lualine_inacitve = "_inactive" - local default_color = lualine_color .. lualine_inacitve - local color_start = "%#" - local color_end = "#" + vim.api.nvim_set_hl(0, new_name, { fg = fg, bg = bg }) + return new_name +end - local function get_dap_repl_winbar(separator, active) - local background_color = string.format(lualine_color .. "%s", active and get_mode() or lualine_inacitve) +local function get_dap_repl_winbar(active) + local get_mode = require("lualine.highlight").get_mode_suffix - local controls_string = color_start .. default_color .. color_end .. " " - for control_element in require("dapui.controls").controls():gmatch("%S+") do - local color, action_element = parse_control_element(control_element) - local new_color = merge_colors(color, default_color) - local out = new_color .. action_element - controls_string = controls_string .. " " .. out - end - local separator_color = active and inverse_color(background_color) or color_start .. default_color .. color_end - return "DAP Repl " .. separator_color .. separator .. controls_string - end + return function() + local filetype = vim.bo.filetype + local disabled_filetypes = { "dap-repl" } - local function get_dapui_winbar(separator, active) - local filetype = vim.bo.filetype - local disabled_filetypes = { "dap-repl" } - if vim.tbl_contains(disabled_filetypes, filetype) then - return get_dap_repl_winbar(separator, active) - else - return vim.fn.expand("%:t") - end - end + if not vim.tbl_contains(disabled_filetypes, filetype) then + return "" + end - dapui.winbar = { - lualine_a = { - { - function() - return get_dapui_winbar(config.active_separator, true) - end, - }, - }, - } + local background_color = string.format("lualine_b" .. "%s", active and get_mode() or "_inactive") - dapui.inactive_winbar = { - lualine_a = { - { - function() - return get_dapui_winbar(config.inactive_separator, false) - end, - }, - }, - } - return dapui + local controls_string = "%#" .. background_color .. "#" + for element in require("dapui.controls").controls():gmatch("%S+") do + local color, action = string.match(element, "%%#(.*)#(%%.*)%%#0#") + controls_string = controls_string .. " %#" .. merge_colors(color, background_color) .. "#" .. action + end + return controls_string + end end +M.winbar = { + lualine_a = { { "filename", file_status = false } }, + lualine_b = { get_dap_repl_winbar(true) }, +} + +M.inactive_winbar = { + lualine_a = { { "filename", file_status = false } }, + lualine_b = { get_dap_repl_winbar(false) }, +} + return M