Skip to content

Commit

Permalink
Merge pull request #790 from adamnemecek/master
Browse files Browse the repository at this point in the history
misc refactoring
  • Loading branch information
gwenn authored Aug 10, 2024
2 parents 8d960ac + b28a874 commit 522b644
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 122 deletions.
8 changes: 4 additions & 4 deletions examples/diy_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ impl Hint for CommandHint {
}

impl CommandHint {
fn new(text: &str, complete_up_to: &str) -> CommandHint {
fn new(text: &str, complete_up_to: &str) -> Self {
assert!(text.starts_with(complete_up_to));
CommandHint {
Self {
display: text.into(),
complete_up_to: complete_up_to.len(),
}
}

fn suffix(&self, strip_chars: usize) -> CommandHint {
CommandHint {
fn suffix(&self, strip_chars: usize) -> Self {
Self {
display: self.display[strip_chars..].to_owned(),
complete_up_to: self.complete_up_to.saturating_sub(strip_chars),
}
Expand Down
12 changes: 6 additions & 6 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ impl KeyEvent {
impl TrieKey for Event {
fn encode_bytes(&self) -> Vec<u8> {
match self {
Event::Any => ANY.to_be_bytes().to_vec(),
Event::KeySeq(keys) => {
Self::Any => ANY.to_be_bytes().to_vec(),
Self::KeySeq(keys) => {
let mut dst = Vec::with_capacity(keys.len() * 4);
for key in keys {
dst.extend_from_slice(&key.encode().to_be_bytes());
}
dst
}
Event::Mouse() => MOUSE.to_be_bytes().to_vec(),
Self::Mouse() => MOUSE.to_be_bytes().to_vec(),
}
}
}
Expand All @@ -132,8 +132,8 @@ pub enum EventHandler {
}

impl From<Cmd> for EventHandler {
fn from(c: Cmd) -> EventHandler {
EventHandler::Simple(c)
fn from(c: Cmd) -> Self {
Self::Simple(c)
}
}

Expand All @@ -147,7 +147,7 @@ pub struct EventContext<'r> {

impl<'r> EventContext<'r> {
pub(crate) fn new(is: &InputState, wrt: &'r dyn Refresher) -> Self {
EventContext {
Self {
mode: is.mode,
input_mode: is.input_mode,
wrt,
Expand Down
2 changes: 1 addition & 1 deletion src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ fn filename_complete(
dir_path.to_path_buf()
};

let mut entries: Vec<Pair> = Vec::new();
let mut entries: Vec<Pair> = vec![];

// if dir doesn't exist, then don't offer any completions
if !dir.exists() {
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ pub enum BellStyle {
impl Default for BellStyle {
#[cfg(any(windows, target_arch = "wasm32"))]
fn default() -> Self {
BellStyle::None
Self::None
}

#[cfg(unix)]
fn default() -> Self {
BellStyle::Audible
Self::Audible
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
prompt: &'prompt str,
helper: Option<&'out H>,
ctx: Context<'out>,
) -> State<'out, 'prompt, H> {
) -> Self {
let prompt_size = out.calculate_position(prompt, Position::default());
State {
Self {
out,
prompt,
prompt_size,
Expand Down
32 changes: 16 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,37 @@ pub enum ReadlineError {
impl fmt::Display for ReadlineError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ReadlineError::Io(ref err) => err.fmt(f),
ReadlineError::Eof => write!(f, "EOF"),
ReadlineError::Interrupted => write!(f, "Interrupted"),
Self::Io(ref err) => err.fmt(f),
Self::Eof => write!(f, "EOF"),
Self::Interrupted => write!(f, "Interrupted"),
#[cfg(unix)]
ReadlineError::Errno(ref err) => err.fmt(f),
ReadlineError::WindowResized => write!(f, "WindowResized"),
Self::Errno(ref err) => err.fmt(f),
Self::WindowResized => write!(f, "WindowResized"),
#[cfg(windows)]
ReadlineError::Decode(ref err) => err.fmt(f),
Self::Decode(ref err) => err.fmt(f),
#[cfg(windows)]
ReadlineError::SystemError(ref err) => err.fmt(f),
Self::SystemError(ref err) => err.fmt(f),
#[cfg(feature = "with-sqlite-history")]
ReadlineError::SQLiteError(ref err) => err.fmt(f),
Self::SQLiteError(ref err) => err.fmt(f),
}
}
}

impl Error for ReadlineError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
ReadlineError::Io(ref err) => Some(err),
ReadlineError::Eof => None,
ReadlineError::Interrupted => None,
Self::Io(ref err) => Some(err),
Self::Eof => None,
Self::Interrupted => None,
#[cfg(unix)]
ReadlineError::Errno(ref err) => Some(err),
ReadlineError::WindowResized => None,
Self::Errno(ref err) => Some(err),
Self::WindowResized => None,
#[cfg(windows)]
ReadlineError::Decode(ref err) => Some(err),
Self::Decode(ref err) => Some(err),
#[cfg(windows)]
ReadlineError::SystemError(_) => None,
Self::SystemError(_) => None,
#[cfg(feature = "with-sqlite-history")]
ReadlineError::SQLiteError(ref err) => Some(err),
Self::SQLiteError(ref err) => Some(err),
}
}
}
Expand Down
118 changes: 59 additions & 59 deletions src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,77 +133,77 @@ impl Cmd {
pub const fn should_reset_kill_ring(&self) -> bool {
#[allow(clippy::match_same_arms)]
match *self {
Cmd::Kill(Movement::BackwardChar(_) | Movement::ForwardChar(_)) => true,
Cmd::ClearScreen
| Cmd::Kill(_)
| Cmd::Replace(..)
| Cmd::Noop
| Cmd::Suspend
| Cmd::Yank(..)
| Cmd::YankPop => false,
Self::Kill(Movement::BackwardChar(_) | Movement::ForwardChar(_)) => true,
Self::ClearScreen
| Self::Kill(_)
| Self::Replace(..)
| Self::Noop
| Self::Suspend
| Self::Yank(..)
| Self::YankPop => false,
_ => true,
}
}

const fn is_repeatable_change(&self) -> bool {
matches!(
*self,
Cmd::Dedent(..)
| Cmd::Indent(..)
| Cmd::Insert(..)
| Cmd::Kill(_)
| Cmd::ReplaceChar(..)
| Cmd::Replace(..)
| Cmd::SelfInsert(..)
| Cmd::ViYankTo(_)
| Cmd::Yank(..) // Cmd::TransposeChars | TODO Validate
Self::Dedent(..)
| Self::Indent(..)
| Self::Insert(..)
| Self::Kill(_)
| Self::ReplaceChar(..)
| Self::Replace(..)
| Self::SelfInsert(..)
| Self::ViYankTo(_)
| Self::Yank(..) // Cmd::TransposeChars | TODO Validate
)
}

const fn is_repeatable(&self) -> bool {
match *self {
Cmd::Move(_) => true,
Self::Move(_) => true,
_ => self.is_repeatable_change(),
}
}

// Replay this command with a possible different `RepeatCount`.
fn redo(&self, new: Option<RepeatCount>, wrt: &dyn Refresher) -> Self {
match *self {
Cmd::Dedent(ref mvt) => Cmd::Dedent(mvt.redo(new)),
Cmd::Indent(ref mvt) => Cmd::Indent(mvt.redo(new)),
Cmd::Insert(previous, ref text) => {
Cmd::Insert(repeat_count(previous, new), text.clone())
Self::Dedent(ref mvt) => Self::Dedent(mvt.redo(new)),
Self::Indent(ref mvt) => Self::Indent(mvt.redo(new)),
Self::Insert(previous, ref text) => {
Self::Insert(repeat_count(previous, new), text.clone())
}
Cmd::Kill(ref mvt) => Cmd::Kill(mvt.redo(new)),
Cmd::Move(ref mvt) => Cmd::Move(mvt.redo(new)),
Cmd::ReplaceChar(previous, c) => Cmd::ReplaceChar(repeat_count(previous, new), c),
Cmd::Replace(ref mvt, ref text) => {
Self::Kill(ref mvt) => Self::Kill(mvt.redo(new)),
Self::Move(ref mvt) => Self::Move(mvt.redo(new)),
Self::ReplaceChar(previous, c) => Self::ReplaceChar(repeat_count(previous, new), c),
Self::Replace(ref mvt, ref text) => {
if text.is_none() {
let last_insert = wrt.last_insert();
if let Movement::ForwardChar(0) = mvt {
Cmd::Replace(
Self::Replace(
Movement::ForwardChar(last_insert.as_ref().map_or(0, String::len)),
last_insert,
)
} else {
Cmd::Replace(mvt.redo(new), last_insert)
Self::Replace(mvt.redo(new), last_insert)
}
} else {
Cmd::Replace(mvt.redo(new), text.clone())
Self::Replace(mvt.redo(new), text.clone())
}
}
Cmd::SelfInsert(previous, c) => {
Self::SelfInsert(previous, c) => {
// consecutive char inserts are repeatable not only the last one...
if let Some(text) = wrt.last_insert() {
Cmd::Insert(repeat_count(previous, new), text)
Self::Insert(repeat_count(previous, new), text)
} else {
Cmd::SelfInsert(repeat_count(previous, new), c)
Self::SelfInsert(repeat_count(previous, new), c)
}
}
// Cmd::TransposeChars => Cmd::TransposeChars,
Cmd::ViYankTo(ref mvt) => Cmd::ViYankTo(mvt.redo(new)),
Cmd::Yank(previous, anchor) => Cmd::Yank(repeat_count(previous, new), anchor),
Self::ViYankTo(ref mvt) => Self::ViYankTo(mvt.redo(new)),
Self::Yank(previous, anchor) => Self::Yank(repeat_count(previous, new), anchor),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -263,10 +263,10 @@ pub enum CharSearch {
impl CharSearch {
const fn opposite(self) -> Self {
match self {
CharSearch::Forward(c) => CharSearch::Backward(c),
CharSearch::ForwardBefore(c) => CharSearch::BackwardAfter(c),
CharSearch::Backward(c) => CharSearch::Forward(c),
CharSearch::BackwardAfter(c) => CharSearch::ForwardBefore(c),
Self::Forward(c) => Self::Backward(c),
Self::ForwardBefore(c) => Self::BackwardAfter(c),
Self::Backward(c) => Self::Forward(c),
Self::BackwardAfter(c) => Self::ForwardBefore(c),
}
}
}
Expand Down Expand Up @@ -309,26 +309,26 @@ impl Movement {
// Replay this movement with a possible different `RepeatCount`.
const fn redo(&self, new: Option<RepeatCount>) -> Self {
match *self {
Movement::WholeLine => Movement::WholeLine,
Movement::BeginningOfLine => Movement::BeginningOfLine,
Movement::ViFirstPrint => Movement::ViFirstPrint,
Movement::EndOfLine => Movement::EndOfLine,
Movement::BackwardWord(previous, word) => {
Movement::BackwardWord(repeat_count(previous, new), word)
Self::WholeLine => Self::WholeLine,
Self::BeginningOfLine => Self::BeginningOfLine,
Self::ViFirstPrint => Self::ViFirstPrint,
Self::EndOfLine => Self::EndOfLine,
Self::BackwardWord(previous, word) => {
Self::BackwardWord(repeat_count(previous, new), word)
}
Movement::ForwardWord(previous, at, word) => {
Movement::ForwardWord(repeat_count(previous, new), at, word)
Self::ForwardWord(previous, at, word) => {
Self::ForwardWord(repeat_count(previous, new), at, word)
}
Movement::ViCharSearch(previous, char_search) => {
Movement::ViCharSearch(repeat_count(previous, new), char_search)
Self::ViCharSearch(previous, char_search) => {
Self::ViCharSearch(repeat_count(previous, new), char_search)
}
Movement::BackwardChar(previous) => Movement::BackwardChar(repeat_count(previous, new)),
Movement::ForwardChar(previous) => Movement::ForwardChar(repeat_count(previous, new)),
Movement::LineUp(previous) => Movement::LineUp(repeat_count(previous, new)),
Movement::LineDown(previous) => Movement::LineDown(repeat_count(previous, new)),
Movement::WholeBuffer => Movement::WholeBuffer,
Movement::BeginningOfBuffer => Movement::BeginningOfBuffer,
Movement::EndOfBuffer => Movement::EndOfBuffer,
Self::BackwardChar(previous) => Self::BackwardChar(repeat_count(previous, new)),
Self::ForwardChar(previous) => Self::ForwardChar(repeat_count(previous, new)),
Self::LineUp(previous) => Self::LineUp(repeat_count(previous, new)),
Self::LineDown(previous) => Self::LineDown(repeat_count(previous, new)),
Self::WholeBuffer => Self::WholeBuffer,
Self::BeginningOfBuffer => Self::BeginningOfBuffer,
Self::EndOfBuffer => Self::EndOfBuffer,
}
}
}
Expand Down Expand Up @@ -1209,14 +1209,14 @@ enum Event {
KeySeq([KeyEvent; 1]),
}
impl From<KeyEvent> for Event {
fn from(k: KeyEvent) -> Event {
Event::KeySeq([k])
fn from(k: KeyEvent) -> Self {
Self::KeySeq([k])
}
}
pub struct Bindings {}
impl Bindings {
pub fn new() -> Bindings {
Bindings {}
pub fn new() -> Self {
Self {}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ impl<'h> Context<'h> {
/// Constructor. Visible for testing.
#[must_use]
pub fn new(history: &'h dyn History) -> Self {
Context {
Self {
history,
history_index: history.len(),
}
Expand Down
6 changes: 1 addition & 5 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ impl LineBuffer {
let max = self.buf.capacity();
if self.must_truncate(buf.len()) {
self.insert_str(0, &buf[..max], cl);
if pos > max {
self.pos = max;
} else {
self.pos = pos;
}
self.pos = max.min(pos);
} else {
self.insert_str(0, buf, cl);
self.pos = pos;
Expand Down
2 changes: 1 addition & 1 deletion src/tty/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Term for DummyTerminal {
_enable_signals: bool,
) -> Result<DummyTerminal> {
Ok(DummyTerminal {
keys: Vec::new(),
keys: vec![],
cursor: 0,
color_mode,
bell_style,
Expand Down
6 changes: 3 additions & 3 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl Term for Console {
bell_style: BellStyle,
_enable_bracketed_paste: bool,
_enable_signals: bool,
) -> Result<Console> {
) -> Result<Self> {
let (conin, conout, close_on_drop) = if behavior == Behavior::PreferTerm {
if let (Ok(conin), Ok(conout)) = (
OpenOptions::new().read(true).write(true).open("CONIN$"),
Expand Down Expand Up @@ -706,7 +706,7 @@ impl Term for Console {
Err(_) => false,
};

Ok(Console {
Ok(Self {
conin_isatty,
conin: conin.unwrap_or(ptr::null_mut()),
conout_isatty,
Expand Down Expand Up @@ -904,7 +904,7 @@ impl super::ExternalPrinter for ExternalPrinter {
fn print(&mut self, msg: String) -> Result<()> {
// write directly to stdout/stderr while not in raw mode
if !self.raw_mode.load(Ordering::SeqCst) {
let mut utf16 = Vec::new();
let mut utf16 = vec![];
write_to_console(self.conout, msg.as_str(), &mut utf16)
} else {
self.sender
Expand Down
Loading

0 comments on commit 522b644

Please sign in to comment.