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
use std::sync::{Arc, Mutex};
use numbat::{Context, compact_str::CompactString, unicode_input::UNICODE_INPUT};
use rustyline::completion::{Completer, Pair, extract_word};
pub struct NumbatCompleter {
pub context: Arc<Mutex<Context>>,
pub modules: Vec<CompactString>,
pub all_timezones: Vec<CompactString>,
}
impl Completer for NumbatCompleter {
type Candidate = Pair;
fn complete(
&self,
line: &str,
pos: usize,
_: &rustyline::Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
for (patterns, replacement) in UNICODE_INPUT {
for pattern in *patterns {
let backslash_pattern = format!("\\{pattern}");
if line[..pos].ends_with(&backslash_pattern) {
return Ok((
pos - (1 + pattern.len()),
vec![Pair {
display: backslash_pattern.to_string(),
replacement: replacement.to_string(),
}],
));
}
}
}
if line.starts_with("use ") {
return Ok((
0,
self.modules
.iter()
.map(|m| {
let line = format!("use {m}");
Pair {
display: m.to_string(),
replacement: line,
}
})
.filter(|p| p.replacement.starts_with(line))
.collect(),
));
} else if line.starts_with("list ") || line.starts_with("ls ") {
let command = if line.starts_with("list ") {
"list"
} else {
"ls"
};
return Ok((
0,
["functions", "dimensions", "units", "variables"]
.iter()
.map(|category| {
let line = format!("{command} {category}");
Pair {
display: category.to_string(),
replacement: line,
}
})
.filter(|p| p.replacement.starts_with(line))
.collect(),
));
}
// does it look like we're tab-completing a timezone?
let complete_tz = line.find("tz(").and_then(|convert_pos| {
if let Some(quote_pos) = line.rfind('"')
&& quote_pos > convert_pos
&& pos > quote_pos
{
return Some(quote_pos + 1);
}
None
});
if let Some(pos_word) = complete_tz {
let word_part = &line[pos_word..];
let matches = self
.all_timezones
.iter()
.filter(|tz| tz.starts_with(word_part))
.collect::<Vec<_>>();
let append_closing_quote = matches.len() <= 1;
return Ok((
pos_word,
matches
.into_iter()
.map(|tz| Pair {
display: tz.to_string(),
replacement: if append_closing_quote {
format!("{tz}\"")
} else {
tz.to_string()
},
})
.collect(),
));
}
let (pos_word, word_part) = extract_word(line, pos, None, |c| {
// TODO: we could use is_identifier_char here potentially
match c {
c if c.is_alphanumeric() => false,
'_' => false,
_ => true,
}
});
// don't add an opening paren if we're completing after a reverse function call
// or when completing conversion functions
let add_paren = !["|>", "->", "→", "➞", "to"]
.iter()
.any(|&s| line[..pos].contains(s));
let binding = self.context.lock().unwrap();
let candidates = binding.get_completions_for(word_part, add_paren);
Ok((
pos_word,
candidates
.map(|w| Pair {
display: w.to_string(),
replacement: w,
})
.collect(),
))
}
}