on master help sign in
dap: Fully extract template parameter prompts
commit5938ab1bf1220913f487155ba7e1d9057d03f73e authorBlaž Hrastnik <blaz@mxxn.io> dateSun 7 Nov 2021, 18:12 (5 years ago) parent3b8d510
3 files changed+86−92
helix-term/src/commands/dap.rs+83−3 helix-term/src/ui/editor.rs+2−86 helix-view/src/editor.rs+1−3
helix-term/src/commands/dap.rs+83−3
@@ -3,7 +3,7 @@ use crate::{
33 commands,
44 compositor::Compositor,
55 job::Callback,
6− ui::{FilePicker, Picker, Popup, Prompt, PromptEvent, Text},
6+ ui::{self, FilePicker, Picker, Popup, Prompt, PromptEvent, Text},
77};
88use helix_core::{
99 syntax::{DebugArgumentValue, DebugConfigCompletion},
@@ -325,12 +325,92 @@ pub fn dap_launch(cx: &mut Context) {
325325 |template| template.name.as_str().into(),
326326 |cx, template, _action| {
327327 let completions = template.completion.clone();
328− cx.editor.debug_config_completions = completions;
329− // TODO: need some way to manipulate the compositor to push a new prompt here
328+ let name = template.name.clone();
329+ let callback = Box::pin(async move {
330+ let call: Callback =
331+ Box::new(move |_editor: &mut Editor, compositor: &mut Compositor| {
332+ let prompt = debug_parameter_prompt(completions, name, Vec::new());
333+ compositor.push(Box::new(prompt));
334+ });
335+ Ok(call)
336+ });
337+ cx.jobs.callback(callback);
330338 },
331339 ))); // TODO: wrap in popup with fixed size
332340}
333341
342+fn debug_parameter_prompt(
343+ completions: Vec<DebugConfigCompletion>,
344+ config_name: String,
345+ mut params: Vec<String>,
346+) -> Prompt {
347+ let i = params.len();
348+ let completion = completions.get(i).unwrap();
349+ let field_type = if let DebugConfigCompletion::Advanced(cfg) = completion {
350+ cfg.completion.clone().unwrap_or_else(|| "".to_owned())
351+ } else {
352+ "".to_owned()
353+ };
354+ let name = match completion {
355+ DebugConfigCompletion::Advanced(cfg) => {
356+ cfg.name.clone().unwrap_or_else(|| field_type.to_owned())
357+ }
358+ DebugConfigCompletion::Named(name) => name.clone(),
359+ };
360+ let default_val = match completion {
361+ DebugConfigCompletion::Advanced(cfg) => {
362+ cfg.default.clone().unwrap_or_else(|| "".to_owned())
363+ }
364+ _ => "".to_owned(),
365+ };
366+
367+ let noop = |_input: &str| Vec::new();
368+ let completer = match &field_type[..] {
369+ "filename" => ui::completers::filename,
370+ "directory" => ui::completers::directory,
371+ _ => noop,
372+ };
373+ Prompt::new(
374+ format!("{}: ", name).into(),
375+ None,
376+ completer,
377+ move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
378+ if event != PromptEvent::Validate {
379+ return;
380+ }
381+
382+ let mut value = input.to_owned();
383+ if value.is_empty() {
384+ value = default_val.clone();
385+ }
386+ params.push(value);
387+
388+ if params.len() < completions.len() {
389+ let completions = completions.clone();
390+ let config_name = config_name.clone();
391+ let params = params.clone();
392+ let callback = Box::pin(async move {
393+ let call: Callback =
394+ Box::new(move |_editor: &mut Editor, compositor: &mut Compositor| {
395+ let prompt = debug_parameter_prompt(completions, config_name, params);
396+ compositor.push(Box::new(prompt));
397+ });
398+ Ok(call)
399+ });
400+ cx.jobs.callback(callback);
401+ } else {
402+ commands::dap_start_impl(
403+ cx.editor,
404+ Some(&config_name),
405+ None,
406+ Some(params.iter().map(|x| x.as_str()).collect()),
407+ );
408+ }
409+ },
410+ None,
411+ )
412+}
413+
334414pub fn dap_toggle_breakpoint(cx: &mut Context) {
335415 let (view, doc) = current!(cx.editor);
336416 let text = doc.text().slice(..);
helix-term/src/ui/editor.rs+2−86
@@ -1,7 +1,6 @@
11use crate::{
22 commands,
3− compositor::{Component, Compositor, Context, EventResult},
4− job::Callback,
3+ compositor::{Component, Context, EventResult},
54 key,
65 keymap::{KeymapResult, KeymapResultKind, Keymaps},
76 ui::{Completion, ProgressSpinners},
@@ -11,7 +10,7 @@ use helix_core::{
1110 coords_at_pos,
1211 graphemes::{ensure_grapheme_boundary_next, next_grapheme_boundary, prev_grapheme_boundary},
1312 movement::Direction,
14− syntax::{self, DebugConfigCompletion, HighlightEvent},
13+ syntax::{self, HighlightEvent},
1514 unicode::segmentation::UnicodeSegmentation,
1615 unicode::width::UnicodeWidthStr,
1716 LineEnding, Position, Range, Selection,
@@ -31,8 +30,6 @@ use std::{borrow::Cow, collections::HashMap, path::PathBuf};
3130use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
3231use tui::buffer::Buffer as Surface;
3332
34−use super::{Prompt, PromptEvent};
35−
3633pub struct EditorView {
3734 keymaps: Keymaps,
3835 on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
@@ -815,79 +812,6 @@ impl EditorView {
815812 );
816813 }
817814
818− fn debug_parameter_prompt(
819− completions: Vec<DebugConfigCompletion>,
820− config_name: String,
821− mut params: Vec<String>,
822− ) -> Prompt {
823− let i = params.len();
824− let completion = completions.get(i).unwrap();
825− let field_type = if let DebugConfigCompletion::Advanced(cfg) = completion {
826− cfg.completion.clone().unwrap_or_else(|| "".to_owned())
827− } else {
828− "".to_owned()
829− };
830− let name = match completion {
831− DebugConfigCompletion::Advanced(cfg) => {
832− cfg.name.clone().unwrap_or_else(|| field_type.to_owned())
833− }
834− DebugConfigCompletion::Named(name) => name.clone(),
835− };
836− let default_val = match completion {
837− DebugConfigCompletion::Advanced(cfg) => {
838− cfg.default.clone().unwrap_or_else(|| "".to_owned())
839− }
840− _ => "".to_owned(),
841− };
842−
843− let noop = |_input: &str| Vec::new();
844− let completer = match &field_type[..] {
845− "filename" => super::completers::filename,
846− "directory" => super::completers::directory,
847− _ => noop,
848− };
849− Prompt::new(
850− format!("{}: ", name).into(),
851− None,
852− completer,
853− move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
854− if event != PromptEvent::Validate {
855− return;
856− }
857−
858− let mut value = input.to_owned();
859− if value.is_empty() {
860− value = default_val.clone();
861− }
862− params.push(value);
863−
864− if params.len() < completions.len() {
865− let completions = completions.clone();
866− let config_name = config_name.clone();
867− let params = params.clone();
868− let callback = Box::pin(async move {
869− let call: Callback =
870− Box::new(move |_editor: &mut Editor, compositor: &mut Compositor| {
871− let prompt =
872− Self::debug_parameter_prompt(completions, config_name, params);
873− compositor.push(Box::new(prompt));
874− });
875− Ok(call)
876− });
877− cx.jobs.callback(callback);
878− } else {
879− commands::dap_start_impl(
880− cx.editor,
881− Some(&config_name),
882− None,
883− Some(params.iter().map(|x| x.as_str()).collect()),
884− );
885− }
886− },
887− None,
888− )
889− }
890−
891815 /// Handle events by looking them up in `self.keymaps`. Returns None
892816 /// if event was handled (a command was executed or a subkeymap was
893817 /// activated). Only KeymapResultKind::{NotFound, Cancelled} is returned
@@ -898,14 +822,6 @@ impl EditorView {
898822 cxt: &mut commands::Context,
899823 event: KeyEvent,
900824 ) -> Option<KeymapResult> {
901− if !cxt.editor.debug_config_completions.is_empty() {
902− let completions = std::mem::take(&mut cxt.editor.debug_config_completions);
903− // TODO name
904− let prompt = Self::debug_parameter_prompt(completions, "test".to_string(), Vec::new());
905− cxt.push_layer(Box::new(prompt));
906− return None;
907− }
908−
909825 let key_result = self.keymaps.get_mut(&mode).unwrap().get(event);
910826 self.autoinfo = key_result.sticky.map(|node| node.infobox());
911827
helix-view/src/editor.rs+1−3
@@ -24,7 +24,7 @@ use anyhow::Error;
2424
2525pub use helix_core::diagnostic::Severity;
2626pub use helix_core::register::Registers;
27−use helix_core::syntax::{self, DebugConfigCompletion};
27+use helix_core::syntax;
2828use helix_core::Position;
2929use helix_dap as dap;
3030
@@ -126,7 +126,6 @@ pub struct Editor {
126126 pub debugger: Option<dap::Client>,
127127 pub debugger_events: SelectAll<UnboundedReceiverStream<dap::Payload>>,
128128 pub breakpoints: HashMap<PathBuf, Vec<dap::SourceBreakpoint>>,
129− pub debug_config_completions: Vec<DebugConfigCompletion>,
130129
131130 pub clipboard_provider: Box<dyn ClipboardProvider>,
132131
@@ -172,7 +171,6 @@ impl Editor {
172171 debugger: None,
173172 debugger_events: SelectAll::new(),
174173 breakpoints: HashMap::new(),
175− debug_config_completions: Vec::new(),
176174 syn_loader: config_loader,
177175 theme_loader: themes,
178176 registers: Registers::default(),