mirror of
https://github.com/typst/typst
synced 2025-05-15 17:45:27 +08:00
fix newline counting
should probably fix the windows build
This commit is contained in:
parent
05a40ed43d
commit
ecb0ee51b1
@ -1075,6 +1075,25 @@ fn count_newlines(text: &str) -> usize {
|
|||||||
newlines
|
newlines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Count newlines in text.
|
||||||
|
/// Only counts up to 2 newlines.
|
||||||
|
pub(crate) fn count_capped_newlines(text: &str) -> u8 {
|
||||||
|
let mut newlines = 0;
|
||||||
|
let mut s = Scanner::new(text);
|
||||||
|
while let Some(c) = s.eat() {
|
||||||
|
if is_newline(c) {
|
||||||
|
if c == '\r' {
|
||||||
|
s.eat_if('\n');
|
||||||
|
}
|
||||||
|
newlines += 1;
|
||||||
|
if newlines == 2 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newlines
|
||||||
|
}
|
||||||
|
|
||||||
/// Whether a string is a valid Typst identifier.
|
/// Whether a string is a valid Typst identifier.
|
||||||
///
|
///
|
||||||
/// In addition to what is specified in the [Unicode Standard][uax31], we allow:
|
/// In addition to what is specified in the [Unicode Standard][uax31], we allow:
|
||||||
|
@ -6,7 +6,7 @@ use std::sync::Arc;
|
|||||||
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
|
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
|
||||||
|
|
||||||
use crate::ast::AstNode;
|
use crate::ast::AstNode;
|
||||||
use crate::{is_newline, FileId, Span, SyntaxKind};
|
use crate::{FileId, Span, SyntaxKind};
|
||||||
|
|
||||||
/// A node in the untyped syntax tree.
|
/// A node in the untyped syntax tree.
|
||||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||||
@ -153,18 +153,15 @@ impl SyntaxNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The amount of newlines in this node or its descendants,
|
/// The amount of newlines in this node or its descendants, capped at 2,
|
||||||
/// capped at 256, that is, a return value of 256 means that the total
|
/// that is, a return value of 2 means that the total amount of newlines
|
||||||
/// amount of newlines may be 256 or larger.
|
/// may be 2 or larger.
|
||||||
pub fn newlines(&self) -> u8 {
|
pub fn capped_newlines(&self) -> u8 {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Repr::Leaf(_) | Repr::Error(_) => self
|
Repr::Leaf(_) | Repr::Error(_) => {
|
||||||
.text()
|
crate::lexer::count_capped_newlines(self.text())
|
||||||
.chars()
|
}
|
||||||
.filter(|c| is_newline(*c))
|
Repr::Inner(inner) => inner.capped_newlines,
|
||||||
.take(u8::MAX as usize)
|
|
||||||
.count() as u8,
|
|
||||||
Repr::Inner(inner) => inner.newlines,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -403,9 +400,9 @@ struct InnerNode {
|
|||||||
erroneous: bool,
|
erroneous: bool,
|
||||||
/// The (capped) amount of newlines in this node's descendants.
|
/// The (capped) amount of newlines in this node's descendants.
|
||||||
/// This is solely used to tell whether this node contains 0, 1, 2 or more
|
/// This is solely used to tell whether this node contains 0, 1, 2 or more
|
||||||
/// newlines. As such, this number is capped at 256, even though there may
|
/// newlines. As such, this number is capped at 2, even though there may be
|
||||||
/// be more newlines inside this node.
|
/// more newlines inside this node.
|
||||||
newlines: u8,
|
capped_newlines: u8,
|
||||||
/// The upper bound of this node's numbering range.
|
/// The upper bound of this node's numbering range.
|
||||||
upper: u64,
|
upper: u64,
|
||||||
/// This node's children, losslessly make up this node.
|
/// This node's children, losslessly make up this node.
|
||||||
@ -421,18 +418,23 @@ impl InnerNode {
|
|||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
let mut descendants = 1;
|
let mut descendants = 1;
|
||||||
let mut erroneous = false;
|
let mut erroneous = false;
|
||||||
|
let mut capped_newlines: u8 = 0;
|
||||||
|
|
||||||
for child in &children {
|
for child in &children {
|
||||||
len += child.len();
|
len += child.len();
|
||||||
descendants += child.descendants();
|
descendants += child.descendants();
|
||||||
erroneous |= child.erroneous();
|
erroneous |= child.erroneous();
|
||||||
|
|
||||||
|
if capped_newlines < 2 {
|
||||||
|
capped_newlines = capped_newlines.saturating_add(child.capped_newlines());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
kind,
|
kind,
|
||||||
len,
|
len,
|
||||||
span: Span::detached(),
|
span: Span::detached(),
|
||||||
newlines: 0,
|
capped_newlines: capped_newlines.min(2),
|
||||||
descendants,
|
descendants,
|
||||||
erroneous,
|
erroneous,
|
||||||
upper: 0,
|
upper: 0,
|
||||||
@ -823,7 +825,7 @@ impl<'a> LinkedNode<'a> {
|
|||||||
/// node).
|
/// node).
|
||||||
pub fn prev_attached_decorator(&self) -> Option<Self> {
|
pub fn prev_attached_decorator(&self) -> Option<Self> {
|
||||||
let mut cursor = self.prev_sibling_inner()?;
|
let mut cursor = self.prev_sibling_inner()?;
|
||||||
let mut newlines = cursor.newlines();
|
let mut newlines = cursor.capped_newlines();
|
||||||
let mut at_previous_line = false;
|
let mut at_previous_line = false;
|
||||||
while newlines < 2 {
|
while newlines < 2 {
|
||||||
if cursor.kind() == SyntaxKind::Decorator {
|
if cursor.kind() == SyntaxKind::Decorator {
|
||||||
@ -844,7 +846,7 @@ impl<'a> LinkedNode<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cursor = cursor.prev_sibling_inner()?;
|
cursor = cursor.prev_sibling_inner()?;
|
||||||
newlines = cursor.newlines();
|
newlines = cursor.capped_newlines();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Found a parbreak or something else with two or more newlines.
|
// Found a parbreak or something else with two or more newlines.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user