on master help sign in
ui: Syntax highlight code inside markdown popups.
commitd8599f3a140eca7cd14f47e9b64f1ae9d829a0eb authorBlaž Hrastnik <blaz@mxxn.io> dateTue 16 Mar 2021, 16:38 (6 years ago) parent54a7e89
2 files changed+75−44
helix-term/src/ui/markdown.rs+75−16 helix-view/src/theme.rs−28
helix-term/src/ui/markdown.rs+75−16
@@ -10,18 +10,22 @@ use tui::{
1010use std::borrow::Cow;
1111
1212use helix_core::Position;
13−use helix_view::Editor;
13+use helix_view::{Editor, Theme};
1414
1515pub struct Markdown {
1616 contents: String,
1717}
1818
19+// TODO: pre-render and self reference via Pin
20+// better yet, just use Tendril + subtendril for references
21+
1922impl Markdown {
2023 pub fn new(contents: String) -> Self {
2124 Self { contents }
2225 }
2326}
24−fn parse(contents: &str) -> tui::text::Text {
27+
28+fn parse<'a>(contents: &'a str, theme: Option<&Theme>) -> tui::text::Text<'a> {
2529 use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag};
2630 use tui::text::{Span, Spans, Text};
2731
@@ -68,10 +72,73 @@ fn parse(contents: &str) -> tui::text::Text {
6872 }
6973 }
7074 Event::Text(text) => {
71− if let Some(Tag::CodeBlock(CodeBlockKind::Fenced(_))) = tags.last() {
72− for line in text.lines() {
73− let mut span = Span::styled(line.to_string(), code_style);
74− lines.push(Spans::from(span));
75+ // TODO: temp workaround
76+ if let Some(Tag::CodeBlock(CodeBlockKind::Fenced(language))) = tags.last() {
77+ if let Some(theme) = theme {
78+ use helix_core::syntax::{self, HighlightEvent, Syntax};
79+ use helix_core::Rope;
80+
81+ let rope = Rope::from(text.as_ref());
82+ let syntax = syntax::LOADER
83+ .language_config_for_scope(&format!("source.{}", language))
84+ .and_then(|config| config.highlight_config(theme.scopes()))
85+ .map(|config| Syntax::new(&rope, config));
86+
87+ if let Some(mut syntax) = syntax {
88+ // if we have a syntax available, highlight_iter and generate spans
89+ let mut highlights = Vec::new();
90+
91+ for event in syntax.highlight_iter(rope.slice(..), None, None, |_| None)
92+ {
93+ match event.unwrap() {
94+ HighlightEvent::HighlightStart(span) => {
95+ highlights.push(span);
96+ }
97+ HighlightEvent::HighlightEnd => {
98+ highlights.pop();
99+ }
100+ HighlightEvent::Source { start, end } => {
101+ let style = match highlights.first() {
102+ Some(span) => {
103+ theme.get(theme.scopes()[span.0].as_str())
104+ }
105+ None => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
106+ };
107+
108+ let mut slice = &text[start..end];
109+ while let Some(end) = slice.find('\n') {
110+ // emit span up to newline
111+ let text = &slice[..end];
112+ let span = Span::styled(text.to_owned(), style);
113+ spans.push(span);
114+
115+ // truncate slice to after newline
116+ slice = &slice[end + 1..];
117+
118+ // make a new line
119+ let spans = std::mem::replace(&mut spans, Vec::new());
120+ lines.push(Spans::from(spans));
121+ }
122+
123+ // if there's anything left, emit it too
124+ if !slice.is_empty() {
125+ let span = Span::styled(slice.to_owned(), style);
126+ spans.push(span);
127+ }
128+ }
129+ }
130+ }
131+ } else {
132+ for line in text.lines() {
133+ let mut span = Span::styled(line.to_string(), code_style);
134+ lines.push(Spans::from(span));
135+ }
136+ }
137+ } else {
138+ for line in text.lines() {
139+ let mut span = Span::styled(line.to_string(), code_style);
140+ lines.push(Spans::from(span));
141+ }
75142 }
76143 } else if let Some(Tag::Heading(_)) = tags.last() {
77144 let mut span = to_span(text);
@@ -113,7 +180,7 @@ impl Component for Markdown {
113180 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
114181 use tui::widgets::{Paragraph, Widget, Wrap};
115182
116− let text = parse(&self.contents);
183+ let text = parse(&self.contents, Some(&cx.editor.theme));
117184
118185 let par = Paragraph::new(text)
119186 .wrap(Wrap { trim: false })
@@ -124,18 +191,10 @@ impl Component for Markdown {
124191 }
125192
126193 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
127− let contents = parse(&self.contents);
194+ let contents = parse(&self.contents, None);
128195 let padding = 2;
129196 let width = std::cmp::min(contents.width() as u16 + padding, viewport.0);
130197 let height = std::cmp::min(contents.height() as u16 + padding, viewport.1);
131198 Some((width, height))
132199 }
133200}
134−
135−#[cfg(test)]
136−mod test {
137− use super::*;
138−
139− #[test]
140− fn it_works() {}
141−}
helix-view/src/theme.rs−28
@@ -88,34 +88,6 @@ pub struct Theme {
8888 mapping: HashMap<&'static str, Style>,
8989}
9090
91−// let highlight_names: Vec<String> = [
92−// "attribute",
93−// "constant.builtin",
94−// "constant",
95−// "function.builtin",
96−// "function.macro",
97−// "function",
98−// "keyword",
99−// "operator",
100−// "property",
101−// "punctuation",
102−// "comment",
103−// "escape",
104−// "label",
105−// // "punctuation.bracket",
106−// "punctuation.delimiter",
107−// "string",
108−// "string.special",
109−// "tag",
110−// "type",
111−// "type.builtin",
112−// "constructor",
113−// "variable",
114−// "variable.builtin",
115−// "variable.parameter",
116−// "path",
117−// ];
118−
11991impl Default for Theme {
12092 fn default() -> Self {
12193 let mapping = hashmap! {