Repository: sxyazi/yazi
File: yazi-widgets/src/input/commands/move.rs (Rust)

Code before:

impl From<CmdCow> for Opt {
	fn from(c: CmdCow) -> Self {
		Self {
			step:         c.first().and_then(|d| d.try_into().ok()).unwrap_or_default(),
			in_operating: c.bool("in-operating"),
		}
	}
}
impl From<isize> for Opt {
	fn from(step: isize) -> Self { Self { step: step.into(), in_operating: false } }
}

impl Input {
	#[yazi_codegen::command]
	pub fn r#move(&mut self, opt: Opt) {
		let snap = self.snap();
		if opt.in_operating && snap.op == InputOp::None {
			return;
		}

		let (o_cur, n_cur) = (snap.cursor, opt.step.cursor(snap));
		render!(self.handle_op(n_cur, false));

		let (limit, snap) = (self.limit, self.snap_mut());
		if snap.value.is_empty() {
			return snap.offset = 0;
		}

		let o_off = snap.offset;
		snap.offset = if n_cur <= o_cur {
			let it = snap.slice(0..n_cur).chars().rev().map(|c| if snap.obscure { '•' } else { c });
			let pad = InputSnap::find_window(it, 0, 5).end;

			if n_cur >= o_off { snap.offset.min(n_cur - pad) } else { n_cur - pad }
		} else {
			let count = snap.count();

			let it = snap.slice(n_cur..count).chars().map(|c| if snap.obscure { '•' } else { c });
			let pad = InputSnap::find_window(it, 0, 5 + snap.mode.delta()).end;

			let it = snap.slice(0..n_cur + pad).chars().rev().map(|c| if snap.obscure { '•' } else { c });
			let max = InputSnap::find_window(it, 0, limit).end;

			if snap.width(o_off..n_cur) < limit as u16 {
				snap.offset.max(n_cur + pad - max)
			} else {
				n_cur + pad - max
			}
		};
	}

Review comment (refactor):
The logic for computing snap.offset is quite complex; consider refactoring this block into a separate helper function to enhance readability and maintainability.

Code after:

impl From<CmdCow> for Opt {
	fn from(c: CmdCow) -> Self {
		Self {
			step:         c.first().and_then(|d| d.try_into().ok()).unwrap_or_default(),
			in_operating: c.bool("in-operating"),
		}
	}
}
impl From<isize> for Opt {
	fn from(step: isize) -> Self { Self { step: step.into(), in_operating: false } }
}

impl Input {
	#[yazi_codegen::command]
	pub fn r#move(&mut self, opt: Opt) {
		let snap = self.snap();
		if opt.in_operating && snap.op == InputOp::None {
			return;
		}

		let (o_cur, n_cur) = (snap.cursor, opt.step.cursor(snap));
		render!(self.handle_op(n_cur, false));

		let (limit, snap) = (self.limit, self.snap_mut());
		if snap.value.is_empty() {
			return snap.offset = 0;
		}

		let (o_off, scrolloff) = (snap.offset, 5.min(limit / 2));
		snap.offset = if n_cur <= o_cur {
			let it = snap.slice(0..n_cur).chars().rev().map(|c| if snap.obscure { '•' } else { c });
			let pad = InputSnap::find_window(it, 0, scrolloff).end;

			if n_cur >= o_off { snap.offset.min(n_cur - pad) } else { n_cur - pad }
		} else {
			let count = snap.count();

			let it = snap.slice(n_cur..count).chars().map(|c| if snap.obscure { '•' } else { c });
			let pad = InputSnap::find_window(it, 0, scrolloff + snap.mode.delta()).end;

			let it = snap.slice(0..n_cur + pad).chars().rev().map(|c| if snap.obscure { '•' } else { c });
			let max = InputSnap::find_window(it, 0, limit).end;

			if snap.width(o_off..n_cur) < limit as u16 {
				snap.offset.max(n_cur + pad - max)
			} else {
				n_cur + pad - max
			}
		};
	}