Skip to content

Commit 2e6083f

Browse files
more cleaning!
1 parent 9f33771 commit 2e6083f

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

crates/djls-source/src/position.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,28 @@ impl Span {
3333
}
3434

3535
#[must_use]
36-
pub fn expand(self, opening: u32, closing: u32) -> Self {
37-
let start_expand = self.start.saturating_sub(opening);
38-
let length_expand = opening + self.length + closing;
39-
Self::new(start_expand, length_expand)
36+
pub fn from_parts(start: usize, length: usize) -> Self {
37+
let start_u32 = u32::try_from(start).unwrap_or(u32::MAX);
38+
let length_u32 = u32::try_from(length).unwrap_or(u32::MAX.saturating_sub(start_u32));
39+
Span::new(start_u32, length_u32)
40+
}
41+
42+
#[must_use]
43+
pub fn with_length_usize(self, length: usize) -> Self {
44+
Self::from_parts(self.start as usize, length)
4045
}
4146

4247
/// Construct a span from integer bounds expressed as byte offsets.
4348
#[must_use]
4449
pub fn from_bounds(start: usize, end: usize) -> Self {
45-
let start_u32 = u32::try_from(start).unwrap_or(u32::MAX);
46-
let end_u32 = u32::try_from(end).unwrap_or(u32::MAX);
47-
let length_u32 = end_u32.saturating_sub(start_u32);
48-
Self::new(start_u32, length_u32)
50+
Self::from_parts(start, end.saturating_sub(start))
4951
}
5052

5153
#[must_use]
52-
pub fn from_parts(start: usize, len: usize) -> Self {
53-
let start_u32 = u32::try_from(start).unwrap_or(u32::MAX);
54-
let length_u32 = u32::try_from(len).unwrap_or(u32::MAX.saturating_sub(start_u32));
55-
Span::new(start_u32, length_u32)
54+
pub fn expand(self, opening: u32, closing: u32) -> Self {
55+
let start_expand = self.start.saturating_sub(opening);
56+
let length_expand = opening + self.length + closing;
57+
Self::new(start_expand, length_expand)
5658
}
5759

5860
#[must_use]

crates/djls-templates/src/lexer.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ impl<'db> Lexer<'db> {
3131

3232
let token =
3333
match self.peek() {
34-
'{' => {
35-
let remaining = &self.source[self.current..];
34+
TagDelimiter::CHAR_OPEN => {
35+
let remaining = self.remaining_source();
3636

3737
match TagDelimiter::from_input(remaining) {
3838
Some(TagDelimiter::Block) => self
@@ -68,6 +68,7 @@ impl<'db> Lexer<'db> {
6868
token_fn: impl FnOnce(TokenContent<'db>, Span) -> Token<'db>,
6969
) -> Token<'db> {
7070
let content_start = self.start + TagDelimiter::LENGTH;
71+
7172
self.consume_n(TagDelimiter::LENGTH);
7273

7374
match self.consume_until(delimiter.closer()) {
@@ -116,10 +117,11 @@ impl<'db> Lexer<'db> {
116117
let text_start = self.current;
117118

118119
while !self.is_at_end() {
119-
let slice = &self.source[self.current..];
120-
if (self.peek() == '{' && TagDelimiter::from_input(slice).is_some())
121-
|| slice.starts_with('\n')
122-
|| slice.starts_with('\r')
120+
let remaining = self.remaining_source();
121+
if (self.peek() == TagDelimiter::CHAR_OPEN
122+
&& TagDelimiter::from_input(remaining).is_some())
123+
|| remaining.starts_with('\n')
124+
|| remaining.starts_with('\r')
123125
{
124126
break;
125127
}
@@ -134,7 +136,12 @@ impl<'db> Lexer<'db> {
134136

135137
#[inline]
136138
fn peek(&self) -> char {
137-
self.source[self.current..].chars().next().unwrap_or('\0')
139+
self.remaining_source().chars().next().unwrap_or('\0')
140+
}
141+
142+
#[inline]
143+
fn remaining_source(&self) -> &str {
144+
&self.source[self.current..]
138145
}
139146

140147
#[inline]
@@ -144,7 +151,7 @@ impl<'db> Lexer<'db> {
144151

145152
#[inline]
146153
fn consume(&mut self) {
147-
if let Some(ch) = self.source[self.current..].chars().next() {
154+
if let Some(ch) = self.remaining_source().chars().next() {
148155
self.current += ch.len_utf8();
149156
}
150157
}
@@ -160,18 +167,17 @@ impl<'db> Lexer<'db> {
160167
let mut fallback: Option<usize> = None;
161168

162169
while self.current < self.source.len() {
163-
let slice = &self.source[self.current..];
164-
if slice.starts_with(delimiter) {
165-
return Ok(self.source[offset..self.current].to_string());
166-
}
170+
let remaining = self.remaining_source();
167171

168-
if fallback.is_none() && TagDelimiter::from_input(slice).is_some() {
169-
fallback = Some(self.current);
172+
if remaining.starts_with(delimiter) {
173+
return Ok(self.source[offset..self.current].to_string());
170174
}
171175

172-
let ch = self.peek();
173-
if fallback.is_none() && matches!(ch, '\n' | '\r') {
174-
fallback = Some(self.current);
176+
if fallback.is_none() {
177+
let ch = self.peek();
178+
if TagDelimiter::from_input(remaining).is_some() || matches!(ch, '\n' | '\r') {
179+
fallback = Some(self.current);
180+
}
175181
}
176182

177183
self.consume();

crates/djls-templates/src/nodelist.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,11 @@ impl<'db> Node<'db> {
6464
match self {
6565
Node::Tag { name, span, .. } => {
6666
// Just the tag name (e.g., "if" in "{% if user.is_authenticated %}")
67-
let name_len = name.text(db).len();
68-
Some(Span {
69-
start: span.start,
70-
length: u32::try_from(name_len).unwrap_or(0),
71-
})
67+
Some(span.with_length_usize(name.text(db).len()))
7268
}
7369
Node::Variable { var, span, .. } => {
7470
// Just the variable name (e.g., "user" in "{{ user.name|title }}")
75-
let var_len = var.text(db).len();
76-
Some(Span {
77-
start: span.start,
78-
length: u32::try_from(var_len).unwrap_or(0),
79-
})
71+
Some(span.with_length_usize(var.text(db).len()))
8072
}
8173
Node::Comment { .. } | Node::Text { .. } | Node::Error { .. } => None,
8274
}

crates/djls-templates/src/tokens.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub enum TagDelimiter {
1010
}
1111

1212
impl TagDelimiter {
13+
pub const CHAR_OPEN: char = '{';
1314
pub const LENGTH: usize = 2;
1415
pub const LENGTH_U32: u32 = 2;
1516

@@ -100,9 +101,24 @@ impl<'db> Token<'db> {
100101
/// Get the lexeme as it appears in source
101102
pub fn lexeme(&self, db: &'db dyn TemplateDb) -> String {
102103
match self {
103-
Token::Block { content, .. } => format!("{{% {} %}}", content.text(db)),
104-
Token::Variable { content, .. } => format!("{{{{ {} }}}}", content.text(db)),
105-
Token::Comment { content, .. } => format!("{{# {} #}}", content.text(db)),
104+
Token::Block { content, .. } => format!(
105+
"{} {} {}",
106+
TagDelimiter::Block.opener(),
107+
content.text(db),
108+
TagDelimiter::Block.closer()
109+
),
110+
Token::Variable { content, .. } => format!(
111+
"{} {} {}",
112+
TagDelimiter::Variable.opener(),
113+
content.text(db),
114+
TagDelimiter::Variable.closer()
115+
),
116+
Token::Comment { content, .. } => format!(
117+
"{} {} {}",
118+
TagDelimiter::Comment.opener(),
119+
content.text(db),
120+
TagDelimiter::Comment.closer()
121+
),
106122
Token::Text { content, .. } | Token::Error { content, .. } => content.text(db).clone(),
107123
Token::Whitespace { span, .. } => " ".repeat(span.length as usize),
108124
Token::Newline { span, .. } => {

0 commit comments

Comments
 (0)