summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-04-30 14:12:28 +0200
committerLaurenz <laurmaedje@gmail.com>2022-04-30 14:12:28 +0200
commitf9e115daf54c29358f890b137f50a33a781af680 (patch)
tree496de52246629ea8039db6beea94eb779ed2851d /src/syntax
parentf7c67cde72e6a67f45180856b332bae9863243bd (diff)
New block spacing model
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/ast.rs25
-rw-r--r--src/syntax/highlight.rs4
-rw-r--r--src/syntax/mod.rs25
3 files changed, 30 insertions, 24 deletions
diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs
index 5232b1f1..42a4235d 100644
--- a/src/syntax/ast.rs
+++ b/src/syntax/ast.rs
@@ -2,6 +2,7 @@
//!
//! The AST is rooted in the [`Markup`] node.
+use std::num::NonZeroUsize;
use std::ops::Deref;
use super::{Green, GreenData, NodeKind, RedNode, RedRef, Span};
@@ -62,7 +63,9 @@ impl Markup {
self.0.children().filter_map(|node| match node.kind() {
NodeKind::Space(2 ..) => Some(MarkupNode::Parbreak),
NodeKind::Space(_) => Some(MarkupNode::Space),
- NodeKind::Linebreak(j) => Some(MarkupNode::Linebreak(*j)),
+ &NodeKind::Linebreak { justified } => {
+ Some(MarkupNode::Linebreak { justified })
+ }
NodeKind::Text(s) => Some(MarkupNode::Text(s.clone())),
NodeKind::Escape(c) => Some(MarkupNode::Text((*c).into())),
NodeKind::NonBreakingSpace => Some(MarkupNode::Text('\u{00A0}'.into())),
@@ -70,7 +73,7 @@ impl Markup {
NodeKind::EnDash => Some(MarkupNode::Text('\u{2013}'.into())),
NodeKind::EmDash => Some(MarkupNode::Text('\u{2014}'.into())),
NodeKind::Ellipsis => Some(MarkupNode::Text('\u{2026}'.into())),
- NodeKind::Quote(d) => Some(MarkupNode::Quote(*d)),
+ &NodeKind::Quote { double } => Some(MarkupNode::Quote { double }),
NodeKind::Strong => node.cast().map(MarkupNode::Strong),
NodeKind::Emph => node.cast().map(MarkupNode::Emph),
NodeKind::Raw(raw) => Some(MarkupNode::Raw(raw.as_ref().clone())),
@@ -88,15 +91,14 @@ impl Markup {
pub enum MarkupNode {
/// Whitespace containing less than two newlines.
Space,
- /// A forced line break. If `true` (`\`), the preceding line can still be
- /// justified, if `false` (`\+`) not.
- Linebreak(bool),
+ /// A forced line break: `\` or `\+` if justified.
+ Linebreak { justified: bool },
/// A paragraph break: Two or more newlines.
Parbreak,
/// Plain text.
Text(EcoString),
- /// A smart quote: `'` (`false`) or `"` (true).
- Quote(bool),
+ /// A smart quote: `'` or `"`.
+ Quote { double: bool },
/// Strong content: `*Strong*`.
Strong(StrongNode),
/// Emphasized content: `_Emphasized_`.
@@ -176,8 +178,13 @@ impl HeadingNode {
}
/// The section depth (numer of equals signs).
- pub fn level(&self) -> usize {
- self.0.children().filter(|n| n.kind() == &NodeKind::Eq).count()
+ pub fn level(&self) -> NonZeroUsize {
+ self.0
+ .children()
+ .filter(|n| n.kind() == &NodeKind::Eq)
+ .count()
+ .try_into()
+ .expect("heading is missing equals sign")
}
}
diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs
index 9bee73ae..8e62424f 100644
--- a/src/syntax/highlight.rs
+++ b/src/syntax/highlight.rs
@@ -126,7 +126,7 @@ impl Category {
_ => Some(Category::Operator),
},
NodeKind::EnumNumbering(_) => Some(Category::List),
- NodeKind::Linebreak(_) => Some(Category::Shortcut),
+ NodeKind::Linebreak { .. } => Some(Category::Shortcut),
NodeKind::NonBreakingSpace => Some(Category::Shortcut),
NodeKind::Shy => Some(Category::Shortcut),
NodeKind::EnDash => Some(Category::Shortcut),
@@ -206,7 +206,7 @@ impl Category {
NodeKind::Markup(_) => None,
NodeKind::Space(_) => None,
NodeKind::Text(_) => None,
- NodeKind::Quote(_) => None,
+ NodeKind::Quote { .. } => None,
NodeKind::List => None,
NodeKind::Enum => None,
NodeKind::CodeBlock => None,
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index 2272b3e0..d21597ff 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -588,9 +588,8 @@ pub enum NodeKind {
Space(usize),
/// A consecutive non-markup string.
Text(EcoString),
- /// A forced line break. If `true` (`\`), the preceding line can still be
- /// justified, if `false` (`\+`) not.
- Linebreak(bool),
+ /// A forced line break: `\` or `\+` if justified.
+ Linebreak { justified: bool },
/// A non-breaking space: `~`.
NonBreakingSpace,
/// A soft hyphen: `-?`.
@@ -601,8 +600,8 @@ pub enum NodeKind {
EmDash,
/// An ellipsis: `...`.
Ellipsis,
- /// A smart quote: `'` (`false`) or `"` (true).
- Quote(bool),
+ /// A smart quote: `'` or `"`.
+ Quote { double: bool },
/// A slash and the letter "u" followed by a hexadecimal unicode entity
/// enclosed in curly braces: `\u{1F5FA}`.
Escape(char),
@@ -773,13 +772,13 @@ impl NodeKind {
pub fn only_in_mode(&self) -> Option<TokenMode> {
match self {
Self::Markup(_)
- | Self::Linebreak(_)
+ | Self::Linebreak { .. }
| Self::Text(_)
| Self::NonBreakingSpace
| Self::EnDash
| Self::EmDash
| Self::Ellipsis
- | Self::Quote(_)
+ | Self::Quote { .. }
| Self::Escape(_)
| Self::Strong
| Self::Emph
@@ -867,16 +866,16 @@ impl NodeKind {
Self::Markup(_) => "markup",
Self::Space(2 ..) => "paragraph break",
Self::Space(_) => "space",
- Self::Linebreak(false) => "linebreak",
- Self::Linebreak(true) => "justified linebreak",
+ Self::Linebreak { justified: false } => "linebreak",
+ Self::Linebreak { justified: true } => "justified linebreak",
Self::Text(_) => "text",
Self::NonBreakingSpace => "non-breaking space",
Self::Shy => "soft hyphen",
Self::EnDash => "en dash",
Self::EmDash => "em dash",
Self::Ellipsis => "ellipsis",
- Self::Quote(false) => "single quote",
- Self::Quote(true) => "double quote",
+ Self::Quote { double: false } => "single quote",
+ Self::Quote { double: true } => "double quote",
Self::Escape(_) => "escape sequence",
Self::Strong => "strong content",
Self::Emph => "emphasized content",
@@ -993,14 +992,14 @@ impl Hash for NodeKind {
Self::From => {}
Self::Markup(c) => c.hash(state),
Self::Space(n) => n.hash(state),
- Self::Linebreak(s) => s.hash(state),
+ Self::Linebreak { justified } => justified.hash(state),
Self::Text(s) => s.hash(state),
Self::NonBreakingSpace => {}
Self::Shy => {}
Self::EnDash => {}
Self::EmDash => {}
Self::Ellipsis => {}
- Self::Quote(d) => d.hash(state),
+ Self::Quote { double } => double.hash(state),
Self::Escape(c) => c.hash(state),
Self::Strong => {}
Self::Emph => {}