forked from kkawakam/rustyline
-
Notifications
You must be signed in to change notification settings - Fork 2
Reduce tokenizer/parser overhead #4
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| use std::borrow::Cow; | ||
|
|
||
| use rustyline::highlight::Highlighter; | ||
| use rustyline::validate::{ValidationContext, ValidationResult, Validator}; | ||
| use rustyline::{Cmd, Editor, EventHandler, Helper, KeyCode, KeyEvent, Modifiers, Result}; | ||
| use rustyline::{Completer, Hinter}; | ||
|
|
||
| #[derive(Completer, Hinter)] | ||
| struct InputValidator { | ||
| bracket_level: i32, | ||
| /// re-render only when input just changed | ||
| /// not render after cursor moving | ||
| need_render: bool, | ||
| } | ||
|
|
||
| impl Helper for InputValidator { | ||
| fn update_after_edit(&mut self, line: &str, _pos: usize, _forced_refresh: bool) { | ||
| self.bracket_level = line.chars().fold(0, |level, c| { | ||
| if c == '(' { | ||
| level + 1 | ||
| } else if c == ')' { | ||
| level - 1 | ||
| } else { | ||
| level | ||
| } | ||
| }); | ||
| self.need_render = true; | ||
| } | ||
| } | ||
|
|
||
| impl Validator for InputValidator { | ||
| fn validate(&mut self, _ctx: &mut ValidationContext) -> Result<ValidationResult> { | ||
| if self.bracket_level > 0 { | ||
| Ok(ValidationResult::Incomplete) | ||
| // Ok(ValidationResult::Incomplete(2)) | ||
| } else if self.bracket_level < 0 { | ||
| Ok(ValidationResult::Invalid(Some(format!( | ||
| " - excess {} close bracket", | ||
| -self.bracket_level | ||
| )))) | ||
| } else { | ||
| Ok(ValidationResult::Valid(None)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Highlighter for InputValidator { | ||
| fn highlight_char(&mut self, _line: &str, _pos: usize, _forced: bool) -> bool { | ||
| self.need_render | ||
| } | ||
| fn highlight<'l>(&mut self, line: &'l str, pos: usize) -> Cow<'l, str> { | ||
| self.need_render = false; | ||
| Cow::Borrowed(&line) | ||
| } | ||
| } | ||
|
|
||
| fn main() -> Result<()> { | ||
| let h = InputValidator { | ||
| bracket_level: 0, | ||
| need_render: true, | ||
| }; | ||
| let mut rl = Editor::new()?; | ||
| rl.set_helper(Some(h)); | ||
| rl.bind_sequence( | ||
| KeyEvent(KeyCode::Char('s'), Modifiers::CTRL), | ||
| EventHandler::Simple(Cmd::Newline), | ||
| ); | ||
|
|
||
| let input = rl.readline(">> ")?; | ||
| println!("Input: {input}"); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,7 +90,7 @@ pub trait Completer { | |
| /// | ||
| /// ("ls /usr/loc", 11) => Ok((3, vec!["/usr/local/"])) | ||
| fn complete( | ||
| &self, // FIXME should be `&mut self` | ||
| &mut self, // FIXME should be `&mut self` | ||
| line: &str, | ||
| pos: usize, | ||
| ctx: &Context<'_>, | ||
|
|
@@ -99,7 +99,7 @@ pub trait Completer { | |
| Ok((0, Vec::with_capacity(0))) | ||
| } | ||
| /// Updates the edited `line` with the `elected` candidate. | ||
| fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) { | ||
| fn update(&mut self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) { | ||
| let end = line.pos(); | ||
| line.replace(start..end, elected, cl); | ||
| } | ||
|
|
@@ -108,48 +108,48 @@ pub trait Completer { | |
| impl Completer for () { | ||
| type Candidate = String; | ||
|
|
||
| fn update(&self, _line: &mut LineBuffer, _start: usize, _elected: &str, _cl: &mut Changeset) { | ||
| fn update(&mut self, _line: &mut LineBuffer, _start: usize, _elected: &str, _cl: &mut Changeset) { | ||
| unreachable!(); | ||
| } | ||
| } | ||
|
|
||
| impl<'c, C: ?Sized + Completer> Completer for &'c C { | ||
| impl<'c, C: ?Sized + Completer> Completer for &'c mut C { | ||
| type Candidate = C::Candidate; | ||
|
|
||
| fn complete( | ||
| &self, | ||
| &mut self, | ||
| line: &str, | ||
| pos: usize, | ||
| ctx: &Context<'_>, | ||
| ) -> Result<(usize, Vec<Self::Candidate>)> { | ||
| (**self).complete(line, pos, ctx) | ||
| } | ||
|
|
||
| fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) { | ||
| fn update(&mut self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) { | ||
| (**self).update(line, start, elected, cl); | ||
| } | ||
| } | ||
| macro_rules! box_completer { | ||
| ($($id: ident)*) => { | ||
| $( | ||
| impl<C: ?Sized + Completer> Completer for $id<C> { | ||
| type Candidate = C::Candidate; | ||
|
|
||
| fn complete(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Result<(usize, Vec<Self::Candidate>)> { | ||
| (**self).complete(line, pos, ctx) | ||
| } | ||
| fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) { | ||
| (**self).update(line, start, elected, cl) | ||
| } | ||
| } | ||
| )* | ||
| } | ||
| } | ||
| // macro_rules! box_completer { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure ? |
||
| // ($($id: ident)*) => { | ||
| // $( | ||
| // impl<C: ?Sized + Completer> Completer for $id<C> { | ||
| // type Candidate = C::Candidate; | ||
|
|
||
| // fn complete(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Result<(usize, Vec<Self::Candidate>)> { | ||
| // (**self).complete(line, pos, ctx) | ||
| // } | ||
| // fn update(&self, line: &mut LineBuffer, start: usize, elected: &str, cl: &mut Changeset) { | ||
| // (**self).update(line, start, elected, cl) | ||
| // } | ||
| // } | ||
| // )* | ||
| // } | ||
| // } | ||
|
|
||
| use crate::undo::Changeset; | ||
| use std::rc::Rc; | ||
| use std::sync::Arc; | ||
| box_completer! { Box Rc Arc } | ||
| // use std::sync::Arc; | ||
| // box_completer! { Box Rc Arc } | ||
|
|
||
| /// A `Completer` for file and folder names. | ||
| pub struct FilenameCompleter { | ||
|
|
@@ -257,7 +257,7 @@ impl Default for FilenameCompleter { | |
| impl Completer for FilenameCompleter { | ||
| type Candidate = Pair; | ||
|
|
||
| fn complete(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Result<(usize, Vec<Pair>)> { | ||
| fn complete(&mut self, line: &str, pos: usize, _ctx: &Context<'_>) -> Result<(usize, Vec<Pair>)> { | ||
| self.complete_path(line, pos) | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FIXME not removed