Merge pull request #2417 from zed-industries/hover-markdown
Render markdown more correctly in the editor hover popover
This commit is contained in:
commit
7258db7a4e
14 changed files with 827 additions and 307 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1994,6 +1994,7 @@ dependencies = [
|
||||||
"parking_lot 0.11.2",
|
"parking_lot 0.11.2",
|
||||||
"postage",
|
"postage",
|
||||||
"project",
|
"project",
|
||||||
|
"pulldown-cmark",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"rpc",
|
"rpc",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -4727,7 +4728,6 @@ dependencies = [
|
||||||
"parking_lot 0.11.2",
|
"parking_lot 0.11.2",
|
||||||
"postage",
|
"postage",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
"pulldown-cmark",
|
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"regex",
|
"regex",
|
||||||
"rpc",
|
"rpc",
|
||||||
|
|
|
@ -23,7 +23,7 @@ use language::{
|
||||||
};
|
};
|
||||||
use live_kit_client::MacOSDisplay;
|
use live_kit_client::MacOSDisplay;
|
||||||
use lsp::LanguageServerId;
|
use lsp::LanguageServerId;
|
||||||
use project::{search::SearchQuery, DiagnosticSummary, Project, ProjectPath};
|
use project::{search::SearchQuery, DiagnosticSummary, HoverBlockKind, Project, ProjectPath};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use settings::{Formatter, Settings};
|
use settings::{Formatter, Settings};
|
||||||
|
@ -4693,11 +4693,13 @@ async fn test_lsp_hover(
|
||||||
vec![
|
vec![
|
||||||
project::HoverBlock {
|
project::HoverBlock {
|
||||||
text: "Test hover content.".to_string(),
|
text: "Test hover content.".to_string(),
|
||||||
language: None,
|
kind: HoverBlockKind::Markdown,
|
||||||
},
|
},
|
||||||
project::HoverBlock {
|
project::HoverBlock {
|
||||||
text: "let foo = 42;".to_string(),
|
text: "let foo = 42;".to_string(),
|
||||||
language: Some("Rust".to_string()),
|
kind: HoverBlockKind::Code {
|
||||||
|
language: "Rust".to_string()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
|
@ -55,6 +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.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
|
||||||
|
|
|
@ -463,6 +463,7 @@ pub struct EditorStyle {
|
||||||
pub text: TextStyle,
|
pub text: TextStyle,
|
||||||
pub placeholder_text: Option<TextStyle>,
|
pub placeholder_text: Option<TextStyle>,
|
||||||
pub theme: theme::Editor,
|
pub theme: theme::Editor,
|
||||||
|
pub theme_id: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompletionId = usize;
|
type CompletionId = usize;
|
||||||
|
@ -7310,6 +7311,7 @@ fn build_style(
|
||||||
) -> EditorStyle {
|
) -> EditorStyle {
|
||||||
let font_cache = cx.font_cache();
|
let font_cache = cx.font_cache();
|
||||||
|
|
||||||
|
let theme_id = settings.theme.meta.id;
|
||||||
let mut theme = settings.theme.editor.clone();
|
let mut theme = settings.theme.editor.clone();
|
||||||
let mut style = if let Some(get_field_editor_theme) = get_field_editor_theme {
|
let mut style = if let Some(get_field_editor_theme) = get_field_editor_theme {
|
||||||
let field_editor_theme = get_field_editor_theme(&settings.theme);
|
let field_editor_theme = get_field_editor_theme(&settings.theme);
|
||||||
|
@ -7323,6 +7325,7 @@ fn build_style(
|
||||||
text: field_editor_theme.text,
|
text: field_editor_theme.text,
|
||||||
placeholder_text: field_editor_theme.placeholder_text,
|
placeholder_text: field_editor_theme.placeholder_text,
|
||||||
theme,
|
theme,
|
||||||
|
theme_id,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let font_family_id = settings.buffer_font_family;
|
let font_family_id = settings.buffer_font_family;
|
||||||
|
@ -7344,6 +7347,7 @@ fn build_style(
|
||||||
},
|
},
|
||||||
placeholder_text: None,
|
placeholder_text: None,
|
||||||
theme,
|
theme,
|
||||||
|
theme_id,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
use futures::FutureExt;
|
use futures::FutureExt;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions,
|
actions,
|
||||||
elements::{Flex, MouseEventHandler, Padding, Text},
|
elements::{Flex, MouseEventHandler, Padding, ParentElement, Text},
|
||||||
|
fonts::{HighlightStyle, Underline, Weight},
|
||||||
impl_internal_actions,
|
impl_internal_actions,
|
||||||
platform::{CursorStyle, MouseButton},
|
platform::{CursorStyle, MouseButton},
|
||||||
AnyElement, AppContext, Axis, Element, ModelHandle, Task, ViewContext,
|
AnyElement, AppContext, CursorRegion, Element, ModelHandle, MouseRegion, Task, ViewContext,
|
||||||
};
|
};
|
||||||
use language::{Bias, DiagnosticEntry, DiagnosticSeverity};
|
use language::{Bias, DiagnosticEntry, DiagnosticSeverity, Language, LanguageRegistry};
|
||||||
use project::{HoverBlock, Project};
|
use project::{HoverBlock, HoverBlockKind, Project};
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use std::{ops::Range, time::Duration};
|
use std::{ops::Range, sync::Arc, time::Duration};
|
||||||
use util::TryFutureExt;
|
use util::TryFutureExt;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -233,7 +234,8 @@ fn show_hover(
|
||||||
Some(InfoPopover {
|
Some(InfoPopover {
|
||||||
project: project.clone(),
|
project: project.clone(),
|
||||||
symbol_range: range,
|
symbol_range: range,
|
||||||
contents: hover_result.contents,
|
blocks: hover_result.contents,
|
||||||
|
rendered_content: None,
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -261,6 +263,225 @@ fn show_hover(
|
||||||
editor.hover_state.info_task = Some(task);
|
editor.hover_state.info_task = Some(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_blocks(
|
||||||
|
theme_id: usize,
|
||||||
|
blocks: &[HoverBlock],
|
||||||
|
language_registry: &Arc<LanguageRegistry>,
|
||||||
|
style: &EditorStyle,
|
||||||
|
) -> RenderedInfo {
|
||||||
|
let mut text = String::new();
|
||||||
|
let mut highlights = Vec::new();
|
||||||
|
let mut region_ranges = Vec::new();
|
||||||
|
let mut regions = Vec::new();
|
||||||
|
|
||||||
|
for block in blocks {
|
||||||
|
match &block.kind {
|
||||||
|
HoverBlockKind::PlainText => {
|
||||||
|
new_paragraph(&mut text, &mut Vec::new());
|
||||||
|
text.push_str(&block.text);
|
||||||
|
}
|
||||||
|
HoverBlockKind::Markdown => {
|
||||||
|
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
|
||||||
|
|
||||||
|
let mut bold_depth = 0;
|
||||||
|
let mut italic_depth = 0;
|
||||||
|
let mut link_url = None;
|
||||||
|
let mut current_language = None;
|
||||||
|
let mut list_stack = Vec::new();
|
||||||
|
|
||||||
|
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);
|
||||||
|
if let CodeBlockKind::Fenced(language) = kind {
|
||||||
|
current_language = language_registry
|
||||||
|
.language_for_name(language.as_ref())
|
||||||
|
.now_or_never()
|
||||||
|
.and_then(Result::ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 } => {
|
||||||
|
if let Some(language) = language_registry
|
||||||
|
.language_for_name(language)
|
||||||
|
.now_or_never()
|
||||||
|
.and_then(Result::ok)
|
||||||
|
{
|
||||||
|
render_code(&mut text, &mut highlights, &block.text, &language, style);
|
||||||
|
} else {
|
||||||
|
text.push_str(&block.text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !text.is_empty() && !text.ends_with('\n') {
|
||||||
|
text.push('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderedInfo {
|
||||||
|
theme_id,
|
||||||
|
text,
|
||||||
|
highlights,
|
||||||
|
region_ranges,
|
||||||
|
regions,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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>,
|
||||||
|
@ -275,7 +496,7 @@ impl HoverState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(
|
pub fn render(
|
||||||
&self,
|
&mut self,
|
||||||
snapshot: &EditorSnapshot,
|
snapshot: &EditorSnapshot,
|
||||||
style: &EditorStyle,
|
style: &EditorStyle,
|
||||||
visible_rows: Range<u32>,
|
visible_rows: Range<u32>,
|
||||||
|
@ -304,7 +525,7 @@ impl HoverState {
|
||||||
if let Some(diagnostic_popover) = self.diagnostic_popover.as_ref() {
|
if let Some(diagnostic_popover) = self.diagnostic_popover.as_ref() {
|
||||||
elements.push(diagnostic_popover.render(style, cx));
|
elements.push(diagnostic_popover.render(style, cx));
|
||||||
}
|
}
|
||||||
if let Some(info_popover) = self.info_popover.as_ref() {
|
if let Some(info_popover) = self.info_popover.as_mut() {
|
||||||
elements.push(info_popover.render(style, cx));
|
elements.push(info_popover.render(style, cx));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,44 +537,92 @@ impl HoverState {
|
||||||
pub struct InfoPopover {
|
pub struct InfoPopover {
|
||||||
pub project: ModelHandle<Project>,
|
pub project: ModelHandle<Project>,
|
||||||
pub symbol_range: Range<Anchor>,
|
pub symbol_range: Range<Anchor>,
|
||||||
pub contents: Vec<HoverBlock>,
|
pub blocks: Vec<HoverBlock>,
|
||||||
|
rendered_content: Option<RenderedInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct RenderedInfo {
|
||||||
|
theme_id: usize,
|
||||||
|
text: String,
|
||||||
|
highlights: Vec<(Range<usize>, HighlightStyle)>,
|
||||||
|
region_ranges: Vec<Range<usize>>,
|
||||||
|
regions: Vec<RenderedRegion>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct RenderedRegion {
|
||||||
|
code: bool,
|
||||||
|
link_url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InfoPopover {
|
impl InfoPopover {
|
||||||
pub fn render(&self, style: &EditorStyle, cx: &mut ViewContext<Editor>) -> AnyElement<Editor> {
|
pub fn render(
|
||||||
|
&mut self,
|
||||||
|
style: &EditorStyle,
|
||||||
|
cx: &mut ViewContext<Editor>,
|
||||||
|
) -> AnyElement<Editor> {
|
||||||
|
if let Some(rendered) = &self.rendered_content {
|
||||||
|
if rendered.theme_id != style.theme_id {
|
||||||
|
self.rendered_content = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let rendered_content = self.rendered_content.get_or_insert_with(|| {
|
||||||
|
render_blocks(
|
||||||
|
style.theme_id,
|
||||||
|
&self.blocks,
|
||||||
|
self.project.read(cx).languages(),
|
||||||
|
style,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
MouseEventHandler::<InfoPopover, _>::new(0, cx, |_, cx| {
|
MouseEventHandler::<InfoPopover, _>::new(0, cx, |_, cx| {
|
||||||
let mut flex = Flex::new(Axis::Vertical).scrollable::<HoverBlock>(1, None, cx);
|
let mut region_id = 0;
|
||||||
flex.extend(self.contents.iter().map(|content| {
|
let view_id = cx.view_id();
|
||||||
let languages = self.project.read(cx).languages();
|
|
||||||
if let Some(language) = content.language.clone().and_then(|language| {
|
|
||||||
languages.language_for_name(&language).now_or_never()?.ok()
|
|
||||||
}) {
|
|
||||||
let runs = language
|
|
||||||
.highlight_text(&content.text.as_str().into(), 0..content.text.len());
|
|
||||||
|
|
||||||
Text::new(content.text.clone(), style.text.clone())
|
let code_span_background_color = style.document_highlight_read_background;
|
||||||
.with_soft_wrap(true)
|
let regions = rendered_content.regions.clone();
|
||||||
.with_highlights(
|
Flex::column()
|
||||||
runs.iter()
|
.scrollable::<HoverBlock>(1, None, cx)
|
||||||
.filter_map(|(range, id)| {
|
.with_child(
|
||||||
id.style(style.theme.syntax.as_ref())
|
Text::new(rendered_content.text.clone(), style.text.clone())
|
||||||
.map(|style| (range.clone(), style))
|
.with_highlights(rendered_content.highlights.clone())
|
||||||
})
|
.with_custom_runs(
|
||||||
.collect(),
|
rendered_content.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| {
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.into_any()
|
.with_soft_wrap(true),
|
||||||
} else {
|
)
|
||||||
let mut text_style = style.hover_popover.prose.clone();
|
.contained()
|
||||||
text_style.font_size = style.text.font_size;
|
.with_style(style.hover_popover.container)
|
||||||
|
|
||||||
Text::new(content.text.clone(), text_style)
|
|
||||||
.with_soft_wrap(true)
|
|
||||||
.contained()
|
|
||||||
.with_style(style.hover_popover.block_style)
|
|
||||||
.into_any()
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
flex.contained().with_style(style.hover_popover.container)
|
|
||||||
})
|
})
|
||||||
.on_move(|_, _, _| {}) // Consume move events so they don't reach regions underneath.
|
.on_move(|_, _, _| {}) // Consume move events so they don't reach regions underneath.
|
||||||
.with_cursor_style(CursorStyle::Arrow)
|
.with_cursor_style(CursorStyle::Arrow)
|
||||||
|
@ -437,16 +706,16 @@ impl DiagnosticPopover {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::test::editor_lsp_test_context::EditorLspTestContext;
|
||||||
|
use gpui::fonts::Weight;
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
|
||||||
use language::{Diagnostic, DiagnosticSet};
|
use language::{Diagnostic, DiagnosticSet};
|
||||||
use lsp::LanguageServerId;
|
use lsp::LanguageServerId;
|
||||||
use project::HoverBlock;
|
use project::{HoverBlock, HoverBlockKind};
|
||||||
use smol::stream::StreamExt;
|
use smol::stream::StreamExt;
|
||||||
|
use unindent::Unindent;
|
||||||
use crate::test::editor_lsp_test_context::EditorLspTestContext;
|
use util::test::marked_text_ranges;
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
async fn test_mouse_hover_info_popover(cx: &mut gpui::TestAppContext) {
|
async fn test_mouse_hover_info_popover(cx: &mut gpui::TestAppContext) {
|
||||||
|
@ -487,10 +756,7 @@ mod tests {
|
||||||
Ok(Some(lsp::Hover {
|
Ok(Some(lsp::Hover {
|
||||||
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
||||||
kind: lsp::MarkupKind::Markdown,
|
kind: lsp::MarkupKind::Markdown,
|
||||||
value: indoc! {"
|
value: "some basic docs".to_string(),
|
||||||
# Some basic docs
|
|
||||||
Some test documentation"}
|
|
||||||
.to_string(),
|
|
||||||
}),
|
}),
|
||||||
range: Some(symbol_range),
|
range: Some(symbol_range),
|
||||||
}))
|
}))
|
||||||
|
@ -502,17 +768,11 @@ mod tests {
|
||||||
cx.editor(|editor, _| {
|
cx.editor(|editor, _| {
|
||||||
assert!(editor.hover_state.visible());
|
assert!(editor.hover_state.visible());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
editor.hover_state.info_popover.clone().unwrap().contents,
|
editor.hover_state.info_popover.clone().unwrap().blocks,
|
||||||
vec![
|
vec![HoverBlock {
|
||||||
HoverBlock {
|
text: "some basic docs".to_string(),
|
||||||
text: "Some basic docs".to_string(),
|
kind: HoverBlockKind::Markdown,
|
||||||
language: None
|
},]
|
||||||
},
|
|
||||||
HoverBlock {
|
|
||||||
text: "Some test documentation".to_string(),
|
|
||||||
language: None
|
|
||||||
}
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -563,10 +823,7 @@ mod tests {
|
||||||
Ok(Some(lsp::Hover {
|
Ok(Some(lsp::Hover {
|
||||||
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
||||||
kind: lsp::MarkupKind::Markdown,
|
kind: lsp::MarkupKind::Markdown,
|
||||||
value: indoc! {"
|
value: "some other basic docs".to_string(),
|
||||||
# Some other basic docs
|
|
||||||
Some other test documentation"}
|
|
||||||
.to_string(),
|
|
||||||
}),
|
}),
|
||||||
range: Some(symbol_range),
|
range: Some(symbol_range),
|
||||||
}))
|
}))
|
||||||
|
@ -577,17 +834,11 @@ mod tests {
|
||||||
cx.condition(|editor, _| editor.hover_state.visible()).await;
|
cx.condition(|editor, _| editor.hover_state.visible()).await;
|
||||||
cx.editor(|editor, _| {
|
cx.editor(|editor, _| {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
editor.hover_state.info_popover.clone().unwrap().contents,
|
editor.hover_state.info_popover.clone().unwrap().blocks,
|
||||||
vec![
|
vec![HoverBlock {
|
||||||
HoverBlock {
|
text: "some other basic docs".to_string(),
|
||||||
text: "Some other basic docs".to_string(),
|
kind: HoverBlockKind::Markdown,
|
||||||
language: None
|
}]
|
||||||
},
|
|
||||||
HoverBlock {
|
|
||||||
text: "Some other test documentation".to_string(),
|
|
||||||
language: None
|
|
||||||
}
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -644,10 +895,7 @@ mod tests {
|
||||||
Ok(Some(lsp::Hover {
|
Ok(Some(lsp::Hover {
|
||||||
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
||||||
kind: lsp::MarkupKind::Markdown,
|
kind: lsp::MarkupKind::Markdown,
|
||||||
value: indoc! {"
|
value: "some new docs".to_string(),
|
||||||
# Some other basic docs
|
|
||||||
Some other test documentation"}
|
|
||||||
.to_string(),
|
|
||||||
}),
|
}),
|
||||||
range: Some(range),
|
range: Some(range),
|
||||||
}))
|
}))
|
||||||
|
@ -660,4 +908,144 @@ mod tests {
|
||||||
hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
|
hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
fn test_render_blocks(cx: &mut gpui::TestAppContext) {
|
||||||
|
Settings::test_async(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>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let rows = &[
|
||||||
|
// Strong emphasis
|
||||||
|
Row {
|
||||||
|
blocks: vec![HoverBlock {
|
||||||
|
text: "one **two** three".to_string(),
|
||||||
|
kind: HoverBlockKind::Markdown,
|
||||||
|
}],
|
||||||
|
expected_marked_text: "one «two» three\n".to_string(),
|
||||||
|
expected_styles: vec![HighlightStyle {
|
||||||
|
weight: Some(Weight::BOLD),
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
// Links
|
||||||
|
Row {
|
||||||
|
blocks: vec three".to_string(),
|
||||||
|
kind: HoverBlockKind::Markdown,
|
||||||
|
}],
|
||||||
|
expected_marked_text: "one «two» three\n".to_string(),
|
||||||
|
expected_styles: vec![HighlightStyle {
|
||||||
|
underline: Some(Underline {
|
||||||
|
thickness: 1.0.into(),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
// Lists
|
||||||
|
Row {
|
||||||
|
blocks: vec
|
||||||
|
- d
|
||||||
|
"
|
||||||
|
.unindent(),
|
||||||
|
kind: HoverBlockKind::Markdown,
|
||||||
|
}],
|
||||||
|
expected_marked_text: "
|
||||||
|
lists:
|
||||||
|
- one
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
- two
|
||||||
|
- «c»
|
||||||
|
- d
|
||||||
|
"
|
||||||
|
.unindent(),
|
||||||
|
expected_styles: vec![HighlightStyle {
|
||||||
|
underline: Some(Underline {
|
||||||
|
thickness: 1.0.into(),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
// Multi-paragraph list items
|
||||||
|
Row {
|
||||||
|
blocks: vec![HoverBlock {
|
||||||
|
text: "
|
||||||
|
* one two
|
||||||
|
three
|
||||||
|
|
||||||
|
* four five
|
||||||
|
* six seven
|
||||||
|
eight
|
||||||
|
|
||||||
|
nine
|
||||||
|
* ten
|
||||||
|
* six
|
||||||
|
"
|
||||||
|
.unindent(),
|
||||||
|
kind: HoverBlockKind::Markdown,
|
||||||
|
}],
|
||||||
|
expected_marked_text: "
|
||||||
|
- one two three
|
||||||
|
- four five
|
||||||
|
- six seven eight
|
||||||
|
|
||||||
|
nine
|
||||||
|
- ten
|
||||||
|
- six
|
||||||
|
"
|
||||||
|
.unindent(),
|
||||||
|
expected_styles: vec![HighlightStyle {
|
||||||
|
underline: Some(Underline {
|
||||||
|
thickness: 1.0.into(),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for Row {
|
||||||
|
blocks,
|
||||||
|
expected_marked_text,
|
||||||
|
expected_styles,
|
||||||
|
} in &rows[0..]
|
||||||
|
{
|
||||||
|
let rendered = render_blocks(0, &blocks, &Default::default(), &style);
|
||||||
|
|
||||||
|
let (expected_text, ranges) = marked_text_ranges(expected_marked_text, false);
|
||||||
|
let expected_highlights = ranges
|
||||||
|
.into_iter()
|
||||||
|
.zip(expected_styles.iter().cloned())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
assert_eq!(
|
||||||
|
rendered.text,
|
||||||
|
dbg!(expected_text),
|
||||||
|
"wrong text for input {blocks:?}"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
rendered.highlights, expected_highlights,
|
||||||
|
"wrong highlights for input {blocks:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
editor
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
use gpui::{
|
use gpui::{
|
||||||
color::Color,
|
color::Color,
|
||||||
fonts::{Properties, Weight},
|
elements::Text,
|
||||||
text_layout::RunStyle,
|
fonts::{HighlightStyle, TextStyle},
|
||||||
AnyElement, Element, Quad, SceneBuilder, View, ViewContext,
|
platform::{CursorStyle, MouseButton},
|
||||||
|
AnyElement, CursorRegion, Element, MouseRegion,
|
||||||
};
|
};
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use pathfinder_geometry::rect::RectF;
|
|
||||||
use simplelog::SimpleLogger;
|
use simplelog::SimpleLogger;
|
||||||
use std::ops::Range;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
|
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
|
||||||
|
@ -19,7 +18,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TextView;
|
struct TextView;
|
||||||
struct TextElement;
|
|
||||||
|
|
||||||
impl gpui::Entity for TextView {
|
impl gpui::Entity for TextView {
|
||||||
type Event = ();
|
type Event = ();
|
||||||
|
@ -30,104 +28,53 @@ impl gpui::View for TextView {
|
||||||
"View"
|
"View"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(&mut self, _: &mut gpui::ViewContext<Self>) -> AnyElement<TextView> {
|
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> AnyElement<TextView> {
|
||||||
TextElement.into_any()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<V: View> Element<V> for TextElement {
|
|
||||||
type LayoutState = ();
|
|
||||||
|
|
||||||
type PaintState = ();
|
|
||||||
|
|
||||||
fn layout(
|
|
||||||
&mut self,
|
|
||||||
constraint: gpui::SizeConstraint,
|
|
||||||
_: &mut V,
|
|
||||||
_: &mut ViewContext<V>,
|
|
||||||
) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) {
|
|
||||||
(constraint.max, ())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn paint(
|
|
||||||
&mut self,
|
|
||||||
scene: &mut SceneBuilder,
|
|
||||||
bounds: RectF,
|
|
||||||
visible_bounds: RectF,
|
|
||||||
_: &mut Self::LayoutState,
|
|
||||||
_: &mut V,
|
|
||||||
cx: &mut ViewContext<V>,
|
|
||||||
) -> Self::PaintState {
|
|
||||||
let font_size = 12.;
|
let font_size = 12.;
|
||||||
let family = cx
|
let family = cx
|
||||||
.font_cache
|
.font_cache
|
||||||
.load_family(&["SF Pro Display"], &Default::default())
|
.load_family(&["Monaco"], &Default::default())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let normal = RunStyle {
|
let font_id = cx
|
||||||
font_id: cx
|
.font_cache
|
||||||
.font_cache
|
.select_font(family, &Default::default())
|
||||||
.select_font(family, &Default::default())
|
.unwrap();
|
||||||
.unwrap(),
|
let view_id = cx.view_id();
|
||||||
color: Color::default(),
|
|
||||||
underline: Default::default(),
|
|
||||||
};
|
|
||||||
let bold = RunStyle {
|
|
||||||
font_id: cx
|
|
||||||
.font_cache
|
|
||||||
.select_font(
|
|
||||||
family,
|
|
||||||
&Properties {
|
|
||||||
weight: Weight::BOLD,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
color: Color::default(),
|
|
||||||
underline: Default::default(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let text = "Hello world!";
|
let underline = HighlightStyle {
|
||||||
let line = cx.text_layout_cache().layout_str(
|
underline: Some(gpui::fonts::Underline {
|
||||||
text,
|
thickness: 1.0.into(),
|
||||||
font_size,
|
..Default::default()
|
||||||
&[
|
}),
|
||||||
(1, normal),
|
|
||||||
(1, bold),
|
|
||||||
(1, normal),
|
|
||||||
(1, bold),
|
|
||||||
(text.len() - 4, normal),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
scene.push_quad(Quad {
|
|
||||||
bounds,
|
|
||||||
background: Some(Color::white()),
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
};
|
||||||
line.paint(scene, bounds.origin(), visible_bounds, bounds.height(), cx);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rect_for_text_range(
|
Text::new(
|
||||||
&self,
|
"The text:\nHello, beautiful world, hello!",
|
||||||
_: Range<usize>,
|
TextStyle {
|
||||||
_: RectF,
|
font_id,
|
||||||
_: RectF,
|
font_size,
|
||||||
_: &Self::LayoutState,
|
color: Color::red(),
|
||||||
_: &Self::PaintState,
|
font_family_name: "".into(),
|
||||||
_: &V,
|
font_family_id: family,
|
||||||
_: &ViewContext<V>,
|
underline: Default::default(),
|
||||||
) -> Option<RectF> {
|
font_properties: Default::default(),
|
||||||
None
|
},
|
||||||
}
|
)
|
||||||
|
.with_highlights(vec![(17..26, underline), (34..40, underline)])
|
||||||
fn debug(
|
.with_custom_runs(vec![(17..26), (34..40)], move |ix, bounds, scene, _| {
|
||||||
&self,
|
scene.push_cursor_region(CursorRegion {
|
||||||
_: RectF,
|
bounds,
|
||||||
_: &Self::LayoutState,
|
style: CursorStyle::PointingHand,
|
||||||
_: &Self::PaintState,
|
});
|
||||||
_: &V,
|
scene.push_mouse_region(
|
||||||
_: &ViewContext<V>,
|
MouseRegion::new::<Self>(view_id, ix, bounds).on_click::<Self, _>(
|
||||||
) -> gpui::json::Value {
|
MouseButton::Left,
|
||||||
todo!()
|
move |_, _, _| {
|
||||||
|
eprintln!("clicked link {ix}");
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.into_any()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,8 @@ use crate::{
|
||||||
},
|
},
|
||||||
json::{ToJson, Value},
|
json::{ToJson, Value},
|
||||||
text_layout::{Line, RunStyle, ShapedBoundary},
|
text_layout::{Line, RunStyle, ShapedBoundary},
|
||||||
Element, FontCache, SceneBuilder, SizeConstraint, TextLayoutCache, View, ViewContext,
|
AppContext, Element, FontCache, SceneBuilder, SizeConstraint, TextLayoutCache, View,
|
||||||
|
ViewContext,
|
||||||
};
|
};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
@ -17,7 +18,11 @@ pub struct Text {
|
||||||
text: Cow<'static, str>,
|
text: Cow<'static, str>,
|
||||||
style: TextStyle,
|
style: TextStyle,
|
||||||
soft_wrap: bool,
|
soft_wrap: bool,
|
||||||
highlights: Vec<(Range<usize>, HighlightStyle)>,
|
highlights: Option<Box<[(Range<usize>, HighlightStyle)]>>,
|
||||||
|
custom_runs: Option<(
|
||||||
|
Box<[Range<usize>]>,
|
||||||
|
Box<dyn FnMut(usize, RectF, &mut SceneBuilder, &mut AppContext)>,
|
||||||
|
)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LayoutState {
|
pub struct LayoutState {
|
||||||
|
@ -32,7 +37,8 @@ impl Text {
|
||||||
text: text.into(),
|
text: text.into(),
|
||||||
style,
|
style,
|
||||||
soft_wrap: true,
|
soft_wrap: true,
|
||||||
highlights: Vec::new(),
|
highlights: None,
|
||||||
|
custom_runs: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +47,20 @@ impl Text {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_highlights(mut self, runs: Vec<(Range<usize>, HighlightStyle)>) -> Self {
|
pub fn with_highlights(
|
||||||
self.highlights = runs;
|
mut self,
|
||||||
|
runs: impl Into<Box<[(Range<usize>, HighlightStyle)]>>,
|
||||||
|
) -> Self {
|
||||||
|
self.highlights = Some(runs.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_custom_runs(
|
||||||
|
mut self,
|
||||||
|
runs: impl Into<Box<[Range<usize>]>>,
|
||||||
|
callback: impl 'static + FnMut(usize, RectF, &mut SceneBuilder, &mut AppContext),
|
||||||
|
) -> Self {
|
||||||
|
self.custom_runs = Some((runs.into(), Box::new(callback)));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +83,12 @@ impl<V: View> Element<V> for Text {
|
||||||
// Convert the string and highlight ranges into an iterator of highlighted chunks.
|
// Convert the string and highlight ranges into an iterator of highlighted chunks.
|
||||||
|
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
let mut highlight_ranges = self.highlights.iter().peekable();
|
let mut highlight_ranges = self
|
||||||
|
.highlights
|
||||||
|
.as_ref()
|
||||||
|
.map_or(Default::default(), AsRef::as_ref)
|
||||||
|
.iter()
|
||||||
|
.peekable();
|
||||||
let chunks = std::iter::from_fn(|| {
|
let chunks = std::iter::from_fn(|| {
|
||||||
let result;
|
let result;
|
||||||
if let Some((range, highlight_style)) = highlight_ranges.peek() {
|
if let Some((range, highlight_style)) = highlight_ranges.peek() {
|
||||||
|
@ -152,6 +175,20 @@ impl<V: View> Element<V> for Text {
|
||||||
) -> Self::PaintState {
|
) -> Self::PaintState {
|
||||||
let mut origin = bounds.origin();
|
let mut origin = bounds.origin();
|
||||||
let empty = Vec::new();
|
let empty = Vec::new();
|
||||||
|
let mut callback = |_, _, _: &mut SceneBuilder, _: &mut AppContext| {};
|
||||||
|
|
||||||
|
let mouse_runs;
|
||||||
|
let custom_run_callback;
|
||||||
|
if let Some((runs, build_region)) = &mut self.custom_runs {
|
||||||
|
mouse_runs = runs.iter();
|
||||||
|
custom_run_callback = build_region.as_mut();
|
||||||
|
} else {
|
||||||
|
mouse_runs = [].iter();
|
||||||
|
custom_run_callback = &mut callback;
|
||||||
|
}
|
||||||
|
let mut custom_runs = mouse_runs.enumerate().peekable();
|
||||||
|
|
||||||
|
let mut offset = 0;
|
||||||
for (ix, line) in layout.shaped_lines.iter().enumerate() {
|
for (ix, line) in layout.shaped_lines.iter().enumerate() {
|
||||||
let wrap_boundaries = layout.wrap_boundaries.get(ix).unwrap_or(&empty);
|
let wrap_boundaries = layout.wrap_boundaries.get(ix).unwrap_or(&empty);
|
||||||
let boundaries = RectF::new(
|
let boundaries = RectF::new(
|
||||||
|
@ -169,13 +206,103 @@ impl<V: View> Element<V> for Text {
|
||||||
origin,
|
origin,
|
||||||
visible_bounds,
|
visible_bounds,
|
||||||
layout.line_height,
|
layout.line_height,
|
||||||
wrap_boundaries.iter().copied(),
|
wrap_boundaries,
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
line.paint(scene, origin, visible_bounds, layout.line_height, cx);
|
line.paint(scene, origin, visible_bounds, layout.line_height, cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Paint any custom runs that intersect this line.
|
||||||
|
let end_offset = offset + line.len();
|
||||||
|
if let Some((custom_run_ix, custom_run_range)) = custom_runs.peek().cloned() {
|
||||||
|
if custom_run_range.start < end_offset {
|
||||||
|
let mut current_custom_run = None;
|
||||||
|
if custom_run_range.start <= offset {
|
||||||
|
current_custom_run = Some((custom_run_ix, custom_run_range.end, origin));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut glyph_origin = origin;
|
||||||
|
let mut prev_position = 0.;
|
||||||
|
let mut wrap_boundaries = wrap_boundaries.iter().copied().peekable();
|
||||||
|
for (run_ix, glyph_ix, glyph) in
|
||||||
|
line.runs().iter().enumerate().flat_map(|(run_ix, run)| {
|
||||||
|
run.glyphs()
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(move |(ix, glyph)| (run_ix, ix, glyph))
|
||||||
|
})
|
||||||
|
{
|
||||||
|
glyph_origin.set_x(glyph_origin.x() + glyph.position.x() - prev_position);
|
||||||
|
prev_position = glyph.position.x();
|
||||||
|
|
||||||
|
// If we've reached a soft wrap position, move down one line. If there
|
||||||
|
// is a custom run in-progress, paint it.
|
||||||
|
if wrap_boundaries
|
||||||
|
.peek()
|
||||||
|
.map_or(false, |b| b.run_ix == run_ix && b.glyph_ix == glyph_ix)
|
||||||
|
{
|
||||||
|
if let Some((run_ix, _, run_origin)) = &mut current_custom_run {
|
||||||
|
let bounds = RectF::from_points(
|
||||||
|
*run_origin,
|
||||||
|
glyph_origin + vec2f(0., layout.line_height),
|
||||||
|
);
|
||||||
|
custom_run_callback(*run_ix, bounds, scene, cx);
|
||||||
|
*run_origin =
|
||||||
|
vec2f(origin.x(), glyph_origin.y() + layout.line_height);
|
||||||
|
}
|
||||||
|
wrap_boundaries.next();
|
||||||
|
glyph_origin = vec2f(origin.x(), glyph_origin.y() + layout.line_height);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we've reached the end of the current custom run, paint it.
|
||||||
|
if let Some((run_ix, run_end_offset, run_origin)) = current_custom_run {
|
||||||
|
if offset + glyph.index == run_end_offset {
|
||||||
|
current_custom_run.take();
|
||||||
|
let bounds = RectF::from_points(
|
||||||
|
run_origin,
|
||||||
|
glyph_origin + vec2f(0., layout.line_height),
|
||||||
|
);
|
||||||
|
custom_run_callback(run_ix, bounds, scene, cx);
|
||||||
|
custom_runs.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((_, run_range)) = custom_runs.peek() {
|
||||||
|
if run_range.start >= end_offset {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if run_range.start == offset + glyph.index {
|
||||||
|
current_custom_run =
|
||||||
|
Some((run_ix, run_range.end, glyph_origin));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we've reached the start of a new custom run, start tracking it.
|
||||||
|
if let Some((run_ix, run_range)) = custom_runs.peek() {
|
||||||
|
if offset + glyph.index == run_range.start {
|
||||||
|
current_custom_run = Some((*run_ix, run_range.end, glyph_origin));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a custom run extends beyond the end of the line, paint it.
|
||||||
|
if let Some((run_ix, run_end_offset, run_origin)) = current_custom_run {
|
||||||
|
let line_end = glyph_origin + vec2f(line.width() - prev_position, 0.);
|
||||||
|
let bounds = RectF::from_points(
|
||||||
|
run_origin,
|
||||||
|
line_end + vec2f(0., layout.line_height),
|
||||||
|
);
|
||||||
|
custom_run_callback(run_ix, bounds, scene, cx);
|
||||||
|
if end_offset == run_end_offset {
|
||||||
|
custom_runs.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
offset = end_offset + 1;
|
||||||
origin.set_y(boundaries.max_y());
|
origin.set_y(boundaries.max_y());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,7 +177,14 @@ impl<'a> Hash for CacheKeyRef<'a> {
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct Line {
|
pub struct Line {
|
||||||
layout: Arc<LineLayout>,
|
layout: Arc<LineLayout>,
|
||||||
style_runs: SmallVec<[(u32, Color, Underline); 32]>,
|
style_runs: SmallVec<[StyleRun; 32]>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
struct StyleRun {
|
||||||
|
len: u32,
|
||||||
|
color: Color,
|
||||||
|
underline: Underline,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
|
@ -208,7 +215,11 @@ impl Line {
|
||||||
fn new(layout: Arc<LineLayout>, runs: &[(usize, RunStyle)]) -> Self {
|
fn new(layout: Arc<LineLayout>, runs: &[(usize, RunStyle)]) -> Self {
|
||||||
let mut style_runs = SmallVec::new();
|
let mut style_runs = SmallVec::new();
|
||||||
for (len, style) in runs {
|
for (len, style) in runs {
|
||||||
style_runs.push((*len as u32, style.color, style.underline));
|
style_runs.push(StyleRun {
|
||||||
|
len: *len as u32,
|
||||||
|
color: style.color,
|
||||||
|
underline: style.underline,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Self { layout, style_runs }
|
Self { layout, style_runs }
|
||||||
}
|
}
|
||||||
|
@ -301,28 +312,30 @@ impl Line {
|
||||||
|
|
||||||
let mut finished_underline = None;
|
let mut finished_underline = None;
|
||||||
if glyph.index >= run_end {
|
if glyph.index >= run_end {
|
||||||
if let Some((run_len, run_color, run_underline)) = style_runs.next() {
|
if let Some(style_run) = style_runs.next() {
|
||||||
if let Some((_, underline_style)) = underline {
|
if let Some((_, underline_style)) = underline {
|
||||||
if *run_underline != underline_style {
|
if style_run.underline != underline_style {
|
||||||
finished_underline = underline.take();
|
finished_underline = underline.take();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if run_underline.thickness.into_inner() > 0. {
|
if style_run.underline.thickness.into_inner() > 0. {
|
||||||
underline.get_or_insert((
|
underline.get_or_insert((
|
||||||
vec2f(
|
vec2f(
|
||||||
glyph_origin.x(),
|
glyph_origin.x(),
|
||||||
origin.y() + baseline_offset.y() + 0.618 * self.layout.descent,
|
origin.y() + baseline_offset.y() + 0.618 * self.layout.descent,
|
||||||
),
|
),
|
||||||
Underline {
|
Underline {
|
||||||
color: Some(run_underline.color.unwrap_or(*run_color)),
|
color: Some(
|
||||||
thickness: run_underline.thickness,
|
style_run.underline.color.unwrap_or(style_run.color),
|
||||||
squiggly: run_underline.squiggly,
|
),
|
||||||
|
thickness: style_run.underline.thickness,
|
||||||
|
squiggly: style_run.underline.squiggly,
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
run_end += *run_len as usize;
|
run_end += style_run.len as usize;
|
||||||
color = *run_color;
|
color = style_run.color;
|
||||||
} else {
|
} else {
|
||||||
run_end = self.layout.len;
|
run_end = self.layout.len;
|
||||||
finished_underline = underline.take();
|
finished_underline = underline.take();
|
||||||
|
@ -380,41 +393,85 @@ impl Line {
|
||||||
origin: Vector2F,
|
origin: Vector2F,
|
||||||
visible_bounds: RectF,
|
visible_bounds: RectF,
|
||||||
line_height: f32,
|
line_height: f32,
|
||||||
boundaries: impl IntoIterator<Item = ShapedBoundary>,
|
boundaries: &[ShapedBoundary],
|
||||||
cx: &mut WindowContext,
|
cx: &mut WindowContext,
|
||||||
) {
|
) {
|
||||||
let padding_top = (line_height - self.layout.ascent - self.layout.descent) / 2.;
|
let padding_top = (line_height - self.layout.ascent - self.layout.descent) / 2.;
|
||||||
let baseline_origin = vec2f(0., padding_top + self.layout.ascent);
|
let baseline_offset = vec2f(0., padding_top + self.layout.ascent);
|
||||||
|
|
||||||
let mut boundaries = boundaries.into_iter().peekable();
|
let mut boundaries = boundaries.into_iter().peekable();
|
||||||
let mut color_runs = self.style_runs.iter();
|
let mut color_runs = self.style_runs.iter();
|
||||||
let mut color_end = 0;
|
let mut style_run_end = 0;
|
||||||
let mut color = Color::black();
|
let mut color = Color::black();
|
||||||
|
let mut underline: Option<(Vector2F, Underline)> = None;
|
||||||
|
|
||||||
let mut glyph_origin = vec2f(0., 0.);
|
let mut glyph_origin = origin;
|
||||||
let mut prev_position = 0.;
|
let mut prev_position = 0.;
|
||||||
for run in &self.layout.runs {
|
for (run_ix, run) in self.layout.runs.iter().enumerate() {
|
||||||
for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
|
for (glyph_ix, glyph) in run.glyphs.iter().enumerate() {
|
||||||
if boundaries.peek().map_or(false, |b| b.glyph_ix == glyph_ix) {
|
glyph_origin.set_x(glyph_origin.x() + glyph.position.x() - prev_position);
|
||||||
|
|
||||||
|
if boundaries
|
||||||
|
.peek()
|
||||||
|
.map_or(false, |b| b.run_ix == run_ix && b.glyph_ix == glyph_ix)
|
||||||
|
{
|
||||||
boundaries.next();
|
boundaries.next();
|
||||||
glyph_origin = vec2f(0., glyph_origin.y() + line_height);
|
if let Some((underline_origin, underline_style)) = underline {
|
||||||
} else {
|
scene.push_underline(scene::Underline {
|
||||||
glyph_origin.set_x(glyph_origin.x() + glyph.position.x() - prev_position);
|
origin: underline_origin,
|
||||||
|
width: glyph_origin.x() - underline_origin.x(),
|
||||||
|
thickness: underline_style.thickness.into(),
|
||||||
|
color: underline_style.color.unwrap(),
|
||||||
|
squiggly: underline_style.squiggly,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
glyph_origin = vec2f(origin.x(), glyph_origin.y() + line_height);
|
||||||
}
|
}
|
||||||
prev_position = glyph.position.x();
|
prev_position = glyph.position.x();
|
||||||
|
|
||||||
if glyph.index >= color_end {
|
let mut finished_underline = None;
|
||||||
if let Some(next_run) = color_runs.next() {
|
if glyph.index >= style_run_end {
|
||||||
color_end += next_run.0 as usize;
|
if let Some(style_run) = color_runs.next() {
|
||||||
color = next_run.1;
|
style_run_end += style_run.len as usize;
|
||||||
|
color = style_run.color;
|
||||||
|
if let Some((_, underline_style)) = underline {
|
||||||
|
if style_run.underline != underline_style {
|
||||||
|
finished_underline = underline.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if style_run.underline.thickness.into_inner() > 0. {
|
||||||
|
underline.get_or_insert((
|
||||||
|
glyph_origin
|
||||||
|
+ vec2f(0., baseline_offset.y() + 0.618 * self.layout.descent),
|
||||||
|
Underline {
|
||||||
|
color: Some(
|
||||||
|
style_run.underline.color.unwrap_or(style_run.color),
|
||||||
|
),
|
||||||
|
thickness: style_run.underline.thickness,
|
||||||
|
squiggly: style_run.underline.squiggly,
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
color_end = self.layout.len;
|
style_run_end = self.layout.len;
|
||||||
color = Color::black();
|
color = Color::black();
|
||||||
|
finished_underline = underline.take();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some((underline_origin, underline_style)) = finished_underline {
|
||||||
|
scene.push_underline(scene::Underline {
|
||||||
|
origin: underline_origin,
|
||||||
|
width: glyph_origin.x() - underline_origin.x(),
|
||||||
|
thickness: underline_style.thickness.into(),
|
||||||
|
color: underline_style.color.unwrap(),
|
||||||
|
squiggly: underline_style.squiggly,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let glyph_bounds = RectF::new(
|
let glyph_bounds = RectF::new(
|
||||||
origin + glyph_origin,
|
glyph_origin,
|
||||||
cx.font_cache
|
cx.font_cache
|
||||||
.bounding_box(run.font_id, self.layout.font_size),
|
.bounding_box(run.font_id, self.layout.font_size),
|
||||||
);
|
);
|
||||||
|
@ -424,20 +481,31 @@ impl Line {
|
||||||
font_id: run.font_id,
|
font_id: run.font_id,
|
||||||
font_size: self.layout.font_size,
|
font_size: self.layout.font_size,
|
||||||
id: glyph.id,
|
id: glyph.id,
|
||||||
origin: glyph_bounds.origin() + baseline_origin,
|
origin: glyph_bounds.origin() + baseline_offset,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
scene.push_glyph(scene::Glyph {
|
scene.push_glyph(scene::Glyph {
|
||||||
font_id: run.font_id,
|
font_id: run.font_id,
|
||||||
font_size: self.layout.font_size,
|
font_size: self.layout.font_size,
|
||||||
id: glyph.id,
|
id: glyph.id,
|
||||||
origin: glyph_bounds.origin() + baseline_origin,
|
origin: glyph_bounds.origin() + baseline_offset,
|
||||||
color,
|
color,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some((underline_origin, underline_style)) = underline.take() {
|
||||||
|
let line_end_x = glyph_origin.x() + self.layout.width - prev_position;
|
||||||
|
scene.push_underline(scene::Underline {
|
||||||
|
origin: underline_origin,
|
||||||
|
width: line_end_x - underline_origin.x(),
|
||||||
|
thickness: underline_style.thickness.into(),
|
||||||
|
color: underline_style.color.unwrap(),
|
||||||
|
squiggly: underline_style.squiggly,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ settings = { path = "../settings" }
|
||||||
sum_tree = { path = "../sum_tree" }
|
sum_tree = { path = "../sum_tree" }
|
||||||
terminal = { path = "../terminal" }
|
terminal = { path = "../terminal" }
|
||||||
util = { path = "../util" }
|
util = { path = "../util" }
|
||||||
|
|
||||||
aho-corasick = "0.7"
|
aho-corasick = "0.7"
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
async-trait.workspace = true
|
async-trait.workspace = true
|
||||||
|
@ -47,7 +48,6 @@ lazy_static.workspace = true
|
||||||
log.workspace = true
|
log.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 }
|
|
||||||
rand.workspace = true
|
rand.workspace = true
|
||||||
regex.workspace = true
|
regex.workspace = true
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
DocumentHighlight, Hover, HoverBlock, Location, LocationLink, Project, ProjectTransaction,
|
DocumentHighlight, Hover, HoverBlock, HoverBlockKind, Location, LocationLink, Project,
|
||||||
|
ProjectTransaction,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
@ -13,7 +14,6 @@ use language::{
|
||||||
Completion, OffsetRangeExt, PointUtf16, ToOffset, ToPointUtf16, Unclipped,
|
Completion, OffsetRangeExt, PointUtf16, ToOffset, ToPointUtf16, Unclipped,
|
||||||
};
|
};
|
||||||
use lsp::{DocumentHighlightKind, LanguageServer, LanguageServerId, ServerCapabilities};
|
use lsp::{DocumentHighlightKind, LanguageServer, LanguageServerId, ServerCapabilities};
|
||||||
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
|
|
||||||
use std::{cmp::Reverse, ops::Range, path::Path, sync::Arc};
|
use std::{cmp::Reverse, ops::Range, path::Path, sync::Arc};
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
|
@ -1092,76 +1092,49 @@ impl LspCommand for GetHover {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let contents = cx.read(|_| match hover.contents {
|
fn hover_blocks_from_marked_string(
|
||||||
lsp::HoverContents::Scalar(marked_string) => {
|
marked_string: lsp::MarkedString,
|
||||||
HoverBlock::try_new(marked_string).map(|contents| vec![contents])
|
) -> Option<HoverBlock> {
|
||||||
}
|
let block = match marked_string {
|
||||||
lsp::HoverContents::Array(marked_strings) => {
|
lsp::MarkedString::String(content) => HoverBlock {
|
||||||
let content: Vec<HoverBlock> = marked_strings
|
text: content,
|
||||||
.into_iter()
|
kind: HoverBlockKind::Markdown,
|
||||||
.filter_map(HoverBlock::try_new)
|
},
|
||||||
.collect();
|
lsp::MarkedString::LanguageString(lsp::LanguageString { language, value }) => {
|
||||||
if content.is_empty() {
|
HoverBlock {
|
||||||
None
|
text: value,
|
||||||
} else {
|
kind: HoverBlockKind::Code { language },
|
||||||
Some(content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lsp::HoverContents::Markup(markup_content) => {
|
|
||||||
let mut contents = Vec::new();
|
|
||||||
let mut language = None;
|
|
||||||
let mut current_text = String::new();
|
|
||||||
for event in Parser::new_ext(&markup_content.value, Options::all()) {
|
|
||||||
match event {
|
|
||||||
Event::SoftBreak => {
|
|
||||||
current_text.push(' ');
|
|
||||||
}
|
|
||||||
Event::Text(text) | Event::Code(text) => {
|
|
||||||
current_text.push_str(&text.to_string());
|
|
||||||
}
|
|
||||||
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(new_language))) => {
|
|
||||||
if !current_text.is_empty() {
|
|
||||||
let text = std::mem::take(&mut current_text).trim().to_string();
|
|
||||||
contents.push(HoverBlock { text, language });
|
|
||||||
}
|
|
||||||
|
|
||||||
language = if new_language.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(new_language.to_string())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Event::End(Tag::CodeBlock(_))
|
|
||||||
| Event::End(Tag::Paragraph)
|
|
||||||
| Event::End(Tag::Heading(_, _, _))
|
|
||||||
| Event::End(Tag::BlockQuote)
|
|
||||||
| Event::HardBreak => {
|
|
||||||
if !current_text.is_empty() {
|
|
||||||
let text = std::mem::take(&mut current_text).trim().to_string();
|
|
||||||
contents.push(HoverBlock { text, language });
|
|
||||||
}
|
|
||||||
language = None;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
if !current_text.trim().is_empty() {
|
if block.text.is_empty() {
|
||||||
contents.push(HoverBlock {
|
None
|
||||||
text: current_text,
|
} else {
|
||||||
language,
|
Some(block)
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if contents.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(contents)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let contents = cx.read(|_| match hover.contents {
|
||||||
|
lsp::HoverContents::Scalar(marked_string) => {
|
||||||
|
hover_blocks_from_marked_string(marked_string)
|
||||||
|
.into_iter()
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
lsp::HoverContents::Array(marked_strings) => marked_strings
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(hover_blocks_from_marked_string)
|
||||||
|
.collect(),
|
||||||
|
lsp::HoverContents::Markup(markup_content) => vec![HoverBlock {
|
||||||
|
text: markup_content.value,
|
||||||
|
kind: if markup_content.kind == lsp::MarkupKind::Markdown {
|
||||||
|
HoverBlockKind::Markdown
|
||||||
|
} else {
|
||||||
|
HoverBlockKind::PlainText
|
||||||
|
},
|
||||||
|
}],
|
||||||
});
|
});
|
||||||
|
|
||||||
contents.map(|contents| Hover { contents, range })
|
Some(Hover { contents, range })
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1218,7 +1191,12 @@ impl LspCommand for GetHover {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|block| proto::HoverBlock {
|
.map(|block| proto::HoverBlock {
|
||||||
text: block.text,
|
text: block.text,
|
||||||
language: block.language,
|
is_markdown: block.kind == HoverBlockKind::Markdown,
|
||||||
|
language: if let HoverBlockKind::Code { language } = block.kind {
|
||||||
|
Some(language)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -1255,7 +1233,13 @@ impl LspCommand for GetHover {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|block| HoverBlock {
|
.map(|block| HoverBlock {
|
||||||
text: block.text,
|
text: block.text,
|
||||||
language: block.language,
|
kind: if let Some(language) = block.language {
|
||||||
|
HoverBlockKind::Code { language }
|
||||||
|
} else if block.is_markdown {
|
||||||
|
HoverBlockKind::Markdown
|
||||||
|
} else {
|
||||||
|
HoverBlockKind::PlainText
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ use language::{
|
||||||
};
|
};
|
||||||
use lsp::{
|
use lsp::{
|
||||||
DiagnosticSeverity, DiagnosticTag, DidChangeWatchedFilesRegistrationOptions,
|
DiagnosticSeverity, DiagnosticTag, DidChangeWatchedFilesRegistrationOptions,
|
||||||
DocumentHighlightKind, LanguageServer, LanguageServerId, LanguageString, MarkedString,
|
DocumentHighlightKind, LanguageServer, LanguageServerId,
|
||||||
};
|
};
|
||||||
use lsp_command::*;
|
use lsp_command::*;
|
||||||
use lsp_glob_set::LspGlobSet;
|
use lsp_glob_set::LspGlobSet;
|
||||||
|
@ -287,27 +287,14 @@ pub struct Symbol {
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct HoverBlock {
|
pub struct HoverBlock {
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub language: Option<String>,
|
pub kind: HoverBlockKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HoverBlock {
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
fn try_new(marked_string: MarkedString) -> Option<Self> {
|
pub enum HoverBlockKind {
|
||||||
let result = match marked_string {
|
PlainText,
|
||||||
MarkedString::LanguageString(LanguageString { language, value }) => HoverBlock {
|
Markdown,
|
||||||
text: value,
|
Code { language: String },
|
||||||
language: Some(language),
|
|
||||||
},
|
|
||||||
MarkedString::String(text) => HoverBlock {
|
|
||||||
text,
|
|
||||||
language: None,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
if result.text.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
@ -633,6 +633,7 @@ message GetHoverResponse {
|
||||||
message HoverBlock {
|
message HoverBlock {
|
||||||
string text = 1;
|
string text = 1;
|
||||||
optional string language = 2;
|
optional string language = 2;
|
||||||
|
bool is_markdown = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ApplyCodeAction {
|
message ApplyCodeAction {
|
||||||
|
|
|
@ -46,6 +46,8 @@ pub struct Theme {
|
||||||
|
|
||||||
#[derive(Deserialize, Default, Clone)]
|
#[derive(Deserialize, Default, Clone)]
|
||||||
pub struct ThemeMeta {
|
pub struct ThemeMeta {
|
||||||
|
#[serde(skip_deserializing)]
|
||||||
|
pub id: usize,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub is_light: bool,
|
pub is_light: bool,
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,20 @@ use gpui::{fonts, AssetSource, FontCache};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
sync::{
|
||||||
|
atomic::{AtomicUsize, Ordering::SeqCst},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
pub struct ThemeRegistry {
|
pub struct ThemeRegistry {
|
||||||
assets: Box<dyn AssetSource>,
|
assets: Box<dyn AssetSource>,
|
||||||
themes: Mutex<HashMap<String, Arc<Theme>>>,
|
themes: Mutex<HashMap<String, Arc<Theme>>>,
|
||||||
theme_data: Mutex<HashMap<String, Arc<Value>>>,
|
theme_data: Mutex<HashMap<String, Arc<Value>>>,
|
||||||
font_cache: Arc<FontCache>,
|
font_cache: Arc<FontCache>,
|
||||||
|
next_theme_id: AtomicUsize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ThemeRegistry {
|
impl ThemeRegistry {
|
||||||
|
@ -19,6 +26,7 @@ impl ThemeRegistry {
|
||||||
assets: Box::new(source),
|
assets: Box::new(source),
|
||||||
themes: Default::default(),
|
themes: Default::default(),
|
||||||
theme_data: Default::default(),
|
theme_data: Default::default(),
|
||||||
|
next_theme_id: Default::default(),
|
||||||
font_cache,
|
font_cache,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -66,6 +74,7 @@ impl ThemeRegistry {
|
||||||
|
|
||||||
// Reset name to be the file path, so that we can use it to access the stored themes
|
// Reset name to be the file path, so that we can use it to access the stored themes
|
||||||
theme.meta.name = name.into();
|
theme.meta.name = name.into();
|
||||||
|
theme.meta.id = self.next_theme_id.fetch_add(1, SeqCst);
|
||||||
let theme: Arc<Theme> = theme.into();
|
let theme: Arc<Theme> = theme.into();
|
||||||
self.themes.lock().insert(name.to_string(), theme.clone());
|
self.themes.lock().insert(name.to_string(), theme.clone());
|
||||||
Ok(theme)
|
Ok(theme)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue