on master help sign in
Use a newtype for ThreadId
commitbda05ec4bfb861c9880dd700ab31c1f9bc79e926 authorBlaž Hrastnik <blaz@mxxn.io> dateSun 17 Oct 2021, 14:06 (5 years ago) parent83a8167
4 files changed+41−25
helix-dap/src/client.rs+10−10 helix-dap/src/types.rs+22−11 helix-term/src/application.rs+6−1 helix-term/src/commands/dap.rs+3−3
helix-dap/src/client.rs+10−10
@@ -1,7 +1,7 @@
11use crate::{
22 transport::{Payload, Request, Transport},
33 types::*,
4− Error, Result,
4+ Error, Result, ThreadId,
55};
66use helix_core::syntax::DebuggerQuirks;
77
@@ -30,9 +30,9 @@ pub struct Client {
3030 request_counter: AtomicU64,
3131 pub caps: Option<DebuggerCapabilities>,
3232 // thread_id -> frames
33− pub stack_frames: HashMap<isize, Vec<StackFrame>>,
34− pub thread_states: HashMap<isize, String>,
35− pub thread_id: Option<isize>,
33+ pub stack_frames: HashMap<ThreadId, Vec<StackFrame>>,
34+ pub thread_states: HashMap<ThreadId, String>,
35+ pub thread_id: Option<ThreadId>,
3636 /// Currently active frame for the current thread.
3737 pub active_frame: Option<usize>,
3838 pub breakpoints: Vec<Breakpoint>,
@@ -311,7 +311,7 @@ impl Client {
311311 self.request::<requests::ConfigurationDone>(()).await
312312 }
313313
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>> {
315315 let args = requests::ContinueArguments { thread_id };
316316
317317 let response = self.request::<requests::Continue>(args).await?;
@@ -320,7 +320,7 @@ impl Client {
320320
321321 pub async fn stack_trace(
322322 &mut self,
323− thread_id: isize,
323+ thread_id: ThreadId,
324324 ) -> Result<(Vec<StackFrame>, Option<usize>)> {
325325 let args = requests::StackTraceArguments {
326326 thread_id,
@@ -358,7 +358,7 @@ impl Client {
358358 Ok(response.variables)
359359 }
360360
361− pub async fn step_in(&mut self, thread_id: isize) -> Result<()> {
361+ pub async fn step_in(&mut self, thread_id: ThreadId) -> Result<()> {
362362 let args = requests::StepInArguments {
363363 thread_id,
364364 target_id: None,
@@ -368,7 +368,7 @@ impl Client {
368368 self.request::<requests::StepIn>(args).await
369369 }
370370
371− pub async fn step_out(&mut self, thread_id: isize) -> Result<()> {
371+ pub async fn step_out(&mut self, thread_id: ThreadId) -> Result<()> {
372372 let args = requests::StepOutArguments {
373373 thread_id,
374374 granularity: None,
@@ -377,7 +377,7 @@ impl Client {
377377 self.request::<requests::StepOut>(args).await
378378 }
379379
380− pub async fn next(&mut self, thread_id: isize) -> Result<()> {
380+ pub async fn next(&mut self, thread_id: ThreadId) -> Result<()> {
381381 let args = requests::NextArguments {
382382 thread_id,
383383 granularity: None,
@@ -386,7 +386,7 @@ impl Client {
386386 self.request::<requests::Next>(args).await
387387 }
388388
389− pub async fn pause(&mut self, thread_id: isize) -> Result<()> {
389+ pub async fn pause(&mut self, thread_id: ThreadId) -> Result<()> {
390390 let args = requests::PauseArguments { thread_id };
391391
392392 self.request::<requests::Pause>(args).await
helix-dap/src/types.rs+22−11
@@ -2,6 +2,17 @@ use serde::{Deserialize, Serialize};
22use serde_json::Value;
33use std::path::PathBuf;
44
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+
516pub trait Request {
617 type Arguments: serde::de::DeserializeOwned + serde::Serialize;
718 type Result: serde::de::DeserializeOwned + serde::Serialize;
@@ -157,7 +168,7 @@ pub struct StackFrame {
157168#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
158169#[serde(rename_all = "camelCase")]
159170pub struct Thread {
160− pub id: isize,
171+ pub id: ThreadId,
161172 pub name: String,
162173}
163174
@@ -317,7 +328,7 @@ pub mod requests {
317328 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
318329 #[serde(rename_all = "camelCase")]
319330 pub struct ContinueArguments {
320− pub thread_id: isize,
331+ pub thread_id: ThreadId,
321332 }
322333
323334 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
@@ -338,7 +349,7 @@ pub mod requests {
338349 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
339350 #[serde(rename_all = "camelCase")]
340351 pub struct StackTraceArguments {
341− pub thread_id: isize,
352+ pub thread_id: ThreadId,
342353 pub start_frame: Option<usize>,
343354 pub levels: Option<usize>,
344355 pub format: Option<StackFrameFormat>,
@@ -424,7 +435,7 @@ pub mod requests {
424435 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
425436 #[serde(rename_all = "camelCase")]
426437 pub struct StepInArguments {
427− pub thread_id: isize,
438+ pub thread_id: ThreadId,
428439 pub target_id: Option<usize>,
429440 pub granularity: Option<String>,
430441 }
@@ -441,7 +452,7 @@ pub mod requests {
441452 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
442453 #[serde(rename_all = "camelCase")]
443454 pub struct StepOutArguments {
444− pub thread_id: isize,
455+ pub thread_id: ThreadId,
445456 pub granularity: Option<String>,
446457 }
447458
@@ -457,7 +468,7 @@ pub mod requests {
457468 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
458469 #[serde(rename_all = "camelCase")]
459470 pub struct NextArguments {
460− pub thread_id: isize,
471+ pub thread_id: ThreadId,
461472 pub granularity: Option<String>,
462473 }
463474
@@ -473,7 +484,7 @@ pub mod requests {
473484 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
474485 #[serde(rename_all = "camelCase")]
475486 pub struct PauseArguments {
476− pub thread_id: isize,
487+ pub thread_id: ThreadId,
477488 }
478489
479490 #[derive(Debug)]
@@ -574,7 +585,7 @@ pub mod events {
574585 pub struct Stopped {
575586 pub reason: String,
576587 pub description: Option<String>,
577− pub thread_id: Option<isize>,
588+ pub thread_id: Option<ThreadId>,
578589 pub preserve_focus_hint: Option<bool>,
579590 pub text: Option<String>,
580591 pub all_threads_stopped: Option<bool>,
@@ -584,7 +595,7 @@ pub mod events {
584595 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
585596 #[serde(rename_all = "camelCase")]
586597 pub struct Continued {
587− pub thread_id: isize,
598+ pub thread_id: ThreadId,
588599 pub all_threads_continued: Option<bool>,
589600 }
590601
@@ -604,7 +615,7 @@ pub mod events {
604615 #[serde(rename_all = "camelCase")]
605616 pub struct Thread {
606617 pub reason: String,
607− pub thread_id: isize,
618+ pub thread_id: ThreadId,
608619 }
609620
610621 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
@@ -661,7 +672,7 @@ pub mod events {
661672 // #[serde(rename_all = "camelCase")]
662673 // pub struct Invalidated {
663674 // pub areas: Vec<InvalidatedArea>,
664− // pub thread_id: Option<usize>,
675+ // pub thread_id: Option<ThreadId>,
665676 // pub stack_frame_id: Option<usize>,
666677 // }
667678
helix-term/src/application.rs+6−1
@@ -320,7 +320,12 @@ impl Application {
320320 for thread in threads {
321321 fetch_stack_trace(debugger, thread.id).await;
322322 }
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;
324329 }
325330 } else if let Some(thread_id) = thread_id {
326331 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::{
66 ui::{FilePicker, Prompt, PromptEvent},
77};
88use helix_core::{syntax::DebugConfigCompletion, Selection};
9−use helix_dap::{self as dap, Client};
9+use helix_dap::{self as dap, Client, ThreadId};
1010use helix_lsp::block_on;
1111
1212use serde_json::{to_value, Value};
@@ -34,7 +34,7 @@ pub fn resume_application(debugger: &mut Client) {
3434 debugger.thread_id = None;
3535}
3636
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) {
3838 let debugger = match &mut editor.debugger {
3939 Some(debugger) => debugger,
4040 None => return,
@@ -53,7 +53,7 @@ pub async fn select_thread_id(editor: &mut Editor, thread_id: isize, force: bool
5353 }
5454}
5555
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) {
5757 let (frames, _) = match debugger.stack_trace(thread_id).await {
5858 Ok(frames) => frames,
5959 Err(_) => return,