outline: Refactor outline render_item to reuse existing TextStyle (#49166)

Xiaobo Liu created

Release Notes:

- N/A

Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>

Change summary

crates/outline/src/outline.rs             | 30 +++++++---------------
crates/outline_panel/src/outline_panel.rs | 33 +++++++++++-------------
2 files changed, 25 insertions(+), 38 deletions(-)

Detailed changes

crates/outline/src/outline.rs 🔗

@@ -10,8 +10,7 @@ use editor::{MultiBufferOffset, RowHighlightOptions, SelectionEffects};
 use fuzzy::StringMatch;
 use gpui::{
     App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, HighlightStyle,
-    ParentElement, Point, Render, Styled, StyledText, Task, TextStyle, WeakEntity, Window, div,
-    rems,
+    ParentElement, Point, Render, Styled, StyledText, Task, WeakEntity, Window, div, rems,
 };
 use language::{Outline, OutlineItem};
 use ordered_float::OrderedFloat;
@@ -407,7 +406,7 @@ pub fn render_item<T>(
     outline_item: &OutlineItem<T>,
     match_ranges: impl IntoIterator<Item = Range<usize>>,
     cx: &App,
-) -> StyledText {
+) -> impl IntoElement {
     let highlight_style = HighlightStyle {
         background_color: Some(cx.theme().colors().text_accent.alpha(0.3)),
         ..Default::default()
@@ -415,28 +414,19 @@ pub fn render_item<T>(
     let custom_highlights = match_ranges
         .into_iter()
         .map(|range| (range, highlight_style));
-
-    let settings = ThemeSettings::get_global(cx);
-
-    // TODO: We probably shouldn't need to build a whole new text style here
-    // but I'm not sure how to get the current one and modify it.
-    // Before this change TextStyle::default() was used here, which was giving us the wrong font and text color.
-    let text_style = TextStyle {
-        color: cx.theme().colors().text,
-        font_family: settings.buffer_font.family.clone(),
-        font_features: settings.buffer_font.features.clone(),
-        font_fallbacks: settings.buffer_font.fallbacks.clone(),
-        font_size: settings.buffer_font_size(cx).into(),
-        font_weight: settings.buffer_font.weight,
-        line_height: relative(1.),
-        ..Default::default()
-    };
     let highlights = gpui::combine_highlights(
         custom_highlights,
         outline_item.highlight_ranges.iter().cloned(),
     );
 
-    StyledText::new(outline_item.text.clone()).with_default_highlights(&text_style, highlights)
+    let settings = ThemeSettings::get_global(cx);
+
+    div()
+        .text_color(cx.theme().colors().text)
+        .font(settings.buffer_font.clone())
+        .text_size(settings.buffer_font_size(cx))
+        .line_height(relative(1.))
+        .child(StyledText::new(outline_item.text.clone()).with_highlights(highlights))
 }
 
 #[cfg(test)]

crates/outline_panel/src/outline_panel.rs 🔗

@@ -2618,24 +2618,21 @@ impl OutlinePanel {
         } else {
             &search_matches
         };
-        let label_element = outline::render_item(
-            &OutlineItem {
-                depth,
-                annotation_range: None,
-                range: search_data.context_range.clone(),
-                text: search_data.context_text.clone(),
-                source_range_for_text: search_data.context_range.clone(),
-                highlight_ranges: search_data
-                    .highlights_data
-                    .get()
-                    .cloned()
-                    .unwrap_or_default(),
-                name_ranges: search_data.search_match_indices.clone(),
-                body_range: Some(search_data.context_range.clone()),
-            },
-            match_ranges.iter().cloned(),
-            cx,
-        );
+        let outline_item = OutlineItem {
+            depth,
+            annotation_range: None,
+            range: search_data.context_range.clone(),
+            text: search_data.context_text.clone(),
+            source_range_for_text: search_data.context_range.clone(),
+            highlight_ranges: search_data
+                .highlights_data
+                .get()
+                .cloned()
+                .unwrap_or_default(),
+            name_ranges: search_data.search_match_indices.clone(),
+            body_range: Some(search_data.context_range.clone()),
+        };
+        let label_element = outline::render_item(&outline_item, match_ranges.iter().cloned(), cx);
         let truncated_contents_label = || Label::new(TRUNCATED_CONTEXT_MARK);
         let entire_label = h_flex()
             .justify_center()