|
|
|
@ -19,7 +19,7 @@ const ALL_RESERVED_WORDS: &[&str] = &[
|
|
|
|
|
pub struct CompletionComponent {
|
|
|
|
|
key_config: KeyConfig,
|
|
|
|
|
state: ListState,
|
|
|
|
|
offset: bool,
|
|
|
|
|
is_empty: bool,
|
|
|
|
|
word: String,
|
|
|
|
|
candidates: Vec<String>,
|
|
|
|
|
}
|
|
|
|
@ -38,14 +38,14 @@ impl CompletionComponent {
|
|
|
|
|
.map(|w| w.to_string())
|
|
|
|
|
.collect()
|
|
|
|
|
},
|
|
|
|
|
offset: false,
|
|
|
|
|
is_empty: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update(&mut self, word: impl Into<String>) {
|
|
|
|
|
self.word = word.into();
|
|
|
|
|
self.state.select(Some(0));
|
|
|
|
|
self.offset = false;
|
|
|
|
|
self.is_empty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) {
|
|
|
|
@ -112,8 +112,10 @@ impl MovableComponent for CompletionComponent {
|
|
|
|
|
.map(|c| ListItem::new(c.to_string()))
|
|
|
|
|
.collect::<Vec<ListItem>>();
|
|
|
|
|
if candidates.clone().is_empty() {
|
|
|
|
|
self.offset = false;
|
|
|
|
|
self.is_empty = true;
|
|
|
|
|
return Ok(());
|
|
|
|
|
} else {
|
|
|
|
|
self.is_empty = false;
|
|
|
|
|
}
|
|
|
|
|
let candidate_list = List::new(candidates.clone())
|
|
|
|
|
.block(Block::default().borders(Borders::ALL))
|
|
|
|
@ -129,8 +131,6 @@ impl MovableComponent for CompletionComponent {
|
|
|
|
|
(candidates.len().min(5) as u16 + 2)
|
|
|
|
|
.min(f.size().bottom().saturating_sub(area.y + y + 2)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.offset = true;
|
|
|
|
|
f.render_widget(Clear, area);
|
|
|
|
|
f.render_stateful_widget(candidate_list, area, &mut self.state);
|
|
|
|
|
}
|
|
|
|
@ -142,10 +142,10 @@ impl Component for CompletionComponent {
|
|
|
|
|
fn commands(&self, _out: &mut Vec<CommandInfo>) {}
|
|
|
|
|
|
|
|
|
|
fn event(&mut self, key: Key) -> Result<EventState> {
|
|
|
|
|
if key == self.key_config.move_down && self.offset {
|
|
|
|
|
if key == self.key_config.move_down && !self.is_empty {
|
|
|
|
|
self.next();
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
|
} else if key == self.key_config.move_up && self.offset {
|
|
|
|
|
} else if key == self.key_config.move_up && !self.is_empty {
|
|
|
|
|
self.previous();
|
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
|
}
|
|
|
|
|