Skip to content

Commit

Permalink
User completion no longer shows highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan C committed Jul 6, 2023
1 parent 1820448 commit b4c380a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
11 changes: 10 additions & 1 deletion src/widget/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,16 @@ where
}
Event::Tab => {
state.completion.tab();
None
if let Some(entry) = state.completion.select() {
if entry.is_user() {
state.input = entry.complete_input(&state.input);
Some(self.on_completion.clone())
} else {
None
}
} else {
None
}
}
Event::Up => {
state.completion.reset();
Expand Down
43 changes: 29 additions & 14 deletions src/widget/input/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,36 +219,31 @@ impl Completion {
}
}
// Command fully typed & already selected, do nothing
Selection::SelectedCommand(_) => {}
Selection::SelectedCommand(_) | Selection::SelectingUser(_) => {}
}
}

/// If the trailing word starts with an @ we want to populate applicable user completions
fn process_users(&mut self, input: &str, users: &[User]) {
let (_, rest) = input.rsplit_once(' ').unwrap_or(("", input));

// Only show user completions if using @
let Some((_, nick)) = rest.split_once('@') else {
self.reset();
return;
};

match self.selection {
Selection::None | Selection::Highlighted(_) => {
self.selection = Selection::None;
Selection::None | Selection::SelectingUser(_) => {
self.selection = Selection::SelectingUser(0);
self.filtered_entries = users
.iter()
.filter_map(|user| {
let nickname = user.nickname();
nickname
.as_ref()
.starts_with(nick)
.starts_with(rest)
.then(|| nickname.to_string())
})
.map(Entry::User)
.collect();
}
Selection::SelectedCommand(_) => {}
// No highlighting for user completion
Selection::SelectedCommand(_) | Selection::Highlighted(_) => {}
}
}

Expand All @@ -263,15 +258,20 @@ impl Completion {

pub fn is_selecting(&self) -> bool {
match self.selection {
Selection::None | Selection::Highlighted(_) => !self.filtered_entries.is_empty(),
Selection::None | Selection::Highlighted(_) | Selection::SelectingUser(_) => {
!self.filtered_entries.is_empty()
}
Selection::SelectedCommand(_) => false,
}
}

fn is_active(&self) -> bool {
match self.selection {
Selection::None | Selection::Highlighted(_) => !self.filtered_entries.is_empty(),
Selection::None | Selection::Highlighted(_) => {
!self.filtered_entries.is_empty()
}
Selection::SelectedCommand(_) => true,
Selection::SelectingUser(_) => false
}
}

Expand All @@ -280,6 +280,13 @@ impl Completion {
Selection::None => {
self.filtered_entries = vec![];
}
// When selecting a user, don't clear out the filtered entries so we can continue to
// tab through the available options
Selection::SelectingUser(index) => {
if let Some(entry) = self.filtered_entries.get(index).cloned() {
return Some(entry);
}
}
Selection::Highlighted(index) => {
if let Some(entry) = self.filtered_entries.get(index).cloned() {
self.filtered_entries = vec![];
Expand All @@ -297,7 +304,9 @@ impl Completion {
}

pub fn tab(&mut self) {
if let Selection::Highlighted(index) = &mut self.selection {
if let &mut Selection::Highlighted(ref mut index)
| &mut Selection::SelectingUser(ref mut index) = &mut self.selection
{
*index = (*index + 1) % self.filtered_entries.len();
} else if matches!(self.selection, Selection::None) {
self.selection = Selection::Highlighted(0);
Expand Down Expand Up @@ -349,6 +358,7 @@ impl Completion {
)
}
Selection::SelectedCommand(command) => Some(command.view(input)),
Selection::SelectingUser(_) => None,
}
} else {
None
Expand All @@ -360,6 +370,7 @@ impl Completion {
enum Selection {
None,
Highlighted(usize),
SelectingUser(usize),
SelectedCommand(Command),
}

Expand Down Expand Up @@ -389,6 +400,10 @@ impl Entry {
},
}
}

pub fn is_user(&self) -> bool {
matches!(self, Self::User(_))
}
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit b4c380a

Please sign in to comment.