Compare commits

...

3 Commits

Author SHA1 Message Date
Richard Feldman
0ff95fec62 Add a tooltip to the icon for the language name 2025-05-01 10:58:41 -04:00
Richard Feldman
845cf799c6 Only show icons, not language names, above code blocks 2025-05-01 10:58:41 -04:00
Richard Feldman
22c1090163 Respect cursor_pointer when ButtonLike is disabled 2025-05-01 10:57:44 -04:00
2 changed files with 69 additions and 34 deletions

View File

@@ -21,9 +21,9 @@ use editor::scroll::Autoscroll;
use editor::{Editor, EditorElement, EditorEvent, EditorStyle, MultiBuffer};
use gpui::{
AbsoluteLength, Animation, AnimationExt, AnyElement, App, ClickEvent, ClipboardEntry,
ClipboardItem, DefiniteLength, EdgesRefinement, Empty, Entity, EventEmitter, Focusable, Hsla,
ListAlignment, ListState, MouseButton, PlatformDisplay, ScrollHandle, Stateful,
StyleRefinement, Subscription, Task, TextStyle, TextStyleRefinement, Transformation,
ClipboardItem, CursorStyle, DefiniteLength, EdgesRefinement, Empty, Entity, EventEmitter,
Focusable, Hsla, ListAlignment, ListState, MouseButton, PlatformDisplay, ScrollHandle,
Stateful, StyleRefinement, Subscription, Task, TextStyle, TextStyleRefinement, Transformation,
UnderlineStyle, WeakEntity, WindowHandle, linear_color_stop, linear_gradient, list, percentage,
pulsating_between,
};
@@ -45,8 +45,8 @@ use std::time::Duration;
use text::ToPoint;
use theme::ThemeSettings;
use ui::{
Disclosure, IconButton, KeyBinding, PopoverMenuHandle, Scrollbar, ScrollbarState, TextSize,
Tooltip, prelude::*,
ButtonLike, Disclosure, IconButton, KeyBinding, PopoverMenuHandle, Scrollbar, ScrollbarState,
TextSize, Tooltip, prelude::*,
};
use util::ResultExt as _;
use util::markdown::MarkdownCodeBlock;
@@ -360,25 +360,42 @@ fn render_markdown_code_block(
cx,
)),
CodeBlockKind::FencedSrc(path_range) => path_range.path.file_name().map(|file_name| {
let language = parsed_markdown
.languages_by_path
.get(&path_range.path)
.or_else(|| {
path_range
.path
.extension()
.and_then(OsStr::to_str)
.and_then(|str| {
let ext = SharedString::new(str.to_string());
parsed_markdown.languages_by_name.get(&ext)
})
});
// We tell the model to use /dev/null for the path instead of using ```language
// because otherwise it consistently fails to use code citations.
if path_range.path.starts_with("/dev/null") {
let ext = path_range
.path
.extension()
.and_then(OsStr::to_str)
.map(|str| SharedString::new(str.to_string()))
.unwrap_or_default();
let icon = language.and_then(|language| {
language
.config()
.matcher
.path_suffixes
.iter()
.find_map(|extension| {
file_icons::FileIcons::get_icon(Path::new(extension), cx)
})
.map(|icon_path| {
code_block_icon(ix, icon_path, Some(language.name().into()))
})
});
render_code_language(
parsed_markdown
.languages_by_path
.get(&path_range.path)
.or_else(|| parsed_markdown.languages_by_name.get(&ext)),
ext,
cx,
)
div().children(icon).into_any_element()
} else {
let icon = file_icons::FileIcons::get_icon(&path_range.path, cx).map(|icon_path| {
code_block_icon(ix, icon_path, language.map(|lang| lang.name().into()))
});
let content = if let Some(parent) = path_range.path.parent() {
h_flex()
.ml_1()
@@ -411,19 +428,11 @@ fn render_markdown_code_block(
.hover(|item| item.bg(cx.theme().colors().element_hover.opacity(0.5)))
.tooltip(Tooltip::text("Jump to File"))
.child(
h_flex()
.gap_0p5()
.children(
file_icons::FileIcons::get_icon(&path_range.path, cx)
.map(Icon::from_path)
.map(|icon| icon.color(Color::Muted).size(IconSize::XSmall)),
)
.child(content)
.child(
Icon::new(IconName::ArrowUpRight)
.size(IconSize::XSmall)
.color(Color::Ignored),
),
h_flex().gap_0p5().children(icon).child(content).child(
Icon::new(IconName::ArrowUpRight)
.size(IconSize::XSmall)
.color(Color::Ignored),
),
)
.on_click({
let path_range = path_range.clone();
@@ -612,6 +621,26 @@ fn render_markdown_code_block(
)
}
fn code_block_icon(
ix: usize,
icon_path: SharedString,
tooltip: Option<SharedString>,
) -> ButtonLike {
let without_tooltip = ButtonLike::new(("code_block_icon", ix))
.disabled(true)
.cursor_style(CursorStyle::Arrow)
.child(
Icon::from_path(icon_path)
.color(Color::Muted)
.size(IconSize::XSmall),
);
match tooltip {
Some(tooltip) => without_tooltip.tooltip(Tooltip::text(tooltip)),
None => without_tooltip,
}
}
fn render_code_language(
language: Option<&Arc<Language>>,
name_fallback: SharedString,

View File

@@ -535,9 +535,15 @@ impl RenderOnce for ButtonLike {
ButtonSize::None => this,
})
.bg(style.enabled(self.layer, cx).background)
.when(self.disabled, |this| this.cursor_not_allowed())
.when(self.disabled, |this| {
if self.cursor_style == CursorStyle::PointingHand {
this.cursor_not_allowed()
} else {
this.cursor(self.cursor_style)
}
})
.when(!self.disabled, |this| {
this.cursor_pointer()
this.cursor(self.cursor_style)
.hover(|hover| hover.bg(style.hovered(self.layer, cx).background))
.active(|active| active.bg(style.active(cx).background))
})