BlueGit

timfa posprint

Fix editor crash when pasting multi-byte characters like √.

egui CCursor indexes characters; convert to UTF-8 byte offsets before slicing the body.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-07-17 · 4 months ago

Hash
c8ba41d4631fc1929dbce93af9ff34181022481b
Parent
f0465192
2 files changed +59 −13
src/app.rs +5 −7
@@ -4,7 +4,7 @@ use eframe::egui;
4 4
5 5 use posprint::dsl::{self, parse};
6 6 use posprint::editor_assist::{
7 apply_directive_completion, at_directive_prefix, barcode_insert_options,
7 apply_directive_completion, at_directive_prefix, barcode_insert_options, char_index_to_byte,
8 8 complete_directive, date_time_insert_options, directive_ghost_suffix, image_insert_options,
9 9 insert_at_cursor, parameter_insert_options, qr_insert_options,
10 10 };
@@ -482,13 +482,11 @@ impl PosPrintApp {
482 482 self.editor_cursor = output
483 483 .cursor_range
484 484 .as_ref()
485 .map(|r| r.primary.ccursor.index)
485 .map(|r| char_index_to_byte(&self.body, r.primary.ccursor.index))
486 486 .or_else(|| {
487 output
488 .state
489 .cursor
490 .char_range()
491 .map(|r| r.primary.index)
487 output.state.cursor.char_range().map(|r| {
488 char_index_to_byte(&self.body, r.primary.index)
489 })
492 490 })
493 491 .unwrap_or(self.body.len());
494 492
@@ -4,7 +4,7 @@ use eframe::egui;
4
4
5use posprint::dsl::{self, parse};
5use posprint::dsl::{self, parse};
6use posprint::editor_assist::{
6use posprint::editor_assist::{
7 apply_directive_completion, at_directive_prefix, barcode_insert_options,
7 apply_directive_completion, at_directive_prefix, barcode_insert_options, char_index_to_byte,
8 complete_directive, date_time_insert_options, directive_ghost_suffix, image_insert_options,
8 complete_directive, date_time_insert_options, directive_ghost_suffix, image_insert_options,
9 insert_at_cursor, parameter_insert_options, qr_insert_options,
9 insert_at_cursor, parameter_insert_options, qr_insert_options,
10};
10};
@@ -482,13 +482,11 @@ impl PosPrintApp {
482 self.editor_cursor = output
482 self.editor_cursor = output
483 .cursor_range
483 .cursor_range
484 .as_ref()
484 .as_ref()
485 .map(|r| r.primary.ccursor.index)
485 .map(|r| char_index_to_byte(&self.body, r.primary.ccursor.index))
486 .or_else(|| {
486 .or_else(|| {
487 output
487 output.state.cursor.char_range().map(|r| {
488 .state
488 char_index_to_byte(&self.body, r.primary.index)
489 .cursor
489 })
490 .char_range()
491 .map(|r| r.primary.index)
492 })
490 })
493 .unwrap_or(self.body.len());
491 .unwrap_or(self.body.len());
494
492
src/editor_assist.rs +54 −6
@@ -64,16 +64,15 @@ fn longest_common_prefix(matches: &[&'static str]) -> Option<&'static str> {
64 64 Some(common)
65 65 }
66 66
67 /// Replace the partial directive after `@` with `completed`; returns new cursor index.
67 /// Replace the partial directive after `@` with `completed`; returns new byte cursor.
68 68 pub fn apply_directive_completion(
69 69 body: &mut String,
70 70 at_index: usize,
71 71 cursor: usize,
72 72 completed: &str,
73 73 ) -> usize {
74 let replace_start = at_index + 1;
75 let cursor = cursor.min(body.len());
76 let replace_start = replace_start.min(cursor);
74 let cursor = floor_char_boundary(body, cursor.min(body.len()));
75 let replace_start = floor_char_boundary(body, (at_index + 1).min(cursor));
77 76 body.replace_range(replace_start..cursor, completed);
78 77 at_index + 1 + completed.len()
79 78 }
@@ -204,9 +203,29 @@ pub fn parameter_insert_options(source: &str) -> Vec<InsertOption> {
204 203 out
205 204 }
206 205
206 /// Convert an egui `CCursor` character index to a UTF-8 byte offset in `s`.
207 pub fn char_index_to_byte(s: &str, char_index: usize) -> usize {
208 s.char_indices()
209 .nth(char_index)
210 .map(|(i, _)| i)
211 .unwrap_or(s.len())
212 }
213
214 /// Clamp `index` down to the nearest char boundary (avoids UTF-8 slice panics).
215 pub fn floor_char_boundary(s: &str, mut index: usize) -> usize {
216 if index >= s.len() {
217 return s.len();
218 }
219 while index > 0 && !s.is_char_boundary(index) {
220 index -= 1;
221 }
222 index
223 }
224
207 225 /// If the cursor is after `@` on the current line, return (start_index, typed_prefix).
226 /// `cursor` is a UTF-8 **byte** offset.
208 227 pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {
209 let cursor = cursor.min(body.len());
228 let cursor = floor_char_boundary(body, cursor.min(body.len()));
210 229 let before = &body[..cursor];
211 230 let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
212 231 let line = &before[line_start..];
@@ -221,8 +240,9 @@ pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)>
221 240 Some((line_start + at, fragment.to_string()))
222 241 }
223 242
243 /// Insert `text` at a UTF-8 **byte** cursor offset; returns new body and cursor.
224 244 pub fn insert_at_cursor(body: &str, cursor: usize, text: &str) -> (String, usize) {
225 let cursor = cursor.min(body.len());
245 let cursor = floor_char_boundary(body, cursor.min(body.len()));
226 246 let mut out = String::with_capacity(body.len() + text.len());
227 247 out.push_str(&body[..cursor]);
228 248 out.push_str(text);
@@ -279,4 +299,32 @@ mod tests {
279 299 assert_eq!(text, "aXb");
280 300 assert_eq!(cur, 2);
281 301 }
302
303 #[test]
304 fn char_index_to_byte_handles_multibyte() {
305 let s = "a√b"; // √ is U+221A, three UTF-8 bytes
306 assert_eq!(char_index_to_byte(s, 0), 0);
307 assert_eq!(char_index_to_byte(s, 1), 1);
308 assert_eq!(char_index_to_byte(s, 2), 4);
309 assert_eq!(char_index_to_byte(s, 3), 5);
310 assert_eq!(char_index_to_byte(s, 99), 5);
311 }
312
313 #[test]
314 fn floor_char_boundary_avoids_mid_glyph() {
315 let s = "a√b";
316 assert_eq!(floor_char_boundary(s, 2), 1);
317 assert_eq!(floor_char_boundary(s, 3), 1);
318 assert_eq!(floor_char_boundary(s, 4), 4);
319 }
320
321 #[test]
322 fn insert_and_prefix_survive_sqrt_cursor_misalign() {
323 let body = "[\u{221A}] done";
324 // Deliberately pass a mid-character byte index (inside √).
325 let (text, cur) = insert_at_cursor(body, 2, "!");
326 assert!(text.contains('!'));
327 assert!(text.is_char_boundary(cur));
328 assert!(at_directive_prefix(body, 2).is_none());
329 }
282 330 }
@@ -64,16 +64,15 @@ fn longest_common_prefix(matches: &[&'static str]) -> Option<&'static str> {
64 Some(common)
64 Some(common)
65}
65}
66
66
67/// Replace the partial directive after `@` with `completed`; returns new cursor index.
67/// Replace the partial directive after `@` with `completed`; returns new byte cursor.
68pub fn apply_directive_completion(
68pub fn apply_directive_completion(
69 body: &mut String,
69 body: &mut String,
70 at_index: usize,
70 at_index: usize,
71 cursor: usize,
71 cursor: usize,
72 completed: &str,
72 completed: &str,
73) -> usize {
73) -> usize {
74 let replace_start = at_index + 1;
74 let cursor = floor_char_boundary(body, cursor.min(body.len()));
75 let cursor = cursor.min(body.len());
75 let replace_start = floor_char_boundary(body, (at_index + 1).min(cursor));
76 let replace_start = replace_start.min(cursor);
77 body.replace_range(replace_start..cursor, completed);
76 body.replace_range(replace_start..cursor, completed);
78 at_index + 1 + completed.len()
77 at_index + 1 + completed.len()
79}
78}
@@ -204,9 +203,29 @@ pub fn parameter_insert_options(source: &str) -> Vec<InsertOption> {
204 out
203 out
205}
204}
206
205
206/// Convert an egui `CCursor` character index to a UTF-8 byte offset in `s`.
207pub fn char_index_to_byte(s: &str, char_index: usize) -> usize {
208 s.char_indices()
209 .nth(char_index)
210 .map(|(i, _)| i)
211 .unwrap_or(s.len())
212}
213
214/// Clamp `index` down to the nearest char boundary (avoids UTF-8 slice panics).
215pub fn floor_char_boundary(s: &str, mut index: usize) -> usize {
216 if index >= s.len() {
217 return s.len();
218 }
219 while index > 0 && !s.is_char_boundary(index) {
220 index -= 1;
221 }
222 index
223}
224
207/// If the cursor is after `@` on the current line, return (start_index, typed_prefix).
225/// If the cursor is after `@` on the current line, return (start_index, typed_prefix).
226/// `cursor` is a UTF-8 **byte** offset.
208pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {
227pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)> {
209 let cursor = cursor.min(body.len());
228 let cursor = floor_char_boundary(body, cursor.min(body.len()));
210 let before = &body[..cursor];
229 let before = &body[..cursor];
211 let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
230 let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
212 let line = &before[line_start..];
231 let line = &before[line_start..];
@@ -221,8 +240,9 @@ pub fn at_directive_prefix(body: &str, cursor: usize) -> Option<(usize, String)>
221 Some((line_start + at, fragment.to_string()))
240 Some((line_start + at, fragment.to_string()))
222}
241}
223
242
243/// Insert `text` at a UTF-8 **byte** cursor offset; returns new body and cursor.
224pub fn insert_at_cursor(body: &str, cursor: usize, text: &str) -> (String, usize) {
244pub fn insert_at_cursor(body: &str, cursor: usize, text: &str) -> (String, usize) {
225 let cursor = cursor.min(body.len());
245 let cursor = floor_char_boundary(body, cursor.min(body.len()));
226 let mut out = String::with_capacity(body.len() + text.len());
246 let mut out = String::with_capacity(body.len() + text.len());
227 out.push_str(&body[..cursor]);
247 out.push_str(&body[..cursor]);
228 out.push_str(text);
248 out.push_str(text);
@@ -279,4 +299,32 @@ mod tests {
279 assert_eq!(text, "aXb");
299 assert_eq!(text, "aXb");
280 assert_eq!(cur, 2);
300 assert_eq!(cur, 2);
281 }
301 }
302
303 #[test]
304 fn char_index_to_byte_handles_multibyte() {
305 let s = "a√b"; // √ is U+221A, three UTF-8 bytes
306 assert_eq!(char_index_to_byte(s, 0), 0);
307 assert_eq!(char_index_to_byte(s, 1), 1);
308 assert_eq!(char_index_to_byte(s, 2), 4);
309 assert_eq!(char_index_to_byte(s, 3), 5);
310 assert_eq!(char_index_to_byte(s, 99), 5);
311 }
312
313 #[test]
314 fn floor_char_boundary_avoids_mid_glyph() {
315 let s = "a√b";
316 assert_eq!(floor_char_boundary(s, 2), 1);
317 assert_eq!(floor_char_boundary(s, 3), 1);
318 assert_eq!(floor_char_boundary(s, 4), 4);
319 }
320
321 #[test]
322 fn insert_and_prefix_survive_sqrt_cursor_misalign() {
323 let body = "[\u{221A}] done";
324 // Deliberately pass a mid-character byte index (inside √).
325 let (text, cur) = insert_at_cursor(body, 2, "!");
326 assert!(text.contains('!'));
327 assert!(text.is_char_boundary(cur));
328 assert!(at_directive_prefix(body, 2).is_none());
329 }
282}
330}