feat(input): whole-line indents for textarea lines
- set_line_indents starts every row of a line at its indent and wraps that much earlier; caret, selection, hit testing and up/down follow - Combines with a hanging indent on the same line Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -18,6 +18,7 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
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
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 |
21
| `src/input/base/state.rs`, `src/input/editor/display_map/*`, `src/input/base/element.rs` | `set_line_indents(Vec<(line start, Pixels)>)`: every row of a line starts at its indent and wraps that much earlier; `LineLayout.first_indent` feeds caret, selection, hit testing and up/down; combines with a hanging indent | nested list levels, quote gap and code padding without padding characters in the text |
21
22
| `src/input/editor/decorations.rs` | `RangeDecorationStyle::{Pill, Block, Bar}`, `RangeDecoration::with_border()` / `with_radius()`; Block and Bar keep empty ranges (`TrackedDecoration::keeps_empty`) and the index finds them | rounded inline code pills, full-width code block fill, quote bar; a block over one empty line |
22
23
| `src/input/base/element.rs` | `layout_range_decoration_quads()` paints pills, blocks and bars as quads below the fills | rounded corners and borders, which paths cannot draw |
23
24
| `src/input/base/state.rs` | `apply_edits(&[(Range, String)])` on all states | several edits in one undo step without touching inline tokens between them |
@@ -18,6 +18,7 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in18
| `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
| `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 |21
| `src/input/base/state.rs`, `src/input/editor/display_map/*`, `src/input/base/element.rs` | `set_line_indents(Vec<(line start, Pixels)>)`: every row of a line starts at its indent and wraps that much earlier; `LineLayout.first_indent` feeds caret, selection, hit testing and up/down; combines with a hanging indent | nested list levels, quote gap and code padding without padding characters in the text |21
| `src/input/editor/decorations.rs` | `RangeDecorationStyle::{Pill, Block, Bar}`, `RangeDecoration::with_border()` / `with_radius()`; Block and Bar keep empty ranges (`TrackedDecoration::keeps_empty`) and the index finds them | rounded inline code pills, full-width code block fill, quote bar; a block over one empty line |22
| `src/input/editor/decorations.rs` | `RangeDecorationStyle::{Pill, Block, Bar}`, `RangeDecoration::with_border()` / `with_radius()`; Block and Bar keep empty ranges (`TrackedDecoration::keeps_empty`) and the index finds them | rounded inline code pills, full-width code block fill, quote bar; a block over one empty line |22
| `src/input/base/element.rs` | `layout_range_decoration_quads()` paints pills, blocks and bars as quads below the fills | rounded corners and borders, which paths cannot draw |23
| `src/input/base/element.rs` | `layout_range_decoration_quads()` paints pills, blocks and bars as quads below the fills | rounded corners and borders, which paths cannot draw |23
| `src/input/base/state.rs` | `apply_edits(&[(Range, String)])` on all states | several edits in one undo step without touching inline tokens between them |24
| `src/input/base/state.rs` | `apply_edits(&[(Range, String)])` on all states | several edits in one undo step without touching inline tokens between them |@@ -780,7 +780,7 @@ impl<M: InputModeKind> TextElement<M> {
780
780
// A wrap boundary is not a newline and must not get such a cell.
781
781
let newline = is_last && range.start <= end && range.end > end;
782
782
if start_ix < end_ix || newline {
783
let indent = if row == 0 { px(0.) } else { line.wrap_indent };
783
let indent = line.line_indent(row);
784
784
let left = alignment + indent + shaped.x_for_index(start_ix.min(end) - offset);
785
785
let right = alignment
786
786
+ indent
@@ -2002,6 +2002,7 @@ impl<M: InputModeKind> TextElement<M> {
2002
2002
LineLayout::new()
2003
2003
.inline_lines(lines)
2004
2004
.wrap_indent(wrap_indent)
2005
.first_indent(state.display_map.line_indent(line_start))
2005
2006
})
2006
2007
.collect()
2007
2008
}
@@ -2199,6 +2200,11 @@ impl<M: InputModeKind> TextElement<M> {
2199
2200
let line_layout = LineLayout::new()
2200
2201
.lines(wrapped_lines)
2201
2202
.wrap_indent(wrap_indent)
2203
.first_indent(
2204
state
2205
.display_map
2206
.line_indent(last_layout.visible_line_byte_offsets[vi]),
2207
)
2202
2208
.with_background(line_has_background)
2203
2209
.with_whitespaces(whitespace_indicators.clone());
2204
2210
lines.push(line_layout);
@@ -780,7 +780,7 @@ impl<M: InputModeKind> TextElement<M> {780
// A wrap boundary is not a newline and must not get such a cell.780
// A wrap boundary is not a newline and must not get such a cell.781
let newline = is_last && range.start <= end && range.end > end;781
let newline = is_last && range.start <= end && range.end > end;782
if start_ix < end_ix || newline {782
if start_ix < end_ix || newline {783
let indent = if row == 0 { px(0.) } else { line.wrap_indent };783
let indent = line.line_indent(row);784
let left = alignment + indent + shaped.x_for_index(start_ix.min(end) - offset);784
let left = alignment + indent + shaped.x_for_index(start_ix.min(end) - offset);785
let right = alignment785
let right = alignment786
+ indent786
+ indent@@ -2002,6 +2002,7 @@ impl<M: InputModeKind> TextElement<M> {2002
LineLayout::new()2002
LineLayout::new()2003
.inline_lines(lines)2003
.inline_lines(lines)2004
.wrap_indent(wrap_indent)2004
.wrap_indent(wrap_indent)2005
.first_indent(state.display_map.line_indent(line_start))2005
})2006
})2006
.collect()2007
.collect()2007
}2008
}@@ -2199,6 +2200,11 @@ impl<M: InputModeKind> TextElement<M> {2199
let line_layout = LineLayout::new()2200
let line_layout = LineLayout::new()2200
.lines(wrapped_lines)2201
.lines(wrapped_lines)2201
.wrap_indent(wrap_indent)2202
.wrap_indent(wrap_indent)2203
.first_indent(2204
state2205
.display_map2206
.line_indent(last_layout.visible_line_byte_offsets[vi]),2207
)2202
.with_background(line_has_background)2208
.with_background(line_has_background)2203
.with_whitespaces(whitespace_indicators.clone());2209
.with_whitespaces(whitespace_indicators.clone());2204
lines.push(line_layout);2210
lines.push(line_layout);@@ -8763,6 +8763,64 @@ mod tests {
8763
8763
});
8764
8764
}
8765
8765
8766
#[gpui::test]
8767
fn test_line_indent_shifts_every_row_and_combines_with_a_hanging_indent(
8768
cx: &mut TestAppContext,
8769
) {
8770
cx.update(crate::init);
8771
let text = format!("top\n• {}", "a ".repeat(300));
8772
let view = InputView::build_textarea(cx, move |state| state.rows(4).default_value(text));
8773
let mut visual = VisualTestContext::from_window(view.window_handle.into(), cx);
8774
visual.update(|window, cx| window.draw(cx).clear(cx));
8775
let plain_rows = view.input.read_with(&visual, |state, _| {
8776
state.last_layout.as_ref().unwrap().lines[1]
8777
.wrapped_lines
8778
.iter()
8779
.map(|row| row.len)
8780
.collect::<Vec<_>>()
8781
});
8782
let indent = px(18.);
8783
visual.update(|_, cx| {
8784
view.input.update(cx, |state, cx| {
8785
state.set_line_indents(vec![(4, indent), (2, px(30.))], cx);
8786
state.set_hanging_indents(vec![4..8], cx);
8787
})
8788
});
8789
visual.update(|window, cx| window.draw(cx).clear(cx));
8790
visual.update(|window, cx| {
8791
view.input.update(cx, |state, cx| {
8792
let layout = state.last_layout.as_ref().unwrap();
8793
assert_eq!(layout.lines[0].first_indent, px(0.));
8794
let line = &layout.lines[1];
8795
assert_eq!(line.first_indent, indent);
8796
assert!(line.wrapped_lines[0].len < plain_rows[0]);
8797
for row in line.wrapped_lines.iter() {
8798
assert!(row.width + indent <= layout.wrap_width.unwrap() + px(1.));
8799
}
8800
let marker_x = line.wrapped_lines[0].x_for_index(4);
8801
assert_eq!(
8802
line.position_for_index(0, layout, false),
8803
Some(point(indent, px(0.)))
8804
);
8805
let row_start = line.wrapped_lines[0].len;
8806
assert_eq!(
8807
line.position_for_index(row_start, layout, false),
8808
Some(point(indent + marker_x, layout.line_height))
8809
);
8810
let (hit, _) = line
8811
.closest_index_for_position(
8812
point(indent + marker_x + px(1.), layout.line_height + px(1.)),
8813
layout,
8814
)
8815
.unwrap();
8816
assert_eq!(hit, row_start);
8817
state.set_selected_range(4 + row_start..4 + row_start, cx);
8818
state.up(&MoveUp, window, cx);
8819
assert_eq!(state.cursor(), 8);
8820
});
8821
});
8822
}
8823
8766
8824
#[gpui::test]
8767
8825
fn test_editor_decorations_follow_typing(cx: &mut TestAppContext) {
8768
8826
let view = InputView::<EditorMode>::new(cx);
@@ -10787,6 +10845,27 @@ impl<M: crate::input::MultiLineMode> InputBaseState<M> {
10787
10845
self.display_map.set_hanging_indents(markers.into(), cx);
10788
10846
cx.notify();
10789
10847
}
10848
10849
/// Every row of the line starting at each byte offset starts `indent` further right,
10850
/// and wraps that much earlier. Combines with a hanging indent on the same line.
10851
/// Entries for offsets that are not line starts are ignored; set them again on change.
10852
pub fn set_line_indents(&mut self, indents: Vec<(usize, Pixels)>, cx: &mut Context<Self>) {
10853
let mut indents: Vec<(usize, Pixels)> = indents
10854
.into_iter()
10855
.filter(|(start, indent)| {
10856
*indent > px(0.)
10857
&& *start <= self.text.len()
10858
&& self
10859
.text
10860
.line_start_offset(self.text.offset_to_point(*start).row)
10861
== *start
10862
})
10863
.collect();
10864
indents.sort_by_key(|(start, _)| *start);
10865
indents.dedup_by_key(|(start, _)| *start);
10866
self.display_map.set_line_indents(indents.into(), cx);
10867
cx.notify();
10868
}
10790
10869
}
10791
10870
10792
10871
/// Methods that only ordinary multi-line text offers.
@@ -8763,6 +8763,64 @@ mod tests {8763
});8763
});8764
}8764
}8765
8765
8766
#[gpui::test]8767
fn test_line_indent_shifts_every_row_and_combines_with_a_hanging_indent(8768
cx: &mut TestAppContext,8769
) {8770
cx.update(crate::init);8771
let text = format!("top\n• {}", "a ".repeat(300));8772
let view = InputView::build_textarea(cx, move |state| state.rows(4).default_value(text));8773
let mut visual = VisualTestContext::from_window(view.window_handle.into(), cx);8774
visual.update(|window, cx| window.draw(cx).clear(cx));8775
let plain_rows = view.input.read_with(&visual, |state, _| {8776
state.last_layout.as_ref().unwrap().lines[1]8777
.wrapped_lines8778
.iter()8779
.map(|row| row.len)8780
.collect::<Vec<_>>()8781
});8782
let indent = px(18.);8783
visual.update(|_, cx| {8784
view.input.update(cx, |state, cx| {8785
state.set_line_indents(vec![(4, indent), (2, px(30.))], cx);8786
state.set_hanging_indents(vec![4..8], cx);8787
})8788
});8789
visual.update(|window, cx| window.draw(cx).clear(cx));8790
visual.update(|window, cx| {8791
view.input.update(cx, |state, cx| {8792
let layout = state.last_layout.as_ref().unwrap();8793
assert_eq!(layout.lines[0].first_indent, px(0.));8794
let line = &layout.lines[1];8795
assert_eq!(line.first_indent, indent);8796
assert!(line.wrapped_lines[0].len < plain_rows[0]);8797
for row in line.wrapped_lines.iter() {8798
assert!(row.width + indent <= layout.wrap_width.unwrap() + px(1.));8799
}8800
let marker_x = line.wrapped_lines[0].x_for_index(4);8801
assert_eq!(8802
line.position_for_index(0, layout, false),8803
Some(point(indent, px(0.)))8804
);8805
let row_start = line.wrapped_lines[0].len;8806
assert_eq!(8807
line.position_for_index(row_start, layout, false),8808
Some(point(indent + marker_x, layout.line_height))8809
);8810
let (hit, _) = line8811
.closest_index_for_position(8812
point(indent + marker_x + px(1.), layout.line_height + px(1.)),8813
layout,8814
)8815
.unwrap();8816
assert_eq!(hit, row_start);8817
state.set_selected_range(4 + row_start..4 + row_start, cx);8818
state.up(&MoveUp, window, cx);8819
assert_eq!(state.cursor(), 8);8820
});8821
});8822
}8823
8766
#[gpui::test]8824
#[gpui::test]8767
fn test_editor_decorations_follow_typing(cx: &mut TestAppContext) {8825
fn test_editor_decorations_follow_typing(cx: &mut TestAppContext) {8768
let view = InputView::<EditorMode>::new(cx);8826
let view = InputView::<EditorMode>::new(cx);@@ -10787,6 +10845,27 @@ impl<M: crate::input::MultiLineMode> InputBaseState<M> {10787
self.display_map.set_hanging_indents(markers.into(), cx);10845
self.display_map.set_hanging_indents(markers.into(), cx);10788
cx.notify();10846
cx.notify();10789
}10847
}10848
10849
/// Every row of the line starting at each byte offset starts `indent` further right,10850
/// and wraps that much earlier. Combines with a hanging indent on the same line.10851
/// Entries for offsets that are not line starts are ignored; set them again on change.10852
pub fn set_line_indents(&mut self, indents: Vec<(usize, Pixels)>, cx: &mut Context<Self>) {10853
let mut indents: Vec<(usize, Pixels)> = indents10854
.into_iter()10855
.filter(|(start, indent)| {10856
*indent > px(0.)10857
&& *start <= self.text.len()10858
&& self10859
.text10860
.line_start_offset(self.text.offset_to_point(*start).row)10861
== *start10862
})10863
.collect();10864
indents.sort_by_key(|(start, _)| *start);10865
indents.dedup_by_key(|(start, _)| *start);10866
self.display_map.set_line_indents(indents.into(), cx);10867
cx.notify();10868
}10790
}10869
}10791
10870
10792
/// Methods that only ordinary multi-line text offers.10871
/// Methods that only ordinary multi-line text offers.@@ -255,6 +255,20 @@ impl DisplayMap {
255
255
self.rebuild_fold_projection();
256
256
}
257
257
258
/// Start every row of each line at its indent.
259
pub(crate) fn set_line_indents(
260
&mut self,
261
line_indents: std::rc::Rc<[(usize, Pixels)]>,
262
cx: &mut App,
263
) {
264
self.wrap_map.set_line_indents(line_indents, cx);
265
self.rebuild_fold_projection();
266
}
267
268
pub(crate) fn line_indent(&self, line_start: usize) -> Pixels {
269
self.wrap_map.line_indent(line_start)
270
}
271
258
272
pub fn on_layout_changed(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {
259
273
self.wrap_map.on_layout_changed(wrap_width, cx);
260
274
self.rebuild_fold_projection();
@@ -255,6 +255,20 @@ impl DisplayMap {255
self.rebuild_fold_projection();255
self.rebuild_fold_projection();256
}256
}257
257
258
/// Start every row of each line at its indent.259
pub(crate) fn set_line_indents(260
&mut self,261
line_indents: std::rc::Rc<[(usize, Pixels)]>,262
cx: &mut App,263
) {264
self.wrap_map.set_line_indents(line_indents, cx);265
self.rebuild_fold_projection();266
}267
268
pub(crate) fn line_indent(&self, line_start: usize) -> Pixels {269
self.wrap_map.line_indent(line_start)270
}271
258
pub fn on_layout_changed(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {272
pub fn on_layout_changed(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {259
self.wrap_map.on_layout_changed(wrap_width, cx);273
self.wrap_map.on_layout_changed(wrap_width, cx);260
self.rebuild_fold_projection();274
self.rebuild_fold_projection();@@ -180,6 +180,13 @@ fn hanging_indent_at(
180
180
.then(|| marker.end.min(line_start + line_len) - line_start)
181
181
}
182
182
183
/// The indent of every row of the line starting at `line_start`.
184
fn line_indent_at(line_indents: &[(usize, Pixels)], line_start: usize) -> Pixels {
185
line_indents
186
.binary_search_by_key(&line_start, |(start, _)| *start)
187
.map_or(px(0.), |found| line_indents[found].1)
188
}
189
183
190
/// Ranges whose entry differs between two sorted span lists.
184
191
fn changed_ranges<T: PartialEq>(
185
192
old: &[(Range<usize>, T)],
@@ -341,6 +348,8 @@ pub(crate) struct TextWrapper {
341
348
font_overrides: Rc<[(Range<usize>, FontOverride)]>,
342
349
/// Sorted list marker ranges, at most one per line; continuation rows start under their end.
343
350
hanging_indents: Rc<[Range<usize>]>,
351
/// Sorted `(line start, indent)`: every row of that line starts `indent` further right.
352
line_indents: Rc<[(usize, Pixels)]>,
344
353
_initialized: bool,
345
354
}
346
355
@@ -357,6 +366,7 @@ impl TextWrapper {
357
366
inline_metrics: Rc::from([]),
358
367
font_overrides: Rc::from([]),
359
368
hanging_indents: Rc::from([]),
369
line_indents: Rc::from([]),
360
370
_initialized: false,
361
371
}
362
372
}
@@ -487,6 +497,7 @@ impl TextWrapper {
487
497
let metrics = self.inline_metrics.clone();
488
498
let font_overrides = self.font_overrides.clone();
489
499
let hanging_indents = self.hanging_indents.clone();
500
let line_indents = self.line_indents.clone();
490
501
let text_system = gpui::WindowTextSystem::new(cx.text_system().clone());
491
502
let font = self.font.clone();
492
503
let font_size = self.font_size;
@@ -534,6 +545,8 @@ impl TextWrapper {
534
545
.width
535
546
};
536
547
let token_ranges: Vec<_> = tokens.iter().map(|(range, _)| range.clone()).collect();
548
let wrap_width =
549
(wrap_width - line_indent_at(&line_indents, line_start)).max(px(1.));
537
550
measured_wrap_boundaries(
538
551
line_str,
539
552
wrap_width,
@@ -624,6 +637,45 @@ impl TextWrapper {
624
637
self.rewrap_rows_of(&affected, cx);
625
638
}
626
639
640
pub(crate) fn adjust_line_indents(&mut self, range: &Range<usize>, new_len: usize) {
641
if self.line_indents.is_empty() {
642
return;
643
}
644
let shift = new_len as isize - range.len() as isize;
645
self.line_indents = self
646
.line_indents
647
.iter()
648
.filter_map(|(start, indent)| {
649
if *start <= range.start {
650
Some((*start, *indent))
651
} else if *start >= range.end {
652
Some((start.checked_add_signed(shift)?, *indent))
653
} else {
654
None
655
}
656
})
657
.collect();
658
}
659
660
pub(crate) fn set_line_indents(&mut self, line_indents: Rc<[(usize, Pixels)]>, cx: &mut App) {
661
if self.line_indents == line_indents {
662
return;
663
}
664
let as_spans = |indents: &[(usize, Pixels)]| -> Vec<(Range<usize>, Pixels)> {
665
indents
666
.iter()
667
.map(|(start, indent)| (*start..*start, *indent))
668
.collect()
669
};
670
let affected = changed_ranges(&as_spans(&self.line_indents), &as_spans(&line_indents));
671
self.line_indents = line_indents;
672
self.rewrap_rows_of(&affected, cx);
673
}
674
675
pub(crate) fn line_indent(&self, line_start: usize) -> Pixels {
676
line_indent_at(&self.line_indents, line_start)
677
}
678
627
679
pub(crate) fn set_inline_metrics(
628
680
&mut self,
629
681
metrics: Rc<[(Range<usize>, Pixels)]>,
@@ -692,7 +744,9 @@ impl TextWrapper {
692
744
let line = changed_text.slice_line(row);
693
745
let line_start = changed_text.line_start_offset(row);
694
746
let wrapping_indent =
695
if hanging_indent_at(&self.hanging_indents, line_start, line.len()).is_some() {
747
if hanging_indent_at(&self.hanging_indents, line_start, line.len()).is_some()
748
|| line_indent_at(&self.line_indents, line_start) > px(0.)
749
{
696
750
WrappingIndent::Same
697
751
} else {
698
752
self.wrapping_indent
@@ -896,6 +950,8 @@ pub(crate) struct LineLayout {
896
950
/// Extra left offset applied to continuation wrapped lines, used to reserve the first line's
897
951
/// indentation when [`WrappingIndent::Same`] is used.
898
952
pub(crate) wrap_indent: Pixels,
953
/// Left offset of every visual line, from a whole-line indent.
954
pub(crate) first_indent: Pixels,
899
955
pub(crate) longest_width: Pixels,
900
956
pub(crate) whitespace_indicators: Option<WhitespaceIndicators>,
901
957
/// Whitespace indicators: (line_index, x_position, is_tab)
@@ -912,6 +968,7 @@ impl LineLayout {
912
968
longest_width: px(0.),
913
969
wrapped_lines: SmallVec::new(),
914
970
wrap_indent: px(0.),
971
first_indent: px(0.),
915
972
whitespace_chars: Vec::new(),
916
973
whitespace_indicators: None,
917
974
has_background: false,
@@ -930,14 +987,20 @@ impl LineLayout {
930
987
self
931
988
}
932
989
933
/// The pixel indent applied to the given visual line, relative to the line's
934
/// leading text. Only continuation lines (index > 0) are indented.
990
/// Set the left offset of every visual line.
991
pub(crate) fn first_indent(mut self, first_indent: Pixels) -> Self {
992
self.first_indent = first_indent;
993
self
994
}
995
996
/// The pixel indent applied to the given visual line: the whole-line indent, plus the
997
/// continuation indent for lines after the first.
935
998
#[inline]
936
fn line_indent(&self, line_index: usize) -> Pixels {
999
pub(crate) fn line_indent(&self, line_index: usize) -> Pixels {
937
1000
if line_index == 0 {
938
px(0.)
1001
self.first_indent
939
1002
} else {
940
self.wrap_indent
1003
self.first_indent + self.wrap_indent
941
1004
}
942
1005
}
943
1006
@@ -180,6 +180,13 @@ fn hanging_indent_at(180
.then(|| marker.end.min(line_start + line_len) - line_start)180
.then(|| marker.end.min(line_start + line_len) - line_start)181
}181
}182
182
183
/// The indent of every row of the line starting at `line_start`.184
fn line_indent_at(line_indents: &[(usize, Pixels)], line_start: usize) -> Pixels {185
line_indents186
.binary_search_by_key(&line_start, |(start, _)| *start)187
.map_or(px(0.), |found| line_indents[found].1)188
}189
183
/// Ranges whose entry differs between two sorted span lists.190
/// Ranges whose entry differs between two sorted span lists.184
fn changed_ranges<T: PartialEq>(191
fn changed_ranges<T: PartialEq>(185
old: &[(Range<usize>, T)],192
old: &[(Range<usize>, T)],@@ -341,6 +348,8 @@ pub(crate) struct TextWrapper {341
font_overrides: Rc<[(Range<usize>, FontOverride)]>,348
font_overrides: Rc<[(Range<usize>, FontOverride)]>,342
/// Sorted list marker ranges, at most one per line; continuation rows start under their end.349
/// Sorted list marker ranges, at most one per line; continuation rows start under their end.343
hanging_indents: Rc<[Range<usize>]>,350
hanging_indents: Rc<[Range<usize>]>,351
/// Sorted `(line start, indent)`: every row of that line starts `indent` further right.352
line_indents: Rc<[(usize, Pixels)]>,344
_initialized: bool,353
_initialized: bool,345
}354
}346
355
@@ -357,6 +366,7 @@ impl TextWrapper {357
inline_metrics: Rc::from([]),366
inline_metrics: Rc::from([]),358
font_overrides: Rc::from([]),367
font_overrides: Rc::from([]),359
hanging_indents: Rc::from([]),368
hanging_indents: Rc::from([]),369
line_indents: Rc::from([]),360
_initialized: false,370
_initialized: false,361
}371
}362
}372
}@@ -487,6 +497,7 @@ impl TextWrapper {487
let metrics = self.inline_metrics.clone();497
let metrics = self.inline_metrics.clone();488
let font_overrides = self.font_overrides.clone();498
let font_overrides = self.font_overrides.clone();489
let hanging_indents = self.hanging_indents.clone();499
let hanging_indents = self.hanging_indents.clone();500
let line_indents = self.line_indents.clone();490
let text_system = gpui::WindowTextSystem::new(cx.text_system().clone());501
let text_system = gpui::WindowTextSystem::new(cx.text_system().clone());491
let font = self.font.clone();502
let font = self.font.clone();492
let font_size = self.font_size;503
let font_size = self.font_size;@@ -534,6 +545,8 @@ impl TextWrapper {534
.width545
.width535
};546
};536
let token_ranges: Vec<_> = tokens.iter().map(|(range, _)| range.clone()).collect();547
let token_ranges: Vec<_> = tokens.iter().map(|(range, _)| range.clone()).collect();548
let wrap_width =549
(wrap_width - line_indent_at(&line_indents, line_start)).max(px(1.));537
measured_wrap_boundaries(550
measured_wrap_boundaries(538
line_str,551
line_str,539
wrap_width,552
wrap_width,@@ -624,6 +637,45 @@ impl TextWrapper {624
self.rewrap_rows_of(&affected, cx);637
self.rewrap_rows_of(&affected, cx);625
}638
}626
639
640
pub(crate) fn adjust_line_indents(&mut self, range: &Range<usize>, new_len: usize) {641
if self.line_indents.is_empty() {642
return;643
}644
let shift = new_len as isize - range.len() as isize;645
self.line_indents = self646
.line_indents647
.iter()648
.filter_map(|(start, indent)| {649
if *start <= range.start {650
Some((*start, *indent))651
} else if *start >= range.end {652
Some((start.checked_add_signed(shift)?, *indent))653
} else {654
None655
}656
})657
.collect();658
}659
660
pub(crate) fn set_line_indents(&mut self, line_indents: Rc<[(usize, Pixels)]>, cx: &mut App) {661
if self.line_indents == line_indents {662
return;663
}664
let as_spans = |indents: &[(usize, Pixels)]| -> Vec<(Range<usize>, Pixels)> {665
indents666
.iter()667
.map(|(start, indent)| (*start..*start, *indent))668
.collect()669
};670
let affected = changed_ranges(&as_spans(&self.line_indents), &as_spans(&line_indents));671
self.line_indents = line_indents;672
self.rewrap_rows_of(&affected, cx);673
}674
675
pub(crate) fn line_indent(&self, line_start: usize) -> Pixels {676
line_indent_at(&self.line_indents, line_start)677
}678
627
pub(crate) fn set_inline_metrics(679
pub(crate) fn set_inline_metrics(628
&mut self,680
&mut self,629
metrics: Rc<[(Range<usize>, Pixels)]>,681
metrics: Rc<[(Range<usize>, Pixels)]>,@@ -692,7 +744,9 @@ impl TextWrapper {692
let line = changed_text.slice_line(row);744
let line = changed_text.slice_line(row);693
let line_start = changed_text.line_start_offset(row);745
let line_start = changed_text.line_start_offset(row);694
let wrapping_indent =746
let wrapping_indent =695
if hanging_indent_at(&self.hanging_indents, line_start, line.len()).is_some() {747
if hanging_indent_at(&self.hanging_indents, line_start, line.len()).is_some()748
|| line_indent_at(&self.line_indents, line_start) > px(0.)749
{696
WrappingIndent::Same750
WrappingIndent::Same697
} else {751
} else {698
self.wrapping_indent752
self.wrapping_indent@@ -896,6 +950,8 @@ pub(crate) struct LineLayout {896
/// Extra left offset applied to continuation wrapped lines, used to reserve the first line's950
/// Extra left offset applied to continuation wrapped lines, used to reserve the first line's897
/// indentation when [`WrappingIndent::Same`] is used.951
/// indentation when [`WrappingIndent::Same`] is used.898
pub(crate) wrap_indent: Pixels,952
pub(crate) wrap_indent: Pixels,953
/// Left offset of every visual line, from a whole-line indent.954
pub(crate) first_indent: Pixels,899
pub(crate) longest_width: Pixels,955
pub(crate) longest_width: Pixels,900
pub(crate) whitespace_indicators: Option<WhitespaceIndicators>,956
pub(crate) whitespace_indicators: Option<WhitespaceIndicators>,901
/// Whitespace indicators: (line_index, x_position, is_tab)957
/// Whitespace indicators: (line_index, x_position, is_tab)@@ -912,6 +968,7 @@ impl LineLayout {912
longest_width: px(0.),968
longest_width: px(0.),913
wrapped_lines: SmallVec::new(),969
wrapped_lines: SmallVec::new(),914
wrap_indent: px(0.),970
wrap_indent: px(0.),971
first_indent: px(0.),915
whitespace_chars: Vec::new(),972
whitespace_chars: Vec::new(),916
whitespace_indicators: None,973
whitespace_indicators: None,917
has_background: false,974
has_background: false,@@ -930,14 +987,20 @@ impl LineLayout {930
self987
self931
}988
}932
989
933
/// The pixel indent applied to the given visual line, relative to the line's990
/// Set the left offset of every visual line.934
/// leading text. Only continuation lines (index > 0) are indented.991
pub(crate) fn first_indent(mut self, first_indent: Pixels) -> Self {992
self.first_indent = first_indent;993
self994
}995
996
/// The pixel indent applied to the given visual line: the whole-line indent, plus the997
/// continuation indent for lines after the first.935
#[inline]998
#[inline]936
fn line_indent(&self, line_index: usize) -> Pixels {999
pub(crate) fn line_indent(&self, line_index: usize) -> Pixels {937
if line_index == 0 {1000
if line_index == 0 {938
px(0.)1001
self.first_indent939
} else {1002
} else {940
self.wrap_indent1003
self.first_indent + self.wrap_indent941
}1004
}942
}1005
}943
1006
@@ -110,6 +110,7 @@ impl WrapMap {
110
110
self.wrapper.adjust_inline_metrics(range, new_text.len());
111
111
self.wrapper.adjust_font_overrides(range, new_text.len());
112
112
self.wrapper.adjust_hanging_indents(range, new_text.len());
113
self.wrapper.adjust_line_indents(range, new_text.len());
113
114
self.wrapper.update(changed_text, range, new_text, cx);
114
115
}
115
116
@@ -138,6 +139,18 @@ impl WrapMap {
138
139
self.wrapper.set_hanging_indents(hanging_indents, cx);
139
140
}
140
141
142
pub(super) fn set_line_indents(
143
&mut self,
144
line_indents: std::rc::Rc<[(usize, Pixels)]>,
145
cx: &mut App,
146
) {
147
self.wrapper.set_line_indents(line_indents, cx);
148
}
149
150
pub(super) fn line_indent(&self, line_start: usize) -> Pixels {
151
self.wrapper.line_indent(line_start)
152
}
153
141
154
pub(super) fn on_layout_changed(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {
142
155
self.wrapper.set_wrap_width(wrap_width, cx);
143
156
}
@@ -110,6 +110,7 @@ impl WrapMap {110
self.wrapper.adjust_inline_metrics(range, new_text.len());110
self.wrapper.adjust_inline_metrics(range, new_text.len());111
self.wrapper.adjust_font_overrides(range, new_text.len());111
self.wrapper.adjust_font_overrides(range, new_text.len());112
self.wrapper.adjust_hanging_indents(range, new_text.len());112
self.wrapper.adjust_hanging_indents(range, new_text.len());113
self.wrapper.adjust_line_indents(range, new_text.len());113
self.wrapper.update(changed_text, range, new_text, cx);114
self.wrapper.update(changed_text, range, new_text, cx);114
}115
}115
116
@@ -138,6 +139,18 @@ impl WrapMap {138
self.wrapper.set_hanging_indents(hanging_indents, cx);139
self.wrapper.set_hanging_indents(hanging_indents, cx);139
}140
}140
141
142
pub(super) fn set_line_indents(143
&mut self,144
line_indents: std::rc::Rc<[(usize, Pixels)]>,145
cx: &mut App,146
) {147
self.wrapper.set_line_indents(line_indents, cx);148
}149
150
pub(super) fn line_indent(&self, line_start: usize) -> Pixels {151
self.wrapper.line_indent(line_start)152
}153
141
pub(super) fn on_layout_changed(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {154
pub(super) fn on_layout_changed(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {142
self.wrapper.set_wrap_width(wrap_width, cx);155
self.wrapper.set_wrap_width(wrap_width, cx);143
}156
}