Start work on tagging unit test

Co-Authored-By: Patrick Thomson <patrickt@users.noreply.github.com>
This commit is contained in:
Max Brunsfeld 2020-03-05 13:04:49 -08:00
parent 38a9f33d9e
commit a3f0087b11
3 changed files with 81 additions and 8 deletions

View file

@ -4,5 +4,6 @@ mod highlight_test;
mod node_test;
mod parser_test;
mod query_test;
mod tags_test;
mod test_highlight_test;
mod tree_test;

View file

@ -0,0 +1,56 @@
use super::helpers::fixtures::get_language;
use tree_sitter_tags::{TagKind, TagsConfiguration, TagsContext};
#[test]
fn test_tags_javascript() {
let language = get_language("python");
let tags_config = TagsConfiguration::new(
language,
r#"
((function_definition
name: (identifier) @name
body: (block
. (string) @doc)) @function
(set! replace @doc "(^['\s]*)|(['\s]*$)"))
(function_definition
name: (identifier) @name) @function
(class_definition
name: (identifier) @name) @class
(call
function: (identifier) @name) @call
"#,
"",
)
.unwrap();
let mut tag_context = TagsContext::new();
let tags = tag_context.generate_tags(
&tags_config,
br#"
class Customer:
"""
Data about a customer
"""
def age(self):
"""
Get the customer's age
"""
compute_age(self.id);
}
"#,
);
assert_eq!(
tags.iter().map(|t| (t.name, t.kind)).collect::<Vec<_>>(),
&[
("Customer", TagKind::Class),
("age", TagKind::Function),
("compute_age", TagKind::Call),
]
);
assert_eq!(tags[0].docs, Some("Data about a customer"));
assert_eq!(tags[1].docs, Some("Get the customer's age"));
}

View file

@ -14,30 +14,31 @@ pub struct TagsContext {
cursor: QueryCursor,
}
#[derive(Serialize)]
#[derive(Debug, Serialize)]
pub struct Range {
pub start: i64,
pub end: i64,
}
#[derive(Serialize)]
#[derive(Debug, Serialize)]
pub struct Loc {
pub byte_range: Range,
pub span: Span,
}
#[derive(Serialize)]
#[derive(Debug, Serialize)]
pub struct Span {
pub start: Pos,
pub end: Pos,
}
#[derive(Serialize)]
#[derive(Debug, Serialize)]
pub struct Pos {
pub line: i64,
pub column: i64,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TagKind {
Function,
Method,
@ -46,7 +47,7 @@ pub enum TagKind {
Call,
}
#[derive(Serialize)]
#[derive(Debug, Serialize)]
pub struct Tag<'a> {
pub kind: TagKind,
pub loc: Loc,
@ -88,8 +89,6 @@ impl TagsConfiguration {
}
}
query.pattern_count();
query.start_byte_for_pattern(5);
Ok(TagsConfiguration {
language,
query,
@ -107,6 +106,23 @@ impl TagsContext {
}
pub fn generate_tags(&mut self, config: &TagsConfiguration, source: &[u8]) -> Vec<Tag> {
Vec::new()
self.parser
.set_language(config.language)
.expect("Incompatible language");
let tree = self
.parser
.parse(source, None)
.expect("Parsing failed unexpectedly");
let matches = self
.cursor
.matches(&config.query, tree.root_node(), |node| {
&source[node.byte_range()]
});
matches
.map(|mat| {
for capture in mat.captures {}
unimplemented!();
})
.collect()
}
}