Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 80 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,22 @@ impl App {
j
}

/// Start of the space-separated word before the cursor (Ctrl+W); a paste or image token goes as one.
fn ww(&self) -> usize {
let cs: Vec<char> = self.inp.chars().collect();
let mut j = self.cur;
while j > 0 && cs[j - 1] == ' ' {
j -= 1
}
if let Some(o) = tokend(&cs[..j]) {
return o;
}
while j > 0 && cs[j - 1] != ' ' {
j -= 1
}
j
}

fn wr(&self) -> usize {
let cs: Vec<char> = self.inp.chars().collect();
let n = cs.len();
Expand Down Expand Up @@ -2351,10 +2367,16 @@ impl App {
.iter()
.map(|(v, d, ds, _)| (d.clone(), one_line(ds, 60), v.clone()))
.collect();
// exact id first: "claude-opus-5" is a prefix of "claude-opus-5-5", listed earlier
let sel = self
.models
.iter()
.position(|m| m.0 == cur || m.0.starts_with(cur) || cur.starts_with(&m.0))
.position(|m| m.0 == cur)
.or_else(|| {
self.models
.iter()
.position(|m| m.0.starts_with(cur) || cur.starts_with(&m.0))
})
.unwrap_or(0);
self.pk = Some(Pk {
kind: 0,
Expand Down Expand Up @@ -3318,17 +3340,8 @@ impl App {
KeyCode::Char('b') if k.modifiers.contains(Km::ALT) => self.cur = self.wl(),
KeyCode::Char('f') if k.modifiers.contains(Km::ALT) => self.cur = self.wr(),
KeyCode::Char('w') if ct => {
let cs: Vec<char> = self.inp.chars().collect();
let mut j = self.cur;
while j > 0 && cs[j - 1] == ' ' {
j -= 1
}
while j > 0 && cs[j - 1] != ' ' {
j -= 1
}
let s: String = cs[..j].iter().chain(cs[self.cur..].iter()).collect();
self.inp = s;
self.cur = j
let j = self.ww();
self.cut(j, self.cur)
}
KeyCode::BackTab => {
self.mode = self.nextmode();
Expand Down Expand Up @@ -3795,10 +3808,28 @@ const KW: [&str; 16] = [
"match", "return", "as",
];

/// Byte index of a `//` comment that is not inside a string literal.
fn cmt_at(s: &str) -> Option<usize> {
let mut q = false;
let mut esc = false;
for (i, c) in s.char_indices() {
if esc {
esc = false
} else if q && c == '\\' {
esc = true
} else if c == '"' {
q = !q
} else if !q && s[i..].starts_with("//") {
return Some(i);
}
}
None
}

/// Syntax-highlights one line of code into spans.
pub fn hl(s: &str, m: &Md) -> Vec<Span<'static>> {
let mut v = vec![];
let (code, cmt) = match s.find("//") {
let (code, cmt) = match cmt_at(s) {
Some(i) => (&s[..i], Some(&s[i..])),
None => (s, None),
};
Expand Down Expand Up @@ -5244,6 +5275,42 @@ impl App {
}
}

#[cfg(test)]
mod five_fix_tests {
use super::*;

#[test]
fn model_picker_marks_the_exact_model() {
let mut a = App::new();
for (i, (id, _)) in MID_ALL.iter().enumerate() {
a.model = i;
a.modelpicker();
let p = a.pk.as_ref().unwrap();
assert_eq!(p.items[p.sel].2, *id);
}
}

#[test]
fn ctrl_w_takes_a_token_whole() {
let mut a = App::new();
for tok in ["[image #2]", "[\u{2060}pasted #1 +5 lines]"] {
a.inp = format!("see {tok} ");
a.cur = a.inp.chars().count();
a.key(KeyEvent::new(KeyCode::Char('w'), Km::CONTROL));
assert_eq!(a.inp, "see ");
a.key(KeyEvent::new(KeyCode::Char('w'), Km::CONTROL));
assert_eq!(a.inp, "");
}
}

#[test]
fn slashes_in_strings_are_not_comments() {
assert_eq!(cmt_at(r#"let u = "https://x.com"; // c"#), Some(25));
assert_eq!(cmt_at(r#"let u = "a\"//b";"#), None);
assert_eq!(cmt_at("x // y"), Some(2));
}
}

#[cfg(test)]
mod ctrlc_tests {
use super::*;
Expand Down
29 changes: 29 additions & 0 deletions src/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ fn strip(s: &str) -> String {
break;
}
}
} else if it.peek() == Some(&']') {
// OSC (hyperlink, title): runs to BEL or ESC \
it.next();
while let Some(n) = it.next() {
if n == '\x07' {
break;
}
if n == '\x1b' {
if it.peek() == Some(&'\\') {
it.next();
}
break;
}
}
} else {
it.next();
}
Expand Down Expand Up @@ -3483,3 +3497,18 @@ mod queue_tests {
let _ = sent(a, cat);
}
}

#[cfg(test)]
mod strip_tests {
use super::strip;

#[test]
fn osc_sequences_are_removed_whole() {
assert_eq!(
strip("a\x1b]8;;https://x.y\x1b\\link\x1b]8;;\x1b\\ b"),
"alink b"
);
assert_eq!(strip("\x1b]0;title\x07ok"), "ok");
assert_eq!(strip("\x1b[1mbold\x1b[0m"), "bold");
}
}
27 changes: 26 additions & 1 deletion src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,24 @@ fn memory_files(cwd: &str) -> Vec<String> {
.ok()
.filter(|x| !x.is_empty())
.unwrap_or_else(|| format!("{home}/.claude"));
memory_files_in(cwd, &home, &cfg)
}

fn memory_files_in(cwd: &str, home: &str, cfg: &str) -> Vec<String> {
// the live view keeps the folder as "~/…"; walk the real path
let cwd = match cwd.strip_prefix('~') {
Some(r) if !home.is_empty() && (r.is_empty() || r.starts_with(['/', '\\'])) => {
format!("{home}{r}")
}
_ => cwd.to_string(),
};
let cwd = cwd.as_str();
let mut v = vec![];
let mut add = |p: String| {
if std::path::Path::new(&p).is_file() {
let r = if let Some(x) = p.strip_prefix(&format!("{cwd}/")) {
x.to_string()
} else if let Some(x) = p.strip_prefix(&home).filter(|_| !home.is_empty()) {
} else if let Some(x) = p.strip_prefix(home).filter(|_| !home.is_empty()) {
format!("~{x}")
} else {
p
Expand Down Expand Up @@ -987,6 +999,19 @@ mod tests {
use super::*;
use serde_json::json;

#[test]
fn memory_files_follow_a_tilde_folder() {
let home = std::env::temp_dir().join(format!("nn-mem-{}", std::process::id()));
let proj = home.join("proj");
std::fs::create_dir_all(&proj).unwrap();
std::fs::write(home.join("CLAUDE.md"), "x").unwrap();
std::fs::write(proj.join("CLAUDE.md"), "x").unwrap();
let h = home.to_string_lossy().to_string();
let v = memory_files_in("~/proj", &h, &format!("{h}/.claude"));
std::fs::remove_dir_all(&home).ok();
assert_eq!(v, ["~/CLAUDE.md", "CLAUDE.md"]);
}

#[test]
fn tabs_by_command() {
assert_eq!(TABS[tab_of("status")], "Status");
Expand Down
Loading