mirror of
https://github.com/joshuadavidthomas/django-language-server.git
synced 2025-09-11 12:56:46 +00:00
simplify
This commit is contained in:
parent
0b8ca4061c
commit
660fc64b78
11 changed files with 326 additions and 313 deletions
|
@ -36,7 +36,12 @@ impl Ast {
|
|||
pub enum Node {
|
||||
Text(String),
|
||||
Comment(String),
|
||||
Block(BlockNode),
|
||||
Block {
|
||||
block_type: BlockType,
|
||||
name: String,
|
||||
bits: Vec<String>,
|
||||
children: Option<Vec<Node>>,
|
||||
},
|
||||
Variable {
|
||||
bits: Vec<String>,
|
||||
filters: Vec<DjangoFilter>,
|
||||
|
@ -44,21 +49,10 @@ pub enum Node {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub enum BlockNode {
|
||||
Standard {
|
||||
name: String,
|
||||
bits: Vec<String>,
|
||||
children: Vec<Node>,
|
||||
},
|
||||
Branch {
|
||||
name: String,
|
||||
bits: Vec<String>,
|
||||
children: Vec<Node>,
|
||||
},
|
||||
Closing {
|
||||
name: String,
|
||||
bits: Vec<String>,
|
||||
},
|
||||
pub enum BlockType {
|
||||
Standard,
|
||||
Branch,
|
||||
Closing,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::ast::{Ast, AstError, BlockNode, DjangoFilter, Node};
|
||||
use crate::ast::{Ast, AstError, BlockType, DjangoFilter, Node};
|
||||
use crate::tagspecs::TagSpec;
|
||||
use crate::tokens::{Token, TokenStream, TokenType};
|
||||
use thiserror::Error;
|
||||
|
@ -151,16 +151,19 @@ impl Parser {
|
|||
if spec.closing.as_deref() == Some(&tag) {
|
||||
// If we have a current branch, add it to children
|
||||
if let Some((name, bits, branch_children)) = current_branch {
|
||||
children.push(Node::Block(BlockNode::Branch {
|
||||
children.push(Node::Block {
|
||||
block_type: BlockType::Branch,
|
||||
name,
|
||||
bits,
|
||||
children: branch_children,
|
||||
}));
|
||||
children: Some(branch_children),
|
||||
});
|
||||
}
|
||||
children.push(Node::Block(BlockNode::Closing {
|
||||
children.push(Node::Block {
|
||||
block_type: BlockType::Closing,
|
||||
name: tag,
|
||||
bits: vec![],
|
||||
}));
|
||||
children: None,
|
||||
});
|
||||
found_closing_tag = true;
|
||||
break;
|
||||
}
|
||||
|
@ -169,11 +172,12 @@ impl Parser {
|
|||
if let Some(branch) = branches.iter().find(|i| i.name == tag) {
|
||||
// If we have a current branch, add it to children
|
||||
if let Some((name, bits, branch_children)) = current_branch {
|
||||
children.push(Node::Block(BlockNode::Branch {
|
||||
children.push(Node::Block {
|
||||
block_type: BlockType::Branch,
|
||||
name,
|
||||
bits,
|
||||
children: branch_children,
|
||||
}));
|
||||
children: Some(branch_children),
|
||||
});
|
||||
}
|
||||
// Create new branch node
|
||||
let branch_bits = if branch.args {
|
||||
|
@ -194,11 +198,12 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
// If we get here, it's an unexpected tag
|
||||
let node = Node::Block(BlockNode::Standard {
|
||||
let node = Node::Block {
|
||||
block_type: BlockType::Standard,
|
||||
name: tag_name.clone(),
|
||||
bits: bits.clone(),
|
||||
children: children.clone(),
|
||||
});
|
||||
children: Some(children.clone()),
|
||||
};
|
||||
return Err(ParserError::Ast(AstError::UnexpectedTag(tag), Some(node)));
|
||||
}
|
||||
Err(ParserError::Ast(AstError::StreamError(kind), _)) if kind == "AtEnd" => {
|
||||
|
@ -208,11 +213,12 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
|
||||
let node = Node::Block(BlockNode::Standard {
|
||||
let node = Node::Block {
|
||||
block_type: BlockType::Standard,
|
||||
name: tag_name.clone(),
|
||||
bits,
|
||||
children,
|
||||
});
|
||||
children: Some(children),
|
||||
};
|
||||
|
||||
if !found_closing_tag {
|
||||
return Err(ParserError::Ast(
|
||||
|
|
|
@ -4,7 +4,7 @@ expression: ast
|
|||
---
|
||||
nodes:
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -14,7 +14,7 @@ nodes:
|
|||
children:
|
||||
- Text: Positive
|
||||
- Block:
|
||||
Branch:
|
||||
block_type: Branch
|
||||
name: elif
|
||||
bits:
|
||||
- x
|
||||
|
@ -23,13 +23,14 @@ nodes:
|
|||
children:
|
||||
- Text: Negative
|
||||
- Block:
|
||||
Branch:
|
||||
block_type: Branch
|
||||
name: else
|
||||
bits: []
|
||||
children:
|
||||
- Text: Zero
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
errors: []
|
||||
|
|
|
@ -4,7 +4,7 @@ expression: ast
|
|||
---
|
||||
nodes:
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: for
|
||||
bits:
|
||||
- for
|
||||
|
@ -17,13 +17,14 @@ nodes:
|
|||
- item
|
||||
filters: []
|
||||
- Block:
|
||||
Branch:
|
||||
block_type: Branch
|
||||
name: empty
|
||||
bits: []
|
||||
children:
|
||||
- Text: No items
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endfor
|
||||
bits: []
|
||||
children: ~
|
||||
errors: []
|
||||
|
|
|
@ -4,7 +4,7 @@ expression: ast
|
|||
---
|
||||
nodes:
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -12,7 +12,8 @@ nodes:
|
|||
children:
|
||||
- Text: Welcome
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
errors: []
|
||||
|
|
|
@ -5,7 +5,7 @@ expression: ast
|
|||
nodes:
|
||||
- Text: "Welcome, "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -24,7 +24,7 @@ nodes:
|
|||
- "'Guest'"
|
||||
- Text: "\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: for
|
||||
bits:
|
||||
- for
|
||||
|
@ -34,7 +34,7 @@ nodes:
|
|||
children:
|
||||
- Text: "\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -42,9 +42,10 @@ nodes:
|
|||
children:
|
||||
- Text: (
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "\n "
|
||||
- Variable:
|
||||
bits:
|
||||
|
@ -53,7 +54,7 @@ nodes:
|
|||
filters: []
|
||||
- Text: "\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -62,12 +63,13 @@ nodes:
|
|||
children:
|
||||
- Text: ", "
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -75,30 +77,33 @@ nodes:
|
|||
children:
|
||||
- Text: )
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "\n "
|
||||
- Block:
|
||||
Branch:
|
||||
block_type: Branch
|
||||
name: empty
|
||||
bits: []
|
||||
children:
|
||||
- Text: "\n (no groups)\n "
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endfor
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "\n"
|
||||
- Block:
|
||||
Branch:
|
||||
block_type: Branch
|
||||
name: else
|
||||
bits: []
|
||||
children:
|
||||
- Text: "\n Guest\n"
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "!"
|
||||
errors: []
|
||||
|
|
|
@ -4,7 +4,7 @@ expression: ast
|
|||
---
|
||||
nodes:
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: for
|
||||
bits:
|
||||
- for
|
||||
|
@ -13,7 +13,7 @@ nodes:
|
|||
- items
|
||||
children:
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -25,11 +25,13 @@ nodes:
|
|||
- name
|
||||
filters: []
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endfor
|
||||
bits: []
|
||||
children: ~
|
||||
errors: []
|
||||
|
|
|
@ -5,7 +5,7 @@ expression: ast
|
|||
nodes:
|
||||
- Text: "<div class=\"container\">\n <h1>Header</h1>\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -23,7 +23,7 @@ nodes:
|
|||
- Comment: "This div is unclosed which doesn't matter"
|
||||
- Text: "\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: for
|
||||
bits:
|
||||
- for
|
||||
|
@ -38,9 +38,10 @@ nodes:
|
|||
filters: []
|
||||
- Text: "</span>\n "
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endfor
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "\n <footer>Page Footer</footer>\n</div>"
|
||||
errors:
|
||||
- UnclosedTag: if
|
||||
|
|
|
@ -4,7 +4,7 @@ expression: ast
|
|||
---
|
||||
nodes:
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: for
|
||||
bits:
|
||||
- for
|
||||
|
|
|
@ -4,7 +4,7 @@ expression: ast
|
|||
---
|
||||
nodes:
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
|
|
@ -5,7 +5,7 @@ expression: ast
|
|||
nodes:
|
||||
- Text: "<!DOCTYPE html>\n<html>\n <head>\n <style type=\"text/css\">\n /* Style header */\n .header { color: blue; }\n </style>\n <script type=\"text/javascript\">\n // Init app\n const app = {\n /* Config */\n debug: true\n };\n </script>\n </head>\n <body>\n <!-- Header section -->\n <div class=\"header\" id=\"main\" data-value=\"123\" disabled>\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -26,7 +26,7 @@ nodes:
|
|||
- "'Guest'"
|
||||
- Text: "!</h1>\n "
|
||||
- Block:
|
||||
Standard:
|
||||
block_type: Standard
|
||||
name: if
|
||||
bits:
|
||||
- if
|
||||
|
@ -34,19 +34,21 @@ nodes:
|
|||
children:
|
||||
- Text: "\n <span>Admin</span>\n "
|
||||
- Block:
|
||||
Branch:
|
||||
block_type: Branch
|
||||
name: else
|
||||
bits: []
|
||||
children:
|
||||
- Text: "\n <span>User</span>\n "
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "\n "
|
||||
- Block:
|
||||
Closing:
|
||||
block_type: Closing
|
||||
name: endif
|
||||
bits: []
|
||||
children: ~
|
||||
- Text: "\n </div>\n </body>\n</html>"
|
||||
errors: []
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue