mirror of
https://github.com/joshuadavidthomas/django-language-server.git
synced 2025-09-12 05:16:46 +00:00
add closing tag and adjust branching strat
This commit is contained in:
parent
0ea2dea1a9
commit
ec05c62c2a
11 changed files with 213 additions and 215 deletions
|
@ -63,8 +63,8 @@ pub enum TagNode {
|
|||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct DjangoFilter {
|
||||
pub name: String,
|
||||
pub arguments: Vec<String>,
|
||||
name: String,
|
||||
arguments: Vec<String>,
|
||||
}
|
||||
|
||||
impl DjangoFilter {
|
||||
|
|
|
@ -155,22 +155,28 @@ impl Parser {
|
|||
|
||||
let specs = TagSpec::load_builtin_specs().unwrap_or_default();
|
||||
|
||||
// Check if closing or intermediate tag according to any spec
|
||||
// Check if this is a closing tag
|
||||
for (_, spec) in specs.iter() {
|
||||
if Some(&tag_name) == spec.closing.as_ref() {
|
||||
let node = Node::Django(DjangoNode::Tag(TagNode::Closing {
|
||||
name: tag_name.clone(),
|
||||
bits: bits[1..].to_vec(),
|
||||
}));
|
||||
return Err(ParserError::ErrorSignal(Signal::SpecialTag(tag_name)));
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this is a branch tag according to any spec
|
||||
for (_, spec) in specs.iter() {
|
||||
if let Some(intermediates) = &spec.intermediates {
|
||||
if intermediates.contains(&tag_name) {
|
||||
if intermediates.iter().any(|i| i.name == tag_name) {
|
||||
return Err(ParserError::ErrorSignal(Signal::SpecialTag(tag_name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let tag_spec = specs.get(tag_name.as_str()).cloned();
|
||||
|
||||
let mut children = Vec::new();
|
||||
let mut branches = Vec::new();
|
||||
|
||||
while !self.is_at_end() {
|
||||
match self.next_node() {
|
||||
|
@ -181,40 +187,44 @@ impl Parser {
|
|||
if let Some(spec) = &tag_spec {
|
||||
// Check if closing tag
|
||||
if Some(&tag) == spec.closing.as_ref() {
|
||||
let tag_node = if !branches.is_empty() {
|
||||
TagNode::Branching {
|
||||
children.push(Node::Django(DjangoNode::Tag(TagNode::Closing {
|
||||
name: tag,
|
||||
bits: vec![],
|
||||
})));
|
||||
return Ok(Node::Django(DjangoNode::Tag(TagNode::Block {
|
||||
name: tag_name,
|
||||
bits,
|
||||
children,
|
||||
branches,
|
||||
}
|
||||
} else {
|
||||
TagNode::Block {
|
||||
name: tag_name,
|
||||
bits,
|
||||
children,
|
||||
}
|
||||
};
|
||||
return Ok(Node::Django(DjangoNode::Tag(tag_node)));
|
||||
})));
|
||||
}
|
||||
// Check if intermediate tag
|
||||
if let Some(intermediates) = &spec.intermediates {
|
||||
if intermediates.contains(&tag) {
|
||||
branches.push(TagNode::Block {
|
||||
name: tag.clone(),
|
||||
bits: vec![tag.clone()],
|
||||
children,
|
||||
});
|
||||
children = Vec::new();
|
||||
if let Some(intermediate) = intermediates.iter().find(|i| i.name == tag) {
|
||||
// Create branch node with the current children
|
||||
let branch_bits = if intermediate.args {
|
||||
match &self.tokens[self.current - 1].token_type() {
|
||||
TokenType::DjangoBlock(content) => content
|
||||
.split_whitespace()
|
||||
.skip(1) // Skip the tag name
|
||||
.map(|s| s.to_string())
|
||||
.collect(),
|
||||
_ => vec![tag.clone()],
|
||||
}
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
children.push(Node::Django(DjangoNode::Tag(TagNode::Branch {
|
||||
name: tag,
|
||||
bits: branch_bits,
|
||||
children: Vec::new(),
|
||||
})));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Err(ParserError::UnexpectedTag(tag));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ expression: ast
|
|||
nodes:
|
||||
- Django:
|
||||
Tag:
|
||||
Branching:
|
||||
Block:
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -13,17 +13,26 @@ nodes:
|
|||
- ">"
|
||||
- "0"
|
||||
children:
|
||||
- Text: Zero
|
||||
branches:
|
||||
- Block:
|
||||
- Text: Positive
|
||||
- Django:
|
||||
Tag:
|
||||
Branch:
|
||||
name: elif
|
||||
bits:
|
||||
- elif
|
||||
children:
|
||||
- Text: Positive
|
||||
- Block:
|
||||
name: else
|
||||
bits:
|
||||
- else
|
||||
children:
|
||||
- x
|
||||
- "<"
|
||||
- "0"
|
||||
children: []
|
||||
- Text: Negative
|
||||
- Django:
|
||||
Tag:
|
||||
Branch:
|
||||
name: else
|
||||
bits: []
|
||||
children: []
|
||||
- Text: Zero
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
|
|
|
@ -5,23 +5,28 @@ expression: ast
|
|||
nodes:
|
||||
- Django:
|
||||
Tag:
|
||||
Branching:
|
||||
Block:
|
||||
name: for
|
||||
bits:
|
||||
- for
|
||||
- item
|
||||
- in
|
||||
- items
|
||||
children:
|
||||
- Text: No items
|
||||
branches:
|
||||
- Block:
|
||||
name: empty
|
||||
bits:
|
||||
- empty
|
||||
children:
|
||||
- Django:
|
||||
Variable:
|
||||
bits:
|
||||
- item
|
||||
filters: []
|
||||
- Django:
|
||||
Tag:
|
||||
Branch:
|
||||
name: empty
|
||||
bits: []
|
||||
children: []
|
||||
- Text: No items
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endfor
|
||||
bits: []
|
||||
|
|
|
@ -12,3 +12,8 @@ nodes:
|
|||
- user.is_authenticated
|
||||
children:
|
||||
- Text: Welcome
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
|
|
|
@ -6,18 +6,11 @@ nodes:
|
|||
- Text: "Welcome, "
|
||||
- Django:
|
||||
Tag:
|
||||
Branching:
|
||||
Block:
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
- user.is_authenticated
|
||||
children:
|
||||
- Text: Guest
|
||||
branches:
|
||||
- Block:
|
||||
name: else
|
||||
bits:
|
||||
- else
|
||||
children:
|
||||
- Django:
|
||||
Variable:
|
||||
|
@ -32,20 +25,13 @@ nodes:
|
|||
- "'Guest'"
|
||||
- Django:
|
||||
Tag:
|
||||
Branching:
|
||||
Block:
|
||||
name: for
|
||||
bits:
|
||||
- for
|
||||
- group
|
||||
- in
|
||||
- user.groups
|
||||
children:
|
||||
- Text: (no groups)
|
||||
branches:
|
||||
- Block:
|
||||
name: empty
|
||||
bits:
|
||||
- empty
|
||||
children:
|
||||
- Django:
|
||||
Tag:
|
||||
|
@ -56,6 +42,11 @@ nodes:
|
|||
- forloop.first
|
||||
children:
|
||||
- Text: (
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
- Django:
|
||||
Variable:
|
||||
bits:
|
||||
|
@ -72,6 +63,11 @@ nodes:
|
|||
- forloop.last
|
||||
children:
|
||||
- Text: ", "
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
- Django:
|
||||
Tag:
|
||||
Block:
|
||||
|
@ -81,4 +77,33 @@ nodes:
|
|||
- forloop.last
|
||||
children:
|
||||
- Text: )
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
- Django:
|
||||
Tag:
|
||||
Branch:
|
||||
name: empty
|
||||
bits: []
|
||||
children: []
|
||||
- Text: (no groups)
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endfor
|
||||
bits: []
|
||||
- Django:
|
||||
Tag:
|
||||
Branch:
|
||||
name: else
|
||||
bits: []
|
||||
children: []
|
||||
- Text: Guest
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
- Text: "!"
|
||||
|
|
|
@ -27,3 +27,13 @@ nodes:
|
|||
- item
|
||||
- name
|
||||
filters: []
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endfor
|
||||
bits: []
|
||||
|
|
|
@ -94,7 +94,7 @@ nodes:
|
|||
- Text: "!"
|
||||
- Django:
|
||||
Tag:
|
||||
Branching:
|
||||
Block:
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -105,16 +105,26 @@ nodes:
|
|||
tag_name: span
|
||||
attributes: {}
|
||||
children:
|
||||
- Text: User
|
||||
branches:
|
||||
- Block:
|
||||
- Text: Admin
|
||||
- Django:
|
||||
Tag:
|
||||
Branch:
|
||||
name: else
|
||||
bits:
|
||||
- else
|
||||
children:
|
||||
bits: []
|
||||
children: []
|
||||
- Html:
|
||||
Element:
|
||||
tag_name: span
|
||||
attributes: {}
|
||||
children:
|
||||
- Text: Admin
|
||||
- Text: User
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
- Django:
|
||||
Tag:
|
||||
Closing:
|
||||
name: endif
|
||||
bits: []
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
---
|
||||
source: crates/djls-template-ast/src/parser.rs
|
||||
expression: ast
|
||||
---
|
||||
nodes:
|
||||
- Html:
|
||||
Doctype: "!DOCTYPE"
|
||||
- Html:
|
||||
Element:
|
||||
tag_name: html
|
||||
attributes: {}
|
||||
children:
|
||||
- Html:
|
||||
Element:
|
||||
tag_name: head
|
||||
attributes: {}
|
||||
children:
|
||||
- Html:
|
||||
Element:
|
||||
tag_name: title
|
||||
attributes: {}
|
||||
children:
|
||||
- Text: Test
|
||||
- Html:
|
||||
Element:
|
||||
tag_name: body
|
||||
attributes: {}
|
||||
children:
|
||||
- Html:
|
||||
Element:
|
||||
tag_name: h1
|
||||
attributes: {}
|
||||
children:
|
||||
- Text: Hello World
|
||||
- Html:
|
||||
Element:
|
||||
tag_name: p
|
||||
attributes: {}
|
||||
children:
|
||||
- Text: This is a test
|
||||
- Script:
|
||||
Element:
|
||||
attributes: {}
|
||||
children:
|
||||
- Text: "console.log(\"Hello World\");"
|
||||
- Style:
|
||||
Element:
|
||||
attributes:
|
||||
style: Boolean
|
||||
children:
|
||||
- Text: "h1 "
|
||||
- Text: "{"
|
||||
- Text: "color: red; }"
|
|
@ -1,10 +0,0 @@
|
|||
---
|
||||
source: crates/djls-template-ast/src/parser.rs
|
||||
expression: ast
|
||||
---
|
||||
nodes:
|
||||
- Script:
|
||||
Element:
|
||||
attributes: {}
|
||||
children:
|
||||
- Text: "console.log(\"Hello World\");"
|
|
@ -1,13 +0,0 @@
|
|||
---
|
||||
source: crates/djls-template-ast/src/parser.rs
|
||||
expression: ast
|
||||
---
|
||||
nodes:
|
||||
- Style:
|
||||
Element:
|
||||
attributes:
|
||||
style: Boolean
|
||||
children:
|
||||
- Text: "h1 "
|
||||
- Text: "{"
|
||||
- Text: "color: red; }"
|
Loading…
Add table
Add a link
Reference in a new issue