diff options
| author | mat <git@matdoes.dev> | 2023-10-01 19:12:27 -0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2023-10-01 19:12:27 -0500 |
| commit | 2606de9f4c1563c10a616925a87905ea1ffd2298 (patch) | |
| tree | 0bfe20e49c6b2ffc22076b3e63e26a0afb08c27a /azalea-brigadier/src | |
| parent | 8b65d7c95e9bc4290ff1a6f5ae1de6fc71ef03d4 (diff) | |
| download | azalea-drasl-2606de9f4c1563c10a616925a87905ea1ffd2298.tar.xz | |
start adding brigadier suggestions
Diffstat (limited to 'azalea-brigadier/src')
| -rwxr-xr-x | azalea-brigadier/src/suggestion/mod.rs | 4 | ||||
| -rwxr-xr-x | azalea-brigadier/src/suggestion/suggestions_builder.rs | 92 |
2 files changed, 95 insertions, 1 deletions
diff --git a/azalea-brigadier/src/suggestion/mod.rs b/azalea-brigadier/src/suggestion/mod.rs index 592c674c..22257423 100755 --- a/azalea-brigadier/src/suggestion/mod.rs +++ b/azalea-brigadier/src/suggestion/mod.rs @@ -1,4 +1,5 @@ mod suggestions; + mod suggestions_builder; use crate::context::StringRange; #[cfg(feature = "azalea-buf")] @@ -7,7 +8,8 @@ use azalea_buf::McBufWritable; use azalea_chat::FormattedText; #[cfg(feature = "azalea-buf")] use std::io::Write; -pub use suggestions::*; +pub use suggestions::Suggestions; +pub use suggestions_builder::SuggestionsBuilder; /// A suggestion given to the user for what they might want to type next. /// diff --git a/azalea-brigadier/src/suggestion/suggestions_builder.rs b/azalea-brigadier/src/suggestion/suggestions_builder.rs new file mode 100755 index 00000000..66f17fb1 --- /dev/null +++ b/azalea-brigadier/src/suggestion/suggestions_builder.rs @@ -0,0 +1,92 @@ +use std::collections::HashSet; + +use crate::context::StringRange; + +use super::{Suggestion, Suggestions}; + +pub struct SuggestionsBuilder { + input: String, + input_lowercase: String, + start: usize, + remaining: String, + remaining_lowercase: String, + result: HashSet<Suggestion>, +} + +impl SuggestionsBuilder { + pub fn new(input: &str, start: usize) -> Self { + Self::new_with_lowercase(input, input.to_lowercase().as_str(), start) + } + + pub fn new_with_lowercase(input: &str, input_lowercase: &str, start: usize) -> Self { + Self { + start, + input: input.to_string(), + input_lowercase: input_lowercase.to_string(), + remaining: input[start..].to_string(), + remaining_lowercase: input_lowercase[start..].to_string(), + result: HashSet::new(), + } + } + + pub fn input(&self) -> &str { + &self.input + } + + pub fn start(&self) -> usize { + self.start + } + + pub fn remianing(&self) -> &str { + &self.remaining + } + + pub fn remaining_lowercase(&self) -> &str { + &self.remaining_lowercase + } + + pub fn build(&self) -> Suggestions { + Suggestions::create(&self.input, &self.result) + } + + pub fn suggest(mut self, text: &str) -> Self { + if text == self.remaining { + return self; + } + self.result.insert(Suggestion { + range: StringRange::between(self.start, self.input.len()), + text: text.to_string(), + tooltip: None, + }); + self + } + + pub fn suggest_with_tooltip(mut self, text: &str, tooltip: String) -> Self { + if text == self.remaining { + return self; + } + self.result.insert(Suggestion { + range: StringRange::between(self.start, self.input.len()), + text: text.to_string(), + tooltip: Some(tooltip), + }); + self + } + + // TODO: integer suggestions + // https://github.com/Mojang/brigadier/blob/master/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java#L74 + + #[allow(clippy::should_implement_trait)] + pub fn add(mut self, other: SuggestionsBuilder) -> Self { + self.result.extend(other.result); + self + } + + pub fn create_offset(&self, start: usize) -> Self { + SuggestionsBuilder::new_with_lowercase(&self.input, &self.input_lowercase, start) + } + + pub fn restart(self) -> Self { + self.create_offset(self.start) + } +} |
