BlueGit

ict rusty-teams

fix(input): token lines wrap with font overrides and hanging indent

- lines with inline tokens use measured_wrap_boundaries instead of gpui's LineWrapper
- tokens stay atomic; their width plus shaped text segments drive the break

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-08 · 7 weeks ago

Hash
0c8e684d7dba11cbf2ac7273fc3a91b065b0ffd0
Parent
ee339d2b
2 files changed +95 −46
vendor/gpui-base/PATCH.md +1 −1
@@ -17,11 +17,11 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
17 17 | `src/input/base/kind.rs` | `TextareaExtras` (text + range decorations) as `TextareaMode::Extras`; edit tracking and reset hooks; sealed `DecoratedMode` trait for both multi-line modes | decorations on `TextareaState` (composer stays a textarea) |
18 18 | `src/input/editor/decorations.rs` | `TextDecorationCollection<M = EditorMode>`, `RangeDecorationCollection<M = EditorMode>`, `create_*_collection` on `impl<M: DecoratedMode>`; `DecorationCollections` / `TrackedDecoration` `pub` + `#[doc(hidden)]` | same API on textarea and editor; default type param keeps editor code unchanged |
19 19 | `src/input/editor/mod.rs`, `src/input/mod.rs` | doc no longer says decorations are editor-only; export `DecoratedMode`, `TextareaExtras` | docs, API |
20 | `src/input/editor/display_map/text_wrapper.rs` | lines with inline tokens wrap through `measured_wrap_boundaries` too (`atomic` token ranges, token width + shaped text segments) instead of gpui's `LineWrapper` | mention lines honour font overrides and hanging indent |
20 21
21 22 ## Limits
22 23
23 24 - Syntax-highlight (tree-sitter / LSP) bold or italic still wraps with the base font; only decorations feed wrapping.
24 - Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.
25 25 - Hanging-indent markers are dropped by an edit inside them; the app sets them again on change.
26 26 - The unwrapped longest-line width (soft wrap off) ignores font overrides.
27 27
@@ -17,11 +17,11 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
17| `src/input/base/kind.rs` | `TextareaExtras` (text + range decorations) as `TextareaMode::Extras`; edit tracking and reset hooks; sealed `DecoratedMode` trait for both multi-line modes | decorations on `TextareaState` (composer stays a textarea) |
17| `src/input/base/kind.rs` | `TextareaExtras` (text + range decorations) as `TextareaMode::Extras`; edit tracking and reset hooks; sealed `DecoratedMode` trait for both multi-line modes | decorations on `TextareaState` (composer stays a textarea) |
18| `src/input/editor/decorations.rs` | `TextDecorationCollection<M = EditorMode>`, `RangeDecorationCollection<M = EditorMode>`, `create_*_collection` on `impl<M: DecoratedMode>`; `DecorationCollections` / `TrackedDecoration` `pub` + `#[doc(hidden)]` | same API on textarea and editor; default type param keeps editor code unchanged |
18| `src/input/editor/decorations.rs` | `TextDecorationCollection<M = EditorMode>`, `RangeDecorationCollection<M = EditorMode>`, `create_*_collection` on `impl<M: DecoratedMode>`; `DecorationCollections` / `TrackedDecoration` `pub` + `#[doc(hidden)]` | same API on textarea and editor; default type param keeps editor code unchanged |
19| `src/input/editor/mod.rs`, `src/input/mod.rs` | doc no longer says decorations are editor-only; export `DecoratedMode`, `TextareaExtras` | docs, API |
19| `src/input/editor/mod.rs`, `src/input/mod.rs` | doc no longer says decorations are editor-only; export `DecoratedMode`, `TextareaExtras` | docs, API |
20| `src/input/editor/display_map/text_wrapper.rs` | lines with inline tokens wrap through `measured_wrap_boundaries` too (`atomic` token ranges, token width + shaped text segments) instead of gpui's `LineWrapper` | mention lines honour font overrides and hanging indent |
20
21
21## Limits
22## Limits
22
23
23- Syntax-highlight (tree-sitter / LSP) bold or italic still wraps with the base font; only decorations feed wrapping.
24- Syntax-highlight (tree-sitter / LSP) bold or italic still wraps with the base font; only decorations feed wrapping.
24- Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.
25- Hanging-indent markers are dropped by an edit inside them; the app sets them again on change.
25- Hanging-indent markers are dropped by an edit inside them; the app sets them again on change.
26- The unwrapped longest-line width (soft wrap off) ignores font overrides.
26- The unwrapped longest-line width (soft wrap off) ignores font overrides.
27
27
vendor/gpui-base/src/input/editor/display_map/text_wrapper.rs +94 −45
@@ -5,8 +5,7 @@ use std::ops::Range;
5 5 use std::rc::Rc;
6 6
7 7 use gpui::{
8 App, Font, LineFragment, Pixels, Point, ShapedLine, Size, TextAlign, TextRun, Window, point,
9 px, size,
8 App, Font, Pixels, Point, ShapedLine, Size, TextAlign, TextRun, Window, point, px, size,
10 9 };
11 10 use ropey::Rope;
12 11 use smallvec::SmallVec;
@@ -32,11 +31,13 @@ pub enum WrappingIndent {
32 31 /// Choose Unicode line-break opportunities using the same shaped widths as
33 32 /// painting. Oversized words fall back to complete graphemes, never UTF-8 bytes.
34 33 /// `hanging_indent` is a byte offset continuation rows align under; it wins over `wrapping_indent`.
34 /// `atomic` ranges (inline tokens) are never split.
35 35 fn measured_wrap_boundaries(
36 36 text: &str,
37 37 width: Pixels,
38 38 wrapping_indent: WrappingIndent,
39 39 hanging_indent: Option<usize>,
40 atomic: &[Range<usize>],
40 41 mut measure: impl FnMut(Range<usize>) -> Pixels,
41 42 ) -> Vec<gpui::Boundary> {
42 43 let mut indent = match hanging_indent {
@@ -56,6 +57,11 @@ fn measured_wrap_boundaries(
56 57 let ends: Vec<usize> = text
57 58 .grapheme_indices(true)
58 59 .map(|(ix, grapheme)| ix + grapheme.len())
60 .filter(|&ix| {
61 !atomic
62 .iter()
63 .any(|range| range.start < ix && ix < range.end)
64 })
59 65 .collect();
60 66 let opportunities: Vec<usize> = unicode_linebreak::linebreaks(text)
61 67 .map(|(ix, _)| ix)
@@ -478,9 +484,6 @@ impl TextWrapper {
478 484 new_text: &Rope,
479 485 cx: &mut App,
480 486 ) {
481 let mut line_wrapper = cx
482 .text_system()
483 .line_wrapper(self.font.clone(), self.font_size);
484 487 let metrics = self.inline_metrics.clone();
485 488 let font_overrides = self.font_overrides.clone();
486 489 let hanging_indents = self.hanging_indents.clone();
@@ -493,8 +496,7 @@ impl TextWrapper {
493 496 range,
494 497 new_text,
495 498 &mut |line_str, wrap_width, line_start| {
496 let mut fragments = Vec::new();
497 let mut offset = 0;
499 let mut tokens = Vec::new();
498 500 let first = metrics.partition_point(|(r, _)| r.end <= line_start);
499 501 for (range, width) in &metrics[first..] {
500 502 if range.start >= line_start + line_str.len() {
@@ -509,43 +511,47 @@ impl TextWrapper {
509 511 {
510 512 continue;
511 513 }
512 if offset < range.start {
513 fragments.push(LineFragment::text(&line_str[offset..range.start]));
514 }
515 fragments.push(LineFragment::element(*width, range.len()));
516 offset = range.end;
514 tokens.push((range, *width));
517 515 }
518 if fragments.is_empty() {
519 return measured_wrap_boundaries(
520 line_str,
521 wrap_width,
522 wrapping_indent,
523 hanging_indent_at(&hanging_indents, line_start, line_str.len()),
524 |range| {
525 let runs = split_run_by_font_overrides(
526 TextRun {
527 len: range.len(),
528 font: font.clone(),
529 color: gpui::black(),
530 background_color: None,
531 underline: None,
532 strikethrough: None,
533 },
534 line_start + range.start..line_start + range.end,
535 &font_overrides,
536 );
537 text_system
538 .layout_line(&line_str[range], font_size, &runs, None)
539 .width
516 let shape = |range: Range<usize>| {
517 if range.is_empty() {
518 return px(0.);
519 }
520 let runs = split_run_by_font_overrides(
521 TextRun {
522 len: range.len(),
523 font: font.clone(),
524 color: gpui::black(),
525 background_color: None,
526 underline: None,
527 strikethrough: None,
540 528 },
529 line_start + range.start..line_start + range.end,
530 &font_overrides,
541 531 );
542 }
543 if offset < line_str.len() {
544 fragments.push(LineFragment::text(&line_str[offset..]));
545 }
546 line_wrapper
547 .wrap_line(&fragments, wrap_width, gpui::IndentAdjustment::SameIndent)
548 .collect()
532 text_system
533 .layout_line(&line_str[range], font_size, &runs, None)
534 .width
535 };
536 let token_ranges: Vec<_> = tokens.iter().map(|(range, _)| range.clone()).collect();
537 measured_wrap_boundaries(
538 line_str,
539 wrap_width,
540 wrapping_indent,
541 hanging_indent_at(&hanging_indents, line_start, line_str.len()),
542 &token_ranges,
543 |range| {
544 let mut width = px(0.);
545 let mut offset = range.start;
546 for (token, token_width) in &tokens {
547 if token.start >= offset && token.end <= range.end {
548 width += shape(offset..token.start) + *token_width;
549 offset = token.end;
550 }
551 }
552 width + shape(offset..range.end)
553 },
554 )
549 555 },
550 556 );
551 557 }
@@ -1386,6 +1392,43 @@ mod tests {
1386 1392 });
1387 1393 }
1388 1394
1395 #[cfg(target_os = "linux")]
1396 #[test]
1397 fn production_wrap_keeps_tokens_whole_and_applies_overrides_and_hanging_indent_on_token_lines()
1398 {
1399 let cx = shaping_test_context();
1400 cx.update(|cx| {
1401 let font = gpui::font("DejaVu Sans");
1402 let font_size = px(14.);
1403 let value = "• iiii @tok iiii iiii iiii iiii";
1404 let token = 9..13;
1405 let width = shaped_width("• iiii ", &font, font_size, cx) + px(30.);
1406 let text = Rope::from(value);
1407 let mut wrapper = TextWrapper::new(font.clone(), font_size, Some(width));
1408 wrapper.wrapping_indent = WrappingIndent::None;
1409 wrapper.update(&text, &(0..0), &text, cx);
1410 wrapper.set_inline_metrics(Rc::from([(token.clone(), px(32.))]), cx);
1411 let rows = wrapper.line(0).unwrap().wrapped_lines.to_vec();
1412 assert!(
1413 rows.iter()
1414 .all(|row| !token.contains(&row.start) || row.start == token.start)
1415 );
1416 assert!(rows.iter().any(|row| row.start == token.start), "{rows:?}");
1417 assert_eq!(wrapper.line(0).unwrap().indent, 0);
1418
1419 wrapper.set_hanging_indents(Rc::from([0..4]), cx);
1420 assert_eq!(wrapper.line(0).unwrap().indent, 2);
1421
1422 let plain = wrapper.line(0).unwrap().wrapped_lines.to_vec();
1423 let mono = FontOverride {
1424 family: Some("DejaVu Sans Mono".into()),
1425 ..Default::default()
1426 };
1427 wrapper.set_font_overrides(Rc::from([(14..value.len(), mono)]), cx);
1428 assert_ne!(wrapper.line(0).unwrap().wrapped_lines.to_vec(), plain);
1429 });
1430 }
1431
1389 1432 #[test]
1390 1433 fn font_override_runs_split_at_span_edges_and_follow_edits() {
1391 1434 let run = TextRun {
@@ -1448,9 +1491,14 @@ mod tests {
1448 1491 &(start..previous.len()),
1449 1492 &inserted,
1450 1493 &mut |line, width, _| {
1451 measured_wrap_boundaries(line, width, WrappingIndent::None, None, |range| {
1452 measure(&line[range])
1453 })
1494 measured_wrap_boundaries(
1495 line,
1496 width,
1497 WrappingIndent::None,
1498 None,
1499 &[],
1500 |range| measure(&line[range]),
1501 )
1454 1502 },
1455 1503 );
1456 1504 let expected = if value.ends_with('s') {
@@ -1468,7 +1516,7 @@ mod tests {
1468 1516 #[test]
1469 1517 fn measured_wrap_preserves_words_graphemes_and_indentation() {
1470 1518 let wrap = |text: &str, width, indent| {
1471 measured_wrap_boundaries(text, px(width), indent, None, |range| {
1519 measured_wrap_boundaries(text, px(width), indent, None, &[], |range| {
1472 1520 px(text[range].graphemes(true).count() as f32)
1473 1521 })
1474 1522 .into_iter()
@@ -1493,6 +1541,7 @@ mod tests {
1493 1541 px(8.),
1494 1542 WrappingIndent::None,
1495 1543 hanging_indent,
1544 &[],
1496 1545 |range| px(text[range].graphemes(true).count() as f32),
1497 1546 )
1498 1547 .into_iter()
@@ -5,8 +5,7 @@ use std::ops::Range;
5use std::rc::Rc;
5use std::rc::Rc;
6
6
7use gpui::{
7use gpui::{
8 App, Font, LineFragment, Pixels, Point, ShapedLine, Size, TextAlign, TextRun, Window, point,
8 App, Font, Pixels, Point, ShapedLine, Size, TextAlign, TextRun, Window, point, px, size,
9 px, size,
10};
9};
11use ropey::Rope;
10use ropey::Rope;
12use smallvec::SmallVec;
11use smallvec::SmallVec;
@@ -32,11 +31,13 @@ pub enum WrappingIndent {
32/// Choose Unicode line-break opportunities using the same shaped widths as
31/// Choose Unicode line-break opportunities using the same shaped widths as
33/// painting. Oversized words fall back to complete graphemes, never UTF-8 bytes.
32/// painting. Oversized words fall back to complete graphemes, never UTF-8 bytes.
34/// `hanging_indent` is a byte offset continuation rows align under; it wins over `wrapping_indent`.
33/// `hanging_indent` is a byte offset continuation rows align under; it wins over `wrapping_indent`.
34/// `atomic` ranges (inline tokens) are never split.
35fn measured_wrap_boundaries(
35fn measured_wrap_boundaries(
36 text: &str,
36 text: &str,
37 width: Pixels,
37 width: Pixels,
38 wrapping_indent: WrappingIndent,
38 wrapping_indent: WrappingIndent,
39 hanging_indent: Option<usize>,
39 hanging_indent: Option<usize>,
40 atomic: &[Range<usize>],
40 mut measure: impl FnMut(Range<usize>) -> Pixels,
41 mut measure: impl FnMut(Range<usize>) -> Pixels,
41) -> Vec<gpui::Boundary> {
42) -> Vec<gpui::Boundary> {
42 let mut indent = match hanging_indent {
43 let mut indent = match hanging_indent {
@@ -56,6 +57,11 @@ fn measured_wrap_boundaries(
56 let ends: Vec<usize> = text
57 let ends: Vec<usize> = text
57 .grapheme_indices(true)
58 .grapheme_indices(true)
58 .map(|(ix, grapheme)| ix + grapheme.len())
59 .map(|(ix, grapheme)| ix + grapheme.len())
60 .filter(|&ix| {
61 !atomic
62 .iter()
63 .any(|range| range.start < ix && ix < range.end)
64 })
59 .collect();
65 .collect();
60 let opportunities: Vec<usize> = unicode_linebreak::linebreaks(text)
66 let opportunities: Vec<usize> = unicode_linebreak::linebreaks(text)
61 .map(|(ix, _)| ix)
67 .map(|(ix, _)| ix)
@@ -478,9 +484,6 @@ impl TextWrapper {
478 new_text: &Rope,
484 new_text: &Rope,
479 cx: &mut App,
485 cx: &mut App,
480 ) {
486 ) {
481 let mut line_wrapper = cx
482 .text_system()
483 .line_wrapper(self.font.clone(), self.font_size);
484 let metrics = self.inline_metrics.clone();
487 let metrics = self.inline_metrics.clone();
485 let font_overrides = self.font_overrides.clone();
488 let font_overrides = self.font_overrides.clone();
486 let hanging_indents = self.hanging_indents.clone();
489 let hanging_indents = self.hanging_indents.clone();
@@ -493,8 +496,7 @@ impl TextWrapper {
493 range,
496 range,
494 new_text,
497 new_text,
495 &mut |line_str, wrap_width, line_start| {
498 &mut |line_str, wrap_width, line_start| {
496 let mut fragments = Vec::new();
499 let mut tokens = Vec::new();
497 let mut offset = 0;
498 let first = metrics.partition_point(|(r, _)| r.end <= line_start);
500 let first = metrics.partition_point(|(r, _)| r.end <= line_start);
499 for (range, width) in &metrics[first..] {
501 for (range, width) in &metrics[first..] {
500 if range.start >= line_start + line_str.len() {
502 if range.start >= line_start + line_str.len() {
@@ -509,43 +511,47 @@ impl TextWrapper {
509 {
511 {
510 continue;
512 continue;
511 }
513 }
512 if offset < range.start {
514 tokens.push((range, *width));
513 fragments.push(LineFragment::text(&line_str[offset..range.start]));
514 }
515 fragments.push(LineFragment::element(*width, range.len()));
516 offset = range.end;
517 }
515 }
518 if fragments.is_empty() {
516 let shape = |range: Range<usize>| {
519 return measured_wrap_boundaries(
517 if range.is_empty() {
520 line_str,
518 return px(0.);
521 wrap_width,
519 }
522 wrapping_indent,
520 let runs = split_run_by_font_overrides(
523 hanging_indent_at(&hanging_indents, line_start, line_str.len()),
521 TextRun {
524 |range| {
522 len: range.len(),
525 let runs = split_run_by_font_overrides(
523 font: font.clone(),
526 TextRun {
524 color: gpui::black(),
527 len: range.len(),
525 background_color: None,
528 font: font.clone(),
526 underline: None,
529 color: gpui::black(),
527 strikethrough: None,
530 background_color: None,
531 underline: None,
532 strikethrough: None,
533 },
534 line_start + range.start..line_start + range.end,
535 &font_overrides,
536 );
537 text_system
538 .layout_line(&line_str[range], font_size, &runs, None)
539 .width
540 },
528 },
529 line_start + range.start..line_start + range.end,
530 &font_overrides,
541 );
531 );
542 }
532 text_system
543 if offset < line_str.len() {
533 .layout_line(&line_str[range], font_size, &runs, None)
544 fragments.push(LineFragment::text(&line_str[offset..]));
534 .width
545 }
535 };
546 line_wrapper
536 let token_ranges: Vec<_> = tokens.iter().map(|(range, _)| range.clone()).collect();
547 .wrap_line(&fragments, wrap_width, gpui::IndentAdjustment::SameIndent)
537 measured_wrap_boundaries(
548 .collect()
538 line_str,
539 wrap_width,
540 wrapping_indent,
541 hanging_indent_at(&hanging_indents, line_start, line_str.len()),
542 &token_ranges,
543 |range| {
544 let mut width = px(0.);
545 let mut offset = range.start;
546 for (token, token_width) in &tokens {
547 if token.start >= offset && token.end <= range.end {
548 width += shape(offset..token.start) + *token_width;
549 offset = token.end;
550 }
551 }
552 width + shape(offset..range.end)
553 },
554 )
549 },
555 },
550 );
556 );
551 }
557 }
@@ -1386,6 +1392,43 @@ mod tests {
1386 });
1392 });
1387 }
1393 }
1388
1394
1395 #[cfg(target_os = "linux")]
1396 #[test]
1397 fn production_wrap_keeps_tokens_whole_and_applies_overrides_and_hanging_indent_on_token_lines()
1398 {
1399 let cx = shaping_test_context();
1400 cx.update(|cx| {
1401 let font = gpui::font("DejaVu Sans");
1402 let font_size = px(14.);
1403 let value = "• iiii @tok iiii iiii iiii iiii";
1404 let token = 9..13;
1405 let width = shaped_width("• iiii ", &font, font_size, cx) + px(30.);
1406 let text = Rope::from(value);
1407 let mut wrapper = TextWrapper::new(font.clone(), font_size, Some(width));
1408 wrapper.wrapping_indent = WrappingIndent::None;
1409 wrapper.update(&text, &(0..0), &text, cx);
1410 wrapper.set_inline_metrics(Rc::from([(token.clone(), px(32.))]), cx);
1411 let rows = wrapper.line(0).unwrap().wrapped_lines.to_vec();
1412 assert!(
1413 rows.iter()
1414 .all(|row| !token.contains(&row.start) || row.start == token.start)
1415 );
1416 assert!(rows.iter().any(|row| row.start == token.start), "{rows:?}");
1417 assert_eq!(wrapper.line(0).unwrap().indent, 0);
1418
1419 wrapper.set_hanging_indents(Rc::from([0..4]), cx);
1420 assert_eq!(wrapper.line(0).unwrap().indent, 2);
1421
1422 let plain = wrapper.line(0).unwrap().wrapped_lines.to_vec();
1423 let mono = FontOverride {
1424 family: Some("DejaVu Sans Mono".into()),
1425 ..Default::default()
1426 };
1427 wrapper.set_font_overrides(Rc::from([(14..value.len(), mono)]), cx);
1428 assert_ne!(wrapper.line(0).unwrap().wrapped_lines.to_vec(), plain);
1429 });
1430 }
1431
1389 #[test]
1432 #[test]
1390 fn font_override_runs_split_at_span_edges_and_follow_edits() {
1433 fn font_override_runs_split_at_span_edges_and_follow_edits() {
1391 let run = TextRun {
1434 let run = TextRun {
@@ -1448,9 +1491,14 @@ mod tests {
1448 &(start..previous.len()),
1491 &(start..previous.len()),
1449 &inserted,
1492 &inserted,
1450 &mut |line, width, _| {
1493 &mut |line, width, _| {
1451 measured_wrap_boundaries(line, width, WrappingIndent::None, None, |range| {
1494 measured_wrap_boundaries(
1452 measure(&line[range])
1495 line,
1453 })
1496 width,
1497 WrappingIndent::None,
1498 None,
1499 &[],
1500 |range| measure(&line[range]),
1501 )
1454 },
1502 },
1455 );
1503 );
1456 let expected = if value.ends_with('s') {
1504 let expected = if value.ends_with('s') {
@@ -1468,7 +1516,7 @@ mod tests {
1468 #[test]
1516 #[test]
1469 fn measured_wrap_preserves_words_graphemes_and_indentation() {
1517 fn measured_wrap_preserves_words_graphemes_and_indentation() {
1470 let wrap = |text: &str, width, indent| {
1518 let wrap = |text: &str, width, indent| {
1471 measured_wrap_boundaries(text, px(width), indent, None, |range| {
1519 measured_wrap_boundaries(text, px(width), indent, None, &[], |range| {
1472 px(text[range].graphemes(true).count() as f32)
1520 px(text[range].graphemes(true).count() as f32)
1473 })
1521 })
1474 .into_iter()
1522 .into_iter()
@@ -1493,6 +1541,7 @@ mod tests {
1493 px(8.),
1541 px(8.),
1494 WrappingIndent::None,
1542 WrappingIndent::None,
1495 hanging_indent,
1543 hanging_indent,
1544 &[],
1496 |range| px(text[range].graphemes(true).count() as f32),
1545 |range| px(text[range].graphemes(true).count() as f32),
1497 )
1546 )
1498 .into_iter()
1547 .into_iter()