Skip to content

Commit 725a603

Browse files
working
1 parent c559bae commit 725a603

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

crates/djls-template-ast/src/ast.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ impl LineOffsets {
4747
pub fn position_to_line_col(&self, position: usize) -> (usize, usize) {
4848
let position = position as u32;
4949
let line = match self.0.binary_search(&position) {
50-
Ok(_) => self.0.partition_point(|&x| x <= position),
51-
Err(i) => i,
50+
Ok(exact_line) => exact_line, // Position is at start of this line
51+
Err(0) => 0, // Before first line start
52+
Err(next_line) => next_line - 1, // We're on the previous line
5253
};
54+
55+
// Calculate column as offset from line start
5356
let col = if line == 0 {
5457
position as usize
5558
} else {
56-
(position - self.0[line - 1]) as usize
59+
(position - self.0[line]) as usize
5760
};
61+
62+
// Convert to 1-based line number
5863
(line + 1, col)
5964
}
6065

@@ -348,7 +353,7 @@ mod tests {
348353
eprintln!("Line offsets: {:?}", ast.line_offsets());
349354
eprintln!("Span: {:?}", span);
350355
let (line, col) = ast.line_offsets().position_to_line_col(span.start as usize);
351-
assert_eq!((line, col), (1, 0), "Content should be on line 1, col 0");
356+
assert_eq!((line, col), (2, 0), "Content should be on line 2, col 0");
352357

353358
// Check closing tag
354359
if let Block::Closing { tag } =

crates/djls-template-ast/src/parser.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ impl Parser {
2323
let mut line_offsets = LineOffsets::new();
2424

2525
// First pass: collect line offsets
26+
let mut current_line_start = 0;
2627
for token in self.tokens.tokens() {
2728
if let TokenType::Newline = token.token_type() {
2829
if let Some(start) = token.start() {
29-
line_offsets.add_line(start + 1);
30+
// Add offset for next line
31+
current_line_start = start + 1;
32+
line_offsets.add_line(current_line_start);
3033
}
3134
}
3235
}
@@ -157,9 +160,11 @@ impl Parser {
157160
if !found_closing {
158161
// Push the last branch if we didn't find a closing tag
159162
nodes.push(Node::Block(Block::Branch {
160-
tag: branch_tag,
161-
nodes: branch_nodes,
163+
tag: branch_tag.clone(),
164+
nodes: branch_nodes.clone(),
162165
}));
166+
// Add error for unclosed tag
167+
self.errors.push(ParserError::Ast(AstError::UnclosedTag(tag_name.clone())));
163168
}
164169
if found_closing {
165170
break;
@@ -192,6 +197,13 @@ impl Parser {
192197
}
193198
};
194199

200+
// Add error if we didn't find a closing tag for a block
201+
if let Block::Block { closing: None, tag: tag_ref, .. } = &block {
202+
if let Some(expected_closing) = &spec.closing {
203+
self.errors.push(ParserError::Ast(AstError::UnclosedTag(tag_ref.name.clone())));
204+
}
205+
}
206+
195207
Ok(Node::Block(block))
196208
}
197209

0 commit comments

Comments
 (0)