reedline/completion/base.rs
1/// A span of source code, with positions in bytes
2#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
3pub struct Span {
4 /// The starting position of the span, in bytes
5 pub start: usize,
6
7 /// The ending position of the span, in bytes
8 pub end: usize,
9}
10
11impl Span {
12 /// Creates a new `Span` from start and end inputs.
13 /// The end parameter must be greater than or equal to the start parameter.
14 ///
15 /// # Panics
16 /// If `end < start`
17 pub fn new(start: usize, end: usize) -> Span {
18 assert!(
19 end >= start,
20 "Can't create a Span whose end < start, start={start}, end={end}"
21 );
22
23 Span { start, end }
24 }
25}
26
27/// A trait that defines how to convert a line and position to a list of potential completions in that position.
28pub trait Completer: Send {
29 /// the action that will take the line and position and convert it to a vector of completions, which include the
30 /// span to replace and the contents of that replacement
31 fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion>;
32
33 /// action that will return a partial section of available completions
34 /// this command comes handy when trying to avoid to pull all the data at once
35 /// from the completer
36 fn partial_complete(
37 &mut self,
38 line: &str,
39 pos: usize,
40 start: usize,
41 offset: usize,
42 ) -> Vec<Suggestion> {
43 self.complete(line, pos)
44 .into_iter()
45 .skip(start)
46 .take(offset)
47 .collect()
48 }
49
50 /// number of available completions
51 fn total_completions(&mut self, line: &str, pos: usize) -> usize {
52 self.complete(line, pos).len()
53 }
54}
55
56/// Suggestion returned by the Completer
57#[derive(Debug, Default, Clone, PartialEq, Eq)]
58pub struct Suggestion {
59 /// String replacement that will be introduced to the the buffer
60 pub value: String,
61 /// Optional description for the replacement
62 pub description: Option<String>,
63 /// Optional vector of strings in the suggestion. These can be used to
64 /// represent examples coming from a suggestion
65 pub extra: Option<Vec<String>>,
66 /// Replacement span in the buffer
67 pub span: Span,
68 /// Whether to append a space after selecting this suggestion.
69 /// This helps to avoid that a completer repeats the complete suggestion.
70 pub append_whitespace: bool,
71}