From 4f66907d08a8ed18b41e70188b112d7c915aa0bc Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Tue, 14 Dec 2021 14:24:02 +0100 Subject: Add Code Block syntax highlighting --- src/syntax/highlight.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'src/syntax/highlight.rs') diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs index 9f7365a8..001b28b3 100644 --- a/src/syntax/highlight.rs +++ b/src/syntax/highlight.rs @@ -1,5 +1,8 @@ use std::ops::Range; +use syntect::highlighting::{Highlighter, Style}; +use syntect::parsing::Scope; + use super::{NodeKind, RedRef}; /// Provide highlighting categories for the children of a node that fall into a @@ -19,6 +22,45 @@ where } } +/// Provide syntect highlighting styles for the children of a node. +pub fn highlight_syntect( + node: RedRef, + text: &str, + highlighter: &Highlighter, + f: &mut F, +) where + F: FnMut(Style, &str), +{ + highlight_syntect_impl(node, text, vec![], highlighter, f) +} + +/// Recursive implementation for returning syntect styles. +fn highlight_syntect_impl( + node: RedRef, + text: &str, + scopes: Vec, + highlighter: &Highlighter, + f: &mut F, +) where + F: FnMut(Style, &str), +{ + if node.children().size_hint().0 == 0 { + f( + highlighter.style_for_stack(&scopes), + &text[node.span().to_range()], + ); + return; + } + + for child in node.children() { + let mut scopes = scopes.clone(); + if let Some(category) = Category::determine(child, node) { + scopes.push(Scope::new(category.tm_scope()).unwrap()) + } + highlight_syntect_impl(child, text, scopes, highlighter, f); + } +} + /// The syntax highlighting category of a node. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum Category { @@ -186,6 +228,33 @@ impl Category { NodeKind::IncludeExpr => None, } } + + /// Return the TextMate grammar scope for the given highlighting category. + pub const fn tm_scope(&self) -> &'static str { + match self { + Self::Bracket => "punctuation.definition.typst", + Self::Punctuation => "punctuation.typst", + Self::Comment => "comment.typst", + Self::Strong => "markup.bold.typst", + Self::Emph => "markup.italic.typst", + Self::Raw => "markup.raw.typst", + Self::Math => "string.other.math.typst", + Self::Heading => "markup.heading.typst", + Self::List => "markup.list.typst", + Self::Shortcut => "punctuation.shortcut.typst", + Self::Escape => "constant.character.escape.content.typst", + Self::Keyword => "keyword.typst", + Self::Operator => "keyword.operator.typst", + Self::None => "constant.language.none.typst", + Self::Auto => "constant.language.auto.typst", + Self::Bool => "constant.language.boolean.typst", + Self::Number => "constant.numeric.typst", + Self::String => "string.quoted.double.typst", + Self::Function => "entity.name.function.typst", + Self::Variable => "variable.parameter.typst", + Self::Invalid => "invalid.typst", + } + } } #[cfg(test)] -- cgit v1.2.3 From c183ed3c15110e11bffd40fad5c5fdfb4d1a5814 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 27 Jan 2022 23:07:10 +0100 Subject: Mutex comes from tex and we don't want any --- src/syntax/highlight.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'src/syntax/highlight.rs') 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( - node: RedRef, - text: &str, - highlighter: &Highlighter, - f: &mut F, -) where - F: FnMut(Style, &str), +pub fn highlight_syntect(node: RedRef, highlighter: &Highlighter, f: &mut F) +where + F: FnMut(Range, 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( node: RedRef, - text: &str, scopes: Vec, highlighter: &Highlighter, f: &mut F, ) where - F: FnMut(Style, &str), + F: FnMut(Range, 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( 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", -- cgit v1.2.3