Skip to content

Commit

Permalink
Tab when previous character is whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaSd committed Jun 17, 2022
1 parent 9682105 commit 78a49bb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cli/tools/repl/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rustyline::EventHandler;
use rustyline::KeyCode;
use rustyline::KeyEvent;
use rustyline::Modifiers;
use rustyline::{ConditionalEventHandler, Event, EventContext, RepeatCount};
use rustyline_derive::{Helper, Hinter};
use std::borrow::Cow;
use std::path::PathBuf;
Expand Down Expand Up @@ -369,6 +370,10 @@ impl ReplEditor {
KeyEvent(KeyCode::Char('s'), Modifiers::CTRL),
EventHandler::Simple(Cmd::Newline),
);
editor.bind_sequence(
KeyEvent::from('\t'),
EventHandler::Conditional(Box::new(TabEventHandler)),
);

ReplEditor {
inner: Arc::new(Mutex::new(editor)),
Expand All @@ -391,3 +396,28 @@ impl ReplEditor {
Ok(())
}
}

struct TabEventHandler;
impl ConditionalEventHandler for TabEventHandler {
fn handle(
&self,
evt: &Event,
n: RepeatCount,
_: bool,
ctx: &EventContext,
) -> Option<Cmd> {
debug_assert_eq!(*evt, Event::from(KeyEvent::from('\t')));
if ctx.line().is_empty()
|| ctx.line()[..ctx.pos()]
.chars()
.rev()
.next()
.filter(|c| c.is_whitespace())
.is_some()
{
Some(Cmd::Insert(n, "\t".into()))
} else {
None // default complete
}
}
}

0 comments on commit 78a49bb

Please sign in to comment.