on master help sign in
Use a newtype for ThreadId
bda05ecBlaž Hrastnik
helix-dap/src/client.rs+10−10
@@ -1,7 +1,7 @@
1use crate::{
1use crate::{
2 transport::{Payload, Request, Transport},
2 transport::{Payload, Request, Transport},
3 types::*,
3 types::*,
4− Error, Result,
4+ Error, Result, ThreadId,
5};
5};
6use helix_core::syntax::DebuggerQuirks;
6use helix_core::syntax::DebuggerQuirks;
7
7
@@ -30,9 +30,9 @@ pub struct Client {
30 request_counter: AtomicU64,
30 request_counter: AtomicU64,
31 pub caps: Option<DebuggerCapabilities>,
31 pub caps: Option<DebuggerCapabilities>,
32 // thread_id -> frames
32 // thread_id -> frames
33− pub stack_frames: HashMap<isize, Vec<StackFrame>>,
33+ pub stack_frames: HashMap<ThreadId, Vec<StackFrame>>,
34− pub thread_states: HashMap<isize, String>,
34+ pub thread_states: HashMap<ThreadId, String>,
35− pub thread_id: Option<isize>,
35+ pub thread_id: Option<ThreadId>,
36 /// Currently active frame for the current thread.
36 /// Currently active frame for the current thread.
37 pub active_frame: Option<usize>,
37 pub active_frame: Option<usize>,
38 pub breakpoints: Vec<Breakpoint>,
38 pub breakpoints: Vec<Breakpoint>,
@@ -311,7 +311,7 @@ impl Client {
311 self.request::<requests::ConfigurationDone>(()).await
311 self.request::<requests::ConfigurationDone>(()).await
312 }
312 }
313
313
314− pub async fn continue_thread(&mut self, thread_id: isize) -> Result<Option<bool>> {
314+ pub async fn continue_thread(&mut self, thread_id: ThreadId) -> Result<Option<bool>> {
315 let args = requests::ContinueArguments { thread_id };
315 let args = requests::ContinueArguments { thread_id };
316
316
317 let response = self.request::<requests::Continue>(args).await?;
317 let response = self.request::<requests::Continue>(args).await?;
@@ -320,7 +320,7 @@ impl Client {
320
320
321 pub async fn stack_trace(
321 pub async fn stack_trace(
322 &mut self,
322 &mut self,
323− thread_id: isize,
323+ thread_id: ThreadId,
324 ) -> Result<(Vec<StackFrame>, Option<usize>)> {
324 ) -> Result<(Vec<StackFrame>, Option<usize>)> {
325 let args = requests::StackTraceArguments {
325 let args = requests::StackTraceArguments {
326 thread_id,
326 thread_id,
@@ -358,7 +358,7 @@ impl Client {
358 Ok(response.variables)
358 Ok(response.variables)
359 }
359 }
360
360
361− pub async fn step_in(&mut self, thread_id: isize) -> Result<()> {
361+ pub async fn step_in(&mut self, thread_id: ThreadId) -> Result<()> {
362 let args = requests::StepInArguments {
362 let args = requests::StepInArguments {
363 thread_id,
363 thread_id,
364 target_id: None,
364 target_id: None,
@@ -368,7 +368,7 @@ impl Client {
368 self.request::<requests::StepIn>(args).await
368 self.request::<requests::StepIn>(args).await
369 }
369 }
370
370
371− pub async fn step_out(&mut self, thread_id: isize) -> Result<()> {
371+ pub async fn step_out(&mut self, thread_id: ThreadId) -> Result<()> {
372 let args = requests::StepOutArguments {
372 let args = requests::StepOutArguments {
373 thread_id,
373 thread_id,
374 granularity: None,
374 granularity: None,
@@ -377,7 +377,7 @@ impl Client {
377 self.request::<requests::StepOut>(args).await
377 self.request::<requests::StepOut>(args).await
378 }
378 }
379
379
380− pub async fn next(&mut self, thread_id: isize) -> Result<()> {
380+ pub async fn next(&mut self, thread_id: ThreadId) -> Result<()> {
381 let args = requests::NextArguments {
381 let args = requests::NextArguments {
382 thread_id,
382 thread_id,
383 granularity: None,
383 granularity: None,
@@ -386,7 +386,7 @@ impl Client {
386 self.request::<requests::Next>(args).await
386 self.request::<requests::Next>(args).await
387 }
387 }
388
388
389− pub async fn pause(&mut self, thread_id: isize) -> Result<()> {
389+ pub async fn pause(&mut self, thread_id: ThreadId) -> Result<()> {
390 let args = requests::PauseArguments { thread_id };
390 let args = requests::PauseArguments { thread_id };
391
391
392 self.request::<requests::Pause>(args).await
392 self.request::<requests::Pause>(args).await
helix-dap/src/types.rs+22−11
@@ -2,6 +2,17 @@ use serde::{Deserialize, Serialize};
2use serde_json::Value;
2use serde_json::Value;
3use std::path::PathBuf;
3use std::path::PathBuf;
4
4
5+#[derive(
6+ Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize,
7+)]
8+pub struct ThreadId(isize);
9+
10+impl std::fmt::Display for ThreadId {
11+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12+ self.0.fmt(f)
13+ }
14+}
15+
5pub trait Request {
16pub trait Request {
6 type Arguments: serde::de::DeserializeOwned + serde::Serialize;
17 type Arguments: serde::de::DeserializeOwned + serde::Serialize;
7 type Result: serde::de::DeserializeOwned + serde::Serialize;
18 type Result: serde::de::DeserializeOwned + serde::Serialize;
@@ -157,7 +168,7 @@ pub struct StackFrame {
157#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
168#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
158#[serde(rename_all = "camelCase")]
169#[serde(rename_all = "camelCase")]
159pub struct Thread {
170pub struct Thread {
160− pub id: isize,
171+ pub id: ThreadId,
161 pub name: String,
172 pub name: String,
162}
173}
163
174
@@ -317,7 +328,7 @@ pub mod requests {
317 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
328 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
318 #[serde(rename_all = "camelCase")]
329 #[serde(rename_all = "camelCase")]
319 pub struct ContinueArguments {
330 pub struct ContinueArguments {
320− pub thread_id: isize,
331+ pub thread_id: ThreadId,
321 }
332 }
322
333
323 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
334 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
@@ -338,7 +349,7 @@ pub mod requests {
338 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
349 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
339 #[serde(rename_all = "camelCase")]
350 #[serde(rename_all = "camelCase")]
340 pub struct StackTraceArguments {
351 pub struct StackTraceArguments {
341− pub thread_id: isize,
352+ pub thread_id: ThreadId,
342 pub start_frame: Option<usize>,
353 pub start_frame: Option<usize>,
343 pub levels: Option<usize>,
354 pub levels: Option<usize>,
344 pub format: Option<StackFrameFormat>,
355 pub format: Option<StackFrameFormat>,
@@ -424,7 +435,7 @@ pub mod requests {
424 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
435 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
425 #[serde(rename_all = "camelCase")]
436 #[serde(rename_all = "camelCase")]
426 pub struct StepInArguments {
437 pub struct StepInArguments {
427− pub thread_id: isize,
438+ pub thread_id: ThreadId,
428 pub target_id: Option<usize>,
439 pub target_id: Option<usize>,
429 pub granularity: Option<String>,
440 pub granularity: Option<String>,
430 }
441 }
@@ -441,7 +452,7 @@ pub mod requests {
441 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
452 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
442 #[serde(rename_all = "camelCase")]
453 #[serde(rename_all = "camelCase")]
443 pub struct StepOutArguments {
454 pub struct StepOutArguments {
444− pub thread_id: isize,
455+ pub thread_id: ThreadId,
445 pub granularity: Option<String>,
456 pub granularity: Option<String>,
446 }
457 }
447
458
@@ -457,7 +468,7 @@ pub mod requests {
457 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
468 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
458 #[serde(rename_all = "camelCase")]
469 #[serde(rename_all = "camelCase")]
459 pub struct NextArguments {
470 pub struct NextArguments {
460− pub thread_id: isize,
471+ pub thread_id: ThreadId,
461 pub granularity: Option<String>,
472 pub granularity: Option<String>,
462 }
473 }
463
474
@@ -473,7 +484,7 @@ pub mod requests {
473 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
484 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
474 #[serde(rename_all = "camelCase")]
485 #[serde(rename_all = "camelCase")]
475 pub struct PauseArguments {
486 pub struct PauseArguments {
476− pub thread_id: isize,
487+ pub thread_id: ThreadId,
477 }
488 }
478
489
479 #[derive(Debug)]
490 #[derive(Debug)]
@@ -574,7 +585,7 @@ pub mod events {
574 pub struct Stopped {
585 pub struct Stopped {
575 pub reason: String,
586 pub reason: String,
576 pub description: Option<String>,
587 pub description: Option<String>,
577− pub thread_id: Option<isize>,
588+ pub thread_id: Option<ThreadId>,
578 pub preserve_focus_hint: Option<bool>,
589 pub preserve_focus_hint: Option<bool>,
579 pub text: Option<String>,
590 pub text: Option<String>,
580 pub all_threads_stopped: Option<bool>,
591 pub all_threads_stopped: Option<bool>,
@@ -584,7 +595,7 @@ pub mod events {
584 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
595 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
585 #[serde(rename_all = "camelCase")]
596 #[serde(rename_all = "camelCase")]
586 pub struct Continued {
597 pub struct Continued {
587− pub thread_id: isize,
598+ pub thread_id: ThreadId,
588 pub all_threads_continued: Option<bool>,
599 pub all_threads_continued: Option<bool>,
589 }
600 }
590
601
@@ -604,7 +615,7 @@ pub mod events {
604 #[serde(rename_all = "camelCase")]
615 #[serde(rename_all = "camelCase")]
605 pub struct Thread {
616 pub struct Thread {
606 pub reason: String,
617 pub reason: String,
607− pub thread_id: isize,
618+ pub thread_id: ThreadId,
608 }
619 }
609
620
610 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
621 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
@@ -661,7 +672,7 @@ pub mod events {
661 // #[serde(rename_all = "camelCase")]
672 // #[serde(rename_all = "camelCase")]
662 // pub struct Invalidated {
673 // pub struct Invalidated {
663 // pub areas: Vec<InvalidatedArea>,
674 // pub areas: Vec<InvalidatedArea>,
664− // pub thread_id: Option<usize>,
675+ // pub thread_id: Option<ThreadId>,
665 // pub stack_frame_id: Option<usize>,
676 // pub stack_frame_id: Option<usize>,
666 // }
677 // }
667
678
helix-term/src/application.rs+6−1
@@ -320,7 +320,12 @@ impl Application {
320 for thread in threads {
320 for thread in threads {
321 fetch_stack_trace(debugger, thread.id).await;
321 fetch_stack_trace(debugger, thread.id).await;
322 }
322 }
323− select_thread_id(&mut self.editor, thread_id.unwrap_or(0), false).await;
323+ select_thread_id(
324+ &mut self.editor,
325+ thread_id.unwrap_or_default(),
326+ false,
327+ )
328+ .await;
324 }
329 }
325 } else if let Some(thread_id) = thread_id {
330 } else if let Some(thread_id) = thread_id {
326 debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here
331 debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here
helix-term/src/commands/dap.rs+3−3
@@ -6,7 +6,7 @@ use crate::{
6 ui::{FilePicker, Prompt, PromptEvent},
6 ui::{FilePicker, Prompt, PromptEvent},
7};
7};
8use helix_core::{syntax::DebugConfigCompletion, Selection};
8use helix_core::{syntax::DebugConfigCompletion, Selection};
9−use helix_dap::{self as dap, Client};
9+use helix_dap::{self as dap, Client, ThreadId};
10use helix_lsp::block_on;
10use helix_lsp::block_on;
11
11
12use serde_json::{to_value, Value};
12use serde_json::{to_value, Value};
@@ -34,7 +34,7 @@ pub fn resume_application(debugger: &mut Client) {
34 debugger.thread_id = None;
34 debugger.thread_id = None;
35}
35}
36
36
37−pub async fn select_thread_id(editor: &mut Editor, thread_id: isize, force: bool) {
37+pub async fn select_thread_id(editor: &mut Editor, thread_id: ThreadId, force: bool) {
38 let debugger = match &mut editor.debugger {
38 let debugger = match &mut editor.debugger {
39 Some(debugger) => debugger,
39 Some(debugger) => debugger,
40 None => return,
40 None => return,
@@ -53,7 +53,7 @@ pub async fn select_thread_id(editor: &mut Editor, thread_id: isize, force: bool
53 }
53 }
54}
54}
55
55
56−pub async fn fetch_stack_trace(debugger: &mut Client, thread_id: isize) {
56+pub async fn fetch_stack_trace(debugger: &mut Client, thread_id: ThreadId) {
57 let (frames, _) = match debugger.stack_trace(thread_id).await {
57 let (frames, _) = match debugger.stack_trace(thread_id).await {
58 Ok(frames) => frames,
58 Ok(frames) => frames,
59 Err(_) => return,
59 Err(_) => return,