-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
init.lua
420 lines (376 loc) · 11.5 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
---@tag nvim-dap-ui
---@toc
---@text
--- A UI for nvim-dap which provides a good out of the box configuration.
--- nvim-dap-ui is built on the idea of "elements". These elements are windows
--- which provide different features.
--- Elements are grouped into layouts which can be placed on any side of the
--- screen. There can be any number of layouts, containing whichever elements
--- desired.
---
--- Elements can also be displayed temporarily in a floating window.
---
--- See `:h dapui.setup()` for configuration options and defaults
---
--- It is highly recommended to use neodev.nvim to enable type checking for
--- nvim-dap-ui to get type checking, documentation and autocompletion for
--- all API functions.
---
--- ```lua
--- require("neodev").setup({
--- library = { plugins = { "nvim-dap-ui" }, types = true },
--- ...
--- })
--- ```
---
--- The default icons use codicons(https://github.com/microsoft/vscode-codicons).
--- It's recommended to use this fork(https://github.com/ChristianChiarulli/neovim-codicons)
--- which fixes alignment issues for the terminal. If your terminal doesn't
--- support font fallback and you need to have icons included in your font,
--- you can patch it via Font Patcher(https://github.com/ryanoasis/nerd-fonts#option-8-patch-your-own-font).
--- There is a simple step by step guide here: https://github.com/mortepau/codicons.nvim#how-to-patch-fonts.
local success, _ = pcall(require, "nio")
if not success then
error(
"nvim-dap-ui requires nvim-nio to be installed. Install from https://github.com/nvim-neotest/nvim-nio"
)
end
local dap = require("dap")
---@class dapui
---@nodoc
local dapui = {}
local windows = require("dapui.windows")
local config = require("dapui.config")
local util = require("dapui.util")
local nio = require("nio")
local controls = require("dapui.controls")
---@type table<string, dapui.Element>
---@nodoc
local elements = {}
local open_float = nil
local function query_elem_name()
local entries = {}
for name, _ in pairs(elements) do
if name ~= "hover" then
entries[#entries + 1] = name
end
end
return nio.ui.select(entries, {
prompt = "Select an element:",
format_item = function(entry)
return entry:sub(1, 1):upper() .. entry:sub(2)
end,
})
end
---@toc_entry Setup
---@text
--- Configure nvim-dap-ui
---@seealso |dapui.Config|
---
---@eval return require('dapui.config')._format_default()
---@param user_config? dapui.Config
function dapui.setup(user_config)
util.stop_render_tasks()
config.setup(user_config)
local client = require("dapui.client")(dap.session)
---@type table<string, dapui.Element>
for _, module in pairs({
"breakpoints",
"repl",
"scopes",
"stacks",
"watches",
"hover",
"console",
}) do
local existing_elem = elements[module]
if existing_elem then
local buffer = existing_elem.buffer()
if vim.api.nvim_buf_is_valid(buffer) then
vim.api.nvim_buf_delete(buffer, { force = true })
end
end
---@type dapui.Element
local elem = require("dapui.elements." .. module)(client)
elements[module] = elem
end
local element_buffers = {}
for name, elem in pairs(elements) do
element_buffers[name] = elem.buffer
end
windows.setup(element_buffers)
end
---@class dapui.FloatElementArgs
---@field width integer Fixed width of window
---@field height integer Fixed height of window
---@field enter boolean Whether or not to enter the window after opening
---@field title string Title of window
---@field position "center" Position of floating window
--- Open a floating window containing the desired element.
---
--- If no fixed dimensions are given, the window will expand to fit the contents
--- of the buffer.
---@param elem_name string
---@param args? dapui.FloatElementArgs
function dapui.float_element(elem_name, args)
nio.run(function()
if not dap.session() then
util.notify("No active debug session", vim.log.levels.WARN)
return
end
if open_float then
return open_float:jump_to()
end
local line_no = nio.fn.screenrow()
local col_no = nio.fn.screencol()
local position = { line = line_no, col = col_no }
elem_name = elem_name or query_elem_name()
if not elem_name then
return
end
local elem = elements[elem_name]
elem.render()
args = vim.tbl_deep_extend(
"keep",
args or {},
elem.float_defaults and elem.float_defaults() or {},
{ title = elem_name }
)
nio.scheduler()
open_float = require("dapui.windows").open_float(elem_name, elem, position, args)
if open_float then
open_float:listen("close", function()
open_float = nil
end)
end
end)
end
local prev_expr = nil
---@class dapui.EvalArgs
---@field context string Context to use for evalutate request, defaults to
--- "hover". Hover requests should have no side effects, if you have errors
--- with evaluation, try changing context to "repl". See the DAP specification
--- for more details.
---@field width integer Fixed width of window
---@field height integer Fixed height of window
---@field enter boolean Whether or not to enter the window after opening
--- Open a floating window containing the result of evaluting an expression
---
--- If no fixed dimensions are given, the window will expand to fit the contents
--- of the buffer.
---@param expr? string Expression to evaluate. If nil, then in normal more the
--- current word is used, and in visual mode the currently highlighted text.
---@param args? dapui.EvalArgs
function dapui.eval(expr, args)
nio.run(function()
if not dap.session() then
util.notify("No active debug session", vim.log.levels.WARN)
return
end
args = args or {}
if not expr then
expr = util.get_current_expr()
end
if open_float then
if prev_expr == expr then
open_float:jump_to()
return
else
open_float:close()
end
end
prev_expr = expr
local elem = dapui.elements.hover
elem.set_expression(expr, args.context)
local win_pos = nio.api.nvim_win_get_position(0)
local position = {
line = win_pos[1] + nio.fn.winline(),
col = win_pos[2] + nio.fn.wincol() - 1,
}
open_float = require("dapui.windows").open_float("hover", elem, position, args)
if open_float then
open_float:listen("close", function()
open_float = nil
end)
end
end)
end
--- Update the config.render settings and re-render windows
---@param update dapui.Config.render Updated settings, from the `render` table of
--- the config
function dapui.update_render(update)
config.update_render(update)
nio.run(function()
for _, elem in pairs(elements) do
elem.render()
end
end)
end
local function keep_cmdheight(cb)
local cmd_height = vim.o.cmdheight
cb()
vim.o.cmdheight = cmd_height
end
---@class dapui.CloseArgs
---@field layout? number Index of layout in config
--- Close one or all of the window layouts
---@param args? dapui.CloseArgs
function dapui.close(args)
keep_cmdheight(function()
args = args or {}
if type(args) == "number" then
args = { layout = args }
end
local layout = args.layout
for _, win_layout in ipairs(windows.layouts) do
win_layout:update_sizes()
end
for i, win_layout in ipairs(windows.layouts) do
if not layout or layout == i then
win_layout:close()
end
end
end)
end
---@generic T
---@param list T[]
---@return fun(): number, T
---@nodoc
local function reverse(list)
local i = #list + 1
return function()
i = i - 1
if i <= 0 then
return nil
end
return i, list[i]
end
end
---@class dapui.OpenArgs
---@field layout? number Index of layout in config
---@field reset? boolean Reset windows to original size
--- Open one or all of the window layouts
---@param args? dapui.OpenArgs
function dapui.open(args)
keep_cmdheight(function()
args = args or {}
if type(args) == "number" then
args = { layout = args }
end
local layout = args.layout
for _, win_layout in ipairs(windows.layouts) do
win_layout:update_sizes()
end
local closed = {}
if layout then
for i = 1, (layout and layout - 1) or #windows.layouts, 1 do
if windows.layouts[i]:is_open() then
closed[#closed + 1] = i
windows.layouts[i]:close()
end
end
end
for i, win_layout in reverse(windows.layouts) do
if not layout or layout == i then
win_layout:open()
end
end
if #closed > 0 then
for _, i in ipairs(closed) do
windows.layouts[i]:open()
end
end
for _, win_layout in ipairs(windows.layouts) do
win_layout:resize(args)
end
end)
dapui.update_render({})
if config.controls.enabled and config.controls.element ~= "" then
controls.enable_controls(elements[config.controls.element])
end
controls.refresh_control_panel()
end
---@class dapui.ToggleArgs
---@field layout? number Index of layout in config
---@field reset? boolean Reset windows to original size
--- Toggle one or all of the window layouts.
---@param args? dapui.ToggleArgs
function dapui.toggle(args)
keep_cmdheight(function()
args = args or {}
if type(args) == "number" then
args = { layout = args }
end
local layout = args.layout
for _, win_layout in reverse(windows.layouts) do
win_layout:update_sizes()
end
local closed = {}
if layout then
for i = 1, (layout and layout - 1) or #windows.layouts, 1 do
if windows.layouts[i]:is_open() then
closed[#closed + 1] = i
windows.layouts[i]:close()
end
end
end
for i, win_layout in reverse(windows.layouts) do
if not layout or layout == i then
win_layout:toggle()
end
end
for _, i in reverse(closed) do
windows.layouts[i]:open()
end
for _, win_layout in ipairs(windows.layouts) do
win_layout:resize(args)
end
end)
dapui.update_render({})
if config.controls.enabled and config.controls.element ~= "" then
controls.enable_controls(elements[config.controls.element])
end
controls.refresh_control_panel()
end
---@text
--- Access the elements currently registered. See elements corresponding help
--- tag for API information.
---
---@class dapui.elements
---@field hover dapui.elements.hover
---@field breakpoints dapui.elements.breakpoints
---@field repl dapui.elements.repl
---@field scopes dapui.elements.scopes
---@field stack dapui.elements.stacks
---@field watches dapui.elements.watches
---@field console dapui.elements.console
dapui.elements = setmetatable({}, {
__newindex = function()
error("Elements should be registered instead of adding them to the elements table")
end,
__index = function(_, key)
return elements[key]
end,
})
---@class dapui.Element
---@field render fun() Triggers the element to refresh its buffer. Used when
--- render settings have changed
---@field buffer fun(): integer Gets the current buffer for the element. The
--- buffer can change over repeated calls
---@field float_defaults? fun(): dapui.FloatElementArgs Default settings for
--- floating windows. Useful for element windows which should be larger than
--- their content
--- Registers a new element that can be used within layouts or floating windows
---@param name string Name of the element
---@param element dapui.Element
function dapui.register_element(name, element)
if elements[name] then
error("Element " .. name .. " already exists")
end
elements[name] = element
windows.register_element(name, element)
nio.run(function()
element.render()
end)
end
return dapui