Preparse documentation markdown when resolving completion
This commit is contained in:
parent
fe62423344
commit
b8876f2b17
6 changed files with 144 additions and 91 deletions
|
@ -1,12 +1,13 @@
|
|||
pub use crate::{
|
||||
diagnostic_set::DiagnosticSet,
|
||||
highlight_map::{HighlightId, HighlightMap},
|
||||
markdown::RenderedMarkdown,
|
||||
markdown::ParsedMarkdown,
|
||||
proto, BracketPair, Grammar, Language, LanguageConfig, LanguageRegistry, PLAIN_TEXT,
|
||||
};
|
||||
use crate::{
|
||||
diagnostic_set::{DiagnosticEntry, DiagnosticGroup},
|
||||
language_settings::{language_settings, LanguageSettings},
|
||||
markdown,
|
||||
outline::OutlineItem,
|
||||
syntax_map::{
|
||||
SyntaxLayerInfo, SyntaxMap, SyntaxMapCapture, SyntaxMapCaptures, SyntaxMapMatches,
|
||||
|
@ -144,12 +145,51 @@ pub struct Diagnostic {
|
|||
pub is_unnecessary: bool,
|
||||
}
|
||||
|
||||
pub 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) => {
|
||||
if text.lines().count() <= 1 {
|
||||
Some(Documentation::SingleLine(text.clone()))
|
||||
} else {
|
||||
Some(Documentation::MultiLinePlainText(text.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
lsp::Documentation::MarkupContent(lsp::MarkupContent { kind, value }) => match kind {
|
||||
lsp::MarkupKind::PlainText => {
|
||||
if value.lines().count() <= 1 {
|
||||
Some(Documentation::SingleLine(value.clone()))
|
||||
} else {
|
||||
Some(Documentation::MultiLinePlainText(value.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
lsp::MarkupKind::Markdown => {
|
||||
let parsed = markdown::parse_markdown(value, language_registry, language, style);
|
||||
Some(Documentation::MultiLineMarkdown(parsed))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Documentation {
|
||||
SingleLine(String),
|
||||
MultiLinePlainText(String),
|
||||
MultiLineMarkdown(ParsedMarkdown),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Completion {
|
||||
pub old_range: Range<Anchor>,
|
||||
pub new_text: String,
|
||||
pub label: CodeLabel,
|
||||
pub alongside_documentation: Option<RenderedMarkdown>,
|
||||
pub documentation: Option<Documentation>,
|
||||
pub server_id: LanguageServerId,
|
||||
pub lsp_completion: lsp::CompletionItem,
|
||||
}
|
||||
|
|
|
@ -7,31 +7,31 @@ use gpui::fonts::{HighlightStyle, Underline, Weight};
|
|||
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RenderedMarkdown {
|
||||
pub struct ParsedMarkdown {
|
||||
pub text: String,
|
||||
pub highlights: Vec<(Range<usize>, HighlightStyle)>,
|
||||
pub region_ranges: Vec<Range<usize>>,
|
||||
pub regions: Vec<RenderedRegion>,
|
||||
pub regions: Vec<ParsedRegion>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RenderedRegion {
|
||||
pub struct ParsedRegion {
|
||||
pub code: bool,
|
||||
pub link_url: Option<String>,
|
||||
}
|
||||
|
||||
pub fn render_markdown(
|
||||
pub fn parse_markdown(
|
||||
markdown: &str,
|
||||
language_registry: &Arc<LanguageRegistry>,
|
||||
language: &Option<Arc<Language>>,
|
||||
language: Option<Arc<Language>>,
|
||||
style: &theme::Editor,
|
||||
) -> RenderedMarkdown {
|
||||
) -> ParsedMarkdown {
|
||||
let mut text = String::new();
|
||||
let mut highlights = Vec::new();
|
||||
let mut region_ranges = Vec::new();
|
||||
let mut regions = Vec::new();
|
||||
|
||||
render_markdown_block(
|
||||
parse_markdown_block(
|
||||
markdown,
|
||||
language_registry,
|
||||
language,
|
||||
|
@ -42,7 +42,7 @@ pub fn render_markdown(
|
|||
&mut regions,
|
||||
);
|
||||
|
||||
RenderedMarkdown {
|
||||
ParsedMarkdown {
|
||||
text,
|
||||
highlights,
|
||||
region_ranges,
|
||||
|
@ -50,15 +50,15 @@ pub fn render_markdown(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn render_markdown_block(
|
||||
pub fn parse_markdown_block(
|
||||
markdown: &str,
|
||||
language_registry: &Arc<LanguageRegistry>,
|
||||
language: &Option<Arc<Language>>,
|
||||
language: Option<Arc<Language>>,
|
||||
style: &theme::Editor,
|
||||
text: &mut String,
|
||||
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
||||
region_ranges: &mut Vec<Range<usize>>,
|
||||
regions: &mut Vec<RenderedRegion>,
|
||||
regions: &mut Vec<ParsedRegion>,
|
||||
) {
|
||||
let mut bold_depth = 0;
|
||||
let mut italic_depth = 0;
|
||||
|
@ -71,7 +71,7 @@ pub fn render_markdown_block(
|
|||
match event {
|
||||
Event::Text(t) => {
|
||||
if let Some(language) = ¤t_language {
|
||||
render_code(text, highlights, t.as_ref(), language, style);
|
||||
highlight_code(text, highlights, t.as_ref(), language, style);
|
||||
} else {
|
||||
text.push_str(t.as_ref());
|
||||
|
||||
|
@ -84,7 +84,7 @@ pub fn render_markdown_block(
|
|||
}
|
||||
if let Some(link_url) = link_url.clone() {
|
||||
region_ranges.push(prev_len..text.len());
|
||||
regions.push(RenderedRegion {
|
||||
regions.push(ParsedRegion {
|
||||
link_url: Some(link_url),
|
||||
code: false,
|
||||
});
|
||||
|
@ -124,7 +124,7 @@ pub fn render_markdown_block(
|
|||
},
|
||||
));
|
||||
}
|
||||
regions.push(RenderedRegion {
|
||||
regions.push(ParsedRegion {
|
||||
code: true,
|
||||
link_url: link_url.clone(),
|
||||
});
|
||||
|
@ -202,7 +202,7 @@ pub fn render_markdown_block(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn render_code(
|
||||
pub fn highlight_code(
|
||||
text: &mut String,
|
||||
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
||||
content: &str,
|
||||
|
|
|
@ -482,7 +482,7 @@ pub async fn deserialize_completion(
|
|||
lsp_completion.filter_text.as_deref(),
|
||||
)
|
||||
}),
|
||||
alongside_documentation: None,
|
||||
documentation: None,
|
||||
server_id: LanguageServerId(completion.server_id as usize),
|
||||
lsp_completion,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue