summary refs log tree commit diff stats
path: root/src/suggestion.rs
blob: f8f4741aa0c84dfa576505e266667b6bcf5babd5 (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2021 Soni L.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// Documentation and comments licensed under CC BY-SA 4.0.

//! Suggestion machinery.

use ::std::future::Future;
use ::std::ops::Range;
use ::std::pin::Pin;

/// A suggested editing operation.
///
/// # Examples
///
/// ```rust
/// use ::iosonism::suggestion::Suggestion;
///
/// let s = Suggestion::new(3..3, "home".into());
/// assert_eq!(s.apply("Go ".into()), "Go home");
/// ```
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Suggestion {
    range: Range<usize>,
    text: String,
}

impl Suggestion {
    /// Creates a new `Suggestion` for `text` for the given `range`.
    pub fn new(range: Range<usize>, text: String) -> Self {
        Self {
            range: range,
            text: text,
        }
    }

    /// Returns the range associated with this suggestion.
    pub fn get_range(&self) -> Range<usize> {
        self.range.clone()
    }

    /// Returns the replacement text associated with this suggestion.
    pub fn get_text(&self) -> &str {
        &self.text
    }

    /// Applies this suggestion on the `input`.
    ///
    /// # Panics
    ///
    /// Panics if the range is outside the `input`'s bounds, or not on an UTF-8
    /// boundary.
    pub fn apply(&self, mut input: String) -> String {
        // the use of String here actually has a performance reason:
        // either you already have a String, in which case this is fast,
        // or you need a String anyway, in which case it's your responsibility
        // to make your &str into one.
        input.replace_range(self.range.clone(), &self.text);
        input
    }

    /// Expands this suggestion to cover the whole given range.
    ///
    /// The text needed to cover the range will be taken from the given input.
    ///
    /// # Panics
    ///
    /// Panics if any range is not on an UTF-8 boundary. Panics if the given
    /// range doesn't cover this suggestion's range, or is outside the input's
    /// bounds.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```rust
    /// use ::iosonism::suggestion::Suggestion;
    ///
    /// let mut s = Suggestion::new(7..7, "orld!".into());
    /// s.expand("hello w", 6..7);
    /// assert_eq!(s, Suggestion::new(6..7, "world!".into()));
    /// ```
    pub fn expand(&mut self, input: &str, range: Range<usize>) {
        let input = &input[range.clone()];
        self.text.insert_str(0, &input[..(self.range.start-range.start)]);
        self.text.push_str(&input[(self.range.end-range.start)..]);
        self.range = range;
    }
}

/// Editing suggestions.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Suggestions {
    // brigadier also supports "tooltips". FIXME revisit this.
    range: Range<usize>,
    suggestions: Vec<Suggestion>,
}

/// [private] An empty `Suggestions`.
const EMPTY: Suggestions = Suggestions { range: 0..0, suggestions: Vec::new() };

impl Suggestions {
    /// Returns the range these suggestions apply to.
    pub fn get_range(&self) -> Range<usize> {
        self.range.clone()
    }

    /// Returns the suggestions.
    pub fn get_list(&self) -> &[Suggestion] {
        &self.suggestions
    }

    /// Returns the suggestions.
    pub fn take_list(self) -> Vec<Suggestion> {
        self.suggestions
    }

    /// Returns `true` if there are no suggestions.
    pub fn is_empty(&self) -> bool {
        self.suggestions.is_empty()
    }

    /// Merges together multiple `Suggestions` for the command.
    pub fn merge<I: IntoIterator<Item=Suggestions>>(
        command: &str,
        input: I,
    ) -> Self {
        // check if empty or single-element
        let mut it = input.into_iter();
        let first = it.next();
        if first.is_none() {
            return EMPTY
        }
        let first = first.unwrap();
        let second = it.next();
        if second.is_none() {
            return first
        }
        let second = second.unwrap();

        // combine everything back together and hope for the best
        Self::create(command, first.take_list().into_iter().chain({
            second.take_list()
        }).chain(it.flat_map(|e| e.take_list())))
    }

