Skip to content

Commit

Permalink
merge (helix-editor#12320) custom typable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nikvoid committed Dec 28, 2024
1 parent a6d280f commit ec9ed96
Show file tree
Hide file tree
Showing 10 changed files with 1,504 additions and 715 deletions.
931 changes: 678 additions & 253 deletions helix-core/src/shellwords.rs

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ use helix_core::{
object, pos_at_coords,
regex::{self, Regex},
search::{self, CharMatcher},
selection, shellwords, surround,
selection,
shellwords::{self, Args},
surround,
syntax::{BlockCommentToken, LanguageServerFeature},
text_annotations::{Overlay, TextAnnotations},
textobject,
Expand Down Expand Up @@ -209,7 +211,7 @@ use helix_view::{align_view, Align};
pub enum MappableCommand {
Typable {
name: String,
args: Vec<String>,
args: String,
doc: String,
},
Static {
Expand Down Expand Up @@ -244,15 +246,17 @@ impl MappableCommand {
pub fn execute(&self, cx: &mut Context) {
match &self {
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()) {
let mut cx = compositor::Context {
editor: cx.editor,
jobs: cx.jobs,
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}"));
}
}
}
Expand Down Expand Up @@ -627,21 +631,15 @@ impl std::str::FromStr for MappableCommand {

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(suffix) = s.strip_prefix(':') {
let mut typable_command = suffix.split(' ').map(|arg| arg.trim());
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>>();
let (name, args) = suffix.split_once(' ').unwrap_or((suffix, ""));
typed::TYPABLE_COMMAND_MAP
.get(name)
.map(|cmd| MappableCommand::Typable {
name: cmd.name.to_owned(),
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('@') {
helix_view::input::parse_macro(suffix).map(|keys| Self::Macro {
name: s.to_string(),
Expand Down Expand Up @@ -3260,7 +3258,7 @@ pub fn command_palette(cx: &mut Context) {
.iter()
.map(|cmd| MappableCommand::Typable {
name: cmd.name.to_owned(),
args: Vec::new(),
args: String::new(),
doc: cmd.doc.to_owned(),
}),
);
Expand Down Expand Up @@ -4334,13 +4332,19 @@ fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
let (view, doc) = current!(editor);
let text = doc.text().slice(..);

let separator = if separator.is_empty() {
doc.line_ending.as_str()
} else {
separator
};

let selection = doc.selection(view.id);
let selections = selection.len();
let joined = selection
.fragments(text)
.fold(String::new(), |mut acc, fragment| {
if !acc.is_empty() {
acc.push_str(separator);
acc.push_str(&helix_core::shellwords::unescape(separator));
}
acc.push_str(&fragment);
acc
Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/commands/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ fn dap_callback<T, F>(
jobs.callback(callback);
}

// TODO: transition to `shellwords::Args` instead of `Option<Vec<Cow>>>`
pub fn dap_start_impl(
cx: &mut compositor::Context,
name: Option<&str>,
Expand Down Expand Up @@ -312,6 +313,7 @@ pub fn dap_restart(cx: &mut Context) {
);
}

// TODO: transition to `shellwords::Args` instead of `Vec<String>`
fn debug_parameter_prompt(
completions: Vec<DebugConfigCompletion>,
config_name: String,
Expand Down
Loading

0 comments on commit ec9ed96

Please sign in to comment.