Merge info popover's and autocomplete docs' markdown rendering
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
display_map::{InlayOffset, ToDisplayPoint},
|
display_map::{InlayOffset, ToDisplayPoint},
|
||||||
link_go_to_definition::{DocumentRange, InlayRange},
|
link_go_to_definition::{DocumentRange, InlayRange},
|
||||||
|
markdown::{self, RenderedRegion},
|
||||||
Anchor, AnchorRangeExt, DisplayPoint, Editor, EditorSettings, EditorSnapshot, EditorStyle,
|
Anchor, AnchorRangeExt, DisplayPoint, Editor, EditorSettings, EditorSnapshot, EditorStyle,
|
||||||
ExcerptId, RangeToAnchorExt,
|
ExcerptId, RangeToAnchorExt,
|
||||||
};
|
};
|
||||||
@@ -8,7 +9,7 @@ use futures::FutureExt;
|
|||||||
use gpui::{
|
use gpui::{
|
||||||
actions,
|
actions,
|
||||||
elements::{Flex, MouseEventHandler, Padding, ParentElement, Text},
|
elements::{Flex, MouseEventHandler, Padding, ParentElement, Text},
|
||||||
fonts::{HighlightStyle, Underline, Weight},
|
fonts::HighlightStyle,
|
||||||
platform::{CursorStyle, MouseButton},
|
platform::{CursorStyle, MouseButton},
|
||||||
AnyElement, AppContext, CursorRegion, Element, ModelHandle, MouseRegion, Task, ViewContext,
|
AnyElement, AppContext, CursorRegion, Element, ModelHandle, MouseRegion, Task, ViewContext,
|
||||||
};
|
};
|
||||||
@@ -364,7 +365,7 @@ fn render_blocks(
|
|||||||
theme_id: usize,
|
theme_id: usize,
|
||||||
blocks: &[HoverBlock],
|
blocks: &[HoverBlock],
|
||||||
language_registry: &Arc<LanguageRegistry>,
|
language_registry: &Arc<LanguageRegistry>,
|
||||||
language: Option<&Arc<Language>>,
|
language: &Option<Arc<Language>>,
|
||||||
style: &EditorStyle,
|
style: &EditorStyle,
|
||||||
) -> RenderedInfo {
|
) -> RenderedInfo {
|
||||||
let mut text = String::new();
|
let mut text = String::new();
|
||||||
@@ -375,160 +376,20 @@ fn render_blocks(
|
|||||||
for block in blocks {
|
for block in blocks {
|
||||||
match &block.kind {
|
match &block.kind {
|
||||||
HoverBlockKind::PlainText => {
|
HoverBlockKind::PlainText => {
|
||||||
new_paragraph(&mut text, &mut Vec::new());
|
markdown::new_paragraph(&mut text, &mut Vec::new());
|
||||||
text.push_str(&block.text);
|
text.push_str(&block.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
HoverBlockKind::Markdown => {
|
HoverBlockKind::Markdown => markdown::render_markdown_block(
|
||||||
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
|
&block.text,
|
||||||
|
language_registry,
|
||||||
let mut bold_depth = 0;
|
language,
|
||||||
let mut italic_depth = 0;
|
style,
|
||||||
let mut link_url = None;
|
&mut text,
|
||||||
let mut current_language = None;
|
&mut highlights,
|
||||||
let mut list_stack = Vec::new();
|
&mut region_ranges,
|
||||||
|
&mut regions,
|
||||||
for event in Parser::new_ext(&block.text, Options::all()) {
|
),
|
||||||
let prev_len = text.len();
|
|
||||||
match event {
|
|
||||||
Event::Text(t) => {
|
|
||||||
if let Some(language) = ¤t_language {
|
|
||||||
render_code(
|
|
||||||
&mut text,
|
|
||||||
&mut highlights,
|
|
||||||
t.as_ref(),
|
|
||||||
language,
|
|
||||||
style,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
text.push_str(t.as_ref());
|
|
||||||
|
|
||||||
let mut style = HighlightStyle::default();
|
|
||||||
if bold_depth > 0 {
|
|
||||||
style.weight = Some(Weight::BOLD);
|
|
||||||
}
|
|
||||||
if italic_depth > 0 {
|
|
||||||
style.italic = Some(true);
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
thickness: 1.0.into(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if style != HighlightStyle::default() {
|
|
||||||
let mut new_highlight = true;
|
|
||||||
if let Some((last_range, last_style)) = highlights.last_mut() {
|
|
||||||
if last_range.end == prev_len && last_style == &style {
|
|
||||||
last_range.end = text.len();
|
|
||||||
new_highlight = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if new_highlight {
|
|
||||||
highlights.push((prev_len..text.len(), style));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Event::Code(t) => {
|
|
||||||
text.push_str(t.as_ref());
|
|
||||||
region_ranges.push(prev_len..text.len());
|
|
||||||
if link_url.is_some() {
|
|
||||||
highlights.push((
|
|
||||||
prev_len..text.len(),
|
|
||||||
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 {
|
|
||||||
Tag::Paragraph => new_paragraph(&mut text, &mut list_stack),
|
|
||||||
|
|
||||||
Tag::Heading(_, _, _) => {
|
|
||||||
new_paragraph(&mut text, &mut list_stack);
|
|
||||||
bold_depth += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Tag::CodeBlock(kind) => {
|
|
||||||
new_paragraph(&mut text, &mut list_stack);
|
|
||||||
current_language = if let CodeBlockKind::Fenced(language) = kind {
|
|
||||||
language_registry
|
|
||||||
.language_for_name(language.as_ref())
|
|
||||||
.now_or_never()
|
|
||||||
.and_then(Result::ok)
|
|
||||||
} else {
|
|
||||||
language.cloned()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Tag::Emphasis => italic_depth += 1,
|
|
||||||
|
|
||||||
Tag::Strong => bold_depth += 1,
|
|
||||||
|
|
||||||
Tag::Link(_, url, _) => link_url = Some(url.to_string()),
|
|
||||||
|
|
||||||
Tag::List(number) => {
|
|
||||||
list_stack.push((number, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
Tag::Item => {
|
|
||||||
let len = list_stack.len();
|
|
||||||
if let Some((list_number, has_content)) = list_stack.last_mut() {
|
|
||||||
*has_content = false;
|
|
||||||
if !text.is_empty() && !text.ends_with('\n') {
|
|
||||||
text.push('\n');
|
|
||||||
}
|
|
||||||
for _ in 0..len - 1 {
|
|
||||||
text.push_str(" ");
|
|
||||||
}
|
|
||||||
if let Some(number) = list_number {
|
|
||||||
text.push_str(&format!("{}. ", number));
|
|
||||||
*number += 1;
|
|
||||||
*has_content = false;
|
|
||||||
} else {
|
|
||||||
text.push_str("- ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
|
|
||||||
Event::End(tag) => match tag {
|
|
||||||
Tag::Heading(_, _, _) => bold_depth -= 1,
|
|
||||||
Tag::CodeBlock(_) => current_language = None,
|
|
||||||
Tag::Emphasis => italic_depth -= 1,
|
|
||||||
Tag::Strong => bold_depth -= 1,
|
|
||||||
Tag::Link(_, _, _) => link_url = None,
|
|
||||||
Tag::List(_) => drop(list_stack.pop()),
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
|
|
||||||
Event::HardBreak => text.push('\n'),
|
|
||||||
|
|
||||||
Event::SoftBreak => text.push(' '),
|
|
||||||
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HoverBlockKind::Code { language } => {
|
HoverBlockKind::Code { language } => {
|
||||||
if let Some(language) = language_registry
|
if let Some(language) = language_registry
|
||||||
@@ -536,7 +397,13 @@ fn render_blocks(
|
|||||||
.now_or_never()
|
.now_or_never()
|
||||||
.and_then(Result::ok)
|
.and_then(Result::ok)
|
||||||
{
|
{
|
||||||
render_code(&mut text, &mut highlights, &block.text, &language, style);
|
markdown::render_code(
|
||||||
|
&mut text,
|
||||||
|
&mut highlights,
|
||||||
|
&block.text,
|
||||||
|
&language,
|
||||||
|
style,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
text.push_str(&block.text);
|
text.push_str(&block.text);
|
||||||
}
|
}
|
||||||
@@ -553,47 +420,6 @@ fn render_blocks(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_code(
|
|
||||||
text: &mut String,
|
|
||||||
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
|
||||||
content: &str,
|
|
||||||
language: &Arc<Language>,
|
|
||||||
style: &EditorStyle,
|
|
||||||
) {
|
|
||||||
let prev_len = text.len();
|
|
||||||
text.push_str(content);
|
|
||||||
for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
|
|
||||||
if let Some(style) = highlight_id.style(&style.syntax) {
|
|
||||||
highlights.push((prev_len + range.start..prev_len + range.end, style));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
|
|
||||||
let mut is_subsequent_paragraph_of_list = false;
|
|
||||||
if let Some((_, has_content)) = list_stack.last_mut() {
|
|
||||||
if *has_content {
|
|
||||||
is_subsequent_paragraph_of_list = true;
|
|
||||||
} else {
|
|
||||||
*has_content = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !text.is_empty() {
|
|
||||||
if !text.ends_with('\n') {
|
|
||||||
text.push('\n');
|
|
||||||
}
|
|
||||||
text.push('\n');
|
|
||||||
}
|
|
||||||
for _ in 0..list_stack.len().saturating_sub(1) {
|
|
||||||
text.push_str(" ");
|
|
||||||
}
|
|
||||||
if is_subsequent_paragraph_of_list {
|
|
||||||
text.push_str(" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct HoverState {
|
pub struct HoverState {
|
||||||
pub info_popover: Option<InfoPopover>,
|
pub info_popover: Option<InfoPopover>,
|
||||||
@@ -666,12 +492,6 @@ struct RenderedInfo {
|
|||||||
regions: Vec<RenderedRegion>,
|
regions: Vec<RenderedRegion>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
struct RenderedRegion {
|
|
||||||
code: bool,
|
|
||||||
link_url: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InfoPopover {
|
impl InfoPopover {
|
||||||
pub fn render(
|
pub fn render(
|
||||||
&mut self,
|
&mut self,
|
||||||
@@ -689,7 +509,7 @@ impl InfoPopover {
|
|||||||
style.theme_id,
|
style.theme_id,
|
||||||
&self.blocks,
|
&self.blocks,
|
||||||
self.project.read(cx).languages(),
|
self.project.read(cx).languages(),
|
||||||
self.language.as_ref(),
|
&self.language,
|
||||||
style,
|
style,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
@@ -829,7 +649,7 @@ mod tests {
|
|||||||
test::editor_lsp_test_context::EditorLspTestContext,
|
test::editor_lsp_test_context::EditorLspTestContext,
|
||||||
};
|
};
|
||||||
use collections::BTreeSet;
|
use collections::BTreeSet;
|
||||||
use gpui::fonts::Weight;
|
use gpui::fonts::{Underline, Weight};
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
use language::{language_settings::InlayHintSettings, Diagnostic, DiagnosticSet};
|
use language::{language_settings::InlayHintSettings, Diagnostic, DiagnosticSet};
|
||||||
use lsp::LanguageServerId;
|
use lsp::LanguageServerId;
|
||||||
@@ -1055,7 +875,7 @@ mod tests {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let style = editor.style(cx);
|
let style = editor.style(cx);
|
||||||
let rendered = render_blocks(0, &blocks, &Default::default(), None, &style);
|
let rendered = render_blocks(0, &blocks, &Default::default(), &None, &style);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
rendered.text,
|
rendered.text,
|
||||||
code_str.trim(),
|
code_str.trim(),
|
||||||
@@ -1247,7 +1067,7 @@ mod tests {
|
|||||||
expected_styles,
|
expected_styles,
|
||||||
} in &rows[0..]
|
} in &rows[0..]
|
||||||
{
|
{
|
||||||
let rendered = render_blocks(0, &blocks, &Default::default(), None, &style);
|
let rendered = render_blocks(0, &blocks, &Default::default(), &None, &style);
|
||||||
|
|
||||||
let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
|
let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
|
||||||
let expected_highlights = ranges
|
let expected_highlights = ranges
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
|
|||||||
use crate::{Editor, EditorStyle};
|
use crate::{Editor, EditorStyle};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct RenderedRegion {
|
pub struct RenderedRegion {
|
||||||
code: bool,
|
pub code: bool,
|
||||||
link_url: Option<String>,
|
pub link_url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_markdown(
|
pub fn render_markdown(
|
||||||
@@ -31,6 +31,59 @@ pub fn render_markdown(
|
|||||||
let mut region_ranges = Vec::new();
|
let mut region_ranges = Vec::new();
|
||||||
let mut regions = Vec::new();
|
let mut regions = Vec::new();
|
||||||
|
|
||||||
|
render_markdown_block(
|
||||||
|
markdown,
|
||||||
|
language_registry,
|
||||||
|
language,
|
||||||
|
style,
|
||||||
|
&mut text,
|
||||||
|
&mut highlights,
|
||||||
|
&mut region_ranges,
|
||||||
|
&mut regions,
|
||||||
|
);
|
||||||
|
|
||||||
|
let code_span_background_color = style.document_highlight_read_background;
|
||||||
|
let view_id = cx.view_id();
|
||||||
|
let mut region_id = 0;
|
||||||
|
Text::new(text, style.text.clone())
|
||||||
|
.with_highlights(highlights)
|
||||||
|
.with_custom_runs(region_ranges, move |ix, bounds, scene, _| {
|
||||||
|
region_id += 1;
|
||||||
|
let region = regions[ix].clone();
|
||||||
|
if let Some(url) = region.link_url {
|
||||||
|
scene.push_cursor_region(CursorRegion {
|
||||||
|
bounds,
|
||||||
|
style: CursorStyle::PointingHand,
|
||||||
|
});
|
||||||
|
scene.push_mouse_region(
|
||||||
|
MouseRegion::new::<Editor>(view_id, region_id, bounds)
|
||||||
|
.on_click::<Editor, _>(MouseButton::Left, move |_, _, cx| {
|
||||||
|
cx.platform().open_url(&url)
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if region.code {
|
||||||
|
scene.push_quad(gpui::Quad {
|
||||||
|
bounds,
|
||||||
|
background: Some(code_span_background_color),
|
||||||
|
border: Default::default(),
|
||||||
|
corner_radii: (2.0).into(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.with_soft_wrap(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render_markdown_block(
|
||||||
|
markdown: &str,
|
||||||
|
language_registry: &Arc<LanguageRegistry>,
|
||||||
|
language: &Option<Arc<Language>>,
|
||||||
|
style: &EditorStyle,
|
||||||
|
text: &mut String,
|
||||||
|
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
||||||
|
region_ranges: &mut Vec<Range<usize>>,
|
||||||
|
regions: &mut Vec<RenderedRegion>,
|
||||||
|
) {
|
||||||
let mut bold_depth = 0;
|
let mut bold_depth = 0;
|
||||||
let mut italic_depth = 0;
|
let mut italic_depth = 0;
|
||||||
let mut link_url = None;
|
let mut link_url = None;
|
||||||
@@ -42,7 +95,7 @@ pub fn render_markdown(
|
|||||||
match event {
|
match event {
|
||||||
Event::Text(t) => {
|
Event::Text(t) => {
|
||||||
if let Some(language) = ¤t_language {
|
if let Some(language) = ¤t_language {
|
||||||
render_code(&mut text, &mut highlights, t.as_ref(), language, style);
|
render_code(text, highlights, t.as_ref(), language, style);
|
||||||
} else {
|
} else {
|
||||||
text.push_str(t.as_ref());
|
text.push_str(t.as_ref());
|
||||||
|
|
||||||
@@ -102,15 +155,15 @@ pub fn render_markdown(
|
|||||||
}
|
}
|
||||||
|
|
||||||
Event::Start(tag) => match tag {
|
Event::Start(tag) => match tag {
|
||||||
Tag::Paragraph => new_paragraph(&mut text, &mut list_stack),
|
Tag::Paragraph => new_paragraph(text, &mut list_stack),
|
||||||
|
|
||||||
Tag::Heading(_, _, _) => {
|
Tag::Heading(_, _, _) => {
|
||||||
new_paragraph(&mut text, &mut list_stack);
|
new_paragraph(text, &mut list_stack);
|
||||||
bold_depth += 1;
|
bold_depth += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Tag::CodeBlock(kind) => {
|
Tag::CodeBlock(kind) => {
|
||||||
new_paragraph(&mut text, &mut list_stack);
|
new_paragraph(text, &mut list_stack);
|
||||||
current_language = if let CodeBlockKind::Fenced(language) = kind {
|
current_language = if let CodeBlockKind::Fenced(language) = kind {
|
||||||
language_registry
|
language_registry
|
||||||
.language_for_name(language.as_ref())
|
.language_for_name(language.as_ref())
|
||||||
@@ -171,40 +224,9 @@ pub fn render_markdown(
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let code_span_background_color = style.document_highlight_read_background;
|
|
||||||
let view_id = cx.view_id();
|
|
||||||
let mut region_id = 0;
|
|
||||||
Text::new(text, style.text.clone())
|
|
||||||
.with_highlights(highlights)
|
|
||||||
.with_custom_runs(region_ranges, move |ix, bounds, scene, _| {
|
|
||||||
region_id += 1;
|
|
||||||
let region = regions[ix].clone();
|
|
||||||
if let Some(url) = region.link_url {
|
|
||||||
scene.push_cursor_region(CursorRegion {
|
|
||||||
bounds,
|
|
||||||
style: CursorStyle::PointingHand,
|
|
||||||
});
|
|
||||||
scene.push_mouse_region(
|
|
||||||
MouseRegion::new::<Editor>(view_id, region_id, bounds)
|
|
||||||
.on_click::<Editor, _>(MouseButton::Left, move |_, _, cx| {
|
|
||||||
cx.platform().open_url(&url)
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if region.code {
|
|
||||||
scene.push_quad(gpui::Quad {
|
|
||||||
bounds,
|
|
||||||
background: Some(code_span_background_color),
|
|
||||||
border: Default::default(),
|
|
||||||
corner_radii: (2.0).into(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.with_soft_wrap(true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_code(
|
pub fn render_code(
|
||||||
text: &mut String,
|
text: &mut String,
|
||||||
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
||||||
content: &str,
|
content: &str,
|
||||||
@@ -220,7 +242,7 @@ fn render_code(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
|
pub fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
|
||||||
let mut is_subsequent_paragraph_of_list = false;
|
let mut is_subsequent_paragraph_of_list = false;
|
||||||
if let Some((_, has_content)) = list_stack.last_mut() {
|
if let Some((_, has_content)) = list_stack.last_mut() {
|
||||||
if *has_content {
|
if *has_content {
|
||||||
|
|||||||
Reference in New Issue
Block a user