This commit is contained in:
Josh Thomas 2025-04-29 13:09:43 -05:00
parent 39862b2d63
commit 5dbffd4494
35 changed files with 121 additions and 143 deletions

View file

@ -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" }

View file

@ -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"

View file

@ -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,
}

View file

@ -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;

View file

@ -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<ParserError>)` on success,
/// or a `ParserError` on failure.
pub fn parse_template(source: &str) -> Result<(Ast, Vec<TemplateError>), TemplateError> {
pub fn parse_template(source: &str) -> Result<(NodeList, Vec<TemplateError>), TemplateError> {
let tokens = Lexer::new(source)
.tokenize()
.map_err(|e| TemplateError::Lexer(e.to_string()))?;

View file

@ -3,14 +3,14 @@ use serde::Serialize;
use thiserror::Error;
#[derive(Clone, Debug, Default, Serialize)]
pub struct Ast {
nodelist: Vec<Node>,
pub struct NodeList {
nodes: Vec<Node>,
line_offsets: LineOffsets,
}
impl Ast {
pub fn nodelist(&self) -> &Vec<Node> {
&self.nodelist
impl NodeList {
pub fn nodes(&self) -> &Vec<Node> {
&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<u32>);
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 { .. }))

View file

@ -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>), ParserError> {
let mut ast = Ast::default();
pub fn parse(&mut self) -> Result<(NodeList, Vec<ParserError>), ParserError> {
let mut ast = NodeList::default();
ast.set_line_offsets(&self.tokens);
while !self.is_at_end() {

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<!-- HTML comment -->"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Tag:
name: if
bits:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Tag:
name: for
bits:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Tag:
name: if
bits:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Tag:
name: url
bits:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Variable:
var: user.name
filters: []

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Variable:
var: user.name
filters:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Variable:
var: value
filters:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "Welcome,"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Tag:
name: for
bits:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<div class=\"container\">"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Tag:
name: for
bits:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Tag:
name: if
bits:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<div>"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<script>console.log('test');"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<style>body { color: blue;"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<!DOCTYPE html>"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<!DOCTYPE html>"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<div class=\"container\">Hello</div>"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<input type=\"text\" />"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<script type=\"text/javascript\">"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: "<style type=\"text/css\">"
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: hello
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: hello
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: hello
span:

View file

@ -2,7 +2,7 @@
source: crates/djls-templates/src/parser.rs
expression: nodelist
---
nodelist:
nodes:
- Text:
content: hello
span:

View file

@ -1,6 +1,5 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf}; // Add PathBuf
@ -226,7 +225,8 @@ fn extract_specs(
if !current_path.is_empty() {
match TagSpec::deserialize(current_value.clone()) {
Ok(tag_spec) => {
if let Some(tag_name) = current_path.split('.').last().filter(|s| !s.is_empty()) {
if let Some(tag_name) = current_path.split('.').last().filter(|s| !s.is_empty())
{
specs_map.insert(tag_name.to_string(), tag_spec);
} else {
return Err(format!(
@ -296,7 +296,7 @@ mod tests {
// Renamed from test_can_load_builtins
let content = r#"
# Using dotted path table names under [tagspecs] base
[tagspecs.django.template.defaulttags.if] // Corrected path
[tagspecs.django.template.defaulttags.if]
end = { tag = "endif" }
[tagspecs.django.template.defaulttags.block]
end = { tag = "endblock" }

View file

@ -1,104 +1,48 @@
[django.template.defaulttags.autoescape]
args = [{ name = "setting", required = true, allowed_values = ["on", "off"] }]
closing = "endautoescape"
type = "container"
[tagspecs.django.template.defaulttags.autoescape]
end = { tag = "endautoescape" }
[django.template.defaulttags.block]
closing = "endblock"
type = "container"
[tagspecs.django.template.defaulttags.block]
end = { tag = "endblock" }
[django.template.defaulttags.comment]
closing = "endcomment"
type = "container"
[tagspecs.django.template.defaulttags.comment]
end = { tag = "endcomment" }
[django.template.defaulttags.cycle]
args = [
{ name = "cyclevars", required = true },
{ name = "variable_name", required = false, is_kwarg = true },
]
type = "single"
[tagspecs.django.template.defaulttags.filter]
end = { tag = "endfilter" }
[django.template.defaulttags.debug]
type = "single"
[tagspecs.django.template.defaulttags.for]
end = { tag = "endfor" }
intermediates = [ "empty" ]
[django.template.defaulttags.extends]
args = [{ name = "parent_name", required = true }]
type = "inclusion"
[tagspecs.django.template.defaulttags.if]
end = { tag = "endif" }
intermediates = [ "elif", "else" ]
[django.template.defaulttags.for]
args = [
{ name = "{item}", required = true },
{ name = "in", required = true },
{ name = "{iterable}", required = true },
]
branches = ["empty"]
closing = "endfor"
type = "container"
[tagspecs.django.template.defaulttags.ifchanged]
end = { tag = "endifchanged" }
intermediates = [ "else" ]
[django.template.defaulttags.filter]
args = [{ name = "filter_expr", required = true }]
closing = "endfilter"
type = "container"
[tagspecs.django.template.defaulttags.spaceless]
end = { tag = "endspaceless" }
[django.template.defaulttags.firstof]
args = [{ name = "variables", required = true }]
type = "single"
[tagspecs.django.template.defaulttags.verbatim]
end = { tag = "endverbatim" }
[django.template.defaulttags.if]
args = [{ name = "condition", required = true }]
branches = ["elif", "else"]
closing = "endif"
type = "container"
[tagspecs.django.template.defaulttags.with]
end = { tag = "endwith" }
[django.template.defaulttags.include]
args = [
{ name = "template", required = true },
{ name = "with", required = false, is_kwarg = true },
{ name = "only", required = false, is_kwarg = true },
]
type = "inclusion"
[tagspecs.django.templatetags.cache.cache]
end = { tag = "endcache" }
[django.template.defaulttags.load]
args = [{ name = "library", required = true }]
type = "single"
[tagspecs.django.templatetags.i10n.localize]
end = { tag = "endlocalize" }
[django.template.defaulttags.now]
args = [{ name = "format_string", required = true }]
type = "single"
[tagspecs.django.templatetags.i18n.blocktranslate]
end = { tag = "endblocktranslate" }
intermediates = [ "plural" ]
[django.template.defaulttags.spaceless]
closing = "endspaceless"
type = "container"
[tagspecs.django.templatetags.tz.localtime]
end = { tag = "endlocaltime" }
[django.template.defaulttags.templatetag]
type = "single"
[[django.template.defaulttags.templatetag.args]]
allowed_values = [
"openblock",
"closeblock",
"openvariable",
"closevariable",
"openbrace",
"closebrace",
"opencomment",
"closecomment",
]
name = "tagtype"
required = true
[django.template.defaulttags.url]
args = [
{ name = "view_name", required = true },
{ name = "asvar", required = false, is_kwarg = true },
]
type = "single"
[django.template.defaulttags.verbatim]
closing = "endverbatim"
type = "container"
[django.template.defaulttags.with]
args = [{ name = "extra_context", required = true }]
closing = "endwith"
type = "container"
[tagspecs.django.templatetags.tz.timezone]
end = { tag = "endtimezone" }