diff --git a/Cargo.toml b/Cargo.toml index 423f947..28be623 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ resolver = "2" [workspace.dependencies] djls = { path = "crates/djls" } +djls-conf = { path = "crates/djls-conf" } djls-project = { path = "crates/djls-project" } djls-server = { path = "crates/djls-server" } djls-templates = { path = "crates/djls-templates" } diff --git a/crates/djls-conf/Cargo.toml b/crates/djls-conf/Cargo.toml new file mode 100644 index 0000000..95aa51f --- /dev/null +++ b/crates/djls-conf/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "djls-conf" +version = "0.0.0" +edition = "2021" + +[dependencies] +djls-templates = { workspace = true } + +anyhow = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } + +config = { version = "0.15", features = ["toml"] } + +[dev-dependencies] +tempfile = "3.19" diff --git a/crates/djls-conf/src/lib.rs b/crates/djls-conf/src/lib.rs new file mode 100644 index 0000000..c8f077d --- /dev/null +++ b/crates/djls-conf/src/lib.rs @@ -0,0 +1,12 @@ +use config::{Config, File, FileFormat, Value}; +use djls_templates::TagSpecs; +use serde::Deserialize; +use thiserror::Error; + +#[derive(Deserialize, Debug, Default, Clone)] +pub struct Settings { + #[serde(default)] + pub debug: bool, + #[serde(default)] + pub tagspecs: TagSpecs, +} diff --git a/crates/djls-templates/src/error.rs b/crates/djls-templates/src/error.rs index dee782e..d954e80 100644 --- a/crates/djls-templates/src/error.rs +++ b/crates/djls-templates/src/error.rs @@ -1,5 +1,5 @@ -use crate::ast::{AstError, Span}; use crate::lexer::LexerError; +use crate::nodes::{AstError, Span}; use crate::parser::ParserError; use serde::Serialize; use thiserror::Error; diff --git a/crates/djls-templates/src/lib.rs b/crates/djls-templates/src/lib.rs index d26ab8d..2e53157 100644 --- a/crates/djls-templates/src/lib.rs +++ b/crates/djls-templates/src/lib.rs @@ -1,15 +1,15 @@ -mod ast; mod error; mod lexer; +mod nodes; mod parser; mod tagspecs; mod tokens; -use ast::Ast; pub use error::{to_lsp_diagnostic, QuickFix, TemplateError}; - use lexer::Lexer; +use nodes::NodeList; pub use parser::{Parser, ParserError}; +pub use tagspecs::TagSpecs; /// Parses a Django template and returns the AST and any parsing errors. /// @@ -18,7 +18,7 @@ pub use parser::{Parser, ParserError}; /// /// Returns a `Result` containing a tuple of `(Ast, Vec)` on success, /// or a `ParserError` on failure. -pub fn parse_template(source: &str) -> Result<(Ast, Vec), TemplateError> { +pub fn parse_template(source: &str) -> Result<(NodeList, Vec), TemplateError> { let tokens = Lexer::new(source) .tokenize() .map_err(|e| TemplateError::Lexer(e.to_string()))?; diff --git a/crates/djls-templates/src/ast.rs b/crates/djls-templates/src/nodes.rs similarity index 91% rename from crates/djls-templates/src/ast.rs rename to crates/djls-templates/src/nodes.rs index e520162..be0c1c0 100644 --- a/crates/djls-templates/src/ast.rs +++ b/crates/djls-templates/src/nodes.rs @@ -3,14 +3,14 @@ use serde::Serialize; use thiserror::Error; #[derive(Clone, Debug, Default, Serialize)] -pub struct Ast { - nodelist: Vec, +pub struct NodeList { + nodes: Vec, line_offsets: LineOffsets, } -impl Ast { - pub fn nodelist(&self) -> &Vec { - &self.nodelist +impl NodeList { + pub fn nodes(&self) -> &Vec { + &self.nodes } pub fn line_offsets(&self) -> &LineOffsets { @@ -18,18 +18,11 @@ impl Ast { } pub fn add_node(&mut self, node: Node) { - self.nodelist.push(node); + self.nodes.push(node); } pub fn set_line_offsets(&mut self, tokens: &TokenStream) { - for token in tokens.tokens() { - if let TokenType::Newline = token.token_type() { - if let Some(start) = token.start() { - // Add offset for next line - self.line_offsets.add_line(start + 1); - } - } - } + self.line_offsets = LineOffsets::from_tokens(tokens) } } @@ -37,8 +30,20 @@ impl Ast { pub struct LineOffsets(pub Vec); impl LineOffsets { + pub fn from_tokens(tokens: &TokenStream) -> Self { + let mut offsets = LineOffsets::default(); + for token in tokens.tokens() { + if let TokenType::Newline = token.token_type() { + if let Some(start) = token.start() { + offsets.add_line(start + token.length().unwrap_or(1)); + } + } + } + offsets + } + pub fn add_line(&mut self, offset: u32) { - self.0.push(offset); + self.0.push(offset) } pub fn position_to_line_col(&self, position: usize) -> (usize, usize) { @@ -188,7 +193,7 @@ mod tests { assert!(errors.is_empty()); // Find the variable node - let nodes = nodelist.nodelist(); + let nodes = nodelist.nodes(); let var_node = nodes .iter() .find(|n| matches!(n, Node::Variable { .. })) diff --git a/crates/djls-templates/src/parser.rs b/crates/djls-templates/src/parser.rs index d3b0de9..bdac0a7 100644 --- a/crates/djls-templates/src/parser.rs +++ b/crates/djls-templates/src/parser.rs @@ -1,5 +1,5 @@ -use crate::ast::{Ast, AstError, Node, Span}; use crate::lexer::LexerError; +use crate::nodes::{AstError, Node, NodeList, Span}; use crate::tokens::{Token, TokenStream, TokenType}; use thiserror::Error; @@ -18,8 +18,8 @@ impl Parser { } } - pub fn parse(&mut self) -> Result<(Ast, Vec), ParserError> { - let mut ast = Ast::default(); + pub fn parse(&mut self) -> Result<(NodeList, Vec), ParserError> { + let mut ast = NodeList::default(); ast.set_line_offsets(&self.tokens); while !self.is_at_end() { diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__comments__parse_comments.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__comments__parse_comments.snap index ed0adee..806ae62 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__comments__parse_comments.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__comments__parse_comments.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Text: content: "" span: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_complex_if_elif.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_complex_if_elif.snap index 8afdc7f..26f4ded 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_complex_if_elif.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_complex_if_elif.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Tag: name: if bits: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_for_block.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_for_block.snap index d224789..e3bcc11 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_for_block.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_for_block.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Tag: name: for bits: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_if_block.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_if_block.snap index 070234a..adc46f4 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_if_block.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_if_block.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Tag: name: if bits: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_tag_assignment.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_tag_assignment.snap index b7d3f29..0396ec7 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_tag_assignment.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_tag_assignment.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Tag: name: url bits: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable.snap index d206de8..b4d78a5 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Variable: var: user.name filters: [] diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable_with_filter.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable_with_filter.snap index 095960f..9efd361 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable_with_filter.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_django_variable_with_filter.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Variable: var: user.name filters: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_filter_chains.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_filter_chains.snap index 96e25ff..761c920 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_filter_chains.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_filter_chains.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Variable: var: value filters: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_mixed_content.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_mixed_content.snap index f28f551..15b8763 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_mixed_content.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_mixed_content.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Text: content: "Welcome," span: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_nested_for_if.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_nested_for_if.snap index 27962ae..b014d61 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_nested_for_if.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__django__parse_nested_for_if.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Tag: name: for bits: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_error_recovery.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_error_recovery.snap index 97efc3e..693b6a7 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_error_recovery.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_error_recovery.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Text: content: "
" span: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_for.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_for.snap index f740db4..e24a91e 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_for.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_for.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Tag: name: for bits: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_if.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_if.snap index 27d4f59..b40327e 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_if.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_django_if.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Tag: name: if bits: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_html_tag.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_html_tag.snap index 4484ac1..64396ca 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_html_tag.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_html_tag.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Text: content: "
" span: diff --git a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_script.snap b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_script.snap index 40eff31..f0b7a30 100644 --- a/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_script.snap +++ b/crates/djls-templates/src/snapshots/djls_templates__parser__tests__errors__parse_unclosed_script.snap @@ -2,7 +2,7 @@ source: crates/djls-templates/src/parser.rs expression: nodelist --- -nodelist: +nodes: - Text: content: "