Skip to content

Commit

Permalink
Support completion candidates that are shorter than the input
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Dec 2, 2023
1 parent 3f3538d commit 5f8bec6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn complete_line<H: Helper>(
} else if CompletionType::List == config.completion_type() {
if let Some(lcp) = longest_common_prefix(&candidates) {
// if we can extend the item, extend it
if lcp.len() > s.line.pos() - start {
if lcp.len() > s.line.pos() - start || candidates.len() == 1 {
completer.update(&mut s.line, start, lcp, &mut s.changes);
s.refresh_line()?;
}
Expand Down
34 changes: 31 additions & 3 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::vec::IntoIter;

use crate::completion::Completer;
use crate::config::{Config, EditMode};
use crate::config::{CompletionType, Config, EditMode};
use crate::edit::init_state;
use crate::highlight::Highlighter;
use crate::hint::Hinter;
Expand Down Expand Up @@ -35,7 +35,16 @@ impl Completer for SimpleCompleter {
_pos: usize,
_ctx: &Context<'_>,
) -> Result<(usize, Vec<String>)> {
Ok((0, vec![line.to_owned() + "t"]))
Ok((
0,
if line == "rus" {
vec![line.to_owned() + "t"]
} else if line == "\\hbar" {
vec!["ℏ".to_owned()]
} else {
vec![]
},
))
}
}
impl Hinter for SimpleCompleter {
Expand All @@ -61,7 +70,7 @@ fn complete_line() {
let mut input_state = InputState::new(&config, &bindings);
let keys = vec![E::ENTER];
let mut rdr: IntoIter<KeyEvent> = keys.into_iter();
let cmd = super::complete_line(&mut rdr, &mut s, &mut input_state, &Config::default()).unwrap();
let cmd = super::complete_line(&mut rdr, &mut s, &mut input_state, &config).unwrap();
assert_eq!(
Some(Cmd::AcceptOrInsertLine {
accept_in_the_middle: true
Expand All @@ -72,6 +81,25 @@ fn complete_line() {
assert_eq!(4, s.line.pos());
}

#[test]
fn complete_symbol() {
let mut out = Sink::default();
let history = crate::history::DefaultHistory::new();
let helper = Some(SimpleCompleter);
let mut s = init_state(&mut out, "\\hbar", 5, helper.as_ref(), &history);
let config = Config::builder()
.completion_type(CompletionType::List)
.build();
let bindings = Bindings::new();
let mut input_state = InputState::new(&config, &bindings);
let keys = vec![E::ENTER];
let mut rdr: IntoIter<KeyEvent> = keys.into_iter();
let cmd = super::complete_line(&mut rdr, &mut s, &mut input_state, &config).unwrap();
assert_eq!(None, cmd);
assert_eq!("ℏ", s.line.as_str());
assert_eq!(3, s.line.pos());
}

// `keys`: keys to press
// `expected_line`: line after enter key
fn assert_line(mode: EditMode, keys: &[KeyEvent], expected_line: &str) {
Expand Down

0 comments on commit 5f8bec6

Please sign in to comment.