diff --git a/crates/typst-library/src/text/raw.rs b/crates/typst-library/src/text/raw.rs index 166319d07..b89360538 100644 --- a/crates/typst-library/src/text/raw.rs +++ b/crates/typst-library/src/text/raw.rs @@ -9,7 +9,7 @@ use syntect::highlighting as synt; use syntect::parsing::{SyntaxDefinition, SyntaxSet, SyntaxSetBuilder}; use typst::diag::FileError; use typst::eval::Bytes; -use typst::syntax::{self, is_newline, LinkedNode}; +use typst::syntax::{self, split_newlines, LinkedNode}; use typst::util::option_eq; use unicode_segmentation::UnicodeSegmentation; @@ -291,7 +291,8 @@ impl Synthesize for RawElem { text = align_tabs(&text, tab_size); } - let count = text.lines().count() as i64; + let lines = split_newlines(&text); + let count = lines.len() as i64; let lang = self .lang(styles) @@ -348,7 +349,7 @@ impl Synthesize for RawElem { }) }) { let mut highlighter = syntect::easy::HighlightLines::new(syntax, theme); - for (i, line) in text.lines().enumerate() { + for (i, line) in lines.into_iter().enumerate() { let mut line_content = vec![]; for (style, piece) in highlighter.highlight_line(line, syntax_set).into_iter().flatten() @@ -367,8 +368,7 @@ impl Synthesize for RawElem { ); } } else { - let lines = text.lines(); - seq.extend(lines.enumerate().map(|(i, line)| { + seq.extend(lines.into_iter().enumerate().map(|(i, line)| { RawLine::new( i as i64 + 1, count, @@ -562,7 +562,7 @@ impl<'a> ThemedHighlighter<'a> { let segment = &self.code[self.node.range()]; let mut len = 0; - for (i, line) in segment.split(is_newline).enumerate() { + for (i, line) in split_newlines(segment).into_iter().enumerate() { if i != 0 { (self.line_fn)( self.line, diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs index a909dfa0a..c702551ce 100644 --- a/crates/typst-syntax/src/lexer.rs +++ b/crates/typst-syntax/src/lexer.rs @@ -673,7 +673,7 @@ pub fn link_prefix(text: &str) -> (&str, bool) { } /// Split text at newlines. -pub(super) fn split_newlines(text: &str) -> Vec<&str> { +pub fn split_newlines(text: &str) -> Vec<&str> { let mut s = Scanner::new(text); let mut lines = Vec::new(); let mut start = 0; diff --git a/crates/typst-syntax/src/lib.rs b/crates/typst-syntax/src/lib.rs index 5cf740e7d..7ce30d9aa 100644 --- a/crates/typst-syntax/src/lib.rs +++ b/crates/typst-syntax/src/lib.rs @@ -15,11 +15,13 @@ mod span; pub use self::file::{FileId, PackageSpec, PackageVersion, VirtualPath}; pub use self::highlight::{highlight, highlight_html, Tag}; pub use self::kind::SyntaxKind; -pub use self::lexer::{is_id_continue, is_id_start, is_ident, is_newline, link_prefix}; +pub use self::lexer::{ + is_id_continue, is_id_start, is_ident, is_newline, link_prefix, split_newlines, +}; pub use self::node::{LinkedChildren, LinkedNode, SyntaxError, SyntaxNode}; pub use self::parser::{parse, parse_code, parse_math}; pub use self::source::Source; pub use self::span::{Span, Spanned}; -use self::lexer::{split_newlines, LexMode, Lexer}; +use self::lexer::{LexMode, Lexer}; use self::parser::{reparse_block, reparse_markup};