mirror of
https://github.com/joshuadavidthomas/django-language-server.git
synced 2025-09-08 03:10:34 +00:00
add assignment
This commit is contained in:
parent
fdf1ebb8db
commit
cacad220fa
2 changed files with 47 additions and 3 deletions
|
@ -87,7 +87,7 @@ impl Parser {
|
||||||
pub fn parse_django_block(&mut self) -> Result<Node, ParserError> {
|
pub fn parse_django_block(&mut self) -> Result<Node, ParserError> {
|
||||||
let token = self.peek_previous()?;
|
let token = self.peek_previous()?;
|
||||||
|
|
||||||
let bits: Vec<String> = token
|
let mut bits: Vec<String> = token
|
||||||
.content()
|
.content()
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
|
@ -97,12 +97,25 @@ impl Parser {
|
||||||
let span = Span::from(token);
|
let span = Span::from(token);
|
||||||
let tag_span = Span::new(*span.start(), tag_name.len() as u32);
|
let tag_span = Span::new(*span.start(), tag_name.len() as u32);
|
||||||
|
|
||||||
|
let assignment = if bits.len() >= 2 {
|
||||||
|
let second_to_last_index = bits.len() - 2;
|
||||||
|
if bits[second_to_last_index] == "as" {
|
||||||
|
let value = bits.last().cloned();
|
||||||
|
bits.truncate(bits.len() - 2);
|
||||||
|
value
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let tag = Tag {
|
let tag = Tag {
|
||||||
name: tag_name.clone(),
|
name: tag_name.clone(),
|
||||||
bits: bits.clone(),
|
bits,
|
||||||
span,
|
span,
|
||||||
tag_span,
|
tag_span,
|
||||||
assignment: None,
|
assignment,
|
||||||
};
|
};
|
||||||
|
|
||||||
let specs = TagSpecs::load_builtin_specs()?;
|
let specs = TagSpecs::load_builtin_specs()?;
|
||||||
|
@ -483,6 +496,16 @@ mod tests {
|
||||||
assert!(errors.is_empty());
|
assert!(errors.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_django_tag_assignment() {
|
||||||
|
let source = "{% url 'view-name' as view %}";
|
||||||
|
let tokens = Lexer::new(source).tokenize().unwrap();
|
||||||
|
let mut parser = Parser::new(tokens);
|
||||||
|
let (ast, errors) = parser.parse().unwrap();
|
||||||
|
insta::assert_yaml_snapshot!(ast);
|
||||||
|
assert!(errors.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_nested_for_if() {
|
fn test_parse_nested_for_if() {
|
||||||
let source =
|
let source =
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
source: crates/djls-template-ast/src/parser.rs
|
||||||
|
expression: ast
|
||||||
|
---
|
||||||
|
nodes:
|
||||||
|
- Block:
|
||||||
|
Tag:
|
||||||
|
tag:
|
||||||
|
name: url
|
||||||
|
bits:
|
||||||
|
- url
|
||||||
|
- "'view-name'"
|
||||||
|
span:
|
||||||
|
start: 3
|
||||||
|
length: 23
|
||||||
|
tag_span:
|
||||||
|
start: 3
|
||||||
|
length: 3
|
||||||
|
assignment: view
|
||||||
|
line_offsets:
|
||||||
|
- 0
|
Loading…
Add table
Add a link
Reference in a new issue