-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
dap.lua
1218 lines (1071 loc) · 35.9 KB
/
dap.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local api = vim.api
local M = {}
---@diagnostic disable-next-line: deprecated
local islist = vim.islist or vim.tbl_islist
---@type table<number, dap.Session>
local sessions = {}
---@type dap.Session|nil
local session = nil
local last_run = nil
-- lazy import other modules to have a lower startup footprint
local lazy = setmetatable({
async = nil, --- @module "dap.async"
utils = nil, --- @module "dap.utils"
progress = nil, --- @module "dap.progress"
ui = nil, --- @module "dap.ui"
breakpoints = nil, --- @module "dap.breakpoints"
}, {
__index = function(_, key)
return require('dap.' .. key)
end
})
local function log()
return require('dap.log').create_logger('dap.log')
end
local function notify(...)
lazy.utils.notify(...)
end
--- Sentinel value; signals an operation should be aborted.
---@class dap.Abort
M.ABORT = {}
M.status = function()
return lazy.progress.status()
end
--- @module "dap.repl"
M.repl = setmetatable({}, {
__index = function(_, key)
return require('dap.repl')[key]
end
})
---@class dap.listeners
---@field event_breakpoint table<string, fun(session: dap.Session, body: any)>
---@field event_capabilities table<string, fun(session: dap.Session, body: any)>
---@field event_continued table<string, fun(session: dap.Session, body: any)>
---@field event_exited table<string, fun(session: dap.Session, body: any)>
---@field event_initialized table<string, fun(session: dap.Session, body: any)>
---@field event_invalidated table<string, fun(session: dap.Session, body: any)>
---@field event_loadedSource table<string, fun(session: dap.Session, body: any)>
---@field event_memory table<string, fun(session: dap.Session, body: any)>
---@field event_module table<string, fun(session: dap.Session, body: any)>
---@field event_output table<string, fun(session: dap.Session, body: any)>
---@field event_process table<string, fun(session: dap.Session, body: any)>
---@field event_progressEnd table<string, fun(session: dap.Session, body: dap.ProgressEndEvent)>
---@field event_progressStart table<string, fun(session: dap.Session, body: dap.ProgressStartEvent)>
---@field event_progressUpdate table<string, fun(session: dap.Session, body: dap.ProgressUpdateEvent)>
---@field event_stopped table<string, fun(session: dap.Session, body: dap.StoppedEvent)>
---@field event_terminated table<string, fun(session: dap.Session, body: dap.TerminatedEvent)>
---@field event_thread table<string, fun(session: dap.Session, body: any)>
---@field attach table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field breakpointLocations table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field completions table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field configurationDone table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field continue table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field dataBreakpointInfo table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field disassemble table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field disconnect table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field evaluate table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field exceptionInfo table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field goto table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field gotoTargets table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field initialize table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field launch table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field loadedSources table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field modules table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field next table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field pause table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field readMemory table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field restart table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field restartFrame table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field reverseContinue table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field scopes table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field setBreakpoints table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field setDataBreakpoints table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field setExceptionBreakpoints table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field setExpression table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field setFunctionBreakpoints table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field setInstructionBreakpoints table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field setVariable table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field source table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field stackTrace table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field stepBack table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field stepIn table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field stepInTargets table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field stepOut table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field terminate table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field terminateThreads table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field threads table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field variables table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
---@field writeMemory table<string, fun(session: dap.Session, err: any, body: any, request: any, seq: number)>
M.listeners = {
---@type dap.listeners
before = setmetatable({}, {
__index = function(tbl, key)
rawset(tbl, key, {})
return rawget(tbl, key)
end
}),
---@type dap.listeners
after = setmetatable({}, {
__index = function(tbl, key)
rawset(tbl, key, {})
return rawget(tbl, key)
end
}),
---@type table<string, fun(config: dap.Configuration):dap.Configuration>
on_config = {}
}
M.listeners.after.event_stopped['dap.sessions'] = function(s)
local lsession = session
if not lsession or not lsession.stopped_thread_id then
M.set_session(s)
end
end
local function from_fallback(_, key)
return M.defaults.fallback[key]
end
M.defaults = setmetatable(
{
fallback = {
exception_breakpoints = 'default';
---@type "statement"|"line"|"instruction"
stepping_granularity = 'statement';
---@type string|fun(): number bufnr, number|nil win
terminal_win_cmd = 'belowright new';
focus_terminal = false;
auto_continue_if_many_stopped = true;
---@type string|nil
switchbuf = nil
},
},
{
__index = function(tbl, key)
tbl[key] = {} -- call __newindex to add metatable to child
return rawget(tbl, key)
end,
__newindex = function(tbl, key)
rawset(tbl, key, setmetatable({}, {
__index = from_fallback
}))
end
}
)
local DAP_QUICKFIX_TITLE = "DAP Breakpoints"
local DAP_QUICKFIX_CONTEXT = DAP_QUICKFIX_TITLE
---@class dap.Adapter
---@field type string
---@field id string|nil
---@field options nil|dap.Adapter.options
---@field enrich_config? fun(config: dap.Configuration, on_config: fun(config: dap.Configuration))
---@field reverse_request_handlers? table<string, fun(session: dap.Session, request: dap.Request)>
---@class dap.Adapter.options
---@field initialize_timeout_sec nil|number
---@field disconnect_timeout_sec nil|number
---@field source_filetype nil|string
---@class dap.ExecutableAdapter : dap.Adapter
---@field type "executable"
---@field command string
---@field args string[]
---@field options nil|dap.ExecutableAdapter.options
---@class dap.ExecutableAdapter.options : dap.Adapter.options
---@field env nil|table<string, string>
---@field cwd nil|string
---@field detached nil|boolean
---@class ServerOptions : dap.Adapter.options
---@field max_retries nil|number
---@class dap.ServerAdapter : dap.Adapter
---@field type "server"
---@field host string|nil
---@field port integer
---@field executable nil|dap.ServerAdapterExecutable
---@field options nil|ServerOptions
---@class dap.PipeAdapter.options
---@field timeout? integer max amount of time in ms to wait between spawning the executable and connecting. This gives the executable time to create the pipe. Defaults to 5000
---@class dap.PipeAdapter : dap.Adapter
---@field type "pipe"
---@field pipe string absolute path to the pipe or ${pipe} to use random tmp path
---@field executable? dap.ServerAdapterExecutable
---@field options? dap.PipeAdapter.options
---@class dap.ServerAdapterExecutable
---@field command string
---@field args nil|string[]
---@field cwd nil|string
---@field detached nil|boolean
---@alias Dap.AdapterFactory fun(callback: fun(adapter: dap.Adapter), config: dap.Configuration, parent?: dap.Session)
--- Adapter definitions. See `:help dap-adapter` for more help
---
--- An example:
---
--- ```
--- require('dap').adapter.debugpy = {
--- {
--- type = "executable"
--- command = "/usr/bin/python",
--- args = {"-m", "debugpy.adapter"},
--- },
--- }
--- ```
---@type table<string, dap.Adapter|Dap.AdapterFactory>
M.adapters = {}
---@class dap.Configuration
---@field type string
---@field request "launch"|"attach"
---@field name string
--- Configurations per adapter. See `:help dap-configuration` for more help.
---
--- An example:
---
--- ```
--- require('dap').configurations.python = {
--- {
--- name = "My configuration",
--- type = "debugpy", -- references an entry in dap.adapters
--- request = "launch",
--- -- + Other debug adapter specific configuration options
--- },
--- }
--- ```
---@type table<string, dap.Configuration[]>
M.configurations = {}
local providers = {
---@type table<string, fun(bufnr: integer): dap.Configuration[]>
configs = {},
}
do
local providers_mt = {
__newindex = function()
error("Cannot add item to dap.providers")
end,
}
M.providers = setmetatable(providers, providers_mt)
end
providers.configs["dap.global"] = function(bufnr)
local filetype = vim.bo[bufnr].filetype
local configurations = M.configurations[filetype] or {}
assert(
islist(configurations),
string.format(
'`dap.configurations.%s` must be a list of configurations, got %s',
filetype,
vim.inspect(configurations)
)
)
return configurations
end
providers.configs["dap.launch.json"] = function()
local ok, configs = pcall(require("dap.ext.vscode").getconfigs)
return ok and configs or {}
end
do
local function eval_option(option)
if type(option) == 'function' then
option = option()
end
if type(option) == "thread" then
assert(coroutine.status(option) == "suspended", "If option is a thread it must be suspended")
local co = coroutine.running()
-- Schedule ensures `coroutine.resume` happens _after_ coroutine.yield
-- This is necessary in case the option coroutine is synchronous and
-- gives back control immediately
vim.schedule(function()
coroutine.resume(option, co)
end)
option = coroutine.yield()
end
return option
end
local var_placeholders_once = {
['${command:pickProcess}'] = lazy.utils.pick_process,
['${command:pickFile}'] = lazy.utils.pick_file,
}
local var_placeholders = {
['${file}'] = function(_)
return vim.fn.expand("%:p")
end,
['${fileBasename}'] = function(_)
return vim.fn.expand("%:t")
end,
['${fileBasenameNoExtension}'] = function(_)
return vim.fn.fnamemodify(vim.fn.expand("%:t"), ":r")
end,
['${fileDirname}'] = function(_)
return vim.fn.expand("%:p:h")
end,
['${fileExtname}'] = function(_)
return vim.fn.expand("%:e")
end,
['${relativeFile}'] = function(_)
return vim.fn.expand("%:.")
end,
['${relativeFileDirname}'] = function(_)
return vim.fn.fnamemodify(vim.fn.expand("%:.:h"), ":r")
end,
['${workspaceFolder}'] = function(_)
return vim.fn.getcwd()
end,
['${workspaceFolderBasename}'] = function(_)
return vim.fn.fnamemodify(vim.fn.getcwd(), ":t")
end,
['${env:([%w_]+)}'] = function(match)
return os.getenv(match) or ''
end,
}
local function expand_config_variables(option)
option = eval_option(option)
if option == M.ABORT then
return option
end
if type(option) == "table" then
local mt = getmetatable(option)
local result = {}
for k, v in pairs(option) do
result[expand_config_variables(k)] = expand_config_variables(v)
end
return setmetatable(result, mt)
end
if type(option) ~= "string" then
return option
end
local ret = option
for key, fn in pairs(var_placeholders) do
ret = ret:gsub(key, fn)
end
for key, fn in pairs(var_placeholders_once) do
if ret:find(key) then
local val = eval_option(fn)
ret = ret:gsub(key, val)
end
end
return ret
end
M.listeners.on_config["dap.expand_variable"] = function(config)
return vim.tbl_map(expand_config_variables, config)
end
end
local signs = {
DapBreakpoint = { text = "B", texthl = "SignColumn", linehl = "", numhl = "" },
DapBreakpointCondition = { text = "C", texthl = "SignColumn", linehl = "", numhl = "" },
DapBreakpointRejected = { text = 'R', texthl = "SignColumn", linehl = '', numhl = '' },
DapLogPoint = { text = 'L', texthl = "SignColumn", linehl = '', numhl = '' },
DapStopped = { text = '→', texthl = "SignColumn", linehl = 'debugPC', numhl = '' },
}
local function sign_try_define(name)
local s = vim.fn.sign_getdefined(name)
if vim.tbl_isempty(s) then
local opts = signs[name]
vim.fn.sign_define(name, opts)
end
end
for name in pairs(signs) do
sign_try_define(name)
end
---@param lsession dap.Session
local function add_reset_session_hook(lsession)
lsession.on_close['dap.session'] = function(s)
assert(s.id == lsession.id, "on_close must not be called with a foreign session")
lazy.progress.report('Closed session: ' .. tostring(s.id))
sessions[s.id] = nil
M.set_session(nil)
end
end
local function run_adapter(adapter, configuration, opts)
local name = configuration.name or '[no name]'
local options = adapter.options or {}
opts = vim.tbl_extend('keep', opts, {
cwd = options.cwd,
env = options.env
})
if adapter.type == 'executable' then
lazy.progress.report('Running: ' .. name)
M.launch(adapter, configuration, opts)
elseif adapter.type == 'server' then
lazy.progress.report('Running: ' .. name)
M.attach(adapter, configuration, opts)
elseif adapter.type == "pipe" then
lazy.progress.report("Running: " .. name)
local lsession
lsession = require("dap.session").pipe(adapter, opts, function(err)
if not err then
lsession:initialize(configuration)
end
end)
add_reset_session_hook(lsession)
M.set_session(lsession)
else
notify(string.format('Invalid adapter type %s, expected `executable` or `server`', adapter.type), vim.log.levels.ERROR)
end
end
local function maybe_enrich_config_and_run(adapter, configuration, opts)
assert(type(adapter) == 'table', 'adapter must be a table, not' .. vim.inspect(adapter))
assert(
adapter.type,
'Adapter for ' .. configuration.type .. ' must have the `type` property set to `executable` or `server`'
)
if adapter.enrich_config then
assert(
type(adapter.enrich_config) == 'function',
'`enrich_config` property of adapter must be a function: ' .. vim.inspect(adapter)
)
adapter.enrich_config(configuration, function(config)
run_adapter(adapter, config, opts)
end)
else
run_adapter(adapter, configuration, opts)
end
end
local function select_config_and_run(opts)
local bufnr = api.nvim_get_current_buf()
local filetype = vim.bo[bufnr].filetype
lazy.async.run(function()
local all_configs = {}
local provider_keys = vim.tbl_keys(providers.configs)
table.sort(provider_keys)
for _, provider in ipairs(provider_keys) do
local config_provider = providers.configs[provider]
local configs = config_provider(bufnr)
if islist(configs) then
vim.list_extend(all_configs, configs)
else
local msg = "Configuration provider %s must return a list of configurations. Got: %s"
notify(msg:format(provider, vim.inspect(configs)), vim.log.levels.WARN)
end
end
if #all_configs == 0 then
local msg = 'No configuration found for `%s`. You need to add configs to `dap.configurations.%s` (See `:h dap-configuration`)'
notify(string.format(msg, filetype, filetype), vim.log.levels.INFO)
return
end
opts = opts or {}
opts.filetype = opts.filetype or filetype
lazy.ui.pick_if_many(
all_configs,
"Configuration: ",
function(i) return i.name end,
function(configuration)
if configuration then
M.run(configuration, opts)
else
notify('No configuration selected', vim.log.levels.INFO)
end
end
)
end)
end
--- Get the first stopped session.
--- If no session is stopped, it returns the active session or next in sessions.
---@return dap.Session|nil
local function first_stopped_session()
if session and session.stopped_thread_id then
return session
end
for _, s in pairs(sessions) do
if s.stopped_thread_id then
return s
end
end
if session then
return session
end
local _, s = next(sessions)
return s
end
---@param config dap.Configuration
---@result dap.Configuration
local function prepare_config(config)
local co, is_main = coroutine.running()
assert(co and not is_main, "prepare_config must be running in coroutine")
local mt = getmetatable(config)
if mt and type(mt.__call) == "function" then
config = config()
assert(config and type(config) == "table", "config metatable __call must return a config table")
end
for _, on_config in pairs(M.listeners.on_config) do
config = on_config(config)
end
return config
end
---@class dap.run.opts
---@field new? boolean force new session
---@field before? fun(config: dap.Configuration): dap.Configuration pre-process config
--- Start a debug session
---@param config dap.Configuration
---@param opts dap.run.opts?
function M.run(config, opts)
assert(
type(config) == 'table',
'dap.run() must be called with a valid configuration, got ' .. vim.inspect(config))
opts = opts or {}
if session and (opts.new == false or (opts.new == nil and session.config.name == config.name)) then
M.restart(config, opts)
return
end
opts.filetype = opts.filetype or vim.bo.filetype
opts.new = nil
last_run = {
config = config,
opts = opts,
}
if opts.before then
config = opts.before(config)
end
local trigger_run = function()
config = prepare_config(config)
for _, val in pairs(config) do
if val == M.ABORT then
notify("Run aborted", vim.log.levels.INFO)
return
end
end
local adapter = M.adapters[config.type]
if type(adapter) == 'table' then
lazy.progress.report('Launching debug adapter')
maybe_enrich_config_and_run(adapter, config, opts)
elseif type(adapter) == 'function' then
lazy.progress.report('Launching debug adapter')
adapter(
function(resolved_adapter)
maybe_enrich_config_and_run(resolved_adapter, config, opts)
end,
config
)
elseif adapter == nil then
notify(string.format(
'Config references missing adapter `%s`. Available are: %s',
config.type,
table.concat(vim.tbl_keys(M.adapters), ", ")
), vim.log.levels.ERROR)
else
notify(string.format(
'Invalid adapter `%s` for config `%s`. Expected a table or function. '
.. 'Read :help dap-adapter and define a valid adapter.',
vim.inspect(adapter),
config.type
),
vim.log.levels.ERROR
)
end
end
lazy.async.run(trigger_run)
end
--- Run the last debug session again
function M.run_last()
if last_run then
M.run(last_run.config, last_run.opts)
else
notify('No configuration available to re-run', vim.log.levels.INFO)
end
end
--- Step over the current line
---@param opts table|nil
function M.step_over(opts)
session = first_stopped_session()
if not session then
return
end
session:_step('next', opts)
end
function M.focus_frame()
if session then
if session.current_frame then
session:_frame_set(session.current_frame)
else
local w = require('dap.ui.widgets')
w.centered_float(w.threads).open()
end
else
notify('No active session', vim.log.levels.INFO)
end
end
function M.restart_frame()
if session then
session:restart_frame()
else
notify('No active session', vim.log.levels.INFO)
end
end
---@param opts? {askForTargets?: boolean, steppingGranularity?: dap.SteppingGranularity}
function M.step_into(opts)
session = first_stopped_session()
if not session then
return
end
---@type {[any]: any}
opts = opts or {}
local askForTargets = opts.askForTargets
opts.askForTargets = nil
if not (askForTargets and session.capabilities.supportsStepInTargetsRequest) then
session:_step('stepIn', opts)
return
end
session:request('stepInTargets', { frameId = session.current_frame.id }, function(err, response)
if err then
notify(
'Error on step_into: ' .. lazy.utils.fmt_error(err) .. ' (while requesting stepInTargets)',
vim.log.levels.ERROR
)
return
end
lazy.ui.pick_if_many(
response.targets,
"Step into which function?",
function(target) return target.label end,
function(target)
if not target or not target.id then
notify('No target selected. No stepping.', vim.log.levels.INFO)
else
opts.targetId = target.id
session:_step('stepIn', opts)
end
end)
end)
end
function M.step_out(opts)
session = first_stopped_session()
if not session then
return
end
session:_step('stepOut', opts)
end
function M.step_back(opts)
session = first_stopped_session()
if not session then
return
end
if session.capabilities.supportsStepBack then
session:_step('stepBack', opts)
else
notify('Debug Adapter does not support stepping backwards.', vim.log.levels.ERROR)
end
end
function M.reverse_continue(opts)
if not session then return end
if session.capabilities.supportsStepBack then
session:_step('reverseContinue', opts)
else
notify('Debug Adapter does not support stepping backwards.', vim.log.levels.ERROR)
end
end
function M.pause(thread_id)
if session then
session:_pause(thread_id)
end
end
function M.stop()
notify('dap.stop() is deprecated. Call dap.close() instead', vim.log.levels.WARN)
M.close()
end
local function terminate(lsession, terminate_opts, disconnect_opts, cb)
cb = cb or function() end
if not lsession then
notify('No active session')
cb()
return
end
if lsession.closed then
log().warn('User called terminate on already closed session that is still in use')
sessions[lsession.id] = nil
M.set_session(nil)
cb()
return
end
local capabilities = lsession.capabilities or {}
if capabilities.supportsTerminateRequest then
capabilities.supportsTerminateRequest = false
local opts = terminate_opts or vim.empty_dict()
local timeout_sec = (lsession.adapter.options or {}).disconnect_timeout_sec or 3
local timeout_ms = timeout_sec * 1000
lsession:request_with_timeout('terminate', opts, timeout_ms, function(err)
if err then
log().warn(lazy.utils.fmt_error(err))
end
if not lsession.closed then
lsession:close()
end
notify('Session terminated')
cb()
end)
else
local opts = disconnect_opts or { terminateDebuggee = true }
lsession:disconnect(opts, cb)
end
end
function M.terminate(terminate_opts, disconnect_opts, cb)
local lsession = session
if not lsession then
local _, s = next(sessions)
if s then
log().info("Terminate called without active session, switched to", s.id)
end
lsession = s
end
terminate(lsession, terminate_opts, disconnect_opts, cb)
end
function M.close()
if session then
session:close()
M.set_session(nil)
end
end
function M.up()
if session then
session:_frame_delta(1)
end
end
function M.down()
if session then
session:_frame_delta(-1)
end
end
function M.goto_(line)
if session then
local source, col
if not line then
line, col = unpack(api.nvim_win_get_cursor(0))
col = col + 1
source = { path = vim.uri_from_bufnr(0) }
end
session:_goto(line, source, col)
end
end
---@param config dap.Configuration?
---@param opts? dap.run.opts
function M.restart(config, opts)
local lsession = session
if not lsession then
notify('No active session', vim.log.levels.INFO)
return
end
config = config or lsession.config
if lsession.capabilities.supportsRestartRequest then
lazy.async.run(function()
config = prepare_config(config)
lsession:request('restart', config, function(err0, _)
if err0 then
notify('Error restarting debug adapter: ' .. lazy.utils.fmt_error(err0), vim.log.levels.ERROR)
else
notify('Restarted debug adapter', vim.log.levels.INFO)
end
end)
end)
else
terminate(lsession, nil, nil, vim.schedule_wrap(function()
local nopts = opts and vim.deepcopy(opts) or {}
nopts.new = true
M.run(config, nopts)
end))
end
end
---@param openqf boolean?
function M.list_breakpoints(openqf)
local qf_list = lazy.breakpoints.to_qf_list(lazy.breakpoints.get())
local current_qflist_title = vim.fn.getqflist({ title = 1 }).title
local action = ' '
if current_qflist_title == DAP_QUICKFIX_TITLE then
action = 'r'
end
vim.fn.setqflist({}, action, {
items = qf_list,
context = DAP_QUICKFIX_CONTEXT,
title = DAP_QUICKFIX_TITLE
})
if openqf then
if #qf_list == 0 then
notify('No breakpoints set!', vim.log.levels.INFO)
else
api.nvim_command('copen')
end
end
end
---@param condition string?
---@param hit_condition string?
---@param log_message string?
function M.set_breakpoint(condition, hit_condition, log_message)
M.toggle_breakpoint(condition, hit_condition, log_message, true)
end
---@param lsessions table<integer, dap.Session>
---@param fn fun(lsession: dap.Session)
local function broadcast(lsessions, fn)
for _, lsession in pairs(lsessions) do
fn(lsession)
broadcast(lsession.children, fn)
end
end
---@param condition string?
---@param hit_condition string?
---@param log_message string?
---@param replace_old boolean?
function M.toggle_breakpoint(condition, hit_condition, log_message, replace_old)
assert(
not condition or type(condition) == "string",
"breakpoint condition must be a string. Got: " .. vim.inspect(condition)
)
assert(
not hit_condition or type(hit_condition) == "string",
"breakpoint hit-condition must be a string. Got: " .. vim.inspect(hit_condition)
)
assert(
not log_message or type(log_message) == "string",
"breakpoint log-message must be a string. Got: " .. vim.inspect(log_message)
)
lazy.breakpoints.toggle({
condition = condition,
hit_condition = hit_condition,
log_message = log_message,
replace = replace_old
})
local bufnr = api.nvim_get_current_buf()
local bps = lazy.breakpoints.get(bufnr)
broadcast(sessions, function(s)
s:set_breakpoints(bps)
end)
if vim.fn.getqflist({context = DAP_QUICKFIX_CONTEXT}).context == DAP_QUICKFIX_CONTEXT then
M.list_breakpoints(false)
end
end
function M.clear_breakpoints()
local bps = lazy.breakpoints.get()
for bufnr, _ in pairs(bps) do
bps[bufnr] = {}
end
lazy.breakpoints.clear()
broadcast(sessions, function(lsession)
lsession:set_breakpoints(bps)
end)
end
-- setExceptionBreakpoints (https://microsoft.github.io/debug-adapter-protocol/specification#Requests_SetExceptionBreakpoints)
--- filters: string[]
--- exceptionOptions: exceptionOptions?: ExceptionOptions[] (https://microsoft.github.io/debug-adapter-protocol/specification#Types_ExceptionOptions)
function M.set_exception_breakpoints(filters, exceptionOptions)
if session then
session:set_exception_breakpoints(filters, exceptionOptions)
else
notify('Cannot set exception breakpoints: No active session!', vim.log.levels.INFO)
end
end
function M.run_to_cursor()
local lsession = session
if not lsession then
notify('Cannot use run_to_cursor without active session', vim.log.levels.INFO)
return
end
if not lsession.stopped_thread_id then
notify('run_to_cursor can only be used if stopped at a breakpoint', vim.log.levels.INFO)
return
end
local bps_before = lazy.breakpoints.get()
lazy.breakpoints.clear()
local cur_bufnr = api.nvim_get_current_buf()
local lnum = api.nvim_win_get_cursor(0)[1]
lazy.breakpoints.set({}, cur_bufnr, lnum)
local temp_bps = lazy.breakpoints.get(cur_bufnr)
for bufnr, _ in pairs(bps_before) do
if bufnr ~= cur_bufnr then
temp_bps[bufnr] = {}
end
end
if bps_before[cur_bufnr] == nil then
bps_before[cur_bufnr] = {}
end
local function restore_breakpoints()
M.listeners.before.event_stopped['dap.run_to_cursor'] = nil
M.listeners.before.event_terminated['dap.run_to_cursor'] = nil
lazy.breakpoints.clear()
for buf, buf_bps in pairs(bps_before) do
for _, bp in pairs(buf_bps) do
local line = bp.line
local opts = {
condition = bp.condition,