Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better handling of killing path separator #385

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions yazi-core/src/input/commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ use yazi_shared::{event::Exec, CharKind};

use crate::input::Input;

#[derive(Debug, Copy, Clone)]
enum KillMode {
Default,
BigWord,
}

pub struct Opt<'a> {
kind: &'a str,
mode: KillMode,
}

impl<'a> From<&'a Exec> for Opt<'a> {
fn from(e: &'a Exec) -> Self {
Self { kind: e.args.first().map(|s| s.as_str()).unwrap_or_default() }
Self {
kind: e.args.first().map(|s| s.as_str()).unwrap_or_default(),
mode: e.named.get("big-word").map_or(KillMode::Default, |_| KillMode::BigWord),
Copy link
Owner

@sxyazi sxyazi Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its better to impl the FromStr (also Default) trait for the KillMode to do this.

}
}
}

Expand Down Expand Up @@ -45,7 +55,7 @@ impl Input {
///
/// Otherwise, returns how many characters to move to reach right *AFTER* the
/// word boundary, or the end of the iterator.
fn find_word_boundary(input: impl Iterator<Item = char> + Clone) -> usize {
fn find_word_boundary(input: impl Iterator<Item = char> + Clone, mode: KillMode) -> usize {
fn count_spaces(input: impl Iterator<Item = char>) -> usize {
// Move until we don't see any more whitespace.
input.take_while(|&c| CharKind::new(c) == CharKind::Space).count()
Expand All @@ -62,8 +72,12 @@ impl Input {
input.take_while(|&c| CharKind::new(c) == first).count() + 1
}

let spaces = count_spaces(input.clone());
spaces + count_characters(input.skip(spaces))
let space_or_trailing_slash = match input.clone().next() {
Some(std::path::MAIN_SEPARATOR) if matches!(mode, KillMode::BigWord) => 1,
Some(c) if CharKind::new(c) == CharKind::Space => count_spaces(input.clone()),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that if CharKind::new(c) == CharKind::Space is unnecessary.

_ => 0,
};
space_or_trailing_slash + count_characters(input.skip(space_or_trailing_slash))
}

pub fn kill<'a>(&mut self, opt: impl Into<Opt<'a>>) -> bool {
Expand All @@ -81,12 +95,12 @@ impl Input {
}
b"backward" => {
let end = snap.idx(snap.cursor).unwrap_or(snap.len());
let start = end - Self::find_word_boundary(snap.value[..end].chars().rev());
let start = end - Self::find_word_boundary(snap.value[..end].chars().rev(), opt.mode);
self.kill_range(start..end)
}
b"forward" => {
let start = snap.idx(snap.cursor).unwrap_or(snap.len());
let end = start + Self::find_word_boundary(snap.value[start..].chars());
let end = start + Self::find_word_boundary(snap.value[start..].chars(), opt.mode);
self.kill_range(start..end)
}
_ => false,
Expand Down