Classify every comment as tautology, stale, excuse or decision record. Tautology/stale/excuse comments are deleted (excuses filed as issues #196-#236), decisions moved to docs/adr/0001-0034. - scripts/lint-comments.sh + .comment-baseline: per-file comment count may only go down; wired into tests/all.sh - .claude/skills/delete-comment: repeatable audit workflow - cargo fmt applied to touched crates Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
101 lines
2.9 KiB
Rust
101 lines
2.9 KiB
Rust
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
use hx_core::command::Completion;
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub enum Act {
|
|
Consumed,
|
|
Close,
|
|
Accept,
|
|
Pass,
|
|
}
|
|
|
|
pub struct Popup {
|
|
pub comp: Completion,
|
|
pub sel: usize,
|
|
}
|
|
|
|
impl Popup {
|
|
pub fn new(comp: Completion) -> Option<Self> {
|
|
(!comp.items.is_empty()).then_some(Self { comp, sel: 0 })
|
|
}
|
|
|
|
pub fn selected_label(&self) -> &str {
|
|
&self.comp.items[self.sel].label
|
|
}
|
|
|
|
pub fn key(&mut self, k: KeyEvent, buf: &str, cur: usize) -> Act {
|
|
let n = self.comp.items.len();
|
|
match k.code {
|
|
KeyCode::Up => self.sel = (self.sel + n - 1) % n,
|
|
KeyCode::Down => self.sel = (self.sel + 1) % n,
|
|
KeyCode::Esc => return Act::Close,
|
|
KeyCode::Tab => return Act::Accept,
|
|
KeyCode::Enter
|
|
if !k
|
|
.modifiers
|
|
.intersects(KeyModifiers::ALT | KeyModifiers::SHIFT)
|
|
&& !matches!(&buf[self.comp.start..cur], "" | "/" | "@")
|
|
&& buf[self.comp.start..cur] != *self.selected_label() =>
|
|
{
|
|
return Act::Accept;
|
|
}
|
|
_ => return Act::Pass,
|
|
}
|
|
Act::Consumed
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use hx_core::command::Candidate;
|
|
|
|
fn pop() -> Popup {
|
|
let item = |l: &str| Candidate {
|
|
label: l.into(),
|
|
detail: String::new(),
|
|
};
|
|
Popup::new(Completion {
|
|
start: 0,
|
|
items: vec![item("/clear"), item("/compact")],
|
|
})
|
|
.unwrap()
|
|
}
|
|
|
|
fn press(code: KeyCode) -> KeyEvent {
|
|
KeyEvent::new(code, KeyModifiers::NONE)
|
|
}
|
|
|
|
#[test]
|
|
fn empty_completion_has_no_popup() {
|
|
assert!(Popup::new(Completion::default()).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn arrows_wrap() {
|
|
let mut p = pop();
|
|
assert_eq!(p.key(press(KeyCode::Up), "/c", 2), Act::Consumed);
|
|
assert_eq!(p.selected_label(), "/compact");
|
|
assert_eq!(p.key(press(KeyCode::Down), "/c", 2), Act::Consumed);
|
|
assert_eq!(p.selected_label(), "/clear");
|
|
}
|
|
|
|
#[test]
|
|
fn tab_accepts_esc_closes_others_pass() {
|
|
let mut p = pop();
|
|
assert_eq!(p.key(press(KeyCode::Tab), "/c", 2), Act::Accept);
|
|
assert_eq!(p.key(press(KeyCode::Esc), "/c", 2), Act::Close);
|
|
assert_eq!(p.key(press(KeyCode::Char('x')), "/c", 2), Act::Pass);
|
|
}
|
|
|
|
#[test]
|
|
fn enter_accepts_only_a_partial_token() {
|
|
let mut p = pop();
|
|
assert_eq!(p.key(press(KeyCode::Enter), "/c", 2), Act::Accept);
|
|
assert_eq!(p.key(press(KeyCode::Enter), "/clear", 6), Act::Pass);
|
|
assert_eq!(p.key(press(KeyCode::Enter), "/", 1), Act::Pass);
|
|
let alt = KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT);
|
|
assert_eq!(p.key(alt, "/c", 2), Act::Pass);
|
|
}
|
|
}
|