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
use anyhow::{anyhow, Result};
use arcstr::ArcStr;
use graphix_compiler::{env::Env, expr::ModPath, typ::Type};
use log::debug;
use netidx::path::Path;
use reedline::{Completer, Span, Suggestion};
#[derive(Debug)]
enum CompletionContext<'a> {
Bind(Span, &'a str),
ArgLbl { span: Span, function: &'a str, arg: &'a str },
}
impl<'a> CompletionContext<'a> {
fn from_str(s: &'a str) -> Result<Self> {
let mut arg_lbl = 0;
let mut fend = 0;
let mut prev = 0;
for (i, c) in s.char_indices().rev() {
if c == '#' {
arg_lbl = prev;
}
if c == '(' && arg_lbl != 0 {
fend = i;
}
if c.is_whitespace() || c == '(' || c == '{' {
if arg_lbl > 0 && fend > 0 {
let function =
s.get(prev..fend).ok_or_else(|| anyhow!("invalid function"))?;
let arg = s.get(arg_lbl..).ok_or_else(|| anyhow!("invalid arg"))?;
return Ok(Self::ArgLbl {
span: Span { start: arg_lbl, end: s.len() },
function,
arg,
});
} else {
return Ok(Self::Bind(
Span { start: prev, end: s.len() },
s.get(prev..).ok_or_else(|| anyhow!("invalid bind"))?,
));
}
}
prev = i;
}
Ok(Self::Bind(Span { start: 0, end: s.len() }, s))
}
}
pub(super) struct BComplete(pub Env);
impl Completer for BComplete {
fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
debug!("{line}: {pos}");
let mut res = vec![];
let s = line.get(0..pos);
debug!("{s:?}");
if let Some(s) = s {
let cc = CompletionContext::from_str(s);
debug!("{cc:?}");
if let Ok(cc) = cc {
match cc {
CompletionContext::Bind(span, s) => {
let part = ModPath::from_iter(s.split("::"));
for m in self.0.lookup_matching_modules(&ModPath::root(), &part) {
let value = format!("{m}");
res.push(Suggestion {
span,
value,
description: Some("module".into()),
style: None,
extra: None,
append_whitespace: false,
match_indices: None,
display_override: None,
})
}
for (value, id) in self.0.lookup_matching(&ModPath::root(), &part)
{
let description = match self.0.by_id.get(&id) {
None => format!("_"),
Some(b) => {
use std::fmt::Write;
let mut res = String::new();
match &b.typ {
Type::Fn(ft) => {
let ft = ft.replace_auto_constrained();
write!(res, "{} ", ft).unwrap()
}
t => write!(res, "{} ", t).unwrap(),
}
if let Some(doc) = &b.doc {
write!(res, "{doc}").unwrap();
};
res
}
};
let value = match Path::dirname(&part.0) {
None => String::from(value.as_str()),
Some(dir) => {
let path =
Path::from(ArcStr::from(dir)).append(&*value);
format!("{}", ModPath(path))
}
};
res.push(Suggestion {
span,
value,
description: Some(description),
style: None,
extra: Some(vec!["hello world!".into()]),
append_whitespace: false,
match_indices: None,
display_override: None,
})
}
}
CompletionContext::ArgLbl { span, function, arg: part } => {
let function = ModPath::from_iter(function.split("::"));
if let Some((_, b)) =
self.0.lookup_bind(&ModPath::root(), &function)
{
if let Type::Fn(ft) = &b.typ {
for arg in ft.args.iter() {
if let Some(lbl) = arg.label() {
if lbl.starts_with(part) {
let description =
Some(format!("{}", arg.typ));
res.push(Suggestion {
span,
value: lbl.as_str().into(),
description,
style: None,
extra: None,
append_whitespace: false,
match_indices: None,
display_override: None,
})
}
}
}
}
}
}
}
}
}
res
}
}