mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-26 23:22:41 +03:00
Don't need editor style to parse markdown
This commit is contained in:
parent
a881b1f5fb
commit
8dca4c3f9a
@ -60,6 +60,7 @@ use itertools::Itertools;
|
||||
pub use language::{char_kind, CharKind};
|
||||
use language::{
|
||||
language_settings::{self, all_language_settings, InlayHintSettings},
|
||||
markdown::MarkdownHighlight,
|
||||
point_from_lsp, prepare_completion_documentation, AutoindentMode, BracketPair, Buffer,
|
||||
CodeAction, CodeLabel, Completion, CursorShape, Diagnostic, DiagnosticSeverity, Documentation,
|
||||
File, IndentKind, IndentSize, Language, LanguageServerName, OffsetRangeExt, OffsetUtf16, Point,
|
||||
@ -120,21 +121,57 @@ pub const DOCUMENT_HIGHLIGHTS_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis
|
||||
pub const FORMAT_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
|
||||
pub fn render_parsed_markdown(
|
||||
md: &language::ParsedMarkdown,
|
||||
style: &EditorStyle,
|
||||
parsed: &language::ParsedMarkdown,
|
||||
editor_style: &EditorStyle,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> Text {
|
||||
enum RenderedMarkdown {}
|
||||
|
||||
let md = md.clone();
|
||||
let code_span_background_color = style.document_highlight_read_background;
|
||||
let parsed = parsed.clone();
|
||||
let view_id = cx.view_id();
|
||||
let code_span_background_color = editor_style.document_highlight_read_background;
|
||||
|
||||
let mut region_id = 0;
|
||||
Text::new(md.text, style.text.clone())
|
||||
.with_highlights(md.highlights)
|
||||
.with_custom_runs(md.region_ranges, move |ix, bounds, scene, _| {
|
||||
|
||||
Text::new(parsed.text, editor_style.text.clone())
|
||||
.with_highlights(
|
||||
parsed
|
||||
.highlights
|
||||
.iter()
|
||||
.filter_map(|(range, highlight)| {
|
||||
let highlight = match highlight {
|
||||
MarkdownHighlight::Style(style) => {
|
||||
let mut highlight = HighlightStyle::default();
|
||||
|
||||
if style.italic {
|
||||
highlight.italic = Some(true);
|
||||
}
|
||||
|
||||
if style.underline {
|
||||
highlight.underline = Some(fonts::Underline {
|
||||
thickness: 1.0.into(),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
if style.weight != fonts::Weight::default() {
|
||||
highlight.weight = Some(style.weight);
|
||||
}
|
||||
|
||||
highlight
|
||||
}
|
||||
|
||||
MarkdownHighlight::Code(id) => id.style(&editor_style.syntax)?,
|
||||
};
|
||||
|
||||
Some((range.clone(), highlight))
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.with_custom_runs(parsed.region_ranges, move |ix, bounds, scene, _| {
|
||||
region_id += 1;
|
||||
let region = md.regions[ix].clone();
|
||||
let region = parsed.regions[ix].clone();
|
||||
|
||||
if let Some(url) = region.link_url {
|
||||
scene.push_cursor_region(CursorRegion {
|
||||
bounds,
|
||||
@ -147,6 +184,7 @@ pub fn render_parsed_markdown(
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
if region.code {
|
||||
scene.push_quad(gpui::Quad {
|
||||
bounds,
|
||||
@ -831,12 +869,11 @@ impl ContextMenu {
|
||||
fn select_first(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> bool {
|
||||
if self.visible() {
|
||||
match self {
|
||||
ContextMenu::Completions(menu) => menu.select_first(project, style, cx),
|
||||
ContextMenu::Completions(menu) => menu.select_first(project, cx),
|
||||
ContextMenu::CodeActions(menu) => menu.select_first(cx),
|
||||
}
|
||||
true
|
||||
@ -848,12 +885,11 @@ impl ContextMenu {
|
||||
fn select_prev(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> bool {
|
||||
if self.visible() {
|
||||
match self {
|
||||
ContextMenu::Completions(menu) => menu.select_prev(project, style, cx),
|
||||
ContextMenu::Completions(menu) => menu.select_prev(project, cx),
|
||||
ContextMenu::CodeActions(menu) => menu.select_prev(cx),
|
||||
}
|
||||
true
|
||||
@ -865,12 +901,11 @@ impl ContextMenu {
|
||||
fn select_next(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> bool {
|
||||
if self.visible() {
|
||||
match self {
|
||||
ContextMenu::Completions(menu) => menu.select_next(project, style, cx),
|
||||
ContextMenu::Completions(menu) => menu.select_next(project, cx),
|
||||
ContextMenu::CodeActions(menu) => menu.select_next(cx),
|
||||
}
|
||||
true
|
||||
@ -882,12 +917,11 @@ impl ContextMenu {
|
||||
fn select_last(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> bool {
|
||||
if self.visible() {
|
||||
match self {
|
||||
ContextMenu::Completions(menu) => menu.select_last(project, style, cx),
|
||||
ContextMenu::Completions(menu) => menu.select_last(project, cx),
|
||||
ContextMenu::CodeActions(menu) => menu.select_last(cx),
|
||||
}
|
||||
true
|
||||
@ -932,59 +966,54 @@ impl CompletionsMenu {
|
||||
fn select_first(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
self.selected_item = 0;
|
||||
self.list.scroll_to(ScrollTarget::Show(self.selected_item));
|
||||
self.attempt_resolve_selected_completion(project, style, cx);
|
||||
self.attempt_resolve_selected_completion(project, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn select_prev(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
if self.selected_item > 0 {
|
||||
self.selected_item -= 1;
|
||||
self.list.scroll_to(ScrollTarget::Show(self.selected_item));
|
||||
}
|
||||
self.attempt_resolve_selected_completion(project, style, cx);
|
||||
self.attempt_resolve_selected_completion(project, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn select_next(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
if self.selected_item + 1 < self.matches.len() {
|
||||
self.selected_item += 1;
|
||||
self.list.scroll_to(ScrollTarget::Show(self.selected_item));
|
||||
}
|
||||
self.attempt_resolve_selected_completion(project, style, cx);
|
||||
self.attempt_resolve_selected_completion(project, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn select_last(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
self.selected_item = self.matches.len() - 1;
|
||||
self.list.scroll_to(ScrollTarget::Show(self.selected_item));
|
||||
self.attempt_resolve_selected_completion(project, style, cx);
|
||||
self.attempt_resolve_selected_completion(project, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn attempt_resolve_selected_completion(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
style: theme::Editor,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
let index = self.matches[self.selected_item].candidate_id;
|
||||
@ -1029,7 +1058,6 @@ impl CompletionsMenu {
|
||||
&lsp_documentation,
|
||||
&language_registry,
|
||||
None, // TODO: Try to reasonably work out which language the completion is for
|
||||
&style,
|
||||
)
|
||||
.await;
|
||||
|
||||
@ -1097,10 +1125,8 @@ impl CompletionsMenu {
|
||||
.as_ref()
|
||||
.expect("It is impossible have LSP servers without a project");
|
||||
|
||||
let lsp_docs = lsp_docs.clone();
|
||||
let lsp_docs = lsp_docs.clone();
|
||||
let language_registry = project.read(cx).languages().clone();
|
||||
let style = style.theme.clone();
|
||||
let completions = completions.clone();
|
||||
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
@ -1108,7 +1134,6 @@ impl CompletionsMenu {
|
||||
&lsp_docs,
|
||||
&language_registry,
|
||||
None,
|
||||
&style,
|
||||
)
|
||||
.await;
|
||||
|
||||
@ -3365,11 +3390,7 @@ impl Editor {
|
||||
None
|
||||
} else {
|
||||
_ = this.update(&mut cx, |editor, cx| {
|
||||
menu.attempt_resolve_selected_completion(
|
||||
editor.project.as_ref(),
|
||||
editor.style(cx).theme,
|
||||
cx,
|
||||
);
|
||||
menu.attempt_resolve_selected_completion(editor.project.as_ref(), cx);
|
||||
});
|
||||
Some(menu)
|
||||
}
|
||||
@ -5545,16 +5566,13 @@ impl Editor {
|
||||
return;
|
||||
}
|
||||
|
||||
if self.context_menu.is_some() {
|
||||
let style = self.style(cx).theme;
|
||||
if self
|
||||
.context_menu
|
||||
.as_mut()
|
||||
.map(|menu| menu.select_last(self.project.as_ref(), style, cx))
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if self
|
||||
.context_menu
|
||||
.as_mut()
|
||||
.map(|menu| menu.select_last(self.project.as_ref(), cx))
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if matches!(self.mode, EditorMode::SingleLine) {
|
||||
@ -5594,30 +5612,26 @@ impl Editor {
|
||||
}
|
||||
|
||||
pub fn context_menu_first(&mut self, _: &ContextMenuFirst, cx: &mut ViewContext<Self>) {
|
||||
let style = self.style(cx).theme;
|
||||
if let Some(context_menu) = self.context_menu.as_mut() {
|
||||
context_menu.select_first(self.project.as_ref(), style, cx);
|
||||
context_menu.select_first(self.project.as_ref(), cx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_menu_prev(&mut self, _: &ContextMenuPrev, cx: &mut ViewContext<Self>) {
|
||||
let style = self.style(cx).theme;
|
||||
if let Some(context_menu) = self.context_menu.as_mut() {
|
||||
context_menu.select_prev(self.project.as_ref(), style, cx);
|
||||
context_menu.select_prev(self.project.as_ref(), cx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_menu_next(&mut self, _: &ContextMenuNext, cx: &mut ViewContext<Self>) {
|
||||
let style = self.style(cx).theme;
|
||||
if let Some(context_menu) = self.context_menu.as_mut() {
|
||||
context_menu.select_next(self.project.as_ref(), style, cx);
|
||||
context_menu.select_next(self.project.as_ref(), cx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context_menu_last(&mut self, _: &ContextMenuLast, cx: &mut ViewContext<Self>) {
|
||||
let style = self.style(cx).theme;
|
||||
if let Some(context_menu) = self.context_menu.as_mut() {
|
||||
context_menu.select_last(self.project.as_ref(), style, cx);
|
||||
context_menu.select_last(self.project.as_ref(), cx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,13 +8,11 @@ use futures::FutureExt;
|
||||
use gpui::{
|
||||
actions,
|
||||
elements::{Empty, Flex, MouseEventHandler, Padding, ParentElement, Text},
|
||||
fonts::HighlightStyle,
|
||||
platform::{CursorStyle, MouseButton},
|
||||
AnyElement, AppContext, CursorRegion, Element, ModelHandle, MouseRegion, Task, ViewContext,
|
||||
AnyElement, AppContext, Element, ModelHandle, Task, ViewContext,
|
||||
};
|
||||
use language::{
|
||||
markdown::{self, ParsedRegion},
|
||||
Bias, DiagnosticEntry, DiagnosticSeverity, Language, LanguageRegistry,
|
||||
markdown, Bias, DiagnosticEntry, DiagnosticSeverity, Language, LanguageRegistry, ParsedMarkdown,
|
||||
};
|
||||
use project::{HoverBlock, HoverBlockKind, InlayHintLabelPart, Project};
|
||||
use std::{ops::Range, sync::Arc, time::Duration};
|
||||
@ -363,13 +361,11 @@ fn show_hover(
|
||||
editor.hover_state.info_task = Some(task);
|
||||
}
|
||||
|
||||
async fn render_blocks(
|
||||
theme_id: usize,
|
||||
async fn parse_blocks(
|
||||
blocks: &[HoverBlock],
|
||||
language_registry: &Arc<LanguageRegistry>,
|
||||
language: Option<Arc<Language>>,
|
||||
style: &theme::Editor,
|
||||
) -> ParsedInfo {
|
||||
) -> markdown::ParsedMarkdown {
|
||||
let mut text = String::new();
|
||||
let mut highlights = Vec::new();
|
||||
let mut region_ranges = Vec::new();
|
||||
@ -387,7 +383,6 @@ async fn render_blocks(
|
||||
&block.text,
|
||||
language_registry,
|
||||
language.clone(),
|
||||
style,
|
||||
&mut text,
|
||||
&mut highlights,
|
||||
&mut region_ranges,
|
||||
@ -402,13 +397,7 @@ async fn render_blocks(
|
||||
.now_or_never()
|
||||
.and_then(Result::ok)
|
||||
{
|
||||
markdown::highlight_code(
|
||||
&mut text,
|
||||
&mut highlights,
|
||||
&block.text,
|
||||
&language,
|
||||
style,
|
||||
);
|
||||
markdown::highlight_code(&mut text, &mut highlights, &block.text, &language);
|
||||
} else {
|
||||
text.push_str(&block.text);
|
||||
}
|
||||
@ -416,8 +405,7 @@ async fn render_blocks(
|
||||
}
|
||||
}
|
||||
|
||||
ParsedInfo {
|
||||
theme_id,
|
||||
ParsedMarkdown {
|
||||
text: text.trim().to_string(),
|
||||
highlights,
|
||||
region_ranges,
|
||||
@ -485,16 +473,7 @@ pub struct InfoPopover {
|
||||
symbol_range: DocumentRange,
|
||||
pub blocks: Vec<HoverBlock>,
|
||||
language: Option<Arc<Language>>,
|
||||
parsed_content: Option<ParsedInfo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ParsedInfo {
|
||||
theme_id: usize,
|
||||
text: String,
|
||||
highlights: Vec<(Range<usize>, HighlightStyle)>,
|
||||
region_ranges: Vec<Range<usize>>,
|
||||
regions: Vec<ParsedRegion>,
|
||||
parsed_content: Option<ParsedMarkdown>,
|
||||
}
|
||||
|
||||
impl InfoPopover {
|
||||
@ -503,58 +482,14 @@ impl InfoPopover {
|
||||
style: &EditorStyle,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> AnyElement<Editor> {
|
||||
if let Some(parsed) = &self.parsed_content {
|
||||
if parsed.theme_id != style.theme_id {
|
||||
self.parsed_content = None;
|
||||
}
|
||||
}
|
||||
|
||||
let rendered = if let Some(parsed) = &self.parsed_content {
|
||||
let view_id = cx.view_id();
|
||||
let regions = parsed.regions.clone();
|
||||
let code_span_background_color = style.document_highlight_read_background;
|
||||
|
||||
let mut region_id = 0;
|
||||
|
||||
Text::new(parsed.text.clone(), style.text.clone())
|
||||
.with_highlights(parsed.highlights.clone())
|
||||
.with_custom_runs(parsed.region_ranges.clone(), 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::<Self>(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)
|
||||
.into_any()
|
||||
crate::render_parsed_markdown(parsed, style, cx).into_any()
|
||||
} else {
|
||||
let theme_id = style.theme_id;
|
||||
let language_registry = self.project.read(cx).languages().clone();
|
||||
let blocks = self.blocks.clone();
|
||||
let language = self.language.clone();
|
||||
let style = style.theme.clone();
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
let blocks =
|
||||
render_blocks(theme_id, &blocks, &language_registry, language, &style).await;
|
||||
let blocks = parse_blocks(&blocks, &language_registry, language).await;
|
||||
_ = this.update(&mut cx, |_, cx| cx.notify());
|
||||
blocks
|
||||
})
|
||||
@ -563,23 +498,6 @@ impl InfoPopover {
|
||||
Empty::new().into_any()
|
||||
};
|
||||
|
||||
// let rendered_content = self.parsed_content.get_or_insert_with(|| {
|
||||
// let language_registry = self.project.read(cx).languages().clone();
|
||||
// cx.spawn(|this, mut cx| async move {
|
||||
// let blocks = render_blocks(
|
||||
// style.theme_id,
|
||||
// &self.blocks,
|
||||
// &language_registry,
|
||||
// self.language.clone(),
|
||||
// style,
|
||||
// )
|
||||
// .await;
|
||||
// this.update(&mut cx, |_, cx| cx.notify());
|
||||
// blocks
|
||||
// })
|
||||
// .shared()
|
||||
// });
|
||||
|
||||
MouseEventHandler::new::<InfoPopover, _>(0, cx, |_, cx| {
|
||||
Flex::column()
|
||||
.scrollable::<HoverBlock>(1, None, cx)
|
||||
@ -678,9 +596,12 @@ mod tests {
|
||||
test::editor_lsp_test_context::EditorLspTestContext,
|
||||
};
|
||||
use collections::BTreeSet;
|
||||
use gpui::fonts::{Underline, Weight};
|
||||
use gpui::fonts::Weight;
|
||||
use indoc::indoc;
|
||||
use language::{language_settings::InlayHintSettings, Diagnostic, DiagnosticSet};
|
||||
use language::{
|
||||
language_settings::InlayHintSettings, markdown::MarkdownHighlightStyle, Diagnostic,
|
||||
DiagnosticSet,
|
||||
};
|
||||
use lsp::LanguageServerId;
|
||||
use project::{HoverBlock, HoverBlockKind};
|
||||
use smol::stream::StreamExt;
|
||||
@ -893,7 +814,7 @@ mod tests {
|
||||
.await;
|
||||
|
||||
cx.condition(|editor, _| editor.hover_state.visible()).await;
|
||||
cx.editor(|editor, cx| {
|
||||
cx.editor(|editor, _| {
|
||||
let blocks = editor.hover_state.info_popover.clone().unwrap().blocks;
|
||||
assert_eq!(
|
||||
blocks,
|
||||
@ -903,9 +824,7 @@ mod tests {
|
||||
}],
|
||||
);
|
||||
|
||||
let style = editor.style(cx);
|
||||
let rendered =
|
||||
smol::block_on(render_blocks(0, &blocks, &Default::default(), None, &style));
|
||||
let rendered = smol::block_on(parse_blocks(&blocks, &Default::default(), None));
|
||||
assert_eq!(
|
||||
rendered.text,
|
||||
code_str.trim(),
|
||||
@ -984,16 +903,17 @@ mod tests {
|
||||
|
||||
#[gpui::test]
|
||||
fn test_render_blocks(cx: &mut gpui::TestAppContext) {
|
||||
use markdown::MarkdownHighlight;
|
||||
|
||||
init_test(cx, |_| {});
|
||||
|
||||
cx.add_window(|cx| {
|
||||
let editor = Editor::single_line(None, cx);
|
||||
let style = editor.style(cx);
|
||||
|
||||
struct Row {
|
||||
blocks: Vec<HoverBlock>,
|
||||
expected_marked_text: String,
|
||||
expected_styles: Vec<HighlightStyle>,
|
||||
expected_styles: Vec<MarkdownHighlight>,
|
||||
}
|
||||
|
||||
let rows = &[
|
||||
@ -1004,10 +924,10 @@ mod tests {
|
||||
kind: HoverBlockKind::Markdown,
|
||||
}],
|
||||
expected_marked_text: "one «two» three".to_string(),
|
||||
expected_styles: vec![HighlightStyle {
|
||||
weight: Some(Weight::BOLD),
|
||||
expected_styles: vec![MarkdownHighlight::Style(MarkdownHighlightStyle {
|
||||
weight: Weight::BOLD,
|
||||
..Default::default()
|
||||
}],
|
||||
})],
|
||||
},
|
||||
// Links
|
||||
Row {
|
||||
@ -1016,13 +936,10 @@ mod tests {
|
||||
kind: HoverBlockKind::Markdown,
|
||||
}],
|
||||
expected_marked_text: "one «two» three".to_string(),
|
||||
expected_styles: vec![HighlightStyle {
|
||||
underline: Some(Underline {
|
||||
thickness: 1.0.into(),
|
||||
..Default::default()
|
||||
}),
|
||||
expected_styles: vec![MarkdownHighlight::Style(MarkdownHighlightStyle {
|
||||
underline: true,
|
||||
..Default::default()
|
||||
}],
|
||||
})],
|
||||
},
|
||||
// Lists
|
||||
Row {
|
||||
@ -1047,13 +964,10 @@ mod tests {
|
||||
- «c»
|
||||
- d"
|
||||
.unindent(),
|
||||
expected_styles: vec![HighlightStyle {
|
||||
underline: Some(Underline {
|
||||
thickness: 1.0.into(),
|
||||
..Default::default()
|
||||
}),
|
||||
expected_styles: vec![MarkdownHighlight::Style(MarkdownHighlightStyle {
|
||||
underline: true,
|
||||
..Default::default()
|
||||
}],
|
||||
})],
|
||||
},
|
||||
// Multi-paragraph list items
|
||||
Row {
|
||||
@ -1081,13 +995,10 @@ mod tests {
|
||||
- ten
|
||||
- six"
|
||||
.unindent(),
|
||||
expected_styles: vec![HighlightStyle {
|
||||
underline: Some(Underline {
|
||||
thickness: 1.0.into(),
|
||||
..Default::default()
|
||||
}),
|
||||
expected_styles: vec![MarkdownHighlight::Style(MarkdownHighlightStyle {
|
||||
underline: true,
|
||||
..Default::default()
|
||||
}],
|
||||
})],
|
||||
},
|
||||
];
|
||||
|
||||
@ -1097,8 +1008,7 @@ mod tests {
|
||||
expected_styles,
|
||||
} in &rows[0..]
|
||||
{
|
||||
let rendered =
|
||||
smol::block_on(render_blocks(0, &blocks, &Default::default(), None, &style));
|
||||
let rendered = smol::block_on(parse_blocks(&blocks, &Default::default(), None));
|
||||
|
||||
let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
|
||||
let expected_highlights = ranges
|
||||
|
@ -149,7 +149,6 @@ pub async fn prepare_completion_documentation(
|
||||
documentation: &lsp::Documentation,
|
||||
language_registry: &Arc<LanguageRegistry>,
|
||||
language: Option<Arc<Language>>,
|
||||
style: &theme::Editor,
|
||||
) -> Option<Documentation> {
|
||||
match documentation {
|
||||
lsp::Documentation::String(text) => {
|
||||
@ -170,7 +169,7 @@ pub async fn prepare_completion_documentation(
|
||||
}
|
||||
|
||||
lsp::MarkupKind::Markdown => {
|
||||
let parsed = parse_markdown(value, language_registry, language, style).await;
|
||||
let parsed = parse_markdown(value, language_registry, language).await;
|
||||
Some(Documentation::MultiLineMarkdown(parsed))
|
||||
}
|
||||
},
|
||||
|
@ -1,18 +1,31 @@
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{Language, LanguageRegistry};
|
||||
use gpui::fonts::{HighlightStyle, Underline, Weight};
|
||||
use crate::{HighlightId, Language, LanguageRegistry};
|
||||
use gpui::fonts::Weight;
|
||||
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParsedMarkdown {
|
||||
pub text: String,
|
||||
pub highlights: Vec<(Range<usize>, HighlightStyle)>,
|
||||
pub highlights: Vec<(Range<usize>, MarkdownHighlight)>,
|
||||
pub region_ranges: Vec<Range<usize>>,
|
||||
pub regions: Vec<ParsedRegion>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum MarkdownHighlight {
|
||||
Style(MarkdownHighlightStyle),
|
||||
Code(HighlightId),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct MarkdownHighlightStyle {
|
||||
pub italic: bool,
|
||||
pub underline: bool,
|
||||
pub weight: Weight,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParsedRegion {
|
||||
pub code: bool,
|
||||
@ -23,7 +36,6 @@ pub async fn parse_markdown(
|
||||
markdown: &str,
|
||||
language_registry: &Arc<LanguageRegistry>,
|
||||
language: Option<Arc<Language>>,
|
||||
style: &theme::Editor,
|
||||
) -> ParsedMarkdown {
|
||||
let mut text = String::new();
|
||||
let mut highlights = Vec::new();
|
||||
@ -34,7 +46,6 @@ pub async fn parse_markdown(
|
||||
markdown,
|
||||
language_registry,
|
||||
language,
|
||||
style,
|
||||
&mut text,
|
||||
&mut highlights,
|
||||
&mut region_ranges,
|
||||
@ -54,9 +65,8 @@ pub async fn parse_markdown_block(
|
||||
markdown: &str,
|
||||
language_registry: &Arc<LanguageRegistry>,
|
||||
language: Option<Arc<Language>>,
|
||||
style: &theme::Editor,
|
||||
text: &mut String,
|
||||
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
||||
highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
|
||||
region_ranges: &mut Vec<Range<usize>>,
|
||||
regions: &mut Vec<ParsedRegion>,
|
||||
) {
|
||||
@ -71,16 +81,16 @@ pub async fn parse_markdown_block(
|
||||
match event {
|
||||
Event::Text(t) => {
|
||||
if let Some(language) = ¤t_language {
|
||||
highlight_code(text, highlights, t.as_ref(), language, style);
|
||||
highlight_code(text, highlights, t.as_ref(), language);
|
||||
} else {
|
||||
text.push_str(t.as_ref());
|
||||
|
||||
let mut style = HighlightStyle::default();
|
||||
let mut style = MarkdownHighlightStyle::default();
|
||||
if bold_depth > 0 {
|
||||
style.weight = Some(Weight::BOLD);
|
||||
style.weight = Weight::BOLD;
|
||||
}
|
||||
if italic_depth > 0 {
|
||||
style.italic = Some(true);
|
||||
style.italic = true;
|
||||
}
|
||||
if let Some(link_url) = link_url.clone() {
|
||||
region_ranges.push(prev_len..text.len());
|
||||
@ -88,22 +98,22 @@ pub async fn parse_markdown_block(
|
||||
link_url: Some(link_url),
|
||||
code: false,
|
||||
});
|
||||
style.underline = Some(Underline {
|
||||
thickness: 1.0.into(),
|
||||
..Default::default()
|
||||
});
|
||||
style.underline = true;
|
||||
}
|
||||
|
||||
if style != HighlightStyle::default() {
|
||||
if style != MarkdownHighlightStyle::default() {
|
||||
let mut new_highlight = true;
|
||||
if let Some((last_range, last_style)) = highlights.last_mut() {
|
||||
if let Some((last_range, MarkdownHighlight::Style(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));
|
||||
let range = prev_len..text.len();
|
||||
highlights.push((range, MarkdownHighlight::Style(style)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -115,13 +125,10 @@ pub async fn parse_markdown_block(
|
||||
if link_url.is_some() {
|
||||
highlights.push((
|
||||
prev_len..text.len(),
|
||||
HighlightStyle {
|
||||
underline: Some(Underline {
|
||||
thickness: 1.0.into(),
|
||||
..Default::default()
|
||||
}),
|
||||
MarkdownHighlight::Style(MarkdownHighlightStyle {
|
||||
underline: true,
|
||||
..Default::default()
|
||||
},
|
||||
}),
|
||||
));
|
||||
}
|
||||
regions.push(ParsedRegion {
|
||||
@ -204,17 +211,15 @@ pub async fn parse_markdown_block(
|
||||
|
||||
pub fn highlight_code(
|
||||
text: &mut String,
|
||||
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
||||
highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
|
||||
content: &str,
|
||||
language: &Arc<Language>,
|
||||
style: &theme::Editor,
|
||||
) {
|
||||
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));
|
||||
}
|
||||
let highlight = MarkdownHighlight::Code(highlight_id);
|
||||
highlights.push((prev_len + range.start..prev_len + range.end, highlight));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user