Skip to content

Commit

Permalink
Merge pull request #556 from gwenn/complete_hint
Browse files Browse the repository at this point in the history
Fix complete hint
  • Loading branch information
gwenn authored Aug 4, 2021
2 parents 2da002d + dbf7fd0 commit 2626e72
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/diy_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Hinter for DIYHinter {
type Hint = CommandHint;

fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<CommandHint> {
if pos < line.len() {
if line.is_empty() || pos < line.len() {
return None;
}

Expand All @@ -61,7 +61,7 @@ impl Hinter for DIYHinter {
.filter_map(|hint| {
// expect hint after word complete, like redis cli, add condition:
// line.ends_with(" ")
if pos > 0 && hint.display.starts_with(&line[..pos]) {
if hint.display.starts_with(line) {
Some(hint.suffix(pos))
} else {
None
Expand Down
7 changes: 5 additions & 2 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
pub fn hint(&mut self) {
if let Some(hinter) = self.helper {
let hint = hinter.hint(self.line.as_str(), self.line.pos(), &self.ctx);
self.hint = hint.map(|val| Box::new(val) as Box<dyn Hint>)
self.hint = match hint {
Some(val) if !val.display().is_empty() => Some(Box::new(val) as Box<dyn Hint>),
_ => None,
};
} else {
self.hint = None
self.hint = None;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Hinter for HistoryHinter {
type Hint = String;

fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
if pos < line.len() {
if line.is_empty() || pos < line.len() {
return None;
}
let start = if ctx.history_index() == ctx.history().len() {
Expand All @@ -66,9 +66,9 @@ impl Hinter for HistoryHinter {
};
if let Some(sr) = ctx
.history
.starts_with(&line[..pos], start, SearchDirection::Reverse)
.starts_with(line, start, SearchDirection::Reverse)
{
if sr.entry == line || sr.entry == &line[..pos] {
if sr.entry == line {
return None;
}
return Some(sr.entry[pos..].to_owned());
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ fn complete_hint_line<H: Helper>(s: &mut State<'_, '_, H>) -> Result<()> {
} else {
s.out.beep()?;
}
s.refresh_line_with_msg(None)?;
Ok(())
s.refresh_line()
}

fn page_completions<C: Candidate, H: Helper>(
Expand Down

0 comments on commit 2626e72

Please sign in to comment.