Fix potential line splitting issue in raw (#2674)

This commit is contained in:
Sébastien d'Herbais de Thun 2023-11-13 16:56:51 +01:00 committed by GitHub
parent 7d5f6a8b73
commit d77356a16f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 9 deletions

View File

@ -9,7 +9,7 @@ use syntect::highlighting as synt;
use syntect::parsing::{SyntaxDefinition, SyntaxSet, SyntaxSetBuilder}; use syntect::parsing::{SyntaxDefinition, SyntaxSet, SyntaxSetBuilder};
use typst::diag::FileError; use typst::diag::FileError;
use typst::eval::Bytes; use typst::eval::Bytes;
use typst::syntax::{self, is_newline, LinkedNode}; use typst::syntax::{self, split_newlines, LinkedNode};
use typst::util::option_eq; use typst::util::option_eq;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
@ -291,7 +291,8 @@ impl Synthesize for RawElem {
text = align_tabs(&text, tab_size); 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 let lang = self
.lang(styles) .lang(styles)
@ -348,7 +349,7 @@ impl Synthesize for RawElem {
}) })
}) { }) {
let mut highlighter = syntect::easy::HighlightLines::new(syntax, theme); 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![]; let mut line_content = vec![];
for (style, piece) in for (style, piece) in
highlighter.highlight_line(line, syntax_set).into_iter().flatten() highlighter.highlight_line(line, syntax_set).into_iter().flatten()
@ -367,8 +368,7 @@ impl Synthesize for RawElem {
); );
} }
} else { } else {
let lines = text.lines(); seq.extend(lines.into_iter().enumerate().map(|(i, line)| {
seq.extend(lines.enumerate().map(|(i, line)| {
RawLine::new( RawLine::new(
i as i64 + 1, i as i64 + 1,
count, count,
@ -562,7 +562,7 @@ impl<'a> ThemedHighlighter<'a> {
let segment = &self.code[self.node.range()]; let segment = &self.code[self.node.range()];
let mut len = 0; 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 { if i != 0 {
(self.line_fn)( (self.line_fn)(
self.line, self.line,

View File

@ -673,7 +673,7 @@ pub fn link_prefix(text: &str) -> (&str, bool) {
} }
/// Split text at newlines. /// 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 s = Scanner::new(text);
let mut lines = Vec::new(); let mut lines = Vec::new();
let mut start = 0; let mut start = 0;

View File

@ -15,11 +15,13 @@ mod span;
pub use self::file::{FileId, PackageSpec, PackageVersion, VirtualPath}; pub use self::file::{FileId, PackageSpec, PackageVersion, VirtualPath};
pub use self::highlight::{highlight, highlight_html, Tag}; pub use self::highlight::{highlight, highlight_html, Tag};
pub use self::kind::SyntaxKind; 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::node::{LinkedChildren, LinkedNode, SyntaxError, SyntaxNode};
pub use self::parser::{parse, parse_code, parse_math}; pub use self::parser::{parse, parse_code, parse_math};
pub use self::source::Source; pub use self::source::Source;
pub use self::span::{Span, Spanned}; 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}; use self::parser::{reparse_block, reparse_markup};