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

fix(repl): Accept tab when previous character is space #14898

Merged
merged 3 commits into from
Jun 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions cli/tests/integration/repl_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,3 +804,25 @@ fn pty_clear_function() {
assert!(output.contains("3234"));
});
}

#[test]
fn pty_tab_handler() {
// If the last character is **not** whitespace, we show the completions
util::with_pty(&["repl"], |mut console| {
console.write_line("a\t\t");
console.write_line("close();");
let output = console.read_all_output();
assert!(output.contains("addEventListener"));
assert!(output.contains("alert"));
assert!(output.contains("atob"));
});
// If the last character is whitespace, we just insert a tab
util::with_pty(&["repl"], |mut console| {
console.write_line("a \t\t"); // last character is whitespace
console.write_line("close();");
let output = console.read_all_output();
assert!(!output.contains("addEventListener"));
assert!(!output.contains("alert"));
assert!(!output.contains("atob"));
});
}
35 changes: 35 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,33 @@ impl ReplEditor {
Ok(())
}
}

/// A custom tab key event handler
/// It uses a heuristic to determine if the user is requesting completion or if they want to insert an actual tab
/// The heuristic goes like this:
/// - If the last character before the cursor is whitespace, the the user wants to insert a tab
/// - Else the user is requesting completion
struct TabEventHandler;
impl ConditionalEventHandler for TabEventHandler {
Comment on lines +405 to +406
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment about the purpose of this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

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()]
Copy link
Member

Choose a reason for hiding this comment

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

To save other reviewers time, ctx.pos() is indeed byte position https://docs.rs/rustyline/9.1.2/rustyline/struct.EventContext.html

.chars()
.rev()
.next()
.filter(|c| c.is_whitespace())
.is_some()
{
Some(Cmd::Insert(n, "\t".into()))
} else {
None // default complete
}
}
}