Give code spans in markdown a background highlight
This commit is contained in:
parent
8eb9c6563a
commit
6042df393b
2 changed files with 70 additions and 37 deletions
|
@ -55,7 +55,7 @@ log.workspace = true
|
||||||
ordered-float.workspace = true
|
ordered-float.workspace = true
|
||||||
parking_lot.workspace = true
|
parking_lot.workspace = true
|
||||||
postage.workspace = true
|
postage.workspace = true
|
||||||
pulldown-cmark = { version = "0.9.1", default-features = false }
|
pulldown-cmark = { version = "0.9.2", default-features = false }
|
||||||
rand = { workspace = true, optional = true }
|
rand = { workspace = true, optional = true }
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
serde_derive.workspace = true
|
serde_derive.workspace = true
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
use futures::FutureExt;
|
use futures::FutureExt;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions,
|
actions,
|
||||||
color::Color,
|
|
||||||
elements::{Flex, MouseEventHandler, Padding, ParentElement, Text},
|
elements::{Flex, MouseEventHandler, Padding, ParentElement, Text},
|
||||||
fonts::{HighlightStyle, Underline, Weight},
|
fonts::{HighlightStyle, Underline, Weight},
|
||||||
impl_internal_actions,
|
impl_internal_actions,
|
||||||
platform::{CursorStyle, MouseButton},
|
platform::{CursorStyle, MouseButton},
|
||||||
AnyElement, AppContext, Element, ModelHandle, MouseRegion, Task, ViewContext,
|
AnyElement, AppContext, CursorRegion, Element, ModelHandle, MouseRegion, Task, ViewContext,
|
||||||
};
|
};
|
||||||
use language::{Bias, DiagnosticEntry, DiagnosticSeverity, Language, LanguageRegistry};
|
use language::{Bias, DiagnosticEntry, DiagnosticSeverity, Language, LanguageRegistry};
|
||||||
use project::{HoverBlock, HoverBlockKind, Project};
|
use project::{HoverBlock, HoverBlockKind, Project};
|
||||||
|
@ -275,8 +274,8 @@ fn render_blocks(
|
||||||
) -> RenderedInfo {
|
) -> RenderedInfo {
|
||||||
let mut text = String::new();
|
let mut text = String::new();
|
||||||
let mut highlights = Vec::new();
|
let mut highlights = Vec::new();
|
||||||
let mut link_ranges = Vec::new();
|
let mut region_ranges = Vec::new();
|
||||||
let mut link_urls = Vec::new();
|
let mut regions = Vec::new();
|
||||||
|
|
||||||
for block in blocks {
|
for block in blocks {
|
||||||
match &block.kind {
|
match &block.kind {
|
||||||
|
@ -315,7 +314,12 @@ fn render_blocks(
|
||||||
if italic_depth > 0 {
|
if italic_depth > 0 {
|
||||||
style.italic = Some(true);
|
style.italic = Some(true);
|
||||||
}
|
}
|
||||||
if link_url.is_some() {
|
if let Some(link_url) = link_url.clone() {
|
||||||
|
region_ranges.push(prev_len..text.len());
|
||||||
|
regions.push(RenderedRegion {
|
||||||
|
link_url: Some(link_url),
|
||||||
|
code: false,
|
||||||
|
});
|
||||||
style.underline = Some(Underline {
|
style.underline = Some(Underline {
|
||||||
thickness: 1.0.into(),
|
thickness: 1.0.into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
@ -338,13 +342,23 @@ fn render_blocks(
|
||||||
}
|
}
|
||||||
Event::Code(t) => {
|
Event::Code(t) => {
|
||||||
text.push_str(t.as_ref());
|
text.push_str(t.as_ref());
|
||||||
highlights.push((
|
region_ranges.push(prev_len..text.len());
|
||||||
prev_len..text.len(),
|
if link_url.is_some() {
|
||||||
HighlightStyle {
|
highlights.push((
|
||||||
color: Some(Color::red()),
|
prev_len..text.len(),
|
||||||
..Default::default()
|
HighlightStyle {
|
||||||
},
|
underline: Some(Underline {
|
||||||
));
|
thickness: 1.0.into(),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
regions.push(RenderedRegion {
|
||||||
|
code: true,
|
||||||
|
link_url: link_url.clone(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Event::Start(tag) => match tag {
|
Event::Start(tag) => match tag {
|
||||||
Tag::Paragraph => new_paragraph(&mut text, &mut list_stack),
|
Tag::Paragraph => new_paragraph(&mut text, &mut list_stack),
|
||||||
|
@ -363,7 +377,7 @@ fn render_blocks(
|
||||||
}
|
}
|
||||||
Tag::Emphasis => italic_depth += 1,
|
Tag::Emphasis => italic_depth += 1,
|
||||||
Tag::Strong => bold_depth += 1,
|
Tag::Strong => bold_depth += 1,
|
||||||
Tag::Link(_, url, _) => link_url = Some((prev_len, url)),
|
Tag::Link(_, url, _) => link_url = Some(url.to_string()),
|
||||||
Tag::List(number) => {
|
Tag::List(number) => {
|
||||||
list_stack.push((number, false));
|
list_stack.push((number, false));
|
||||||
}
|
}
|
||||||
|
@ -393,15 +407,8 @@ fn render_blocks(
|
||||||
Tag::CodeBlock(_) => current_language = None,
|
Tag::CodeBlock(_) => current_language = None,
|
||||||
Tag::Emphasis => italic_depth -= 1,
|
Tag::Emphasis => italic_depth -= 1,
|
||||||
Tag::Strong => bold_depth -= 1,
|
Tag::Strong => bold_depth -= 1,
|
||||||
Tag::Link(_, _, _) => {
|
Tag::Link(_, _, _) => link_url = None,
|
||||||
if let Some((start_offset, link_url)) = link_url.take() {
|
Tag::List(_) => drop(list_stack.pop()),
|
||||||
link_ranges.push(start_offset..text.len());
|
|
||||||
link_urls.push(link_url.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Tag::List(_) => {
|
|
||||||
list_stack.pop();
|
|
||||||
}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
Event::HardBreak => text.push('\n'),
|
Event::HardBreak => text.push('\n'),
|
||||||
|
@ -432,8 +439,8 @@ fn render_blocks(
|
||||||
theme_id,
|
theme_id,
|
||||||
text,
|
text,
|
||||||
highlights,
|
highlights,
|
||||||
link_ranges,
|
region_ranges,
|
||||||
link_urls,
|
regions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -542,8 +549,14 @@ struct RenderedInfo {
|
||||||
theme_id: usize,
|
theme_id: usize,
|
||||||
text: String,
|
text: String,
|
||||||
highlights: Vec<(Range<usize>, HighlightStyle)>,
|
highlights: Vec<(Range<usize>, HighlightStyle)>,
|
||||||
link_ranges: Vec<Range<usize>>,
|
region_ranges: Vec<Range<usize>>,
|
||||||
link_urls: Vec<String>,
|
regions: Vec<RenderedRegion>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct RenderedRegion {
|
||||||
|
code: bool,
|
||||||
|
link_url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InfoPopover {
|
impl InfoPopover {
|
||||||
|
@ -571,22 +584,42 @@ impl InfoPopover {
|
||||||
let mut region_id = 0;
|
let mut region_id = 0;
|
||||||
let view_id = cx.view_id();
|
let view_id = cx.view_id();
|
||||||
|
|
||||||
let link_urls = rendered_content.link_urls.clone();
|
let code_span_background_color = style.document_highlight_read_background;
|
||||||
|
let regions = rendered_content.regions.clone();
|
||||||
Flex::column()
|
Flex::column()
|
||||||
.scrollable::<HoverBlock>(1, None, cx)
|
.scrollable::<HoverBlock>(1, None, cx)
|
||||||
.with_child(
|
.with_child(
|
||||||
Text::new(rendered_content.text.clone(), style.text.clone())
|
Text::new(rendered_content.text.clone(), style.text.clone())
|
||||||
.with_highlights(rendered_content.highlights.clone())
|
.with_highlights(rendered_content.highlights.clone())
|
||||||
.with_mouse_regions(
|
.with_custom_runs(
|
||||||
rendered_content.link_ranges.clone(),
|
rendered_content.region_ranges.clone(),
|
||||||
move |ix, bounds| {
|
move |ix, bounds, scene, _| {
|
||||||
region_id += 1;
|
region_id += 1;
|
||||||
let url = link_urls[ix].clone();
|
let region = regions[ix].clone();
|
||||||
MouseRegion::new::<Self>(view_id, region_id, bounds)
|
if let Some(url) = region.link_url {
|
||||||
.on_click::<Editor, _>(MouseButton::Left, move |_, _, cx| {
|
scene.push_cursor_region(CursorRegion {
|
||||||
println!("clicked link {url}");
|
bounds,
|
||||||
cx.platform().open_url(&url);
|
style: CursorStyle::PointingHand,
|
||||||
})
|
});
|
||||||
|
scene.push_mouse_region(
|
||||||
|
MouseRegion::new::<Self>(view_id, region_id, bounds)
|
||||||
|
.on_click::<Editor, _>(
|
||||||
|
MouseButton::Left,
|
||||||
|
move |_, _, cx| {
|
||||||
|
println!("clicked link {url}");
|
||||||
|
cx.platform().open_url(&url);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if region.code {
|
||||||
|
scene.push_quad(gpui::Quad {
|
||||||
|
bounds,
|
||||||
|
background: Some(code_span_background_color),
|
||||||
|
border: Default::default(),
|
||||||
|
corner_radius: 2.0,
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.with_soft_wrap(true),
|
.with_soft_wrap(true),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue