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

adding is_empty flag for prevent panic #145

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions src/components/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ALL_RESERVED_WORDS: &[&str] = &[
pub struct CompletionComponent {
key_config: KeyConfig,
state: ListState,
is_empty: bool,
word: String,
candidates: Vec<String>,
}
Expand All @@ -37,13 +38,14 @@ impl CompletionComponent {
.map(|w| w.to_string())
.collect()
},
is_empty: true,
}
}

pub fn update(&mut self, word: impl Into<String>) {
self.word = word.into();
self.state.select(None);
self.state.select(Some(0))
self.state.select(Some(0));
self.is_empty = true;
}

fn next(&mut self) {
Expand Down Expand Up @@ -110,7 +112,10 @@ impl MovableComponent for CompletionComponent {
.map(|c| ListItem::new(c.to_string()))
.collect::<Vec<ListItem>>();
if candidates.clone().is_empty() {
self.is_empty = true;
return Ok(());
} else {
self.is_empty = false;
}
let candidate_list = List::new(candidates.clone())
.block(Block::default().borders(Borders::ALL))
Expand All @@ -137,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 {
if key == self.key_config.move_down && !self.is_empty {
self.next();
return Ok(EventState::Consumed);
} else if key == self.key_config.move_up {
} else if key == self.key_config.move_up && !self.is_empty {
self.previous();
return Ok(EventState::Consumed);
}
Expand Down