Skip to content

Commit

Permalink
Use DebugAdapter.name() instead of string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed Oct 23, 2024
1 parent bd8f5d4 commit f12192c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
15 changes: 15 additions & 0 deletions crates/dap/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
collections::HashMap,
ffi::OsString,
fmt::Debug,
ops::Deref,
path::{Path, PathBuf},
sync::Arc,
};
Expand All @@ -23,6 +24,20 @@ pub trait DapDelegate {

pub struct DebugAdapterName(pub Arc<str>);

impl Deref for DebugAdapterName {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl AsRef<str> for DebugAdapterName {
fn as_ref(&self) -> &str {
&self.0
}
}

impl AsRef<Path> for DebugAdapterName {
fn as_ref(&self) -> &Path {
Path::new(&*self.0)
Expand Down
8 changes: 5 additions & 3 deletions crates/dap_adapters/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl DebugAdapter for JsDebugAdapter {
delegate: &dyn DapDelegate,
) -> Result<AdapterVersion> {
let github_repo = GithubRepo {
repo_name: "vscode-js-debug".to_string(),
repo_name: Self::ADAPTER_NAME.into(),
repo_owner: "microsoft".to_string(),
};

Expand All @@ -50,16 +50,18 @@ impl DebugAdapter for JsDebugAdapter {
.ok_or(anyhow!("Couldn't get npm runtime"))?;

let adapter_path = paths::debug_adapters_dir().join(self.name());
let file_name_prefix = format!("{}_", self.name());

let adapter_path = util::fs::find_file_name_in_dir(adapter_path.as_path(), |file_name| {
file_name.starts_with("vscode-js-debug_")
file_name.starts_with(&file_name_prefix)
})
.await
.ok_or_else(|| anyhow!("Couldn't find javascript dap directory"))?;

let version = adapter_path
.file_name()
.and_then(|file_name| file_name.to_str())
.and_then(|file_name| file_name.strip_prefix("vscode-js-debug_"))
.and_then(|file_name| file_name.strip_prefix(&file_name_prefix))
.ok_or_else(|| anyhow!("Javascript debug adapter has invalid file name"))?
.to_string();

Expand Down
8 changes: 5 additions & 3 deletions crates/dap_adapters/src/php.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl DebugAdapter for PhpDebugAdapter {
delegate: &dyn DapDelegate,
) -> Result<AdapterVersion> {
let github_repo = GithubRepo {
repo_name: "vscode-php-debug".to_string(),
repo_name: Self::ADAPTER_NAME.into(),
repo_owner: "xdebug".to_string(),
};

Expand All @@ -50,16 +50,18 @@ impl DebugAdapter for PhpDebugAdapter {
.ok_or(anyhow!("Couldn't get npm runtime"))?;

let adapter_path = paths::debug_adapters_dir().join(self.name());
let file_name_prefix = format!("{}_", self.name());

let adapter_path = util::fs::find_file_name_in_dir(adapter_path.as_path(), |file_name| {
file_name.starts_with("vscode-php-debug_")
file_name.starts_with(&file_name_prefix)
})
.await
.ok_or_else(|| anyhow!("Couldn't find javascript dap directory"))?;

let version = adapter_path
.file_name()
.and_then(|file_name| file_name.to_str())
.and_then(|file_name| file_name.strip_prefix("vscode-php-debug_"))
.and_then(|file_name| file_name.strip_prefix(&file_name_prefix))
.ok_or_else(|| anyhow!("Javascript debug adapter has invalid file name"))?
.to_string();

Expand Down
7 changes: 4 additions & 3 deletions crates/dap_adapters/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl DebugAdapter for PythonDebugAdapter {
delegate: &dyn DapDelegate,
) -> Result<AdapterVersion> {
let github_repo = GithubRepo {
repo_name: "debugpy".into(),
repo_name: Self::ADAPTER_NAME.into(),
repo_owner: "microsoft".into(),
};

Expand All @@ -51,17 +51,18 @@ impl DebugAdapter for PythonDebugAdapter {
_: &DebugAdapterConfig,
) -> Result<DebugAdapterBinary> {
let adapter_path = paths::debug_adapters_dir().join(self.name());
let file_name_prefix = format!("{}_", self.name());

let debugpy_dir = util::fs::find_file_name_in_dir(adapter_path.as_path(), |file_name| {
file_name.starts_with("debugpy_")
file_name.starts_with(&file_name_prefix)
})
.await
.ok_or_else(|| anyhow!("Debugpy directory not found"))?;

let version = adapter_path
.file_name()
.and_then(|file_name| file_name.to_str())
.and_then(|file_name| file_name.strip_prefix("debugpy_"))
.and_then(|file_name| file_name.strip_prefix(&file_name_prefix))
.ok_or_else(|| anyhow!("Javascript debug adapter has invalid file name"))?
.to_string();

Expand Down

0 comments on commit f12192c

Please sign in to comment.