-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrop_down_auto_complete.rs
72 lines (63 loc) · 2.35 KB
/
drop_down_auto_complete.rs
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
use lineeditor::event::LineEditorEvent;
use lineeditor::keybindings::KeyCombination;
use lineeditor::styled_buffer::StyledBuffer;
use lineeditor::Completer;
use lineeditor::KeyModifiers;
use lineeditor::LineEditor;
use lineeditor::LineEditorResult;
use lineeditor::Span;
use lineeditor::StringPrompt;
use lineeditor::Suggestion;
const GITQL_RESERVED_KEYWORDS: [&str; 31] = [
"set", "select", "distinct", "from", "group", "where", "having", "offset", "limit", "order",
"by", "case", "when", "then", "else", "end", "between", "in", "is", "not", "like", "glob",
"or", "and", "xor", "true", "false", "null", "as", "asc", "desc",
];
pub struct FixedCompleter {}
impl Completer for FixedCompleter {
fn complete(&self, input: &StyledBuffer) -> Vec<Suggestion> {
let mut suggestions: Vec<Suggestion> = vec![];
if input.position() != input.len() {
return suggestions;
}
if let Some(keyword) = input.last_alphabetic_keyword() {
for reserved_keyword in GITQL_RESERVED_KEYWORDS {
if reserved_keyword.starts_with(&keyword) {
let suggestion = Suggestion {
content: StyledBuffer::from(reserved_keyword),
span: Span {
start: input.len() - keyword.len(),
end: input.len(),
},
};
suggestions.push(suggestion);
}
}
}
suggestions
}
}
fn main() {
let prompt = StringPrompt::new("prompt> ".to_string());
let mut line_editor = LineEditor::new(Box::new(prompt));
line_editor.set_completer(Box::new(FixedCompleter {}));
let bindings = line_editor.keybinding();
bindings.register_binding(
KeyCombination {
key_kind: lineeditor::KeyEventKind::Press,
modifier: KeyModifiers::NONE,
key_code: lineeditor::KeyCode::Tab,
},
LineEditorEvent::ToggleAutoComplete,
);
bindings.register_common_control_bindings();
bindings.register_common_navigation_bindings();
bindings.register_common_edit_bindings();
bindings.register_common_selection_bindings();
match line_editor.read_line() {
Ok(LineEditorResult::Success(line)) => {
println!("Line {}", line);
}
_ => {}
}
}