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