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

Support completion candidates that are shorter than the input #750

Merged
merged 1 commit into from
Dec 5, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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