    /// Creates a `Suggestions` from the individual suggestions for the command.
    pub fn create<I: IntoIterator<Item=Suggestion>>(
        command: &str,
        suggestions: I,
    ) -> Self {
        let mut suggestions: Vec<_> = suggestions.into_iter().collect();
        if suggestions.is_empty() {
            return EMPTY
        }

        // find the largest range that spans all the suggestions.
        let mut start = usize::MAX;
        let mut end = 0;
        for suggestion in suggestions.iter() {
            start = ::std::cmp::min(suggestion.get_range().start, start);
            end = ::std::cmp::max(suggestion.get_range().end, end);
        }
        let range = start..end;

        // expand all suggestions.
        for suggestion in suggestions.iter_mut() {
            // cloning a range is basically free.
            suggestion.expand(command, range.clone());
        }

        // brigadier uses a HashSet first for deduplication. we currently use
        // a case-sensitive sort and then do the deduplication. we can probably
        // do something better here, and maybe even be language aware?
        // FIXME investigate
        // note that by the time we're here, all suggestions have been expanded,
        // so the ranges are all the same (and no longer relevant).
        suggestions.sort_unstable_by(|a, b| {
            ::std::cmp::Ord::cmp(a.get_text(), b.get_text())
        });
        suggestions.dedup();

        Self {
            range: range,
            suggestions: suggestions,
        }
    }

    /// Creates an empty `Suggestions` future.
    pub fn empty<'a>() -> Pin<Box<dyn Future<Output=Suggestions> + Send + 'a>> {
        Box::pin(async { EMPTY })
    }
}

/// Helper for building suggestions.
pub struct SuggestionsBuilder<'a> {
    // brigadier throws an inputLowerCase/remainingLowerCase in here.
    // given we're using 'a, it feels *weird* to use String with it.
    // so for now we don't bother. FIXME revisit this?
    // also note that brigadier exists for minecraft, whereas we're porting it
    // for use with a debugger. we can assume our users are familiar with and/or
    // prefer case-sensitivity.
    input: &'a str,
    start: usize,
    remaining: &'a str,
    result: Vec<Suggestion>,
}

impl<'a> SuggestionsBuilder<'a> {
    /// Creates a new `SuggestionsBuilder`.
    pub fn new(input: &'a str, start: usize) -> Self {
        Self {
            input: input,
            start: start,
            remaining: &input[start..],
            result: Vec::new(),
        }
    }

    /// Returns the full input.
    pub fn get_input(&self) -> &'a str {
        self.input
    }

    /// Returns the start position.
    pub fn get_start(&self) -> usize {
        self.start
    }

    /// Returns the remaining input.
    pub fn get_remaining(&self) -> &'a str {
        self.remaining
    }

    /// Builds the final `Suggestions`, draining this builder.
    pub fn drain_build(&mut self) -> Suggestions {
        Suggestions::create(self.input, ::std::mem::take(&mut self.result))
    }

    /// Builds the final `Suggestions`, as a `Future`, draining this builder.
    pub fn drain_build_future(
        &mut self,
    ) -> Pin<Box<dyn Future<Output=Suggestions> + Send + 'a>> {
        let input = self.input;
        let result = ::std::mem::take(&mut self.result);
        Box::pin(async move {
            Suggestions::create(input, result)
        })
    }

    /// Adds a new suggestion.
    pub fn suggest(&mut self, text: String) -> &mut Self {
        if text != self.remaining {
            self.result.push({
                Suggestion::new(self.start..self.input.len(), text)
            });
        }
        self
    }

    /// Adds all the suggestions from the given SuggestionBuilder, draining it.
    pub fn add_drain(&mut self, other: &mut SuggestionsBuilder) -> &mut Self {
        self.result.extend(std::mem::take(&mut other.result));
        self
    }

    /// Creates a *new* `SuggestionsBuilder`, with no suggestions, starting at
    /// the given position.
    pub fn starting_from(&self, start: usize) -> Self {
        Self::new(self.input, start)
    }

    /// Creates a *new* `SuggestionsBuilder`, with no suggestions, starting from
    /// the same position as this one.
    pub fn restart(&self) -> Self {
        self.starting_from(self.start)
    }
}