BlueGit

ict rusty-teams

feat(input): text and range decorations on textarea

- TextareaMode gets TextareaExtras with text and range decorations
- create_decorations_collection / create_range_decorations_collection on both multi-line states
- font-family wrapping, fills and frames work in the textarea

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
2ff3b298e00bb1d24eed292ba5c6f51fa068615f
Parent
9ee0d9f3
6 files changed +272 −35
vendor/gpui-base/PATCH.md +3 −1
@@ -14,6 +14,9 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
14 14 | `src/input/base/state.rs` | `set_hanging_indents(markers)` on multi-line states (textarea and editor) | list lines: continuation rows start under the text after `•`, `◦`, `12.` |
15 15 | `src/input/editor/display_map/text_wrapper.rs` | `hanging_indents` on `TextWrapper`, `hanging_indent_at()`, `measured_wrap_boundaries(.., hanging_indent, ..)`, `shift_span()` shared with inline metrics; a line with a marker always wraps as `WrappingIndent::Same` | wrap continuation rows at `wrap_width - marker width`; the existing `LineItem.indent` / `LineLayout.wrap_indent` path then shifts rendering, caret, selection, hit testing and up/down moves |
16 16 | `src/input/editor/display_map/wrap_map.rs`, `display_map.rs` | pass-through for `set_hanging_indents`, adjust on edit | plumbing |
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 |
19 | `src/input/editor/mod.rs`, `src/input/mod.rs` | doc no longer says decorations are editor-only; export `DecoratedMode`, `TextareaExtras` | docs, API |
17 20
18 21 ## Limits
19 22
@@ -21,7 +24,6 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
21 24 - Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.
22 25 - Hanging-indent markers are dropped by an edit inside them; the app sets them again on change.
23 26 - The unwrapped longest-line width (soft wrap off) ignores font families.
24 - Text decorations (and so font families) exist on `EditorState` only, not `TextareaState`.
25 27
26 28 ## Re-apply on a gpui-kit upgrade
27 29
@@ -14,6 +14,9 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
14| `src/input/base/state.rs` | `set_hanging_indents(markers)` on multi-line states (textarea and editor) | list lines: continuation rows start under the text after `•`, `◦`, `12.` |
14| `src/input/base/state.rs` | `set_hanging_indents(markers)` on multi-line states (textarea and editor) | list lines: continuation rows start under the text after `•`, `◦`, `12.` |
15| `src/input/editor/display_map/text_wrapper.rs` | `hanging_indents` on `TextWrapper`, `hanging_indent_at()`, `measured_wrap_boundaries(.., hanging_indent, ..)`, `shift_span()` shared with inline metrics; a line with a marker always wraps as `WrappingIndent::Same` | wrap continuation rows at `wrap_width - marker width`; the existing `LineItem.indent` / `LineLayout.wrap_indent` path then shifts rendering, caret, selection, hit testing and up/down moves |
15| `src/input/editor/display_map/text_wrapper.rs` | `hanging_indents` on `TextWrapper`, `hanging_indent_at()`, `measured_wrap_boundaries(.., hanging_indent, ..)`, `shift_span()` shared with inline metrics; a line with a marker always wraps as `WrappingIndent::Same` | wrap continuation rows at `wrap_width - marker width`; the existing `LineItem.indent` / `LineLayout.wrap_indent` path then shifts rendering, caret, selection, hit testing and up/down moves |
16| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs` | pass-through for `set_hanging_indents`, adjust on edit | plumbing |
16| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs` | pass-through for `set_hanging_indents`, adjust on edit | plumbing |
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 |
19| `src/input/editor/mod.rs`, `src/input/mod.rs` | doc no longer says decorations are editor-only; export `DecoratedMode`, `TextareaExtras` | docs, API |
17
20
18## Limits
21## Limits
19
22
@@ -21,7 +24,6 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
21- Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.
24- Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.
22- 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.
23- The unwrapped longest-line width (soft wrap off) ignores font families.
26- The unwrapped longest-line width (soft wrap off) ignores font families.
24- Text decorations (and so font families) exist on `EditorState` only, not `TextareaState`.
25
27
26## Re-apply on a gpui-kit upgrade
28## Re-apply on a gpui-kit upgrade
27
29
vendor/gpui-base/src/input/base/element.rs +86 −0
@@ -3706,6 +3706,92 @@ mod tests {
3706 3706 });
3707 3707 }
3708 3708
3709 struct TextareaHarness(Entity<crate::input::TextareaState>);
3710
3711 impl Render for TextareaHarness {
3712 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
3713 div().size_full().child(self.0.clone())
3714 }
3715 }
3716
3717 #[cfg(target_os = "linux")]
3718 #[test]
3719 fn textarea_decorations_render_fill_and_wrap_like_the_editor() {
3720 let platform = gpui_platform::current_platform(true);
3721 let mut cx = TestAppContext::build_with_text_system(
3722 gpui::TestDispatcher::new(0),
3723 None,
3724 platform.text_system(),
3725 );
3726 cx.update(crate::init);
3727 let text = "iiiiiiii ".repeat(8);
3728 let mono_range = 9..35;
3729 let mut textarea = None;
3730 let window = cx.open_window(size(px(240.), px(140.)), |window, cx| {
3731 let state = cx.new(|cx| {
3732 crate::input::TextareaState::new(window, cx)
3733 .rows(4)
3734 .default_value(text.clone())
3735 });
3736 textarea = Some(state.clone());
3737 TextareaHarness(state)
3738 });
3739 let textarea = textarea.unwrap();
3740 let mut cx = VisualTestContext::from_window(window.into(), &mut cx);
3741 cx.update(|window, cx| {
3742 window.draw(cx).clear(cx);
3743 let plain_rows: Vec<_> = textarea.read(cx).last_layout.as_ref().unwrap().lines[0]
3744 .wrapped_lines
3745 .iter()
3746 .map(|row| row.len)
3747 .collect();
3748 let collection = textarea.update(cx, |state, cx| {
3749 state.create_range_decorations_collection(
3750 vec![
3751 RangeDecoration::new(mono_range.clone())
3752 .with_style(RangeDecorationStyle::Fill),
3753 ],
3754 cx,
3755 );
3756 state.create_decorations_collection(Vec::new(), cx)
3757 });
3758 collection.set(
3759 vec![
3760 TextDecoration::new(mono_range.clone(), HighlightStyle::default())
3761 .with_font_family("DejaVu Sans Mono"),
3762 ],
3763 cx,
3764 );
3765 window.draw(cx).clear(cx);
3766 let state = textarea.read(cx);
3767 let layout = state.last_layout.as_ref().unwrap();
3768 let line = &layout.lines[0];
3769 let rows: Vec<_> = line.wrapped_lines.iter().map(|row| row.len).collect();
3770 assert!(rows[0] < plain_rows[0], "{rows:?} vs {plain_rows:?}");
3771 for row in line.wrapped_lines.iter() {
3772 assert!(row.width <= layout.wrap_width.unwrap());
3773 }
3774 let (fills, frames) = TextElement::new(textarea.clone()).layout_range_decorations(
3775 layout,
3776 &state.input_bounds,
3777 window,
3778 state.input_bounds,
3779 cx,
3780 );
3781 assert_eq!(fills.len(), 1);
3782 assert!(frames.is_empty());
3783 assert_eq!(collection.get_ranges(cx), vec![mono_range.clone()]);
3784 });
3785 cx.update(|window, cx| {
3786 textarea.update(cx, |state, cx| {
3787 state.set_selected_range(0..0, cx);
3788 state.replace_text_in_range(None, "X", window, cx);
3789 let layers = state.extras.decoration_layers();
3790 assert_eq!(layers.into_iter().flatten().next().unwrap().range, 10..36);
3791 });
3792 });
3793 }
3794
3709 3795 #[gpui::test]
3710 3796 fn horizontal_scroll_is_clamped_before_text_and_caret_layout(cx: &mut TestAppContext) {
3711 3797 let (editor, window) = decoration_editor(cx, "short text", false);
@@ -3706,6 +3706,92 @@ mod tests {
3706 });
3706 });
3707 }
3707 }
3708
3708
3709 struct TextareaHarness(Entity<crate::input::TextareaState>);
3710
3711 impl Render for TextareaHarness {
3712 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
3713 div().size_full().child(self.0.clone())
3714 }
3715 }
3716
3717 #[cfg(target_os = "linux")]
3718 #[test]
3719 fn textarea_decorations_render_fill_and_wrap_like_the_editor() {
3720 let platform = gpui_platform::current_platform(true);
3721 let mut cx = TestAppContext::build_with_text_system(
3722 gpui::TestDispatcher::new(0),
3723 None,
3724 platform.text_system(),
3725 );
3726 cx.update(crate::init);
3727 let text = "iiiiiiii ".repeat(8);
3728 let mono_range = 9..35;
3729 let mut textarea = None;
3730 let window = cx.open_window(size(px(240.), px(140.)), |window, cx| {
3731 let state = cx.new(|cx| {
3732 crate::input::TextareaState::new(window, cx)
3733 .rows(4)
3734 .default_value(text.clone())
3735 });
3736 textarea = Some(state.clone());
3737 TextareaHarness(state)
3738 });
3739 let textarea = textarea.unwrap();
3740 let mut cx = VisualTestContext::from_window(window.into(), &mut cx);
3741 cx.update(|window, cx| {
3742 window.draw(cx).clear(cx);
3743 let plain_rows: Vec<_> = textarea.read(cx).last_layout.as_ref().unwrap().lines[0]
3744 .wrapped_lines
3745 .iter()
3746 .map(|row| row.len)
3747 .collect();
3748 let collection = textarea.update(cx, |state, cx| {
3749 state.create_range_decorations_collection(
3750 vec![
3751 RangeDecoration::new(mono_range.clone())
3752 .with_style(RangeDecorationStyle::Fill),
3753 ],
3754 cx,
3755 );
3756 state.create_decorations_collection(Vec::new(), cx)
3757 });
3758 collection.set(
3759 vec![
3760 TextDecoration::new(mono_range.clone(), HighlightStyle::default())
3761 .with_font_family("DejaVu Sans Mono"),
3762 ],
3763 cx,
3764 );
3765 window.draw(cx).clear(cx);
3766 let state = textarea.read(cx);
3767 let layout = state.last_layout.as_ref().unwrap();
3768 let line = &layout.lines[0];
3769 let rows: Vec<_> = line.wrapped_lines.iter().map(|row| row.len).collect();
3770 assert!(rows[0] < plain_rows[0], "{rows:?} vs {plain_rows:?}");
3771 for row in line.wrapped_lines.iter() {
3772 assert!(row.width <= layout.wrap_width.unwrap());
3773 }
3774 let (fills, frames) = TextElement::new(textarea.clone()).layout_range_decorations(
3775 layout,
3776 &state.input_bounds,
3777 window,
3778 state.input_bounds,
3779 cx,
3780 );
3781 assert_eq!(fills.len(), 1);
3782 assert!(frames.is_empty());
3783 assert_eq!(collection.get_ranges(cx), vec![mono_range.clone()]);
3784 });
3785 cx.update(|window, cx| {
3786 textarea.update(cx, |state, cx| {
3787 state.set_selected_range(0..0, cx);
3788 state.replace_text_in_range(None, "X", window, cx);
3789 let layers = state.extras.decoration_layers();
3790 assert_eq!(layers.into_iter().flatten().next().unwrap().range, 10..36);
3791 });
3792 });
3793 }
3794
3709 #[gpui::test]
3795 #[gpui::test]
3710 fn horizontal_scroll_is_clamped_before_text_and_caret_layout(cx: &mut TestAppContext) {
3796 fn horizontal_scroll_is_clamped_before_text_and_caret_layout(cx: &mut TestAppContext) {
3711 let (editor, window) = decoration_editor(cx, "short text", false);
3797 let (editor, window) = decoration_editor(cx, "short text", false);
vendor/gpui-base/src/input/base/kind.rs +94 −2
@@ -66,6 +66,65 @@ pub trait MultiLineMode: InputModeKind {}
66 66 impl MultiLineMode for TextareaMode {}
67 67 impl MultiLineMode for EditorMode {}
68 68
69 /// The multi-line modes that own text and range decorations.
70 pub trait DecoratedMode: MultiLineMode {
71 #[doc(hidden)]
72 fn decoration_collections(
73 extras: &mut Self::Extras,
74 ) -> (
75 &mut DecorationCollections,
76 &mut DecorationCollections<RangeDecoration>,
77 );
78
79 #[doc(hidden)]
80 fn decoration_collections_ref(
81 extras: &Self::Extras,
82 ) -> (
83 &DecorationCollections,
84 &DecorationCollections<RangeDecoration>,
85 );
86 }
87
88 impl DecoratedMode for TextareaMode {
89 fn decoration_collections(
90 extras: &mut TextareaExtras,
91 ) -> (
92 &mut DecorationCollections,
93 &mut DecorationCollections<RangeDecoration>,
94 ) {
95 (&mut extras.decorations, &mut extras.range_decorations)
96 }
97
98 fn decoration_collections_ref(
99 extras: &TextareaExtras,
100 ) -> (
101 &DecorationCollections,
102 &DecorationCollections<RangeDecoration>,
103 ) {
104 (&extras.decorations, &extras.range_decorations)
105 }
106 }
107
108 impl DecoratedMode for EditorMode {
109 fn decoration_collections(
110 extras: &mut EditorExtras,
111 ) -> (
112 &mut DecorationCollections,
113 &mut DecorationCollections<RangeDecoration>,
114 ) {
115 (&mut extras.decorations, &mut extras.range_decorations)
116 }
117
118 fn decoration_collections_ref(
119 extras: &EditorExtras,
120 ) -> (
121 &DecorationCollections,
122 &DecorationCollections<RangeDecoration>,
123 ) {
124 (&extras.decorations, &extras.range_decorations)
125 }
126 }
127
69 128 /// What the renderer may read out of a mode's extra state.
70 129 ///
71 130 /// Kept apart from [`InputModeKind`] on purpose. This trait is *data*: the
@@ -343,8 +402,41 @@ impl InputModeKind for InputMode {
343 402 impl InputModeKind for TextareaMode {
344 403 const MULTI_LINE: bool = true;
345 404
346 /// Ordinary multi-line text needs nothing beyond the shared engine.
347 type Extras = ();
405 type Extras = TextareaExtras;
406
407 fn reset_annotations(state: &mut InputBaseState<Self>) {
408 state.extras.decorations.clear();
409 state.extras.range_decorations.clear();
410 }
411
412 fn adjust_annotations(
413 state: &mut InputBaseState<Self>,
414 range: &std::ops::Range<usize>,
415 new_len: usize,
416 ) {
417 state.extras.decorations.adjust_for_edit(range, new_len);
418 state
419 .extras
420 .range_decorations
421 .adjust_for_edit(range, new_len);
422 }
423 }
424
425 /// What ordinary multi-line text adds: application decorations.
426 #[derive(Default)]
427 pub struct TextareaExtras {
428 pub(crate) decorations: DecorationCollections,
429 pub(crate) range_decorations: DecorationCollections<RangeDecoration>,
430 }
431
432 impl InputExtras for TextareaExtras {
433 fn decoration_layers(&self) -> Vec<&[TextDecoration]> {
434 self.decorations.iter().collect()
435 }
436
437 fn range_decorations(&self, ranges: &[std::ops::Range<usize>]) -> Vec<&RangeDecoration> {
438 self.range_decorations.intersecting(ranges)
439 }
348 440 }
349 441 // `EditorMode`'s implementation lives with the editor code, next to the
350 442 // language features it dispatches to.
@@ -66,6 +66,65 @@ pub trait MultiLineMode: InputModeKind {}
66impl MultiLineMode for TextareaMode {}
66impl MultiLineMode for TextareaMode {}
67impl MultiLineMode for EditorMode {}
67impl MultiLineMode for EditorMode {}
68
68
69/// The multi-line modes that own text and range decorations.
70pub trait DecoratedMode: MultiLineMode {
71 #[doc(hidden)]
72 fn decoration_collections(
73 extras: &mut Self::Extras,
74 ) -> (
75 &mut DecorationCollections,
76 &mut DecorationCollections<RangeDecoration>,
77 );
78
79 #[doc(hidden)]
80 fn decoration_collections_ref(
81 extras: &Self::Extras,
82 ) -> (
83 &DecorationCollections,
84 &DecorationCollections<RangeDecoration>,
85 );
86}
87
88impl DecoratedMode for TextareaMode {
89 fn decoration_collections(
90 extras: &mut TextareaExtras,
91 ) -> (
92 &mut DecorationCollections,
93 &mut DecorationCollections<RangeDecoration>,
94 ) {
95 (&mut extras.decorations, &mut extras.range_decorations)
96 }
97
98 fn decoration_collections_ref(
99 extras: &TextareaExtras,
100 ) -> (
101 &DecorationCollections,
102 &DecorationCollections<RangeDecoration>,
103 ) {
104 (&extras.decorations, &extras.range_decorations)
105 }
106}
107
108impl DecoratedMode for EditorMode {
109 fn decoration_collections(
110 extras: &mut EditorExtras,
111 ) -> (
112 &mut DecorationCollections,
113 &mut DecorationCollections<RangeDecoration>,
114 ) {
115 (&mut extras.decorations, &mut extras.range_decorations)
116 }
117
118 fn decoration_collections_ref(
119 extras: &EditorExtras,
120 ) -> (
121 &DecorationCollections,
122 &DecorationCollections<RangeDecoration>,
123 ) {
124 (&extras.decorations, &extras.range_decorations)
125 }
126}
127
69/// What the renderer may read out of a mode's extra state.
128/// What the renderer may read out of a mode's extra state.
70///
129///
71/// Kept apart from [`InputModeKind`] on purpose. This trait is *data*: the
130/// Kept apart from [`InputModeKind`] on purpose. This trait is *data*: the
@@ -343,8 +402,41 @@ impl InputModeKind for InputMode {
343impl InputModeKind for TextareaMode {
402impl InputModeKind for TextareaMode {
344 const MULTI_LINE: bool = true;
403 const MULTI_LINE: bool = true;
345
404
346 /// Ordinary multi-line text needs nothing beyond the shared engine.
405 type Extras = TextareaExtras;
347 type Extras = ();
406
407 fn reset_annotations(state: &mut InputBaseState<Self>) {
408 state.extras.decorations.clear();
409 state.extras.range_decorations.clear();
410 }
411
412 fn adjust_annotations(
413 state: &mut InputBaseState<Self>,
414 range: &std::ops::Range<usize>,
415 new_len: usize,
416 ) {
417 state.extras.decorations.adjust_for_edit(range, new_len);
418 state
419 .extras
420 .range_decorations
421 .adjust_for_edit(range, new_len);
422 }
423}
424
425/// What ordinary multi-line text adds: application decorations.
426#[derive(Default)]
427pub struct TextareaExtras {
428 pub(crate) decorations: DecorationCollections,
429 pub(crate) range_decorations: DecorationCollections<RangeDecoration>,
430}
431
432impl InputExtras for TextareaExtras {
433 fn decoration_layers(&self) -> Vec<&[TextDecoration]> {
434 self.decorations.iter().collect()
435 }
436
437 fn range_decorations(&self, ranges: &[std::ops::Range<usize>]) -> Vec<&RangeDecoration> {
438 self.range_decorations.intersecting(ranges)
439 }
348}
440}
349// `EditorMode`'s implementation lives with the editor code, next to the
441// `EditorMode`'s implementation lives with the editor code, next to the
350// language features it dispatches to.
442// language features it dispatches to.
vendor/gpui-base/src/input/editor/decorations.rs +86 −30
@@ -1,4 +1,4 @@
1 use crate::input::EditorMode;
1 use crate::input::{DecoratedMode, EditorMode};
2 2 use std::{collections::BTreeMap, ops::Range};
3 3
4 4 use gpui::{App, Context, HighlightStyle, Hsla, SharedString, WeakEntity};
@@ -100,13 +100,29 @@ struct DecorationCollectionId(usize);
100 100 ///
101 101 /// This is the GPUI Component counterpart of Monaco's
102 102 /// [`IEditorDecorationsCollection`](https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor_editor_api.editor.IEditorDecorationsCollection.html).
103 #[derive(Clone, Debug)]
104 pub struct TextDecorationCollection {
105 state: WeakEntity<InputBaseState<EditorMode>>,
103 pub struct TextDecorationCollection<M: DecoratedMode = EditorMode> {
104 state: WeakEntity<InputBaseState<M>>,
106 105 id: DecorationCollectionId,
107 106 }
108 107
109 impl TextDecorationCollection {
108 impl<M: DecoratedMode> Clone for TextDecorationCollection<M> {
109 fn clone(&self) -> Self {
110 Self {
111 state: self.state.clone(),
112 id: self.id,
113 }
114 }
115 }
116
117 impl<M: DecoratedMode> std::fmt::Debug for TextDecorationCollection<M> {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 f.debug_struct("TextDecorationCollection")
120 .field("id", &self.id)
121 .finish()
122 }
123 }
124
125 impl<M: DecoratedMode> TextDecorationCollection<M> {
110 126 /// Replace all decorations in this collection.
111 127 ///
112 128 /// This corresponds to Monaco's
@@ -114,7 +130,10 @@ impl TextDecorationCollection {
114 130 pub fn set(&self, decorations: Vec<TextDecoration>, cx: &mut App) {
115 131 let _ = self.state.update(cx, |state, cx| {
116 132 let decorations = normalize(&state.text, decorations);
117 if state.extras.decorations.set(self.id, decorations) {
133 if M::decoration_collections(&mut state.extras)
134 .0
135 .set(self.id, decorations)
136 {
118 137 cx.notify();
119 138 }
120 139 });
@@ -127,7 +146,10 @@ impl TextDecorationCollection {
127 146 pub fn append(&self, decorations: Vec<TextDecoration>, cx: &mut App) {
128 147 let _ = self.state.update(cx, |state, cx| {
129 148 let decorations = normalize(&state.text, decorations);
130 if state.extras.decorations.append(self.id, decorations) {
149 if M::decoration_collections(&mut state.extras)
150 .0
151 .append(self.id, decorations)
152 {
131 153 cx.notify();
132 154 }
133 155 });
@@ -149,8 +171,8 @@ impl TextDecorationCollection {
149 171 self.state
150 172 .read_with(cx, |state, _| {
151 173 state
152 .extras
153 .decorations
174 .decoration_collections()
175 .0
154 176 .get(self.id)
155 177 .unwrap_or_default()
156 178 .iter()
@@ -166,18 +188,37 @@ impl TextDecorationCollection {
166 188 /// Clones address the same collection. Dropping a handle does not clear it; use
167 189 /// [`Self::clear`] to empty it or [`Self::dispose`] to release it permanently.
168 190 /// Operations on a disposed collection or a dropped editor are harmless no-ops.
169 #[derive(Clone, Debug)]
170 pub struct RangeDecorationCollection {
171 state: WeakEntity<InputBaseState<EditorMode>>,
191 pub struct RangeDecorationCollection<M: DecoratedMode = EditorMode> {
192 state: WeakEntity<InputBaseState<M>>,
172 193 id: DecorationCollectionId,
173 194 }
174 195
175 impl RangeDecorationCollection {
196 impl<M: DecoratedMode> Clone for RangeDecorationCollection<M> {
197 fn clone(&self) -> Self {
198 Self {
199 state: self.state.clone(),
200 id: self.id,
201 }
202 }
203 }
204
205 impl<M: DecoratedMode> std::fmt::Debug for RangeDecorationCollection<M> {
206 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
207 f.debug_struct("RangeDecorationCollection")
208 .field("id", &self.id)
209 .finish()
210 }
211 }
212
213 impl<M: DecoratedMode> RangeDecorationCollection<M> {
176 214 /// Replace only this owner's decorations, clipping ranges to UTF-8 boundaries.
177 215 pub fn set(&self, decorations: Vec<RangeDecoration>, cx: &mut App) {
178 216 let _ = self.state.update(cx, |state, cx| {
179 217 let decorations = normalize(&state.text, decorations);
180 if state.extras.range_decorations.set(self.id, decorations) {
218 if M::decoration_collections(&mut state.extras)
219 .1
220 .set(self.id, decorations)
221 {
181 222 cx.notify();
182 223 }
183 224 });
@@ -187,7 +228,10 @@ impl RangeDecorationCollection {
187 228 pub fn append(&self, decorations: Vec<RangeDecoration>, cx: &mut App) {
188 229 let _ = self.state.update(cx, |state, cx| {
189 230 let decorations = normalize(&state.text, decorations);
190 if state.extras.range_decorations.append(self.id, decorations) {
231 if M::decoration_collections(&mut state.extras)
232 .1
233 .append(self.id, decorations)
234 {
191 235 cx.notify();
192 236 }
193 237 });
@@ -201,9 +245,8 @@ impl RangeDecorationCollection {
201 245 /// Release this collection, invalidating all of its cloned handles.
202 246 pub fn dispose(&self, cx: &mut App) {
203 247 let _ = self.state.update(cx, |state, cx| {
204 if state
205 .extras
206 .range_decorations
248 if M::decoration_collections(&mut state.extras)
249 .1
207 250 .entries
208 251 .remove(&self.id)
209 252 .is_some()
@@ -218,8 +261,8 @@ impl RangeDecorationCollection {
218 261 self.state
219 262 .read_with(cx, |state, _| {
220 263 state
221 .extras
222 .range_decorations
264 .decoration_collections()
265 .1
223 266 .get(self.id)
224 267 .unwrap_or_default()
225 268 .iter()
@@ -231,7 +274,8 @@ impl RangeDecorationCollection {
231 274 }
232 275
233 276 /// Both text styles and geometric decorations share normalization and edit affinity.
234 pub(crate) trait TrackedDecoration {
277 #[doc(hidden)]
278 pub trait TrackedDecoration {
235 279 fn range(&self) -> &Range<usize>;
236 280 fn range_mut(&mut self) -> &mut Range<usize>;
237 281 }
@@ -332,7 +376,8 @@ impl<T: TrackedDecoration> DecorationEntries<T> {
332 376 }
333 377 }
334 378
335 pub(crate) struct DecorationCollections<T = TextDecoration> {
379 #[doc(hidden)]
380 pub struct DecorationCollections<T = TextDecoration> {
336 381 entries: BTreeMap<DecorationCollectionId, DecorationEntries<T>>,
337 382 next_id: usize,
338 383 }
@@ -535,7 +580,16 @@ fn normalize<T: TrackedDecoration>(text: &Rope, decorations: Vec<T>) -> Vec<T> {
535 580 .collect()
536 581 }
537 582
538 impl InputBaseState<EditorMode> {
583 impl<M: DecoratedMode> InputBaseState<M> {
584 pub(crate) fn decoration_collections(
585 &self,
586 ) -> (
587 &DecorationCollections,
588 &DecorationCollections<RangeDecoration>,
589 ) {
590 M::decoration_collections_ref(&self.extras)
591 }
592
539 593 /// Create an independently owned collection of geometric range decorations.
540 594 ///
541 595 /// Ranges use UTF-8 byte offsets and the same tracking as text decorations:
@@ -553,11 +607,11 @@ impl InputBaseState<EditorMode> {
553 607 &mut self,
554 608 decorations: Vec<RangeDecoration>,
555 609 cx: &mut Context<Self>,
556 ) -> RangeDecorationCollection {
557 let id = self
558 .extras
559 .range_decorations
560 .create(normalize(&self.text, decorations));
610 ) -> RangeDecorationCollection<M> {
611 let decorations = normalize(&self.text, decorations);
612 let id = M::decoration_collections(&mut self.extras)
613 .1
614 .create(decorations);
561 615 cx.notify();
562 616 RangeDecorationCollection {
563 617 state: cx.entity().downgrade(),
@@ -586,9 +640,11 @@ impl InputBaseState<EditorMode> {
586 640 &mut self,
587 641 decorations: Vec<TextDecoration>,
588 642 cx: &mut Context<Self>,
589 ) -> TextDecorationCollection {
643 ) -> TextDecorationCollection<M> {
590 644 let decorations = normalize(&self.text, decorations);
591 let id = self.extras.decorations.create(decorations);
645 let id = M::decoration_collections(&mut self.extras)
646 .0
647 .create(decorations);
592 648 cx.notify();
593 649 TextDecorationCollection {
594 650 state: cx.entity().downgrade(),
@@ -1,4 +1,4 @@
1use crate::input::EditorMode;
1use crate::input::{DecoratedMode, EditorMode};
2use std::{collections::BTreeMap, ops::Range};
2use std::{collections::BTreeMap, ops::Range};
3
3
4use gpui::{App, Context, HighlightStyle, Hsla, SharedString, WeakEntity};
4use gpui::{App, Context, HighlightStyle, Hsla, SharedString, WeakEntity};
@@ -100,13 +100,29 @@ struct DecorationCollectionId(usize);
100///
100///
101/// This is the GPUI Component counterpart of Monaco's
101/// This is the GPUI Component counterpart of Monaco's
102/// [`IEditorDecorationsCollection`](https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor_editor_api.editor.IEditorDecorationsCollection.html).
102/// [`IEditorDecorationsCollection`](https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor_editor_api.editor.IEditorDecorationsCollection.html).
103#[derive(Clone, Debug)]
103pub struct TextDecorationCollection<M: DecoratedMode = EditorMode> {
104pub struct TextDecorationCollection {
104 state: WeakEntity<InputBaseState<M>>,
105 state: WeakEntity<InputBaseState<EditorMode>>,
106 id: DecorationCollectionId,
105 id: DecorationCollectionId,
107}
106}
108
107
109impl TextDecorationCollection {
108impl<M: DecoratedMode> Clone for TextDecorationCollection<M> {
109 fn clone(&self) -> Self {
110 Self {
111 state: self.state.clone(),
112 id: self.id,
113 }
114 }
115}
116
117impl<M: DecoratedMode> std::fmt::Debug for TextDecorationCollection<M> {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 f.debug_struct("TextDecorationCollection")
120 .field("id", &self.id)
121 .finish()
122 }
123}
124
125impl<M: DecoratedMode> TextDecorationCollection<M> {
110 /// Replace all decorations in this collection.
126 /// Replace all decorations in this collection.
111 ///
127 ///
112 /// This corresponds to Monaco's
128 /// This corresponds to Monaco's
@@ -114,7 +130,10 @@ impl TextDecorationCollection {
114 pub fn set(&self, decorations: Vec<TextDecoration>, cx: &mut App) {
130 pub fn set(&self, decorations: Vec<TextDecoration>, cx: &mut App) {
115 let _ = self.state.update(cx, |state, cx| {
131 let _ = self.state.update(cx, |state, cx| {
116 let decorations = normalize(&state.text, decorations);
132 let decorations = normalize(&state.text, decorations);
117 if state.extras.decorations.set(self.id, decorations) {
133 if M::decoration_collections(&mut state.extras)
134 .0
135 .set(self.id, decorations)
136 {
118 cx.notify();
137 cx.notify();
119 }
138 }
120 });
139 });
@@ -127,7 +146,10 @@ impl TextDecorationCollection {
127 pub fn append(&self, decorations: Vec<TextDecoration>, cx: &mut App) {
146 pub fn append(&self, decorations: Vec<TextDecoration>, cx: &mut App) {
128 let _ = self.state.update(cx, |state, cx| {
147 let _ = self.state.update(cx, |state, cx| {
129 let decorations = normalize(&state.text, decorations);
148 let decorations = normalize(&state.text, decorations);
130 if state.extras.decorations.append(self.id, decorations) {
149 if M::decoration_collections(&mut state.extras)
150 .0
151 .append(self.id, decorations)
152 {
131 cx.notify();
153 cx.notify();
132 }
154 }
133 });
155 });
@@ -149,8 +171,8 @@ impl TextDecorationCollection {
149 self.state
171 self.state
150 .read_with(cx, |state, _| {
172 .read_with(cx, |state, _| {
151 state
173 state
152 .extras
174 .decoration_collections()
153 .decorations
175 .0
154 .get(self.id)
176 .get(self.id)
155 .unwrap_or_default()
177 .unwrap_or_default()
156 .iter()
178 .iter()
@@ -166,18 +188,37 @@ impl TextDecorationCollection {
166/// Clones address the same collection. Dropping a handle does not clear it; use
188/// Clones address the same collection. Dropping a handle does not clear it; use
167/// [`Self::clear`] to empty it or [`Self::dispose`] to release it permanently.
189/// [`Self::clear`] to empty it or [`Self::dispose`] to release it permanently.
168/// Operations on a disposed collection or a dropped editor are harmless no-ops.
190/// Operations on a disposed collection or a dropped editor are harmless no-ops.
169#[derive(Clone, Debug)]
191pub struct RangeDecorationCollection<M: DecoratedMode = EditorMode> {
170pub struct RangeDecorationCollection {
192 state: WeakEntity<InputBaseState<M>>,
171 state: WeakEntity<InputBaseState<EditorMode>>,
172 id: DecorationCollectionId,
193 id: DecorationCollectionId,
173}
194}
174
195
175impl RangeDecorationCollection {
196impl<M: DecoratedMode> Clone for RangeDecorationCollection<M> {
197 fn clone(&self) -> Self {
198 Self {
199 state: self.state.clone(),
200 id: self.id,
201 }
202 }
203}
204
205impl<M: DecoratedMode> std::fmt::Debug for RangeDecorationCollection<M> {
206 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
207 f.debug_struct("RangeDecorationCollection")
208 .field("id", &self.id)
209 .finish()
210 }
211}
212
213impl<M: DecoratedMode> RangeDecorationCollection<M> {
176 /// Replace only this owner's decorations, clipping ranges to UTF-8 boundaries.
214 /// Replace only this owner's decorations, clipping ranges to UTF-8 boundaries.
177 pub fn set(&self, decorations: Vec<RangeDecoration>, cx: &mut App) {
215 pub fn set(&self, decorations: Vec<RangeDecoration>, cx: &mut App) {
178 let _ = self.state.update(cx, |state, cx| {
216 let _ = self.state.update(cx, |state, cx| {
179 let decorations = normalize(&state.text, decorations);
217 let decorations = normalize(&state.text, decorations);
180 if state.extras.range_decorations.set(self.id, decorations) {
218 if M::decoration_collections(&mut state.extras)
219 .1
220 .set(self.id, decorations)
221 {
181 cx.notify();
222 cx.notify();
182 }
223 }
183 });
224 });
@@ -187,7 +228,10 @@ impl RangeDecorationCollection {
187 pub fn append(&self, decorations: Vec<RangeDecoration>, cx: &mut App) {
228 pub fn append(&self, decorations: Vec<RangeDecoration>, cx: &mut App) {
188 let _ = self.state.update(cx, |state, cx| {
229 let _ = self.state.update(cx, |state, cx| {
189 let decorations = normalize(&state.text, decorations);
230 let decorations = normalize(&state.text, decorations);
190 if state.extras.range_decorations.append(self.id, decorations) {
231 if M::decoration_collections(&mut state.extras)
232 .1
233 .append(self.id, decorations)
234 {
191 cx.notify();
235 cx.notify();
192 }
236 }
193 });
237 });
@@ -201,9 +245,8 @@ impl RangeDecorationCollection {
201 /// Release this collection, invalidating all of its cloned handles.
245 /// Release this collection, invalidating all of its cloned handles.
202 pub fn dispose(&self, cx: &mut App) {
246 pub fn dispose(&self, cx: &mut App) {
203 let _ = self.state.update(cx, |state, cx| {
247 let _ = self.state.update(cx, |state, cx| {
204 if state
248 if M::decoration_collections(&mut state.extras)
205 .extras
249 .1
206 .range_decorations
207 .entries
250 .entries
208 .remove(&self.id)
251 .remove(&self.id)
209 .is_some()
252 .is_some()
@@ -218,8 +261,8 @@ impl RangeDecorationCollection {
218 self.state
261 self.state
219 .read_with(cx, |state, _| {
262 .read_with(cx, |state, _| {
220 state
263 state
221 .extras
264 .decoration_collections()
222 .range_decorations
265 .1
223 .get(self.id)
266 .get(self.id)
224 .unwrap_or_default()
267 .unwrap_or_default()
225 .iter()
268 .iter()
@@ -231,7 +274,8 @@ impl RangeDecorationCollection {
231}
274}
232
275
233/// Both text styles and geometric decorations share normalization and edit affinity.
276/// Both text styles and geometric decorations share normalization and edit affinity.
234pub(crate) trait TrackedDecoration {
277#[doc(hidden)]
278pub trait TrackedDecoration {
235 fn range(&self) -> &Range<usize>;
279 fn range(&self) -> &Range<usize>;
236 fn range_mut(&mut self) -> &mut Range<usize>;
280 fn range_mut(&mut self) -> &mut Range<usize>;
237}
281}
@@ -332,7 +376,8 @@ impl<T: TrackedDecoration> DecorationEntries<T> {
332 }
376 }
333}
377}
334
378
335pub(crate) struct DecorationCollections<T = TextDecoration> {
379#[doc(hidden)]
380pub struct DecorationCollections<T = TextDecoration> {
336 entries: BTreeMap<DecorationCollectionId, DecorationEntries<T>>,
381 entries: BTreeMap<DecorationCollectionId, DecorationEntries<T>>,
337 next_id: usize,
382 next_id: usize,
338}
383}
@@ -535,7 +580,16 @@ fn normalize<T: TrackedDecoration>(text: &Rope, decorations: Vec<T>) -> Vec<T> {
535 .collect()
580 .collect()
536}
581}
537
582
538impl InputBaseState<EditorMode> {
583impl<M: DecoratedMode> InputBaseState<M> {
584 pub(crate) fn decoration_collections(
585 &self,
586 ) -> (
587 &DecorationCollections,
588 &DecorationCollections<RangeDecoration>,
589 ) {
590 M::decoration_collections_ref(&self.extras)
591 }
592
539 /// Create an independently owned collection of geometric range decorations.
593 /// Create an independently owned collection of geometric range decorations.
540 ///
594 ///
541 /// Ranges use UTF-8 byte offsets and the same tracking as text decorations:
595 /// Ranges use UTF-8 byte offsets and the same tracking as text decorations:
@@ -553,11 +607,11 @@ impl InputBaseState<EditorMode> {
553 &mut self,
607 &mut self,
554 decorations: Vec<RangeDecoration>,
608 decorations: Vec<RangeDecoration>,
555 cx: &mut Context<Self>,
609 cx: &mut Context<Self>,
556 ) -> RangeDecorationCollection {
610 ) -> RangeDecorationCollection<M> {
557 let id = self
611 let decorations = normalize(&self.text, decorations);
558 .extras
612 let id = M::decoration_collections(&mut self.extras)
559 .range_decorations
613 .1
560 .create(normalize(&self.text, decorations));
614 .create(decorations);
561 cx.notify();
615 cx.notify();
562 RangeDecorationCollection {
616 RangeDecorationCollection {
563 state: cx.entity().downgrade(),
617 state: cx.entity().downgrade(),
@@ -586,9 +640,11 @@ impl InputBaseState<EditorMode> {
586 &mut self,
640 &mut self,
587 decorations: Vec<TextDecoration>,
641 decorations: Vec<TextDecoration>,
588 cx: &mut Context<Self>,
642 cx: &mut Context<Self>,
589 ) -> TextDecorationCollection {
643 ) -> TextDecorationCollection<M> {
590 let decorations = normalize(&self.text, decorations);
644 let decorations = normalize(&self.text, decorations);
591 let id = self.extras.decorations.create(decorations);
645 let id = M::decoration_collections(&mut self.extras)
646 .0
647 .create(decorations);
592 cx.notify();
648 cx.notify();
593 TextDecorationCollection {
649 TextDecorationCollection {
594 state: cx.entity().downgrade(),
650 state: cx.entity().downgrade(),
vendor/gpui-base/src/input/editor/mod.rs +1 −1
@@ -5,7 +5,7 @@ use super::{EditorMode, InputBaseState, InputModeKind};
5 5 /// State for source-code editing.
6 6 ///
7 7 /// This is the shared editing engine in its code-editor kind. Languages, line
8 /// numbers, folding, indent guides, diagnostics, decorations, and the LSP
8 /// numbers, folding, indent guides, diagnostics, and the LSP
9 9 /// providers exist on this kind only, so an ordinary input or textarea never
10 10 /// exposes them.
11 11 pub type EditorState = InputBaseState<EditorMode>;
@@ -5,7 +5,7 @@ use super::{EditorMode, InputBaseState, InputModeKind};
5/// State for source-code editing.
5/// State for source-code editing.
6///
6///
7/// This is the shared editing engine in its code-editor kind. Languages, line
7/// This is the shared editing engine in its code-editor kind. Languages, line
8/// numbers, folding, indent guides, diagnostics, decorations, and the LSP
8/// numbers, folding, indent guides, diagnostics, and the LSP
9/// providers exist on this kind only, so an ordinary input or textarea never
9/// providers exist on this kind only, so an ordinary input or textarea never
10/// exposes them.
10/// exposes them.
11pub type EditorState = InputBaseState<EditorMode>;
11pub type EditorState = InputBaseState<EditorMode>;
vendor/gpui-base/src/input/mod.rs +2 −1
@@ -99,7 +99,8 @@ pub use highlighting::{
99 99 pub use indent::TabSize;
100 100 pub use input::{Input, InputState};
101 101 pub use kind::{
102 EditorExtras, EditorMode, InputExtras, InputMode, InputModeKind, MultiLineMode, TextareaMode,
102 DecoratedMode, EditorExtras, EditorMode, InputExtras, InputMode, InputModeKind, MultiLineMode,
103 TextareaExtras, TextareaMode,
103 104 };
104 105 pub(crate) use language::EditorLanguage;
105 106 pub use language::{LanguageProvider, set_language_config, set_language_provider};
@@ -99,7 +99,8 @@ pub use highlighting::{
99pub use indent::TabSize;
99pub use indent::TabSize;
100pub use input::{Input, InputState};
100pub use input::{Input, InputState};
101pub use kind::{
101pub use kind::{
102 EditorExtras, EditorMode, InputExtras, InputMode, InputModeKind, MultiLineMode, TextareaMode,
102 DecoratedMode, EditorExtras, EditorMode, InputExtras, InputMode, InputModeKind, MultiLineMode,
103 TextareaExtras, TextareaMode,
103};
104};
104pub(crate) use language::EditorLanguage;
105pub(crate) use language::EditorLanguage;
105pub use language::{LanguageProvider, set_language_config, set_language_provider};
106pub use language::{LanguageProvider, set_language_config, set_language_provider};