mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
add suggestions in azalea-brigadier
This commit is contained in:
parent
2fff0e7564
commit
d818bce866
2 changed files with 85 additions and 0 deletions
47
azalea-brigadier/src/suggestion/mod.rs
Normal file
47
azalea-brigadier/src/suggestion/mod.rs
Normal file
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
38
azalea-brigadier/src/suggestion/suggestions.rs
Normal file
38
azalea-brigadier/src/suggestion/suggestions.rs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue