M8: command registry, skills as slash commands, completion engine + popup, rpc complete
Closes #68, closes #69, closes #70, closes #71, closes #72
This commit is contained in:
1 parent
6f0addcd47
commit
65765ce2b3
10 files changed
+651
-125
No files matched your search
@@ -1,4 +1,4 @@
|
||||
//! Multi-line input editor with history and `@file` completion.
|
||||
//! Multi-line input editor with history.
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Editor {
|
||||
@@ -144,26 +144,10 @@ impl Editor {
|
||||
Action::Submit(s)
|
||||
}
|
||||
|
||||
/// The `@partial` token ending at the cursor, if any: (start byte, text after '@').
|
||||
pub fn at_token(&self) -> Option<(usize, &str)> {
|
||||
let before = &self.buf[..self.cur];
|
||||
let start = before
|
||||
.rfind(|c: char| c.is_whitespace())
|
||||
.map_or(0, |i| i + 1);
|
||||
before[start..].strip_prefix('@').map(|t| (start, t))
|
||||
}
|
||||
|
||||
/// Replace the current `@token` with `@path`.
|
||||
pub fn complete(&mut self, path: &str, done: bool) {
|
||||
if let Some((s, _)) = self.at_token() {
|
||||
let rep = if done {
|
||||
format!("@{path} ")
|
||||
} else {
|
||||
format!("@{path}")
|
||||
};
|
||||
self.buf.replace_range(s..self.cur, &rep);
|
||||
self.cur = s + rep.len();
|
||||
}
|
||||
/// Replace `buf[start..cur]` with `text`; cursor lands after it.
|
||||
pub fn replace(&mut self, start: usize, text: &str) {
|
||||
self.buf.replace_range(start..self.cur, text);
|
||||
self.cur = start + text.len();
|
||||
}
|
||||
|
||||
/// (row, col) of the cursor in chars.
|
||||
@@ -184,52 +168,6 @@ fn advance(buf: &str, from: usize, to: usize, col: usize) -> usize {
|
||||
.map_or(to, |(i, _)| from + i)
|
||||
}
|
||||
|
||||
/// Case-insensitive subsequence match; shorter paths and basename hits rank first.
|
||||
pub fn fuzzy<'a>(files: &'a [String], q: &str, n: usize) -> Vec<&'a String> {
|
||||
let q = q.to_lowercase();
|
||||
let mut hits: Vec<(usize, &String)> = files
|
||||
.iter()
|
||||
.filter_map(|f| {
|
||||
let l = f.to_lowercase();
|
||||
let mut it = l.chars();
|
||||
if !q.chars().all(|c| it.any(|x| x == c)) {
|
||||
return None;
|
||||
}
|
||||
let base = l.rsplit('/').next().unwrap_or(&l);
|
||||
let bonus = if base.contains(&q) {
|
||||
0
|
||||
} else if l.contains(&q) {
|
||||
1000
|
||||
} else {
|
||||
2000
|
||||
};
|
||||
Some((bonus + f.len(), f))
|
||||
})
|
||||
.collect();
|
||||
hits.sort();
|
||||
hits.into_iter().take(n).map(|(_, f)| f).collect()
|
||||
}
|
||||
|
||||
pub fn common_prefix(xs: &[&String]) -> String {
|
||||
let Some(first) = xs.first() else {
|
||||
return String::new();
|
||||
};
|
||||
let mut end = first.len();
|
||||
for x in &xs[1..] {
|
||||
end = end.min(
|
||||
first
|
||||
.bytes()
|
||||
.zip(x.bytes())
|
||||
.take_while(|(a, b)| a == b)
|
||||
.count(),
|
||||
);
|
||||
}
|
||||
while !first.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
first[..end].to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -276,28 +214,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn at_completion() {
|
||||
let files: Vec<String> = [
|
||||
"src/main.rs",
|
||||
"src/lib.rs",
|
||||
"crates/x/src/main.rs",
|
||||
"README.md",
|
||||
]
|
||||
.map(String::from)
|
||||
.into();
|
||||
assert_eq!(fuzzy(&files, "main", 5), [&files[0], &files[2]]);
|
||||
assert_eq!(fuzzy(&files, "srlib", 5), [&files[1]]);
|
||||
fn replace_token() {
|
||||
let mut e = Editor::default();
|
||||
e.insert("look at @mai");
|
||||
assert_eq!(e.at_token(), Some((8, "mai")));
|
||||
let hits = fuzzy(&files, "mai", 5);
|
||||
assert_eq!(common_prefix(&hits), "");
|
||||
e.complete("src/main.rs", true);
|
||||
assert_eq!(e.buf, "look at @src/main.rs ");
|
||||
assert_eq!(e.at_token(), None);
|
||||
assert_eq!(
|
||||
common_prefix(&[&"src/a".to_string(), &"src/b".to_string()]),
|
||||
"src/"
|
||||
);
|
||||
e.replace(8, "@src/main.rs ");
|
||||
assert_eq!((e.buf.as_str(), e.cur), ("look at @src/main.rs ", 21));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user