chore: Update .gitignore and refactor diagnostics and parser modules

This commit is contained in:
Josh Thomas 2025-01-07 14:45:07 -06:00 committed by Josh (aider)
parent b9ace15050
commit c4b6bafc49
5 changed files with 19 additions and 17 deletions

1
.gitignore vendored
View file

@ -192,3 +192,4 @@ Cargo.lock
# mkdocs # mkdocs
site/ site/
.aider*

View file

@ -1,17 +1,16 @@
use tower_lsp::lsp_types::*;
use crate::documents::TextDocument; use crate::documents::TextDocument;
use djls_template_ast::ast::{LineOffsets, Node, Span};
use djls_template_ast::{Lexer, Parser}; use djls_template_ast::{Lexer, Parser};
use djls_template_ast::ast::{Ast, Node, LineOffsets, Span}; use tower_lsp::lsp_types::*;
use djls_template_ast::tokens::TokenStream;
pub struct Diagnostics; pub struct Diagnostics;
impl Diagnostics { impl Diagnostics {
pub fn generate_for_document(document: &TextDocument) -> Vec<Diagnostic> { pub fn generate_for_document(document: &TextDocument) -> Vec<Diagnostic> {
let mut diagnostics = Vec::new(); let mut diagnostics = Vec::new();
let text = document.get_text(); let text = document.get_text();
let lexer = Lexer::new(text); let mut lexer = Lexer::new(text);
let token_stream = match lexer.tokenize() { let token_stream = match lexer.tokenize() {
Ok(tokens) => tokens, Ok(tokens) => tokens,
Err(e) => { Err(e) => {
@ -60,7 +59,7 @@ impl Diagnostics {
if let Some(closing) = block.closing() { if let Some(closing) = block.closing() {
} else { } else {
let span = block.tag().span; let span = block.tag().span;
let range = get_range_from_span(&ast.line_offsets(), &span); let range = get_range_from_span(ast.line_offsets(), &span);
diagnostics.push(Diagnostic { diagnostics.push(Diagnostic {
range, range,
severity: Some(DiagnosticSeverity::ERROR), severity: Some(DiagnosticSeverity::ERROR),
@ -70,9 +69,9 @@ impl Diagnostics {
..Default::default() ..Default::default()
}); });
} }
}, }
Node::Variable { .. } => {}, Node::Variable { .. } => {}
_ => {}, _ => {}
} }
} }
@ -81,8 +80,9 @@ impl Diagnostics {
} }
fn get_range_from_span(line_offsets: &LineOffsets, span: &Span) -> Range { fn get_range_from_span(line_offsets: &LineOffsets, span: &Span) -> Range {
let (start_line, start_col) = line_offsets.position_to_line_col(span.start as usize); let (start_line, start_col) = line_offsets.position_to_line_col(span.start() as usize);
let (end_line, end_col) = line_offsets.position_to_line_col((span.start + span.length) as usize); let (end_line, end_col) =
line_offsets.position_to_line_col((span.start() + span.length()) as usize);
Range { Range {
start: Position::new(start_line as u32 - 1, start_col as u32), start: Position::new(start_line as u32 - 1, start_col as u32),

View file

@ -1,3 +1,4 @@
mod diagnostics;
mod documents; mod documents;
mod server; mod server;
mod tasks; mod tasks;

View file

@ -1,8 +1,8 @@
mod ast; pub mod ast;
mod lexer; pub mod lexer;
mod parser; pub mod parser;
mod tagspecs; mod tagspecs;
mod tokens; pub mod tokens;
pub use ast::Ast; pub use lexer::Lexer;
pub use parser::Parser; pub use parser::Parser;

View file

@ -92,7 +92,7 @@ impl Parser {
let tag_name = bits.first().ok_or(ParserError::EmptyTag)?.clone(); let tag_name = bits.first().ok_or(ParserError::EmptyTag)?.clone();
let span = Span::from(token); let span = Span::from(token);
let tag_span = Span::new(*span.start(), tag_name.len() as u32); let tag_span = Span::new(span.start(), tag_name.len() as u32);
let assignment = if bits.len() >= 2 { let assignment = if bits.len() >= 2 {
let second_to_last_index = bits.len() - 2; let second_to_last_index = bits.len() - 2;