Skip to content

Commit 8d254bb

Browse files
authored
Make DapUI extension work with the control bar
1 parent 0a5a668 commit 8d254bb

File tree

2 files changed

+107
-18
lines changed

2 files changed

+107
-18
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,16 @@ extensions = {'quickfix'}
928928
- toggleterm
929929
- trouble
930930

931+
#### nvim-dap-ui
932+
933+
This Extension needs to be loaded with some arguments:
934+
```lua
935+
extensions = {require("lualine.extensions.nvim-dap-ui").setup({
936+
active_separator = ">",
937+
inactive_separator = "|",
938+
})}
939+
```
940+
931941
#### Custom extensions
932942

933943
You can define your own extensions. If you believe an extension may be useful to others, then please submit a PR.
+97-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,97 @@
1-
-- MIT license, see LICENSE for more details.
2-
-- Extension for nvim-dap-ui
3-
local M = {}
4-
5-
M.sections = {
6-
lualine_a = { { 'filename', file_status = false } },
7-
}
8-
9-
M.filetypes = {
10-
'dap-repl',
11-
'dapui_console',
12-
'dapui_watches',
13-
'dapui_stacks',
14-
'dapui_breakpoints',
15-
'dapui_scopes',
16-
}
17-
18-
return M
1+
EXTENSION = {}
2+
3+
local function get_color_codes(name)
4+
local hl = vim.api.nvim_get_hl(0, { name = name })
5+
local fg = string.format("#%06x", hl.fg and hl.fg or 0)
6+
local bg = string.format("#%06x", hl.bg and hl.bg or 0)
7+
return fg, bg
8+
end
9+
10+
local function merge_colors(foreground, background)
11+
local new_name = foreground .. background
12+
local fg, _ = get_color_codes(foreground)
13+
local _, bg = get_color_codes(background)
14+
vim.api.nvim_set_hl(0, new_name, { fg = fg, bg = bg })
15+
return string.format("%%#%s#", new_name)
16+
end
17+
local function inverse_color(name)
18+
local fg, bg = get_color_codes(name)
19+
local new_name = name .. "_inversed"
20+
vim.api.nvim_set_hl(0, new_name, { fg = bg, bg = fg })
21+
return string.format("%%#%s#", new_name)
22+
end
23+
24+
local function parse_control_element(element)
25+
local e = element:match("(.*)%%#0#$")
26+
local color, action_element = e:match("^(.-)#%%(.+)$")
27+
color = color:gsub("^%%#", "")
28+
return color, "%" .. action_element
29+
end
30+
31+
function EXTENSION.setup(config)
32+
local dapui = {}
33+
dapui.filetypes = {
34+
"dap-repl",
35+
"dapui_console",
36+
"dapui_console",
37+
"dapui_watches",
38+
"dapui_stacks",
39+
"dapui_breakpoints",
40+
"dapui_scopes",
41+
}
42+
43+
local get_mode = require("lualine.highlight").get_mode_suffix
44+
45+
local lualine_color = "lualine_a"
46+
local lualine_inacitve = "_inactive"
47+
local default_color = lualine_color .. lualine_inacitve
48+
local color_start = "%#"
49+
local color_end = "#"
50+
51+
local function get_dap_repl_winbar(separator, active)
52+
local background_color = string.format(lualine_color .. "%s", active and get_mode() or lualine_inacitve)
53+
54+
local controls_string = color_start .. default_color .. color_end .. " "
55+
for control_element in require("dapui.controls").controls():gmatch("%S+") do
56+
local color, action_element = parse_control_element(control_element)
57+
local new_color = merge_colors(color, default_color)
58+
local out = new_color .. action_element
59+
controls_string = controls_string .. " " .. out
60+
end
61+
local separator_color = active and inverse_color(background_color) or color_start .. default_color .. color_end
62+
return "DAP Repl " .. separator_color .. separator .. controls_string
63+
end
64+
65+
local function get_dapui_winbar(separator, active)
66+
local filetype = vim.bo.filetype
67+
local disabled_filetypes = { "dap-repl" }
68+
if vim.tbl_contains(disabled_filetypes, filetype) then
69+
return get_dap_repl_winbar(separator, active)
70+
else
71+
return vim.fn.expand("%:t")
72+
end
73+
end
74+
75+
dapui.winbar = {
76+
lualine_a = {
77+
{
78+
function()
79+
return get_dapui_winbar(config.active_separator, true)
80+
end,
81+
},
82+
},
83+
}
84+
85+
dapui.inactive_winbar = {
86+
lualine_a = {
87+
{
88+
function()
89+
return get_dapui_winbar(config.inactive_separator, false)
90+
end,
91+
},
92+
},
93+
}
94+
return dapui
95+
end
96+
97+
return EXTENSION

0 commit comments

Comments
 (0)