use std::collections::BTreeSet;
use tree_sitter::{Node, Parser};
use crate::errors::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CallForm {
Free,
Path,
Method,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Call {
pub name: String,
pub form: CallForm,
pub arity: usize,
pub line: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnDef {
pub name: String,
pub params: usize,
pub takes_self: bool,
pub start_line: usize,
}
#[derive(Debug, Clone, Default)]
pub struct BufferFacts {
pub functions: Vec<FnDef>,
pub calls: Vec<Call>,
pub file_modules: Vec<String>,
pub bound_names: BTreeSet<String>,
}
pub fn analyze(source: &str) -> Result<BufferFacts> {
let mut parser = Parser::new();
parser
.set_language(&tree_sitter_rust::LANGUAGE.into())
.map_err(|e| Error::Parse(e.to_string()))?;
let tree = parser
.parse(source, None)
.ok_or_else(|| Error::Parse("tree-sitter produced no tree".to_string()))?;
let bytes = source.as_bytes();
let mut facts = BufferFacts::default();
let mut stack = vec![tree.root_node()];
while let Some(node) = stack.pop() {
match node.kind() {
"function_item" => collect_function(node, bytes, &mut facts),
"call_expression" => collect_call(node, bytes, &mut facts),
"use_declaration" => collect_bound_idents(node, bytes, &mut facts.bound_names),
"let_declaration" => {
if let Some(pattern) = node.child_by_field_name("pattern") {
collect_bound_idents(pattern, bytes, &mut facts.bound_names);
}
}
"mod_item" if node.child_by_field_name("body").is_none() => {
if let Some(name) = node
.child_by_field_name("name")
.and_then(|n| n.utf8_text(bytes).ok())
{
facts.file_modules.push(name.to_string());
}
}
_ => {}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
stack.push(child);
}
}
Ok(facts)
}
fn collect_function(node: Node, bytes: &[u8], facts: &mut BufferFacts) {
let Some(name) = node
.child_by_field_name("name")
.and_then(|n| n.utf8_text(bytes).ok())
else {
return;
};
let Some(params) = node.child_by_field_name("parameters") else {
return;
};
let mut count = 0usize;
let mut takes_self = false;
let mut cursor = params.walk();
for child in params.children(&mut cursor) {
match child.kind() {
"parameter" => {
count += 1;
if let Some(pattern) = child.child_by_field_name("pattern") {
collect_bound_idents(pattern, bytes, &mut facts.bound_names);
}
}
"self_parameter" => {
count += 1;
takes_self = true;
}
_ => {}
}
}
facts.functions.push(FnDef {
name: name.to_string(),
params: count,
takes_self,
start_line: node.start_position().row + 1,
});
}
fn collect_call(node: Node, bytes: &[u8], facts: &mut BufferFacts) {
let Some(func) = node.child_by_field_name("function") else {
return;
};
let (name, form) = match func.kind() {
"identifier" => (func.utf8_text(bytes).ok(), CallForm::Free),
"scoped_identifier" => (
func.child_by_field_name("name")
.and_then(|n| n.utf8_text(bytes).ok()),
CallForm::Path,
),
"field_expression" => (
func.child_by_field_name("field")
.and_then(|n| n.utf8_text(bytes).ok()),
CallForm::Method,
),
_ => (None, CallForm::Path),
};
let Some(name) = name else { return };
let arity = node.child_by_field_name("arguments").map_or(0, |args| {
let mut cursor = args.walk();
args.children(&mut cursor)
.filter(|c| c.is_named() && c.kind() != "line_comment" && c.kind() != "block_comment")
.count()
});
facts.calls.push(Call {
name: name.to_string(),
form,
arity,
line: node.start_position().row + 1,
});
}
fn collect_bound_idents(node: Node, bytes: &[u8], out: &mut BTreeSet<String>) {
let mut stack = vec![node];
while let Some(n) = stack.pop() {
if n.kind() == "identifier" {
if let Ok(text) = n.utf8_text(bytes) {
out.insert(text.to_string());
}
}
let mut cursor = n.walk();
for child in n.children(&mut cursor) {
stack.push(child);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn call<'a>(facts: &'a BufferFacts, name: &str) -> &'a Call {
facts.calls.iter().find(|c| c.name == name).unwrap()
}
#[test]
fn records_arity_and_form_per_call() {
let facts = analyze("fn f() { free(1, 2); a::b::path(1); x.method(); }").unwrap();
assert_eq!(call(&facts, "free").form, CallForm::Free);
assert_eq!(call(&facts, "free").arity, 2);
assert_eq!(call(&facts, "path").form, CallForm::Path);
assert_eq!(call(&facts, "method").form, CallForm::Method);
assert_eq!(call(&facts, "method").arity, 0);
}
#[test]
fn records_parameter_counts_and_self_receivers() {
let facts =
analyze("fn free(a: i32, b: i32) {}\nstruct S;\nimpl S { fn m(&self, x: u8) {} }")
.unwrap();
let free = facts.functions.iter().find(|f| f.name == "free").unwrap();
assert_eq!(free.params, 2);
assert!(!free.takes_self);
let method = facts.functions.iter().find(|f| f.name == "m").unwrap();
assert_eq!(method.params, 2);
assert!(method.takes_self);
}
#[test]
fn imports_lets_and_params_all_count_as_bound() {
let facts =
analyze("use std::mem::swap;\nfn f(callback: fn()) { let helper = || {}; helper(); }")
.unwrap();
assert!(facts.bound_names.contains("swap"));
assert!(facts.bound_names.contains("helper"));
assert!(facts.bound_names.contains("callback"));
}
#[test]
fn distinguishes_file_modules_from_inline_ones() {
let facts = analyze("mod sibling;\nmod inline { fn g() {} }").unwrap();
assert_eq!(facts.file_modules, vec!["sibling".to_string()]);
}
#[test]
fn a_syntactically_broken_buffer_still_parses() {
for broken in [
"fn f() { broken(1, ",
"fn f() { good(1); broken(1, ",
"fn f( { }",
"",
] {
assert!(analyze(broken).is_ok(), "failed on {broken:?}");
}
let facts = analyze("fn f() { good(1); broken(1, ").unwrap();
assert_eq!(call(&facts, "good").arity, 1);
}
}