on master help sign in
Completion: Format docs tabs & highlight in the doc's native language
87d0617Blaž Hrastnik
helix-term/src/ui/completion.rs+12−3
@@ -231,6 +231,12 @@ impl Component for Completion {
231 // ---
231 // ---
232 // option.documentation
232 // option.documentation
233
233
234+ let (view, doc) = cx.editor.current();
235+ let language = doc
236+ .language()
237+ .and_then(|scope| scope.strip_prefix("source."))
238+ .unwrap_or("");
239+
234 let doc = match &option.documentation {
240 let doc = match &option.documentation {
235 Some(lsp::Documentation::String(contents))
241 Some(lsp::Documentation::String(contents))
236 | Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
242 | Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
@@ -239,7 +245,8 @@ impl Component for Completion {
239 })) => {
245 })) => {
240 // TODO: convert to wrapped text
246 // TODO: convert to wrapped text
241 Markdown::new(format!(
247 Markdown::new(format!(
242− "```rust\n{}\n```\n{}",
248+ "```{}\n{}\n```\n{}",
249+ language,
243 option.detail.as_deref().unwrap_or_default(),
250 option.detail.as_deref().unwrap_or_default(),
244 contents.clone()
251 contents.clone()
245 ))
252 ))
@@ -250,7 +257,8 @@ impl Component for Completion {
250 })) => {
257 })) => {
251 // TODO: set language based on doc scope
258 // TODO: set language based on doc scope
252 Markdown::new(format!(
259 Markdown::new(format!(
253− "```rust\n{}\n```\n{}",
260+ "```{}\n{}\n```\n{}",
261+ language,
254 option.detail.as_deref().unwrap_or_default(),
262 option.detail.as_deref().unwrap_or_default(),
255 contents.clone()
263 contents.clone()
256 ))
264 ))
@@ -260,7 +268,8 @@ impl Component for Completion {
260
268
261 // TODO: set language based on doc scope
269 // TODO: set language based on doc scope
262 Markdown::new(format!(
270 Markdown::new(format!(
263− "```rust\n{}\n```",
271+ "```{}\n{}\n```",
272+ language,
264 option.detail.as_deref().unwrap_or_default(),
273 option.detail.as_deref().unwrap_or_default(),
265 ))
274 ))
266 }
275 }
helix-term/src/ui/markdown.rs+4−2
@@ -113,7 +113,8 @@ fn parse<'a>(contents: &'a str, theme: Option<&Theme>) -> tui::text::Text<'a> {
113 while let Some(end) = slice.find('\n') {
113 while let Some(end) = slice.find('\n') {
114 // emit span up to newline
114 // emit span up to newline
115 let text = &slice[..end];
115 let text = &slice[..end];
116+ let text = text.replace('\t', " "); // replace tabs
116− let span = Span::styled(text.to_owned(), style);
117+ let span = Span::styled(text, style);
117 spans.push(span);
118 spans.push(span);
118
119
119 // truncate slice to after newline
120 // truncate slice to after newline
@@ -126,7 +127,8 @@ fn parse<'a>(contents: &'a str, theme: Option<&Theme>) -> tui::text::Text<'a> {
126
127
127 // if there's anything left, emit it too
128 // if there's anything left, emit it too
128 if !slice.is_empty() {
129 if !slice.is_empty() {
130+ let span =
129− let span = Span::styled(slice.to_owned(), style);
131+ Span::styled(slice.replace('\t', " "), style);
130 spans.push(span);
132 spans.push(span);
131 }
133 }
132 }
134 }