ui: Fix doc tests (#3989)

Marshall Bowers created

There were a ton of doc tests that weren't compiling in the `ui` crate,
so this PR fixes them.

Release Notes:

- N/A

Change summary

crates/ui/src/components/avatar.rs           |  8 +++
crates/ui/src/components/button/button.rs    | 33 ++++++++++++++--
crates/ui/src/components/label/label.rs      | 26 +++++++++++--
crates/ui/src/components/label/label_like.rs |  7 +++
crates/ui/src/utils/format_distance.rs       | 42 ----------------------
5 files changed, 63 insertions(+), 53 deletions(-)

Detailed changes

crates/ui/src/components/avatar.rs 🔗

@@ -16,10 +16,12 @@ pub enum AvatarShape {
 /// # Examples
 ///
 /// ```
+/// use ui::{Avatar, AvatarShape};
+///
 /// Avatar::new("path/to/image.png")
 ///     .shape(AvatarShape::Circle)
 ///     .grayscale(true)
-///     .border_color(cx.theme().colors().border)
+///     .border_color(gpui::red());
 /// ```
 #[derive(IntoElement)]
 pub struct Avatar {
@@ -87,6 +89,8 @@ impl Avatar {
     /// # Examples
     ///
     /// ```
+    /// use ui::{Avatar, AvatarShape};
+    ///
     /// Avatar::new("path/to/image.png").shape(AvatarShape::Circle);
     /// ```
     pub fn shape(mut self, shape: AvatarShape) -> Self {
@@ -102,6 +106,8 @@ impl Avatar {
     /// # Examples
     ///
     /// ```
+    /// use ui::{Avatar, AvatarShape};
+    ///
     /// let avatar = Avatar::new("path/to/image.png").grayscale(true);
     /// ```
     pub fn grayscale(mut self, grayscale: bool) -> Self {

crates/ui/src/components/button/button.rs 🔗

@@ -23,6 +23,8 @@ use super::button_icon::ButtonIcon;
 /// indicates what action will be performed when the button is clicked.
 ///
 /// ```
+/// use ui::prelude::*;
+///
 /// Button::new("button_id", "Click me!")
 ///     .on_click(|event, cx| {
 ///         // Handle click event
@@ -34,9 +36,11 @@ use super::button_icon::ButtonIcon;
 /// a trigger for a popover menu, where clicking the button toggles the visibility of the menu.
 ///
 /// ```
+/// use ui::prelude::*;
+///
 /// Button::new("button_id", "Click me!")
 ///     .icon(IconName::Check)
-///     .selected(some_bool)
+///     .selected(true)
 ///     .on_click(|event, cx| {
 ///         // Handle click event
 ///     });
@@ -45,8 +49,11 @@ use super::button_icon::ButtonIcon;
 /// To change the style of the button when it is selected use the [`selected_style`][Button::selected_style] method.
 ///
 /// ```
+/// use ui::prelude::*;
+/// use ui::TintColor;
+///
 /// Button::new("button_id", "Click me!")
-///     .selected(some_bool)
+///     .selected(true)
 ///     .selected_style(ButtonStyle::Tinted(TintColor::Accent))
 ///     .on_click(|event, cx| {
 ///         // Handle click event
@@ -58,6 +65,8 @@ use super::button_icon::ButtonIcon;
 /// The button's content, including text and icons, is centered by default.
 ///
 /// ```
+/// use ui::prelude::*;
+///
 /// let button = Button::new("button_id", "Click me!")
 ///     .full_width()
 ///     .on_click(|event, cx| {
@@ -167,6 +176,8 @@ impl Selectable for Button {
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    ///
     /// Button::new("button_id", "Click me!")
     ///     .selected(true)
     ///     .on_click(|event, cx| {
@@ -187,6 +198,9 @@ impl SelectableButton for Button {
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    /// use ui::TintColor;
+    ///
     /// Button::new("button_id", "Click me!")
     ///     .selected(true)
     ///     .selected_style(ButtonStyle::Tinted(TintColor::Accent))
@@ -210,6 +224,8 @@ impl Disableable for Button {
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    ///
     /// Button::new("button_id", "Click me!")
     ///     .disabled(true)
     ///     .on_click(|event, cx| {
@@ -244,8 +260,10 @@ impl FixedWidth for Button {
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    ///
     /// Button::new("button_id", "Click me!")
-    ///     .width(DefiniteLength::Pixels(100))
+    ///     .width(px(100.).into())
     ///     .on_click(|event, cx| {
     ///         // Handle click event
     ///     });
@@ -262,6 +280,8 @@ impl FixedWidth for Button {
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    ///
     /// Button::new("button_id", "Click me!")
     ///     .full_width()
     ///     .on_click(|event, cx| {
@@ -303,9 +323,12 @@ impl ButtonCommon for Button {
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    /// use ui::Tooltip;
+    ///
     /// Button::new("button_id", "Click me!")
-    ///     .tooltip(|cx| {
-    ///         Text::new("This is a tooltip").into()
+    ///     .tooltip(move |cx| {
+    ///         Tooltip::text("This is a tooltip", cx)
     ///     })
     ///     .on_click(|event, cx| {
     ///         // Handle click event

crates/ui/src/components/label/label.rs 🔗

@@ -10,18 +10,24 @@ use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
 /// # Examples
 ///
 /// ```
-/// Label::new("Hello, World!")
+/// use ui::prelude::*;
+///
+/// Label::new("Hello, World!");
 /// ```
 ///
 /// **A colored label**, for example labeling a dangerous action:
 ///
 /// ```
+/// use ui::prelude::*;
+///
 /// let my_label = Label::new("Delete").color(Color::Error);
 /// ```
 ///
 /// **A label with a strikethrough**, for example labeling something that has been deleted:
 ///
 /// ```
+/// use ui::prelude::*;
+///
 /// let my_label = Label::new("Deleted").strikethrough(true);
 /// ```
 #[derive(IntoElement)]
@@ -31,11 +37,13 @@ pub struct Label {
 }
 
 impl Label {
-    /// Create a new `Label` with the given text.
+    /// Create a new [`Label`] with the given text.
     ///
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    ///
     /// let my_label = Label::new("Hello, World!");
     /// ```
     pub fn new(label: impl Into<SharedString>) -> Self {
@@ -52,7 +60,9 @@ impl LabelCommon for Label {
     /// # Examples
     ///
     /// ```
-    /// let my_label = Label::new("Hello, World!").size(LabelSize::Large);
+    /// use ui::prelude::*;
+    ///
+    /// let my_label = Label::new("Hello, World!").size(LabelSize::Small);
     /// ```
     fn size(mut self, size: LabelSize) -> Self {
         self.base = self.base.size(size);
@@ -64,7 +74,9 @@ impl LabelCommon for Label {
     /// # Examples
     ///
     /// ```
-    /// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::Normal);
+    /// use ui::prelude::*;
+    ///
+    /// let my_label = Label::new("Hello, World!").line_height_style(LineHeightStyle::UiLabel);
     /// ```
     fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
         self.base = self.base.line_height_style(line_height_style);
@@ -76,7 +88,9 @@ impl LabelCommon for Label {
     /// # Examples
     ///
     /// ```
-    /// let my_label = Label::new("Hello, World!").color(Color::Primary);
+    /// use ui::prelude::*;
+    ///
+    /// let my_label = Label::new("Hello, World!").color(Color::Accent);
     /// ```
     fn color(mut self, color: Color) -> Self {
         self.base = self.base.color(color);
@@ -88,6 +102,8 @@ impl LabelCommon for Label {
     /// # Examples
     ///
     /// ```
+    /// use ui::prelude::*;
+    ///
     /// let my_label = Label::new("Hello, World!").strikethrough(true);
     /// ```
     fn strikethrough(mut self, strikethrough: bool) -> Self {

crates/ui/src/components/label/label_like.rs 🔗

@@ -21,9 +21,16 @@ pub enum LineHeightStyle {
 
 /// A common set of traits all labels must implement.
 pub trait LabelCommon {
+    /// Sets the size of the label using a [`LabelSize`].
     fn size(self, size: LabelSize) -> Self;
+
+    /// Sets the line height style of the label using a [`LineHeightStyle`].
     fn line_height_style(self, line_height_style: LineHeightStyle) -> Self;
+
+    /// Sets the color of the label using a [`Color`].
     fn color(self, color: Color) -> Self;
+
+    /// Sets the strikethrough property of the label.
     fn strikethrough(self, strikethrough: bool) -> Self;
 }
 

crates/ui/src/utils/format_distance.rs 🔗

@@ -234,28 +234,6 @@ fn distance_string(
 /// For example, "less than a minute ago", "about 2 hours ago", "3 months from now", etc.
 ///
 /// Use [`format_distance_from_now`] to compare a NaiveDateTime against now.
-///
-/// # Arguments
-///
-/// * `date` - The [`NaiveDateTime`] to compare.
-/// * `base_date` - The [`NaiveDateTime`] to compare against.
-/// * `include_seconds` - A boolean. If true, distances less than a minute are more detailed
-/// * `add_suffix` - A boolean. If true, result indicates if the time is in the past or future
-///
-/// # Example
-///
-/// ```rust
-/// use chrono::DateTime;
-/// use ui::utils::format_distance;
-///
-/// fn time_between_moon_landings() -> String {
-///     let date = DateTime::parse_from_rfc3339("1969-07-20T00:00:00Z").unwrap().naive_local();
-///     let base_date = DateTime::parse_from_rfc3339("1972-12-14T00:00:00Z").unwrap().naive_local();
-///     format!("There was {} between the first and last crewed moon landings.", naive_format_distance(date, base_date, false, false))
-/// }
-/// ```
-///
-/// Output: `"There was about 3 years between the first and last crewed moon landings."`
 pub fn format_distance(
     date: DateTimeType,
     base_date: NaiveDateTime,
@@ -271,26 +249,6 @@ pub fn format_distance(
 /// Get the time difference between a date and now as relative human readable string.
 ///
 /// For example, "less than a minute ago", "about 2 hours ago", "3 months from now", etc.
-///
-/// # Arguments
-///
-/// * `datetime` - The [`NaiveDateTime`] to compare with the current time.
-/// * `include_seconds` - A boolean. If true, distances less than a minute are more detailed
-/// * `add_suffix` - A boolean. If true, result indicates if the time is in the past or future
-///
-/// # Example
-///
-/// ```rust
-/// use chrono::DateTime;
-/// use ui::utils::naive_format_distance_from_now;
-///
-/// fn time_since_first_moon_landing() -> String {
-///     let date = DateTime::parse_from_rfc3339("1969-07-20T00:00:00Z").unwrap().naive_local();
-///     format!("It's been {} since Apollo 11 first landed on the moon.", naive_format_distance_from_now(date, false, false))
-/// }
-/// ```
-///
-/// Output: `It's been over 54 years since Apollo 11 first landed on the moon.`
 pub fn format_distance_from_now(
     datetime: DateTimeType,
     include_seconds: bool,