aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUbuntu <github@matdoes.dev>2022-08-22 19:27:23 +0000
committerUbuntu <github@matdoes.dev>2022-08-22 19:27:23 +0000
commitd818bce866ff9b073b8834d1d02e903e6e8a924d (patch)
treed56a8749f0574131d8bd545835682037b5de3803
parent2fff0e7564b8ed46c4f130207692703d4dad0cd0 (diff)
downloadazalea-drasl-d818bce866ff9b073b8834d1d02e903e6e8a924d.tar.xz
add suggestions in azalea-brigadier
-rw-r--r--azalea-brigadier/src/suggestion/mod.rs47
-rw-r--r--azalea-brigadier/src/suggestion/suggestions.rs38
2 files changed, 85 insertions, 0 deletions
diff --git a/azalea-brigadier/src/suggestion/mod.rs b/azalea-brigadier/src/suggestion/mod.rs
new file mode 100644
index 00000000..ab3a5964
--- /dev/null
+++ b/azalea-brigadier/src/suggestion/mod.rs
@@ -0,0 +1,47 @@
+mod suggestions;
+
+use crate::{context::StringRange, message::Message};
+
+#[derive(Debug, Clone, Hash, Eq, PartialEq)]
+pub struct Suggestion {
+ pub range: StringRange,
+ pub text: String,
+ pub tooltip: Option<Message>,
+}
+
+impl Suggestion {
+ pub fn apply(&self, input: &str) -> String {
+ if self.range.start() == 0 && self.range.end() == input.len() {
+ return input.to_string();
+ }
+ let mut result = String::with_capacity(self.text.len());
+ if self.range.start() > 0 {
+ result.push_str(&input[0..self.range.start()]);
+ }
+ result.push_str(&self.text);
+ if self.range.end() < input.len() {
+ result.push_str(&input[self.range.end()..])
+ }
+
+ result
+ }
+
+ pub fn expand(&self, command: &str, range: &StringRange) -> Suggestion {
+ if range == &self.range {
+ return self.clone();
+ }
+ let mut result = String::new();
+ if range.start() < self.range.start() {
+ result.push_str(&command[range.start()..self.range.start()]);
+ }
+ result.push_str(&self.text);
+ if range.end() > self.range.end() {
+ result.push_str(&command[self.range.end()..range.end()]);
+ }
+ Suggestion {
+ range: range.clone(),
+ text: result,
+ tooltip: self.tooltip.clone(),
+ }
+ }
+}
diff --git a/azalea-brigadier/src/suggestion/suggestions.rs b/azalea-brigadier/src/suggestion/suggestions.rs
new file mode 100644
index 00000000..6c325039
--- /dev/null
+++ b/azalea-brigadier/src/suggestion/suggestions.rs
@@ -0,0 +1,38 @@
+use super::Suggestion;
+use crate::context::StringRange;
+use std::collections::HashSet;
+
+#[derive(Debug, Clone, Default, Eq, PartialEq, Hash)]
+pub struct Suggestions {
+ pub range: StringRange,
+ pub suggestions: Vec<Suggestion>,
+}
+
+impl Suggestions {
+ pub fn merge(command: &str, input: &[Suggestions]) -> Self {
+ if input.is_empty() {
+ return Suggestions::default();
+ } else if input.len() == 1 {
+ return input[0];
+ };
+
+ let texts = HashSet::new();
+ for suggestions in input {
+ texts.extend(suggestions.suggestions);
+ }
+
+ Suggestions::create(command, texts)
+ }
+
+ pub fn create(command: &str, suggestions: &[Suggestions]) {
+ if suggestions.is_empty() {
+ return Suggestions::default();
+ };
+ let mut start = usize::MAX;
+ let mut end = usize::MIN;
+ for suggestion in suggestions {
+ start = suggestion.range.start().min(start);
+ end = suggestion.range.end().max(end);
+ }
+ }
+}