Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions debug_adapter_schemas/Dart.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"useFvm": {
"type": "boolean",
"description": "Weather to use fvm to run the Dart/Flutter program"
"description": "Whether to use fvm to run the Dart/Flutter program"
},
"args": {
"type": "array",
Expand All @@ -51,6 +51,32 @@
},
"default": []
},
"toolArgs": {
"type": "array",
"description": "Arguments passed to the Flutter/Dart tool (e.g. --flavor, -d chrome).",
"items": {
"type": "string"
},
"default": []
},
"env": {
"type": "object",
"description": "Environment variables to set when launching the debug adapter.",
"additionalProperties": {
"type": "string"
}
},
"flutterMode": {
"type": "string",
"enum": [
"debug",
"profile",
"release",
"test"
],
"description": "The Flutter mode to use.",
"default": "debug"
},
"device_id": {
"type": "string",
"examples": [
Expand Down Expand Up @@ -92,4 +118,4 @@
"adapter"
],
"additionalProperties": false
}
}
8 changes: 4 additions & 4 deletions languages/dart/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
{
"label": "fvm flutter run",
"command": "fvm flutter",
"args": ["run"],
"command": "fvm",
"args": ["flutter", "run"],
"tags": ["flutter-main"]
},
{
Expand All @@ -19,8 +19,8 @@
},
{
"label": "fvm flutter test $ZED_STEM",
"command": "fvm flutter",
"args": ["test", "$ZED_FILE"],
"command": "fvm",
"args": ["flutter", "test", "$ZED_FILE"],
"tags": ["flutter-test-main"]
},
{
Expand Down
90 changes: 67 additions & 23 deletions src/dart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ use zed_extension_api::{
StartDebuggingRequestArguments, StartDebuggingRequestArgumentsRequest, Worktree,
};

fn tool_binary(debug_mode: &str) -> &'static str {
let (os, _) = current_platform();
match (debug_mode, os) {
("flutter", Os::Windows) => "flutter.bat",
("flutter", _) => "flutter",
(_, Os::Windows) => "dart.bat",
(_, _) => "dart",
}
}

struct DartBinary {
pub path: String,
pub args: Option<Vec<String>>,
Expand Down Expand Up @@ -90,21 +100,10 @@ impl zed::Extension for DartExtension {
let debug_mode = user_config
.get("type")
.and_then(|v| v.as_str())
.filter(|s| !s.trim().is_empty()) // Filter out empty strings
.filter(|s| !s.trim().is_empty())
.ok_or_else(|| "type is required and cannot be empty or null".to_string())?;

let (os, _) = current_platform();
let tool = if debug_mode == "flutter" {
match os {
Os::Windows => "flutter.bat",
_ => "flutter",
}
} else {
match os {
Os::Windows => "dart.bat",
_ => "dart",
}
};
let tool = tool_binary(debug_mode);

let (command, arguments) = if use_fvm {
(
Expand Down Expand Up @@ -138,33 +137,78 @@ impl zed::Extension for DartExtension {

let vm_service_uri = user_config.get("vmServiceUri").and_then(|v| v.as_str());

let config_json = json!({
"type": tool,
let stop_on_entry = user_config
.get("stopOnEntry")
.and_then(|v| v.as_bool())
.unwrap_or(false);

let flutter_mode = user_config
.get("flutterMode")
.and_then(|v| v.as_str())
.unwrap_or("debug");

let tool_args = user_config
.get("toolArgs")
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect::<Vec<String>>()
})
.unwrap_or_default();

let env = user_config
.get("env")
.and_then(|v| v.as_object())
.map(|obj| {
obj.iter()
.map(|(k, v)| {
(
k.clone(),
v.as_str().unwrap_or_default().to_string(),
)
})
.collect::<Vec<(String, String)>>()
})
.unwrap_or_default();

// Use debug_mode ("flutter"/"dart") rather than tool binary name so that
// the config is correct on all platforms (e.g. not "flutter.bat" on Windows).
let mut config_json = json!({
"type": debug_mode,
"request": request,
"vmServiceUri": vm_service_uri,
"program": program,
"cwd": cwd.clone().unwrap_or_default(),
"args": args,
"flutterMode": "debug",
"flutterMode": flutter_mode,
"deviceId": device_id,
"platform": platform,
"stopOnEntry": false
})
.to_string();
"stopOnEntry": stop_on_entry,
"sendLogsToClient": true
});

if let Some(uri) = vm_service_uri {
config_json["vmServiceUri"] = json!(uri);
}

if !tool_args.is_empty() {
config_json["toolArgs"] = json!(tool_args);
}

let debug_adapter_binary = DebugAdapterBinary {
command: Some(command),
arguments,
envs: vec![], // Add any Dart-specific env vars if needed
envs: env,
cwd,
connection: None,
request_args: StartDebuggingRequestArguments {
configuration: config_json,
configuration: config_json.to_string(),
request: match request {
"attach" => StartDebuggingRequestArgumentsRequest::Attach,
_ => StartDebuggingRequestArgumentsRequest::Launch,
},
}, // request_args: StartDebuggingRequestArguments:,
},
};
Result::Ok(debug_adapter_binary)
}
Expand Down
Loading