Skip to content

Commit

Permalink
fix: inheritance with mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Aug 30, 2023
1 parent 991a7ae commit 1f911f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
42 changes: 42 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,45 @@ async fn add_root_py(path: PathBuf) -> miette::Result<Output> {
models: out,
})
}

#[cfg(test)]
mod tests {
use crate::index::model_query;
use pretty_assertions::assert_eq;
use tree_sitter::{Parser, QueryCursor};

#[test]
fn test_model_query() {
let mut parser = Parser::new();
parser.set_language(tree_sitter_python::language()).unwrap();
let contents = br#"
class Foo(models.AbstractModel):
_name = 'foo'
_inherit = ['foo', 'bar']
"#;
let ast = parser.parse(&contents[..], None).unwrap();
let query = model_query();
let mut cursor = QueryCursor::new();
let expected: &[&[&str]] = &[&[
"models",
"AbstractModel",
"_name",
"'foo'",
"_inherit",
"'foo'",
"'bar'",
]];
let actual = cursor
.matches(query, ast.root_node(), &contents[..])
.map(|match_| {
match_
.captures
.into_iter()
.skip(1)
.map(|capture| String::from_utf8_lossy(&contents[capture.node.byte_range()]))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
assert_eq!(expected, actual);
}
}
14 changes: 7 additions & 7 deletions src/queries/model.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
[(identifier) @_Model
(attribute (identifier) @_models (identifier) @_Model)])
(block
[(expression_statement ; _name = ".."
(assignment (identifier) @_name (string) @name))
(expression_statement ; _inherit = ".."
(assignment
(identifier) @_inherit
[(string) @inherit
(list (string) @inherit)]))]*)) @model
(expression_statement ; _name = ".."
(assignment (identifier) @_name (string) @name))?
(expression_statement ; _inherit = ".."
(assignment
(identifier) @_inherit
[(string) @inherit
(list ((string) @inherit ","?)*)]))?)) @model
(#eq? @_models "models")
(#match? @_Model "^(Transient|Abstract)?Model$")
(#eq? @_name "_name")
Expand Down

0 comments on commit 1f911f0

Please sign in to comment.