diff options
| author | mat <git@matdoes.dev> | 2023-10-12 20:14:29 -0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2023-10-12 20:14:29 -0500 |
| commit | 38db231ea8fa0fb223e16637db0b6ec65b2b81ef (patch) | |
| tree | f8f61b04ad0b498b85bffb133c51af1670a33ddf /azalea-brigadier/src/suggestion/suggestions.rs | |
| parent | d5f424b8c2fba9b3283aef36fe9e1e051636614c (diff) | |
| download | azalea-drasl-38db231ea8fa0fb223e16637db0b6ec65b2b81ef.tar.xz | |
brigadier usages
Diffstat (limited to 'azalea-brigadier/src/suggestion/suggestions.rs')
| -rwxr-xr-x | azalea-brigadier/src/suggestion/suggestions.rs | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/azalea-brigadier/src/suggestion/suggestions.rs b/azalea-brigadier/src/suggestion/suggestions.rs index 2a8b5e9e..69877786 100755 --- a/azalea-brigadier/src/suggestion/suggestions.rs +++ b/azalea-brigadier/src/suggestion/suggestions.rs @@ -1,6 +1,8 @@ use super::Suggestion; use crate::context::StringRange; #[cfg(feature = "azalea-buf")] +use crate::suggestion::SuggestionValue; +#[cfg(feature = "azalea-buf")] use azalea_buf::{ BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable, }; @@ -11,12 +13,19 @@ use std::io::{Cursor, Write}; use std::{collections::HashSet, hash::Hash}; #[derive(Debug, Clone, Eq, PartialEq, Hash)] -pub struct Suggestions<M = String> { - pub range: StringRange, - pub suggestions: Vec<Suggestion<M>>, +pub struct Suggestions<M> +where + M: Clone + PartialEq + Hash, +{ + range: StringRange, + suggestions: Vec<Suggestion<M>>, } impl<M: Clone + Eq + Hash> Suggestions<M> { + pub fn new(range: StringRange, suggestions: Vec<Suggestion<M>>) -> Self { + Self { range, suggestions } + } + pub fn merge(command: &str, input: &[Suggestions<M>]) -> Self { if input.is_empty() { return Suggestions::default(); @@ -45,20 +54,34 @@ impl<M: Clone + Eq + Hash> Suggestions<M> { let range = StringRange::new(start, end); let mut texts = HashSet::new(); for suggestion in suggestions { - texts.insert(suggestion.expand(command, &range)); + texts.insert(suggestion.expand(command, range)); } let mut sorted = texts.into_iter().collect::<Vec<_>>(); - sorted.sort_by(|a, b| a.text.cmp(&b.text)); + + sorted.sort_by(|a, b| a.value.cmp_ignore_case(&b.value)); + Suggestions { range, suggestions: sorted, } } + + pub fn is_empty(&self) -> bool { + self.suggestions.is_empty() + } + + pub fn list(&self) -> &[Suggestion<M>] { + &self.suggestions + } + + pub fn range(&self) -> StringRange { + self.range + } } // this can't be derived because that'd require the generic to have `Default` // too even if it's not actually necessary -impl<M> Default for Suggestions<M> { +impl<M: Clone + Hash + Eq> Default for Suggestions<M> { fn default() -> Self { Self { range: StringRange::default(), @@ -85,12 +108,12 @@ impl McBufReadable for Suggestions<FormattedText> { let mut suggestions = Vec::<StandaloneSuggestion>::read_from(buf)? .into_iter() .map(|s| Suggestion { - text: s.text, + value: SuggestionValue::Text(s.text), tooltip: s.tooltip, range: range.clone(), }) .collect::<Vec<_>>(); - suggestions.sort_by(|a, b| a.text.cmp(&b.text)); + suggestions.sort_by(|a, b| a.value.cmp(&b.value)); Ok(Suggestions { range, suggestions }) } |
