Skip to main content

code_analyze_mcp/languages/
rust.rs

1use tree_sitter::Node;
2
3/// Tree-sitter query for extracting Rust elements (functions and structs/enums/traits).
4pub const ELEMENT_QUERY: &str = r#"
5(function_item
6  name: (identifier) @func_name
7  parameters: (parameters) @params) @function
8(struct_item) @class
9(enum_item) @class
10(trait_item) @class
11"#;
12
13/// Tree-sitter query for extracting function calls.
14pub const CALL_QUERY: &str = r#"
15(call_expression function: (identifier) @call)
16(call_expression function: (field_expression field: (field_identifier) @call))
17(call_expression function: (scoped_identifier name: (identifier) @call))
18"#;
19
20/// Tree-sitter query for extracting type references.
21pub const REFERENCE_QUERY: &str = r#"
22(type_identifier) @type_ref
23"#;
24
25/// Tree-sitter query for extracting imports.
26pub const IMPORT_QUERY: &str = r#"
27(use_declaration argument: (_) @import_path) @import
28"#;
29
30/// Tree-sitter query for extracting `impl Trait for Type` blocks.
31/// Captures the trait name and the concrete implementor type.
32// Note: matches only simple trait names (type_identifier). Scoped traits
33// (e.g. `impl io::Sink for T`) are not matched; scoped coverage is out of scope for v1.
34pub const IMPL_TRAIT_QUERY: &str = r#"
35(impl_item
36  trait: (type_identifier) @trait_name
37  type: (type_identifier) @impl_type)
38"#;
39
40/// Tree-sitter query for extracting impl blocks and methods.
41pub const IMPL_QUERY: &str = r#"
42(impl_item
43  type: (type_identifier) @impl_type
44  body: (declaration_list
45    (function_item
46      name: (identifier) @method_name
47      parameters: (parameters) @method_params) @method))
48"#;
49
50/// Extract function name from a function node.
51pub fn extract_function_name(node: &Node, source: &str, _query_name: &str) -> Option<String> {
52    if node.kind() != "function_item" {
53        return None;
54    }
55    node.child_by_field_name("name").and_then(|n| {
56        let start = n.start_byte();
57        let end = n.end_byte();
58        if end <= source.len() {
59            Some(source[start..end].to_string())
60        } else {
61            None
62        }
63    })
64}
65
66/// Find method name for a receiver type.
67pub fn find_method_for_receiver(
68    node: &Node,
69    source: &str,
70    _depth: Option<usize>,
71) -> Option<String> {
72    if node.kind() != "method_item" && node.kind() != "function_item" {
73        return None;
74    }
75    node.child_by_field_name("name").and_then(|n| {
76        let start = n.start_byte();
77        let end = n.end_byte();
78        if end <= source.len() {
79            Some(source[start..end].to_string())
80        } else {
81            None
82        }
83    })
84}
85
86/// Find receiver type for a method.
87pub fn find_receiver_type(node: &Node, source: &str) -> Option<String> {
88    if node.kind() != "impl_item" {
89        return None;
90    }
91    node.child_by_field_name("type").and_then(|n| {
92        let start = n.start_byte();
93        let end = n.end_byte();
94        if end <= source.len() {
95            Some(source[start..end].to_string())
96        } else {
97            None
98        }
99    })
100}
101
102/// Extract inheritance information from a Rust class node.
103/// Rust class nodes (struct_item, enum_item, trait_item) have no syntactic inheritance.
104/// Inheritance is via impl blocks, not on the type declaration itself.
105pub fn extract_inheritance(_node: &Node, _source: &str) -> Vec<String> {
106    Vec::new()
107}