Skip to content

Commit 45fb582

Browse files
committed
Improve DAP binary handling and cleanup
Set cwd to worktree root and use user config for request. Validate adapter name and reject unknown adapters. Clean up old 'emmylua-' and 'emmylua_dap-' directories. Compute absolute binary path and cache it.
1 parent 7fa9963 commit 45fb582

File tree

1 file changed

+63
-22
lines changed

1 file changed

+63
-22
lines changed

src/emmylua.rs

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::fs;
1+
use std::path::PathBuf;
2+
use std::str::FromStr;
3+
use std::{env, fs};
24
use zed::lsp::CompletionKind;
35
use zed::{CodeLabel, CodeLabelSpan, LanguageServerId};
46
use zed_extension_api::{self as zed, Result, serde_json::Value};
@@ -17,7 +19,9 @@ impl EmmyLuaExtension {
1719
if let Ok(lsp_settings) = zed::settings::LspSettings::for_worktree("emmylua", worktree) {
1820
if let Some(binary) = lsp_settings.binary {
1921
if let Some(path) = binary.path {
20-
return Ok(path);
22+
if !fs::metadata(&path).map_or(false, |stat| stat.is_file()) {
23+
return Ok(path);
24+
}
2125
}
2226
}
2327
}
@@ -112,8 +116,12 @@ impl EmmyLuaExtension {
112116
fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
113117
for entry in entries {
114118
let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
115-
if entry.file_name().to_str() != Some(&version_dir) {
116-
fs::remove_dir_all(entry.path()).ok();
119+
if let Some(file_name) = entry.file_name().to_str() {
120+
if file_name.starts_with("emmylua-") {
121+
if entry.file_name().to_str() != Some(&version_dir) {
122+
fs::remove_dir_all(entry.path()).ok();
123+
}
124+
}
117125
}
118126
}
119127
}
@@ -276,7 +284,7 @@ impl zed::Extension for EmmyLuaExtension {
276284
fn get_dap_binary(
277285
&mut self,
278286
adapter_name: String,
279-
_config: zed::DebugTaskDefinition,
287+
config: zed::DebugTaskDefinition,
280288
user_provided_debug_adapter_path: Option<String>,
281289
worktree: &zed::Worktree,
282290
) -> std::result::Result<zed::DebugAdapterBinary, String> {
@@ -285,21 +293,29 @@ impl zed::Extension for EmmyLuaExtension {
285293
}
286294

287295
let default_request_args = zed::StartDebuggingRequestArguments {
288-
configuration: Default::default(),
289-
request: zed::StartDebuggingRequestArgumentsRequest::Launch,
296+
configuration: config.config.clone(),
297+
request: self.dap_request_kind(
298+
adapter_name,
299+
Value::from_str(config.config.as_str())
300+
.map_err(|e| format!("Invalid JSON configuration: {e}"))?,
301+
)?,
290302
};
291303

304+
let cwd = Some(worktree.root_path());
305+
292306
// Check if user provided a custom path
293307
if let Some(path) = user_provided_debug_adapter_path {
294-
self.cached_dap_binary_path = Some(path.clone());
295-
return Ok(zed::DebugAdapterBinary {
296-
command: Some(path),
297-
arguments: vec![],
298-
envs: vec![],
299-
cwd: None,
300-
connection: None,
301-
request_args: default_request_args,
302-
});
308+
if !fs::metadata(&path).map_or(false, |stat| stat.is_file()) {
309+
self.cached_dap_binary_path = Some(path.clone());
310+
return Ok(zed::DebugAdapterBinary {
311+
command: Some(path),
312+
arguments: vec![],
313+
envs: vec![],
314+
cwd,
315+
connection: None,
316+
request_args: default_request_args,
317+
});
318+
}
303319
}
304320

305321
// Try to find emmylua_dap in PATH
@@ -308,7 +324,7 @@ impl zed::Extension for EmmyLuaExtension {
308324
command: Some(path),
309325
arguments: vec![],
310326
envs: vec![],
311-
cwd: None,
327+
cwd,
312328
connection: None,
313329
request_args: default_request_args,
314330
});
@@ -321,7 +337,7 @@ impl zed::Extension for EmmyLuaExtension {
321337
command: Some(path.clone()),
322338
arguments: vec![],
323339
envs: vec![],
324-
cwd: None,
340+
cwd,
325341
connection: None,
326342
request_args: default_request_args,
327343
});
@@ -390,24 +406,49 @@ impl zed::Extension for EmmyLuaExtension {
390406
},
391407
)
392408
.map_err(|e| format!("Failed to download DAP binary: {}", e))?;
409+
410+
let entries =
411+
fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
412+
for entry in entries {
413+
let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
414+
if let Some(file_name) = entry.file_name().to_str() {
415+
if file_name.starts_with("emmylua_dap-") {
416+
if entry.file_name().to_str() != Some(&version_dir) {
417+
fs::remove_dir_all(entry.path()).ok();
418+
}
419+
}
420+
}
421+
}
393422
}
394423

395-
self.cached_dap_binary_path = Some(binary_path.clone());
424+
let path = match env::current_dir() {
425+
Ok(current_dir) => current_dir.join(binary_path).to_string_lossy().to_string(),
426+
Err(e) => {
427+
eprintln!("Error: {}", e);
428+
PathBuf::from(binary_path).to_string_lossy().to_string()
429+
}
430+
};
431+
432+
self.cached_dap_binary_path = Some(path.clone());
396433
Ok(zed::DebugAdapterBinary {
397-
command: Some(binary_path),
434+
command: Some(path),
398435
arguments: vec![],
399436
envs: vec![],
400-
cwd: None,
437+
cwd,
401438
connection: None,
402439
request_args: default_request_args,
403440
})
404441
}
405442

406443
fn dap_request_kind(
407444
&mut self,
408-
_adapter_name: String,
445+
adapter_name: String,
409446
config: Value,
410447
) -> std::result::Result<zed::StartDebuggingRequestArgumentsRequest, String> {
448+
if adapter_name != "emmylua_new" {
449+
return Err(format!("Unknown debug adapter: {}", adapter_name));
450+
}
451+
411452
let request_type = config
412453
.get("request")
413454
.and_then(|v| v.as_str())

0 commit comments

Comments
 (0)