on master help sign in
dap: Clean up dap_start_impl, no need to clone arg keys
commit3042ff3e5af6d45f17e3b00e12980ff7d952335f authorBlaž Hrastnik <blaz@mxxn.io> dateSun 7 Nov 2021, 21:47 (5 years ago) parent9963a56
1 file changed+12−14
helix-term/src/commands/dap.rs+12−14
helix-term/src/commands/dap.rs+12−14
@@ -207,27 +207,26 @@ pub fn dap_start_impl(
207207 debugger.quirks = config.quirks.clone();
208208
209209 // TODO: avoid refetching all of this... pass a config in
210− let start_config = match name {
210+ let template = match name {
211211 Some(name) => config.templates.iter().find(|t| t.name == name),
212212 None => config.templates.get(0),
213213 };
214− let start_config = match start_config {
215− Some(c) => c,
214+ let template = match template {
215+ Some(template) => template,
216216 None => {
217217 editor.set_error("No debug config with given name".to_string());
218218 return;
219219 }
220220 };
221221
222− let template = start_config.args.clone();
223− let mut args: HashMap<String, Value> = HashMap::new();
222+ let mut args: HashMap<&str, Value> = HashMap::new();
224223
225224 if let Some(params) = params {
226− for (k, t) in template {
227− let mut value = t;
225+ for (k, t) in &template.args {
226+ let mut value = t.clone();
228227 for (i, x) in params.iter().enumerate() {
229228 let mut param = x.to_string();
230− if let Some(DebugConfigCompletion::Advanced(cfg)) = start_config.completion.get(i) {
229+ if let Some(DebugConfigCompletion::Advanced(cfg)) = template.completion.get(i) {
231230 if cfg.completion == Some("filename".to_owned())
232231 || cfg.completion == Some("directory".to_owned())
233232 {
@@ -238,14 +237,13 @@ pub fn dap_start_impl(
238237 }
239238 }
240239 // For param #0 replace {0} in args
240+ let pattern = format!("{{{}}}", i);
241241 value = match value {
242242 DebugArgumentValue::String(v) => {
243− DebugArgumentValue::String(v.replace(format!("{{{}}}", i).as_str(), &param))
243+ DebugArgumentValue::String(v.replace(&pattern, &param))
244244 }
245245 DebugArgumentValue::Array(arr) => DebugArgumentValue::Array(
246− arr.iter()
247− .map(|v| v.replace(format!("{{{}}}", i).as_str(), &param))
248− .collect(),
246+ arr.iter().map(|v| v.replace(&pattern, &param)).collect(),
249247 ),
250248 };
251249 }
@@ -264,7 +262,7 @@ pub fn dap_start_impl(
264262
265263 let args = to_value(args).unwrap();
266264
267− let result = match &start_config.request[..] {
265+ let result = match &template.request[..] {
268266 "launch" => block_on(debugger.launch(args)),
269267 "attach" => block_on(debugger.attach(args)),
270268 _ => {
@@ -273,7 +271,7 @@ pub fn dap_start_impl(
273271 }
274272 };
275273 if let Err(e) = result {
276− let msg = format!("Failed {} target: {}", start_config.request, e);
274+ let msg = format!("Failed {} target: {}", template.request, e);
277275 editor.set_error(msg);
278276 return;
279277 }