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