summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/eval/mod.rs59
-rw-r--r--src/library/deco.rs29
-rw-r--r--src/syntax/highlight.rs24
3 files changed, 41 insertions, 71 deletions
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index 2759e0d5..58400f5a 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -30,7 +30,6 @@ use std::collections::HashMap;
use std::io;
use std::mem;
use std::path::PathBuf;
-use std::sync::Mutex;
use once_cell::sync::Lazy;
use syntect::easy::HighlightLines;
@@ -43,7 +42,7 @@ use crate::diag::{At, Error, StrResult, Trace, Tracepoint, TypResult};
use crate::geom::{Angle, Fractional, Length, Paint, Relative, RgbaColor};
use crate::image::ImageStore;
use crate::layout::RootNode;
-use crate::library::{self, Decoration, TextNode};
+use crate::library::{self, DecoLine, TextNode};
use crate::loading::Loader;
use crate::parse;
use crate::source::{SourceId, SourceStore};
@@ -53,12 +52,10 @@ use crate::syntax::{RedNode, Span, Spanned};
use crate::util::{EcoString, RefMutExt};
use crate::Context;
-static THEME: Lazy<Mutex<Theme>> = Lazy::new(|| {
- Mutex::new(ThemeSet::load_defaults().themes.remove("InspiredGitHub").unwrap())
-});
+static THEME: Lazy<Theme> =
+ Lazy::new(|| ThemeSet::load_defaults().themes.remove("InspiredGitHub").unwrap());
-static SYNTAXES: Lazy<Mutex<SyntaxSet>> =
- Lazy::new(|| Mutex::new(SyntaxSet::load_defaults_newlines()));
+static SYNTAXES: Lazy<SyntaxSet> = Lazy::new(|| SyntaxSet::load_defaults_newlines());
/// An evaluated module, ready for importing or conversion to a root layout
/// tree.
@@ -237,12 +234,11 @@ impl RawNode {
/// Styled node for a code block, with optional syntax highlighting.
pub fn highlighted(&self) -> Node {
let mut sequence: Vec<Styled<Node>> = vec![];
- let syntaxes = SYNTAXES.lock().unwrap();
let syntax = if let Some(syntax) = self
.lang
.as_ref()
- .and_then(|token| syntaxes.find_syntax_by_token(&token))
+ .and_then(|token| SYNTAXES.find_syntax_by_token(&token))
{
Some(syntax)
} else if matches!(
@@ -254,8 +250,7 @@ impl RawNode {
return Node::Text(self.text.clone()).monospaced();
};
- let theme = THEME.lock().unwrap();
- let foreground = theme
+ let foreground = THEME
.settings
.foreground
.map(RgbaColor::from)
@@ -264,28 +259,31 @@ impl RawNode {
match syntax {
Some(syntax) => {
- let mut highlighter = HighlightLines::new(syntax, &theme);
+ let mut highlighter = HighlightLines::new(syntax, &THEME);
for (i, line) in self.text.lines().enumerate() {
if i != 0 {
sequence.push(Styled::bare(Node::Linebreak));
}
- for (style, line) in highlighter.highlight(line, &syntaxes) {
- sequence.push(Self::styled_line(style, line, foreground));
+ for (style, line) in highlighter.highlight(line, &SYNTAXES) {
+ sequence.push(Self::styled_piece(style, line, foreground));
}
}
}
None => {
- let red_tree =
- RedNode::from_root(parse::parse(&self.text), SourceId::from_raw(0));
- let highlighter = Highlighter::new(&theme);
+ let green = parse::parse(&self.text);
+ let red = RedNode::from_root(green, SourceId::from_raw(0));
+ let highlighter = Highlighter::new(&THEME);
syntax::highlight_syntect(
- red_tree.as_ref(),
- &self.text,
+ red.as_ref(),
&highlighter,
- &mut |style, line| {
- sequence.push(Self::styled_line(style, line, foreground));
+ &mut |range, style| {
+ sequence.push(Self::styled_piece(
+ style,
+ &self.text[range],
+ foreground,
+ ));
},
)
}
@@ -294,26 +292,29 @@ impl RawNode {
Node::Sequence(sequence).monospaced()
}
- fn styled_line(style: SynStyle, line: &str, foreground: Paint) -> Styled<Node> {
+ fn styled_piece(style: SynStyle, piece: &str, foreground: Paint) -> Styled<Node> {
let paint = style.foreground.into();
- let text_node = Node::Text(line.into());
- let mut style_map = StyleMap::new();
+ let node = Node::Text(piece.into());
+
+ let mut styles = StyleMap::new();
if paint != foreground {
- style_map.set(TextNode::FILL, paint);
+ styles.set(TextNode::FILL, paint);
}
if style.font_style.contains(FontStyle::BOLD) {
- style_map.set(TextNode::STRONG, true);
+ styles.set(TextNode::STRONG, true);
}
+
if style.font_style.contains(FontStyle::ITALIC) {
- style_map.set(TextNode::EMPH, true);
+ styles.set(TextNode::EMPH, true);
}
+
if style.font_style.contains(FontStyle::UNDERLINE) {
- style_map.set(TextNode::LINES, vec![Decoration::underline()]);
+ styles.set(TextNode::LINES, vec![DecoLine::Underline.into()]);
}
- Styled::new(text_node, style_map)
+ Styled::new(node, styles)
}
}
diff --git a/src/library/deco.rs b/src/library/deco.rs
index 5f27c8be..ccb657d1 100644
--- a/src/library/deco.rs
+++ b/src/library/deco.rs
@@ -38,33 +38,10 @@ pub struct Decoration {
pub extent: Linear,
}
-impl Decoration {
- /// Create a new underline with default settings.
- pub const fn underline() -> Self {
+impl From<DecoLine> for Decoration {
+ fn from(line: DecoLine) -> Self {
Self {
- line: DecoLine::Underline,
- stroke: None,
- thickness: None,
- offset: None,
- extent: Linear::zero(),
- }
- }
-
- /// Create a new strikethrough with default settings.
- pub const fn strikethrough() -> Self {
- Self {
- line: DecoLine::Underline,
- stroke: None,
- thickness: None,
- offset: None,
- extent: Linear::zero(),
- }
- }
-
- /// Create a new overline with default settings.
- pub const fn overline() -> Self {
- Self {
- line: DecoLine::Overline,
+ line,
stroke: None,
thickness: None,
offset: None,
diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs
index 001b28b3..0f1ee89d 100644
--- a/src/syntax/highlight.rs
+++ b/src/syntax/highlight.rs
@@ -23,32 +23,24 @@ where
}
/// Provide syntect highlighting styles for the children of a node.
-pub fn highlight_syntect<F>(
- node: RedRef,
- text: &str,
- highlighter: &Highlighter,
- f: &mut F,
-) where
- F: FnMut(Style, &str),
+pub fn highlight_syntect<F>(node: RedRef, highlighter: &Highlighter, f: &mut F)
+where
+ F: FnMut(Range<usize>, Style),
{
- highlight_syntect_impl(node, text, vec![], highlighter, f)
+ highlight_syntect_impl(node, vec![], highlighter, f)
}
/// Recursive implementation for returning syntect styles.
fn highlight_syntect_impl<F>(
node: RedRef,
- text: &str,
scopes: Vec<Scope>,
highlighter: &Highlighter,
f: &mut F,
) where
- F: FnMut(Style, &str),
+ F: FnMut(Range<usize>, Style),
{
if node.children().size_hint().0 == 0 {
- f(
- highlighter.style_for_stack(&scopes),
- &text[node.span().to_range()],
- );
+ f(node.span().to_range(), highlighter.style_for_stack(&scopes));
return;
}
@@ -57,7 +49,7 @@ fn highlight_syntect_impl<F>(
if let Some(category) = Category::determine(child, node) {
scopes.push(Scope::new(category.tm_scope()).unwrap())
}
- highlight_syntect_impl(child, text, scopes, highlighter, f);
+ highlight_syntect_impl(child, scopes, highlighter, f);
}
}
@@ -230,7 +222,7 @@ impl Category {
}
/// Return the TextMate grammar scope for the given highlighting category.
- pub const fn tm_scope(&self) -> &'static str {
+ pub fn tm_scope(&self) -> &'static str {
match self {
Self::Bracket => "punctuation.definition.typst",
Self::Punctuation => "punctuation.typst",