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
|
use super::{ReplCommand, REPL_COMMANDS};
use crate::config::GlobalConfig;
use reedline::{Completer, Span, Suggestion};
use std::collections::HashMap;
impl Completer for ReplCompleter {
fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
let mut suggestions = vec![];
let line = &line[0..pos];
let mut parts = split_line(line);
if parts.is_empty() {
return suggestions;
}
if parts[0].0 == r#":::"# {
parts.remove(0);
}
let parts_len = parts.len();
if parts_len == 0 {
return suggestions;
}
let (cmd, cmd_start) = parts[0];
if !cmd.starts_with('.') {
return suggestions;
}
let state = self.config.read().get_state();
let commands: Vec<_> = self
.commands
.iter()
.filter(|cmd| {
if !cmd.is_valid(&state) {
return false;
}
let line = parts
.iter()
.take(2)
.map(|(v, _)| *v)
.collect::<Vec<&str>>()
.join(" ");
cmd.name.starts_with(&line)
})
.collect();
if parts_len > 1 {
let span = Span::new(parts[parts_len - 1].1, pos);
let args: Vec<&str> = parts.iter().skip(1).map(|(v, _)| *v).collect();
suggestions.extend(
self.config
.read()
.repl_complete(cmd, &args)
.iter()
.map(|(value, description)| create_suggestion(value, description, span)),
)
}
if suggestions.is_empty() {
let span = Span::new(cmd_start, pos);
suggestions.extend(commands.iter().map(|cmd| {
let name = cmd.name;
let description = cmd.description;
let has_group = self.groups.get(name).map(|v| *v > 1).unwrap_or_default();
let name = if has_group {
name.to_string()
} else {
format!("{name} ")
};
create_suggestion(&name, description, span)
}))
}
suggestions
}
}
pub struct ReplCompleter {
config: GlobalConfig,
commands: Vec<ReplCommand>,
groups: HashMap<&'static str, usize>,
}
impl ReplCompleter {
pub fn new(config: &GlobalConfig) -> Self {
let mut groups = HashMap::new();
let commands: Vec<ReplCommand> = REPL_COMMANDS.to_vec();
for cmd in REPL_COMMANDS.iter() {
let name = cmd.name;
if let Some(count) = groups.get(name) {
groups.insert(name, count + 1);
} else {
groups.insert(name, 1);
}
}
Self {
config: config.clone(),
commands,
groups,
}
}
}
fn create_suggestion(value: &str, description: &str, span: Span) -> Suggestion {
let description = if description.is_empty() {
None
} else {
Some(description.to_string())
};
Suggestion {
value: value.to_string(),
description,
style: None,
extra: None,
span,
append_whitespace: false,
}
}
fn split_line(line: &str) -> Vec<(&str, usize)> {
let mut parts = vec![];
let mut part_start = None;
for (i, ch) in line.char_indices() {
if ch == ' ' {
if let Some(s) = part_start {
parts.push((&line[s..i], s));
part_start = None;
}
} else if part_start.is_none() {
part_start = Some(i)
}
}
if let Some(s) = part_start {
parts.push((&line[s..], s));
} else {
parts.push(("", line.len()))
}
parts
}
#[test]
fn test_split_line() {
assert_eq!(split_line(".role coder"), vec![(".role", 0), ("coder", 6)],);
assert_eq!(
split_line(" .role coder"),
vec![(".role", 1), ("coder", 9)],
);
assert_eq!(
split_line(".set highlight "),
vec![(".set", 0), ("highlight", 5), ("", 15)],
);
assert_eq!(
split_line(".set highlight t"),
vec![(".set", 0), ("highlight", 5), ("t", 15)],
);
}
|