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

refactor: derive the Into<Opt> trait with procedural macros to avoid bloat #1742

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions yazi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ yazi-boot = { path = "../yazi-boot", version = "0.3.3" }
yazi-config = { path = "../yazi-config", version = "0.3.3" }
yazi-dds = { path = "../yazi-dds", version = "0.3.3" }
yazi-fs = { path = "../yazi-fs", version = "0.3.3" }
yazi-macro = { path = "../yazi-macro", version = "0.3.3" }
yazi-plugin = { path = "../yazi-plugin", version = "0.3.3" }
yazi-proxy = { path = "../yazi-proxy", version = "0.3.3" }
yazi-scheduler = { path = "../yazi-scheduler", version = "0.3.3" }
Expand Down
18 changes: 9 additions & 9 deletions yazi-core/src/completion/commands/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ impl From<Cmd> for Opt {
}

impl Completion {
#[yazi_macro::command]
pub fn arrow(&mut self, opt: Opt) {
if opt.step > 0 {
self.next(opt.step as usize);
} else {
self.prev(opt.step.unsigned_abs());
}
}

fn next(&mut self, step: usize) {
let len = self.cands.len();
if len == 0 {
Expand Down Expand Up @@ -38,13 +47,4 @@ impl Completion {

render!(old != self.cursor);
}

pub fn arrow(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if opt.step > 0 {
self.next(opt.step as usize);
} else {
self.prev(opt.step.unsigned_abs());
}
}
}
5 changes: 2 additions & 3 deletions yazi-core/src/completion/commands/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ impl From<bool> for Opt {
}

impl Completion {
pub fn close(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;

#[yazi_macro::command]
pub fn close(&mut self, opt: Opt) {
if let Some(s) = self.selected().filter(|_| opt.submit) {
InputProxy::complete(s, self.ticket);
}
Expand Down
50 changes: 25 additions & 25 deletions yazi-core/src/completion/commands/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ impl From<Cmd> for Opt {
}

impl Completion {
#[yazi_macro::command]
pub fn show(&mut self, opt: Opt) {
if self.ticket != opt.ticket {
return;
}

if !opt.cache.is_empty() {
self.caches.insert(opt.cache_name.to_owned(), opt.cache);
}
let Some(cache) = self.caches.get(&opt.cache_name) else {
return;
};

self.ticket = opt.ticket;
self.cands = Self::match_candidates(&opt.word, cache);
if self.cands.is_empty() {
return render!(mem::replace(&mut self.visible, false));
}

self.offset = 0;
self.cursor = 0;
self.visible = true;
render!();
}

fn match_candidates(word: &str, cache: &[String]) -> Vec<String> {
let smart = !word.bytes().any(|c| c.is_ascii_uppercase());

Expand Down Expand Up @@ -55,29 +80,4 @@ impl Completion {
}
prefixed.into_iter().map(ToOwned::to_owned).collect()
}

pub fn show(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if self.ticket != opt.ticket {
return;
}

if !opt.cache.is_empty() {
self.caches.insert(opt.cache_name.to_owned(), opt.cache);
}
let Some(cache) = self.caches.get(&opt.cache_name) else {
return;
};

self.ticket = opt.ticket;
self.cands = Self::match_candidates(&opt.word, cache);
if self.cands.is_empty() {
return render!(mem::replace(&mut self.visible, false));
}

self.offset = 0;
self.cursor = 0;
self.visible = true;
render!();
}
}
4 changes: 2 additions & 2 deletions yazi-core/src/completion/commands/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl From<Cmd> for Opt {
}

impl Completion {
pub fn trigger(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
#[yazi_macro::command]
pub fn trigger(&mut self, opt: Opt) {
if opt.ticket < self.ticket {
return;
}
Expand Down
18 changes: 9 additions & 9 deletions yazi-core/src/confirm/commands/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ impl From<Cmd> for Opt {
}

impl Confirm {
#[yazi_macro::command]
pub fn arrow(&mut self, opt: Opt, manager: &Manager) {
if opt.step > 0 {
self.next(opt.step as usize, manager.area(self.position).width)
} else {
self.prev(opt.step.unsigned_abs())
}
}

fn next(&mut self, step: usize, width: u16) {
let height = self.list.line_count(width);
if height == 0 {
Expand All @@ -29,13 +38,4 @@ impl Confirm {

render!(old != self.offset);
}

pub fn arrow(&mut self, opt: impl Into<Opt>, manager: &Manager) {
let opt = opt.into() as Opt;
if opt.step > 0 {
self.next(opt.step as usize, manager.area(self.position).width)
} else {
self.prev(opt.step.unsigned_abs())
}
}
}
4 changes: 2 additions & 2 deletions yazi-core/src/confirm/commands/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl From<bool> for Opt {
}

impl Confirm {
pub fn close(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
#[yazi_macro::command]
pub fn close(&mut self, opt: Opt) {
if let Some(cb) = self.callback.take() {
_ = cb.send(opt.submit);
}
Expand Down
5 changes: 2 additions & 3 deletions yazi-core/src/help/commands/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ impl From<isize> for Opt {
}

impl Help {
#[inline]
pub fn arrow(&mut self, opt: impl Into<Opt>) {
#[yazi_macro::command]
pub fn arrow(&mut self, opt: Opt) {
let max = self.bindings.len().saturating_sub(1);
self.offset = self.offset.min(max);
self.cursor = self.cursor.min(max);

let opt = opt.into() as Opt;
if opt.step > 0 {
self.next(opt.step as usize);
} else {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/backspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ impl From<bool> for Opt {
}

impl Input {
pub fn backspace(&mut self, opt: impl Into<Opt>) {
#[yazi_macro::command]
pub fn backspace(&mut self, opt: Opt) {
let snap = self.snaps.current_mut();
let opt = opt.into() as Opt;
if !opt.under && snap.cursor < 1 {
return;
} else if opt.under && snap.cursor >= snap.value.len() {
Expand Down
5 changes: 2 additions & 3 deletions yazi-core/src/input/commands/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ impl From<bool> for Opt {
}

impl Input {
pub fn close(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;

#[yazi_macro::command]
pub fn close(&mut self, opt: Opt) {
if self.completion {
CompletionProxy::close();
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl From<Cmd> for Opt {
}

impl Input {
pub fn complete(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
#[yazi_macro::command]
pub fn complete(&mut self, opt: Opt) {
if self.ticket != opt.ticket {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ impl From<Cmd> for Opt {
}

impl Input {
pub fn delete(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
#[yazi_macro::command]
pub fn delete(&mut self, opt: Opt) {
match self.snap().op {
InputOp::None => {
self.snap_mut().op = InputOp::Delete(opt.cut, opt.insert, self.snap().cursor);
Expand Down
3 changes: 2 additions & 1 deletion yazi-core/src/input/commands/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ impl From<()> for Opt {
}

impl Input {
pub fn escape(&mut self, _: impl Into<Opt>) {
#[yazi_macro::command]
pub fn escape(&mut self, _: Opt) {
let snap = self.snap_mut();
match snap.mode {
InputMode::Normal if snap.op == InputOp::None => {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ impl From<Cmd> for Opt {
}

impl Input {
pub fn forward(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
#[yazi_macro::command]
pub fn forward(&mut self, opt: Opt) {
let snap = self.snap();

let mut it = snap.value.chars().skip(snap.cursor).enumerate();
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ impl From<bool> for Opt {
}

impl Input {
pub fn insert(&mut self, opt: impl Into<Opt>) {
#[yazi_macro::command]
pub fn insert(&mut self, opt: Opt) {
let snap = self.snap_mut();
if snap.mode == InputMode::Normal {
snap.op = InputOp::None;
Expand All @@ -23,7 +24,6 @@ impl Input {
return;
}

let opt = opt.into() as Opt;
if opt.append {
self.move_(1);
}
Expand Down
55 changes: 27 additions & 28 deletions yazi-core/src/input/commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ impl From<Cmd> for Opt {
}

impl Input {
#[yazi_macro::command]
pub fn kill(&mut self, opt: Opt) {
let snap = self.snap_mut();
match opt.kind.as_str() {
"all" => self.kill_range(..),
"bol" => {
let end = snap.idx(snap.cursor).unwrap_or(snap.len());
self.kill_range(..end)
}
"eol" => {
let start = snap.idx(snap.cursor).unwrap_or(snap.len());
self.kill_range(start..)
}
"backward" => {
let end = snap.idx(snap.cursor).unwrap_or(snap.len());
let start = end - Self::find_word_boundary(snap.value[..end].chars().rev());
self.kill_range(start..end)
}
"forward" => {
let start = snap.idx(snap.cursor).unwrap_or(snap.len());
let end = start + Self::find_word_boundary(snap.value[start..].chars());
self.kill_range(start..end)
}
_ => {}
}
}

fn kill_range(&mut self, range: impl RangeBounds<usize>) {
let snap = self.snap_mut();
snap.cursor = match range.start_bound() {
Expand Down Expand Up @@ -64,32 +91,4 @@ impl Input {
let n = n + count_characters(input.clone().skip(n));
input.take(n).fold(0, |acc, c| acc + c.len_utf8())
}

pub fn kill(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
let snap = self.snap_mut();

match opt.kind.as_str() {
"all" => self.kill_range(..),
"bol" => {
let end = snap.idx(snap.cursor).unwrap_or(snap.len());
self.kill_range(..end)
}
"eol" => {
let start = snap.idx(snap.cursor).unwrap_or(snap.len());
self.kill_range(start..)
}
"backward" => {
let end = snap.idx(snap.cursor).unwrap_or(snap.len());
let start = end - Self::find_word_boundary(snap.value[..end].chars().rev());
self.kill_range(start..end)
}
"forward" => {
let start = snap.idx(snap.cursor).unwrap_or(snap.len());
let end = start + Self::find_word_boundary(snap.value[start..].chars());
self.kill_range(start..end)
}
_ => {}
}
}
}
5 changes: 2 additions & 3 deletions yazi-core/src/input/commands/move_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ impl From<isize> for Opt {
}

impl Input {
pub fn move_(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;

#[yazi_macro::command]
pub fn move_(&mut self, opt: Opt) {
let snap = self.snap();
if opt.in_operating && snap.op == InputOp::None {
return;
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ impl From<Cmd> for Opt {
}

impl Input {
pub fn paste(&mut self, opt: impl Into<Opt>) {
#[yazi_macro::command]
pub fn paste(&mut self, opt: Opt) {
if let Some(start) = self.snap().op.start() {
self.snap_mut().op = InputOp::Delete(false, false, start);
self.handle_op(self.snap().cursor, true);
Expand All @@ -23,7 +24,6 @@ impl Input {
return;
}

let opt = opt.into() as Opt;
self.insert(!opt.before);
self.type_str(&s.to_string_lossy());
self.escape(());
Expand Down
Loading