refactor(commands): propogate `Args` changes

refactor(commands): ignore unit pattern match

refactor(commands): use `if let` over `matches!`

refactor(commands): ignore unit pattern match

perf(commands):remove unnecessary ref for a &str

refactor(commands): change `MappableCommand` `args` from `Vec<String>` to String

refactor(commands): use `Args::raw` over `fold`ing

refactor: use `rest` in `set_option` command

refactor: use `rest` in `toggle_option` command

chore(dap): add TODOs to switch to `Args`
pull/11149/head
Rolo 4 months ago
parent ba026aaab4
commit 5691cb833e

@ -30,7 +30,9 @@ use helix_core::{
object, pos_at_coords, object, pos_at_coords,
regex::{self, Regex}, regex::{self, Regex},
search::{self, CharMatcher}, search::{self, CharMatcher},
selection, shellwords, surround, selection,
shellwords::{self, Args},
surround,
syntax::{BlockCommentToken, LanguageServerFeature}, syntax::{BlockCommentToken, LanguageServerFeature},
text_annotations::{Overlay, TextAnnotations}, text_annotations::{Overlay, TextAnnotations},
textobject, textobject,
@ -190,7 +192,7 @@ use helix_view::{align_view, Align};
pub enum MappableCommand { pub enum MappableCommand {
Typable { Typable {
name: String, name: String,
args: Vec<String>, args: String,
doc: String, doc: String,
}, },
Static { Static {
@ -225,15 +227,17 @@ impl MappableCommand {
pub fn execute(&self, cx: &mut Context) { pub fn execute(&self, cx: &mut Context) {
match &self { match &self {
Self::Typable { name, args, doc: _ } => { Self::Typable { name, args, doc: _ } => {
let args: Vec<Cow<str>> = args.iter().map(Cow::from).collect();
if let Some(command) = typed::TYPABLE_COMMAND_MAP.get(name.as_str()) { if let Some(command) = typed::TYPABLE_COMMAND_MAP.get(name.as_str()) {
let mut cx = compositor::Context { let mut cx = compositor::Context {
editor: cx.editor, editor: cx.editor,
jobs: cx.jobs, jobs: cx.jobs,
scroll: None, scroll: None,
}; };
if let Err(e) = (command.fun)(&mut cx, &args[..], PromptEvent::Validate) {
cx.editor.set_error(format!("{}", e)); if let Err(err) =
(command.fun)(&mut cx, Args::from(args), PromptEvent::Validate)
{
cx.editor.set_error(format!("{err}"));
} }
} }
} }
@ -601,21 +605,15 @@ impl std::str::FromStr for MappableCommand {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(suffix) = s.strip_prefix(':') { if let Some(suffix) = s.strip_prefix(':') {
let mut typable_command = suffix.split(' ').map(|arg| arg.trim()); let (name, args) = suffix.split_once(' ').unwrap_or((suffix, ""));
let name = typable_command
.next()
.ok_or_else(|| anyhow!("Expected typable command name"))?;
let args = typable_command
.map(|s| s.to_owned())
.collect::<Vec<String>>();
typed::TYPABLE_COMMAND_MAP typed::TYPABLE_COMMAND_MAP
.get(name) .get(name)
.map(|cmd| MappableCommand::Typable { .map(|cmd| MappableCommand::Typable {
name: cmd.name.to_owned(), name: cmd.name.to_owned(),
doc: format!(":{} {:?}", cmd.name, args), doc: format!(":{} {:?}", cmd.name, args),
args, args: args.to_string(),
}) })
.ok_or_else(|| anyhow!("No TypableCommand named '{}'", s)) .ok_or_else(|| anyhow!("No TypableCommand named '{}'", name))
} else if let Some(suffix) = s.strip_prefix('@') { } else if let Some(suffix) = s.strip_prefix('@') {
helix_view::input::parse_macro(suffix).map(|keys| Self::Macro { helix_view::input::parse_macro(suffix).map(|keys| Self::Macro {
name: s.to_string(), name: s.to_string(),
@ -3216,7 +3214,7 @@ pub fn command_palette(cx: &mut Context) {
.iter() .iter()
.map(|cmd| MappableCommand::Typable { .map(|cmd| MappableCommand::Typable {
name: cmd.name.to_owned(), name: cmd.name.to_owned(),
args: Vec::new(), args: String::new(),
doc: cmd.doc.to_owned(), doc: cmd.doc.to_owned(),
}), }),
); );
@ -4265,7 +4263,7 @@ fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
.fragments(text) .fragments(text)
.fold(String::new(), |mut acc, fragment| { .fold(String::new(), |mut acc, fragment| {
if !acc.is_empty() { if !acc.is_empty() {
acc.push_str(separator); acc.push_str(&helix_core::shellwords::unescape(separator));
} }
acc.push_str(&fragment); acc.push_str(&fragment);
acc acc

@ -109,6 +109,7 @@ fn dap_callback<T, F>(
jobs.callback(callback); jobs.callback(callback);
} }
// TODO: transition to `shellwords::Args` instead of `Option<Vec<Cow>>>`
pub fn dap_start_impl( pub fn dap_start_impl(
cx: &mut compositor::Context, cx: &mut compositor::Context,
name: Option<&str>, name: Option<&str>,
@ -312,6 +313,7 @@ pub fn dap_restart(cx: &mut Context) {
); );
} }
// TODO: transition to `shellwords::Args` instead of `Vec<String>`
fn debug_parameter_prompt( fn debug_parameter_prompt(
completions: Vec<DebugConfigCompletion>, completions: Vec<DebugConfigCompletion>,
config_name: String, config_name: String,

File diff suppressed because it is too large Load Diff

@ -597,18 +597,14 @@ mod tests {
let expectation = KeyTrie::Node(KeyTrieNode::new( let expectation = KeyTrie::Node(KeyTrieNode::new(
"", "",
hashmap! { hashmap! {
key => KeyTrie::Sequence(vec!{ key => KeyTrie::Sequence(vec![
MappableCommand::select_all, MappableCommand::select_all,
MappableCommand::Typable { MappableCommand::Typable {
name: "pipe".to_string(), name: "pipe".to_string(),
args: vec!{ args: String::from("sed -E 's/\\s+$//g'"),
"sed".to_string(), doc: String::new(),
"-E".to_string(),
"'s/\\s+$//g'".to_string()
},
doc: "".to_string(),
}, },
}) ])
}, },
vec![key], vec![key],
)); ));

Loading…
Cancel
Save