Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

perf(agent-tui): insert a paste in one edit with one hint refresh

Akarshan Biswas25c928b21195
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 7fe41c883..d1051468e 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -4461,6 +4461,22 @@ impl App {
self.refresh_path_hints();
}
+ /// `input_insert` for a whole string, as one edit. A paste used to go
+ /// through `input_insert` per char, each shifting the rest of the input and
+ /// re-scanning it for an `@path`, which is quadratic and hangs on a large
+ /// paste. The hint state each per-char call left behind was overwritten by
+ /// the next, so refreshing once at the end lands on the same state.
+ fn input_insert_str(&mut self, text: &str) {
+ let text: String = text.chars().filter(|&c| c != '\t').collect();
+ if text.is_empty() {
+ return;
+ }
+ self.input.insert_str(self.cursor, &text);
+ self.cursor += text.len();
+ self.reset_slash_hint();
+ self.refresh_path_hints();
+ }
+
/// Delete the char before the caret (Backspace).
fn input_backspace(&mut self) {
if let Some(prev) = self.input[..self.cursor].chars().next_back() {
@@ -11648,9 +11664,7 @@ fn route_paste_event(app: &mut App, event: Event) {
} else if !app.ask_queue.is_empty() {
handle_ask_paste(app, &text);
} else {
- for c in text.chars() {
- app.input_insert(c);
- }
+ app.input_insert_str(&text);
}
}
diff --git a/src-tauri/src/core/cli/tui/tests/perf.rs b/src-tauri/src/core/cli/tui/tests/perf.rs
index fe4b921b4..2e38554e8 100644
--- a/src-tauri/src/core/cli/tui/tests/perf.rs
+++ b/src-tauri/src/core/cli/tui/tests/perf.rs
@@ -231,3 +231,26 @@ fn a_streaming_frame_scans_the_reply_at_most_once() {
let scans = take(&ANSWER_SCANS);
assert!(scans <= 1, "one frame scanned the reply {scans} times");
}
+
+/// A paste is one edit: one insert and one path-hint refresh, however long.
+#[test]
+fn a_paste_refreshes_path_hints_once() {
+ let mut app = test_app();
+ app.input = "ab".into();
+ app.cursor = 1;
+ take(&PATH_HINT_REFRESHES);
+ route_paste_event(&mut app, Event::Paste("x\ty @src".repeat(500)));
+ assert_eq!(take(&PATH_HINT_REFRESHES), 1);
+ assert_eq!(app.input, format!("a{}b", "xy @src".repeat(500)));
+ assert_eq!(app.cursor, 1 + "xy @src".len() * 500);
+}
+
+/// A paste of nothing but tabs is no edit at all, as it was char by char.
+#[test]
+fn a_tab_only_paste_leaves_the_input_untouched() {
+ let mut app = test_app();
+ app.slash_dismissed = true;
+ route_paste_event(&mut app, Event::Paste("\t\t".into()));
+ assert!(app.input.is_empty());
+ assert!(app.slash_dismissed, "no edit, so the popup stays dismissed");
+}