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>
288 lines
9.3 KiB
Rust
288 lines
9.3 KiB
Rust
use crate::theme;
|
|
use hx_core::command::{self as cmd, ArgKind, CmdSpec, Source};
|
|
use ratatui::layout::Rect;
|
|
use ratatui::text::{Line, Span};
|
|
use ratatui::widgets::{Block, Clear, Paragraph};
|
|
use std::path::Path;
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum Pick {
|
|
Insert(String),
|
|
Run(String),
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Entry {
|
|
pub label: String,
|
|
pub detail: String,
|
|
pub pick: Pick,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum Act {
|
|
None,
|
|
Close,
|
|
Run(String),
|
|
Insert(String),
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct Picker {
|
|
query: String,
|
|
sel: usize,
|
|
all: Vec<Entry>,
|
|
}
|
|
|
|
const SESSIONS: usize = 5;
|
|
|
|
impl Picker {
|
|
pub fn new(specs: &[CmdSpec], sessions_dir: &Path, current: &Path) -> Self {
|
|
let mut all = vec![];
|
|
for s in specs {
|
|
let tag = match s.source {
|
|
Source::Skill(_) => "skill · ",
|
|
Source::Mcp => "mcp · ",
|
|
_ => "",
|
|
};
|
|
all.push(Entry {
|
|
label: format!("/{}", s.name),
|
|
detail: format!("{tag}{}", s.summary),
|
|
pick: match s.args {
|
|
ArgKind::None => Pick::Run(format!("/{}", s.name)),
|
|
_ => Pick::Insert(format!("/{} ", s.name)),
|
|
},
|
|
});
|
|
if let (ArgKind::Enum(v), "model") = (&s.args, s.name.as_str()) {
|
|
all.extend(v.iter().map(|m| Entry {
|
|
label: format!("model {m}"),
|
|
detail: "switch model".into(),
|
|
pick: Pick::Run(format!("/model {m}")),
|
|
}));
|
|
}
|
|
}
|
|
all.extend(sessions(sessions_dir, current));
|
|
Self {
|
|
all,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
pub fn matches(&self) -> Vec<&Entry> {
|
|
if self.query.is_empty() {
|
|
return self.all.iter().collect();
|
|
}
|
|
let labels: Vec<String> = self
|
|
.all
|
|
.iter()
|
|
.map(|e| format!("{} {}", e.label, e.detail))
|
|
.collect();
|
|
cmd::rank(labels.iter().map(String::as_str), &self.query, usize::MAX)
|
|
.into_iter()
|
|
.filter_map(|l| labels.iter().position(|x| x == l).map(|i| &self.all[i]))
|
|
.collect()
|
|
}
|
|
|
|
pub fn key(&mut self, k: crossterm::event::KeyEvent) -> Act {
|
|
use crossterm::event::{KeyCode, KeyModifiers};
|
|
let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
|
|
match k.code {
|
|
KeyCode::Esc => return Act::Close,
|
|
KeyCode::Char('c' | 'p') if ctrl => return Act::Close,
|
|
KeyCode::Up => self.move_sel(-1),
|
|
KeyCode::Down | KeyCode::Tab => self.move_sel(1),
|
|
KeyCode::Backspace => {
|
|
self.query.pop();
|
|
self.sel = 0;
|
|
}
|
|
KeyCode::Char(c) => {
|
|
self.query.push(c);
|
|
self.sel = 0;
|
|
}
|
|
KeyCode::Enter => {
|
|
return match self.selected() {
|
|
Some(Pick::Run(l)) => Act::Run(l),
|
|
Some(Pick::Insert(l)) => Act::Insert(l),
|
|
None => Act::Close,
|
|
};
|
|
}
|
|
_ => {}
|
|
}
|
|
Act::None
|
|
}
|
|
|
|
fn selected(&self) -> Option<Pick> {
|
|
self.matches().get(self.sel).map(|e| e.pick.clone())
|
|
}
|
|
|
|
fn move_sel(&mut self, d: isize) {
|
|
let n = self.matches().len().max(1) as isize;
|
|
self.sel = (self.sel as isize + d).rem_euclid(n) as usize;
|
|
}
|
|
|
|
pub fn draw(&self, f: &mut ratatui::Frame) {
|
|
let th = theme::t();
|
|
let a = f.area();
|
|
let w = (a.width * 6 / 10)
|
|
.clamp(50, 110)
|
|
.min(a.width.saturating_sub(4));
|
|
let ms = self.matches();
|
|
let h = (ms.len() as u16 + 4)
|
|
.min((a.height * 6 / 10).max(8))
|
|
.min(a.height.saturating_sub(2))
|
|
.max(5);
|
|
let r = Rect::new(a.x + (a.width - w) / 2, a.y + a.height / 8, w, h);
|
|
let rows = (h - 4) as usize;
|
|
let top = self.sel.saturating_sub(rows.saturating_sub(1));
|
|
let lw = ms
|
|
.iter()
|
|
.map(|e| e.label.chars().count())
|
|
.max()
|
|
.unwrap_or(0)
|
|
.min(w as usize / 2);
|
|
let mut lines = vec![
|
|
Line::from(vec![
|
|
Span::styled(format!("{} ", th.sym.user), th.accent()),
|
|
Span::raw(self.query.clone()),
|
|
]),
|
|
Line::default(),
|
|
];
|
|
lines.extend(ms.iter().enumerate().skip(top).take(rows).map(|(i, e)| {
|
|
let st = if i == self.sel { th.sel() } else { th.plain() };
|
|
let label: String = e.label.chars().take(lw).collect();
|
|
Line::from(vec![
|
|
Span::styled(format!(" {label:<lw$} "), st),
|
|
Span::styled(e.detail.clone(), if i == self.sel { st } else { th.dim() }),
|
|
])
|
|
}));
|
|
f.render_widget(Clear, r);
|
|
f.render_widget(
|
|
Paragraph::new(lines).block(
|
|
Block::bordered()
|
|
.border_style(th.dim())
|
|
.title(Span::styled(" ctrl-p ", th.dim()))
|
|
.title_bottom(Span::styled(" ↑↓ select · ⏎ run · esc close ", th.dim())),
|
|
),
|
|
r,
|
|
);
|
|
f.set_cursor_position((r.x + 3 + self.query.chars().count() as u16, r.y + 1));
|
|
}
|
|
}
|
|
|
|
fn sessions(dir: &Path, current: &Path) -> Vec<Entry> {
|
|
let all: Vec<_> = hx_core::session::list(dir)
|
|
.into_iter()
|
|
.filter(|m| m.path != current)
|
|
.collect();
|
|
let mut v: Vec<Entry> = all
|
|
.iter()
|
|
.take(SESSIONS)
|
|
.map(|m| Entry {
|
|
label: format!("session {}", crate::sessions::ago(m.modified)),
|
|
detail: m.title.clone(),
|
|
pick: Pick::Run(format!("/resume {}", m.path.display())),
|
|
})
|
|
.collect();
|
|
if all.len() > SESSIONS {
|
|
v.push(Entry {
|
|
label: "more sessions…".into(),
|
|
detail: format!("{} in this project", all.len()),
|
|
pick: Pick::Run("/sessions".into()),
|
|
});
|
|
}
|
|
v
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn entries_filter_and_sessions() {
|
|
let t = tempfile::tempdir().unwrap();
|
|
let mut s = hx_core::session::Session::open(t.path().join("1.jsonl")).unwrap();
|
|
s.append(hx_ai::Message::user("review PR 73\nmore".to_string()))
|
|
.unwrap();
|
|
hx_core::session::Session::open(t.path().join("2.jsonl"))
|
|
.unwrap()
|
|
.append(hx_ai::Message::user("current".to_string()))
|
|
.unwrap();
|
|
let p = Picker::new(
|
|
&cmd::builtins(&["opus", "sonnet"]),
|
|
t.path(),
|
|
&t.path().join("2.jsonl"),
|
|
);
|
|
let labels = |p: &Picker| {
|
|
p.matches()
|
|
.iter()
|
|
.map(|e| e.label.clone())
|
|
.collect::<Vec<_>>()
|
|
};
|
|
assert!(labels(&p).contains(&"model opus".to_string()));
|
|
let sess: Vec<_> = p
|
|
.all
|
|
.iter()
|
|
.filter(|e| e.label.starts_with("session"))
|
|
.collect();
|
|
assert_eq!(sess.len(), 1, "current session skipped");
|
|
assert_eq!(sess[0].detail, "review PR 73");
|
|
assert_eq!(
|
|
sess[0].pick,
|
|
Pick::Run(format!("/resume {}", t.path().join("1.jsonl").display()))
|
|
);
|
|
|
|
let mut p = p;
|
|
p.query = "PR 73".into();
|
|
assert_eq!(labels(&p)[0], "session just now");
|
|
p.query = "comp".into();
|
|
assert_eq!(p.selected(), Some(Pick::Run("/compact".into())));
|
|
p.query = "/model".into();
|
|
assert_eq!(p.selected(), Some(Pick::Insert("/model ".into())));
|
|
p.query.clear();
|
|
p.sel = 0;
|
|
p.move_sel(-1);
|
|
assert_eq!(p.sel, p.matches().len() - 1, "wraps to the end");
|
|
}
|
|
|
|
fn press(code: crossterm::event::KeyCode) -> crossterm::event::KeyEvent {
|
|
crossterm::event::KeyEvent::new(code, crossterm::event::KeyModifiers::NONE)
|
|
}
|
|
|
|
fn type_str(p: &mut Picker, s: &str) {
|
|
for c in s.chars() {
|
|
assert_eq!(p.key(press(crossterm::event::KeyCode::Char(c))), Act::None);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn keys_filter_move_and_pick() {
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
let t = tempfile::tempdir().unwrap();
|
|
let mut p = Picker::new(
|
|
&cmd::builtins(&["opus", "sonnet"]),
|
|
t.path(),
|
|
&t.path().join("x"),
|
|
);
|
|
type_str(&mut p, "comp");
|
|
assert_eq!(p.key(press(KeyCode::Enter)), Act::Run("/compact".into()));
|
|
p.key(press(KeyCode::Backspace));
|
|
p.key(press(KeyCode::Backspace));
|
|
p.key(press(KeyCode::Backspace));
|
|
p.key(press(KeyCode::Backspace));
|
|
type_str(&mut p, "/model");
|
|
assert_eq!(p.key(press(KeyCode::Enter)), Act::Insert("/model ".into()));
|
|
for _ in 0..6 {
|
|
p.key(press(KeyCode::Backspace));
|
|
}
|
|
p.key(press(KeyCode::Down));
|
|
assert_eq!(p.sel, 1);
|
|
type_str(&mut p, "m");
|
|
assert_eq!(p.sel, 0);
|
|
type_str(&mut p, "zzzzzz");
|
|
assert_eq!(p.key(press(KeyCode::Enter)), Act::Close);
|
|
assert_eq!(p.key(press(KeyCode::Esc)), Act::Close);
|
|
let ctrl = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL);
|
|
assert_eq!(p.key(ctrl('p')), Act::Close);
|
|
assert_eq!(p.key(ctrl('c')), Act::Close);
|
|
}
|
|
}
|