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
|
use super::{align_view, Align, Context, Editor};
use crate::ui::Picker;
use helix_core::Selection;
use helix_dap::Client;
use helix_lsp::block_on;
use serde_json::{to_value, Value};
use tokio_stream::wrappers::UnboundedReceiverStream;
use std::collections::HashMap;
// general utils:
pub fn dap_pos_to_pos(doc: &helix_core::Rope, line: usize, column: usize) -> Option<usize> {
// 1-indexing to 0 indexing
let line = doc.try_line_to_char(line - 1).ok()?;
let pos = line + column;
// TODO: this is probably utf-16 offsets
Some(pos)
}
pub async fn select_thread_id(editor: &mut Editor, thread_id: usize, force: bool) {
let debugger = match &mut editor.debugger {
Some(debugger) => debugger,
None => return,
};
if !force && debugger.thread_id.is_some() {
return;
}
debugger.thread_id = Some(thread_id);
// fetch stack trace
// TODO: handle requesting more total frames
let (frames, _) = debugger.stack_trace(thread_id).await.unwrap();
debugger.stack_frames.insert(thread_id, frames);
debugger.active_frame = Some(0); // TODO: check how to determine this
let frame = debugger.stack_frames[&thread_id].get(0).cloned();
if let Some(frame) = &frame {
jump_to_stack_frame(editor, frame);
}
}
pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
let path = if let Some(helix_dap::Source {
path: Some(ref path),
..
}) = frame.source
{
path.clone()
} else {
return;
};
editor
.open(path, helix_view::editor::Action::Replace)
.unwrap(); // TODO: there should be no unwrapping!
let (view, doc) = current!(editor);
let text_end = doc.text().len_chars().saturating_sub(1);
let start = dap_pos_to_pos(doc.text(), frame.line, frame.column).unwrap_or(0);
let end = frame
.end_line
.and_then(|end_line| dap_pos_to_pos(doc.text(), end_line, frame.end_column.unwrap_or(0)))
.unwrap_or(start);
let selection = Selection::single(start.min(text_end), end.min(text_end));
doc.set_selection(view.id, selection);
align_view(doc, view, Align::Center);
}
// DAP
pub fn dap_start_impl(
editor: &mut Editor,
name: Option<&str>,
socket: Option<std::net::SocketAddr>,
params: Option<Vec<&str>>,
) {
let (_, doc) = current!(editor);
let path = match doc.path() {
Some(path) => path.to_path_buf(),
None => {
editor.set_error("Can't start debug: document has no path".to_string());
return;
}
};
let config = editor
.syn_loader
.language_config_for_file_name(&path)
.and_then(|x| x.debugger.clone());
let config = match config {
Some(c) => c,
None => {
editor.set_error(
"Can't start debug: no debug adapter available for language".to_string(),
);
return;
}
};
let result = match socket {
Some(socket) => block_on(Client::tcp(socket, 0)),
None => block_on(Client::process(
config.transport.clone(),
config.command.clone(),
config.args.clone(),
config.port_arg.clone(),
0,
)),
};
let (mut debugger, events) = match result {
Ok(r) => r,
Err(e) => {
editor.set_error(format!("Failed to start debug session: {:?}", e));
return;
}
};
let request = debugger.initialize(config.name.clone());
if let Err(e) = block_on(request) {
editor.set_error(format!("Failed to initialize debug adapter: {:?}", e));
return;
}
let start_config = match name {
Some(name) => config.templates.iter().find(|t| t.name == name),
None => config.templates.get(0),
};
let start_config = match start_config {
Some(c) => c,
None => {
editor.set_error("Can't start debug: no debug config with given name".to_string());
return;
}
};
let template = start_config.args.clone();
let mut args: HashMap<String, Value> = HashMap::new();
if let Some(params) = params {
for (k, t) in template {
let mut value = t;
for (i, x) in params.iter().enumerate() {
// For param #0 replace {0} in args
value = value.replace(format!("{{{}}}", i).as_str(), x);
}
if let Ok(integer) = value.parse::<usize>() {
args.insert(k, Value::Number(serde_json::Number::from(integer)));
} else {
args.insert(k, Value::String(value));
}
}
}
let args = to_value(args).unwrap();
let result = match &start_config.request[..] {
"launch" => block_on(debugger.launch(args)),
"attach" => block_on(debugger.attach(args)),
_ => {
editor.set_error("Unsupported request".to_string());
return;
}
};
if let Err(e) = result {
editor.set_error(format!("Failed {} target: {:?}", start_config.request, e));
return;
}
// TODO: either await "initialized" or buffer commands until event is received
editor.debugger = Some(debugger);
let stream = UnboundedReceiverStream::new(events);
editor.debugger_events.push(stream);
}
pub fn dap_launch(cx: &mut Context) {
if cx.editor.debugger.is_some() {
cx.editor
.set_error("Can't start debug: debugger is running".to_string());
return;
}
let (_, doc) = current!(cx.editor);
let path = match doc.path() {
Some(path) => path.to_path_buf(),
None => {
cx.editor
.set_error("Can't start debug: document has no path".to_string());
return;
}
};
let config = cx
.editor
.syn_loader
.language_config_for_file_name(&path)
.and_then(|x| x.debugger.clone());
let config = match config {
Some(c) => c,
None => {
cx.editor.set_error(
"Can't start debug: no debug adapter available for language".to_string(),
);
return;
}
};
cx.editor.debug_config_picker = Some(config.templates.iter().map(|t| t.name.clone()).collect());
cx.editor.debug_config_completions = Some(
config
.templates
.iter()
.map(|t| t.completion.clone())
.collect(),
);
}
pub fn dap_toggle_breakpoint(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let pos = doc.selection(view.id).primary().cursor(text);
let breakpoint = helix_dap::SourceBreakpoint {
line: text.char_to_line(pos) + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
..Default::default()
};
let path = match doc.path() {
Some(path) => path.to_path_buf(),
None => {
cx.editor
.set_error("Can't set breakpoint: document has no path".to_string());
return;
}
};
// TODO: need to map breakpoints over edits and update them?
// we shouldn't really allow editing while debug is running though
if let Some(debugger) = &mut cx.editor.debugger {
let breakpoints = debugger.breakpoints.entry(path.clone()).or_default();
if let Some(pos) = breakpoints.iter().position(|b| b.line == breakpoint.line) {
breakpoints.remove(pos);
} else {
breakpoints.push(breakpoint);
}
let breakpoints = breakpoints.clone();
let request = debugger.set_breakpoints(path, breakpoints);
if let Err(e) = block_on(request) {
cx.editor
.set_error(format!("Failed to set breakpoints: {:?}", e));
}
}
}
pub fn dap_run(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
if debugger.is_running {
cx.editor
.set_status("Debuggee is already running".to_owned());
return;
}
let request = debugger.configuration_done();
if let Err(e) = block_on(request) {
cx.editor.set_error(format!("Failed to run: {:?}", e));
return;
}
debugger.is_running = true;
}
}
pub fn dap_continue(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
if debugger.is_running {
cx.editor
.set_status("Debuggee is already running".to_owned());
return;
}
if let Some(thread_id) = debugger.thread_id {
let request = debugger.continue_thread(debugger.thread_id.unwrap());
if let Err(e) = block_on(request) {
cx.editor.set_error(format!("Failed to continue: {:?}", e));
return;
}
debugger.is_running = true;
debugger.stack_frames.remove(&thread_id);
} else {
cx.editor
.set_error("Currently active thread is not stopped. Switch the thread.".into());
}
}
}
pub fn dap_pause(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
if !debugger.is_running {
cx.editor.set_status("Debuggee is not running".to_owned());
return;
}
// FIXME: correct number here
let request = debugger.pause(0);
if let Err(e) = block_on(request) {
cx.editor.set_error(format!("Failed to pause: {:?}", e));
}
}
}
pub fn dap_step_in(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
if debugger.is_running {
cx.editor
.set_status("Debuggee is already running".to_owned());
return;
}
let request = debugger.step_in(debugger.thread_id.unwrap());
if let Err(e) = block_on(request) {
cx.editor.set_error(format!("Failed to step: {:?}", e));
}
}
}
pub fn dap_step_out(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
if debugger.is_running {
cx.editor
.set_status("Debuggee is already running".to_owned());
return;
}
let request = debugger.step_out(debugger.thread_id.unwrap());
if let Err(e) = block_on(request) {
cx.editor.set_error(format!("Failed to step: {:?}", e));
}
}
}
pub fn dap_next(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
if debugger.is_running {
cx.editor
.set_status("Debuggee is already running".to_owned());
return;
}
let request = debugger.next(debugger.thread_id.unwrap());
if let Err(e) = block_on(request) {
cx.editor.set_error(format!("Failed to step: {:?}", e));
}
}
}
pub fn dap_variables(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
if debugger.is_running {
cx.editor
.set_status("Cannot access variables while target is running".to_owned());
return;
}
let (frame, thread_id) = match (debugger.active_frame, debugger.thread_id) {
(Some(frame), Some(thread_id)) => (frame, thread_id),
_ => {
cx.editor
.set_status("Cannot find current stack frame to access variables".to_owned());
return;
}
};
let frame_id = debugger.stack_frames[&thread_id][frame].id;
let scopes = match block_on(debugger.scopes(frame_id)) {
Ok(s) => s,
Err(e) => {
cx.editor
.set_error(format!("Failed to get scopes: {:?}", e));
return;
}
};
let mut variables = Vec::new();
for scope in scopes.iter() {
let response = block_on(debugger.variables(scope.variables_reference));
if let Ok(vars) = response {
for var in vars {
let prefix = match var.data_type {
Some(data_type) => format!("{} ", data_type),
None => "".to_owned(),
};
variables.push(format!("{}{} = {}\n", prefix, var.name, var.value));
}
}
}
if !variables.is_empty() {
cx.editor.variables = Some(variables);
cx.editor.variables_page = 0;
}
}
}
pub fn dap_terminate(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
let request = debugger.disconnect();
if let Err(e) = block_on(request) {
cx.editor
.set_error(format!("Failed to disconnect: {:?}", e));
return;
}
cx.editor.debugger = None;
}
}
pub fn dap_switch_thread(cx: &mut Context) {
if let Some(debugger) = &mut cx.editor.debugger {
let request = debugger.threads();
let threads = match block_on(request) {
Ok(threads) => threads,
Err(e) => {
cx.editor
.set_error(format!("Failed to retrieve threads: {:?}", e));
return;
}
};
let picker = Picker::new(
true,
threads,
|thread| thread.name.clone().into(),
|editor, thread, _action| {
block_on(select_thread_id(editor, thread.id, true));
},
);
cx.push_layer(Box::new(picker))
}
}
|