azalea_brigadier/suggestion/
suggestions_builder.rs

1use std::collections::HashSet;
2
3use super::{Suggestion, SuggestionValue, Suggestions};
4use crate::context::StringRange;
5
6#[derive(PartialEq, Debug)]
7pub struct SuggestionsBuilder {
8    input: String,
9    input_lowercase: String,
10    start: usize,
11    remaining: String,
12    remaining_lowercase: String,
13    result: HashSet<Suggestion>,
14}
15
16impl SuggestionsBuilder {
17    pub fn new(input: &str, start: usize) -> Self {
18        Self::new_with_lowercase(input, input.to_lowercase().as_str(), start)
19    }
20
21    pub fn new_with_lowercase(input: &str, input_lowercase: &str, start: usize) -> Self {
22        Self {
23            start,
24            input: input.to_string(),
25            input_lowercase: input_lowercase.to_string(),
26            remaining: input[start..].to_string(),
27            remaining_lowercase: input_lowercase[start..].to_string(),
28            result: HashSet::new(),
29        }
30    }
31}
32
33impl SuggestionsBuilder {
34    pub fn input(&self) -> &str {
35        &self.input
36    }
37
38    pub fn start(&self) -> usize {
39        self.start
40    }
41
42    pub fn remaining(&self) -> &str {
43        &self.remaining
44    }
45
46    pub fn remaining_lowercase(&self) -> &str {
47        &self.remaining_lowercase
48    }
49
50    pub fn build(&self) -> Suggestions {
51        Suggestions::create(&self.input, &self.result)
52    }
53
54    pub fn suggest(mut self, text: &str) -> Self {
55        if text == self.remaining {
56            return self;
57        }
58        self.result.insert(Suggestion {
59            range: StringRange::between(self.start, self.input.len()),
60            value: SuggestionValue::Text(text.to_string()),
61            tooltip: None,
62        });
63        self
64    }
65
66    pub fn suggest_with_tooltip(mut self, text: &str, tooltip: String) -> Self {
67        if text == self.remaining {
68            return self;
69        }
70        self.result.insert(Suggestion {
71            range: StringRange::between(self.start, self.input.len()),
72            value: SuggestionValue::Text(text.to_string()),
73            tooltip: Some(tooltip),
74        });
75        self
76    }
77
78    pub fn suggest_integer(mut self, value: i32) -> Self {
79        self.result.insert(Suggestion {
80            range: StringRange::between(self.start, self.input.len()),
81            value: SuggestionValue::Integer(value),
82            tooltip: None,
83        });
84        self
85    }
86
87    pub fn suggest_integer_with_tooltip(mut self, value: i32, tooltip: String) -> Self {
88        self.result.insert(Suggestion {
89            range: StringRange::between(self.start, self.input.len()),
90            value: SuggestionValue::Integer(value),
91            tooltip: Some(tooltip),
92        });
93        self
94    }
95
96    #[allow(clippy::should_implement_trait)]
97    pub fn add(mut self, other: SuggestionsBuilder) -> Self {
98        self.result.extend(other.result);
99        self
100    }
101
102    pub fn create_offset(&self, start: usize) -> SuggestionsBuilder {
103        SuggestionsBuilder::new_with_lowercase(&self.input, &self.input_lowercase, start)
104    }
105
106    pub fn restart(&self) -> SuggestionsBuilder {
107        self.create_offset(self.start)
108    }
109}