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 blocks and methods.
31pub const IMPL_QUERY: &str = r#"
32(impl_item
33  type: (type_identifier) @impl_type
34  body: (declaration_list
35    (function_item
36      name: (identifier) @method_name
37      parameters: (parameters) @method_params) @method))
38"#;
39
40/// Tree-sitter query for extracting variable assignments and reassignments.
41pub const ASSIGNMENT_QUERY: &str = r#"
42(let_declaration
43  pattern: (identifier) @variable
44  value: (_) @value) @assignment
45(assignment_expression
46  left: (identifier) @variable
47  right: (_) @value) @assignment
48"#;
49
50/// Tree-sitter query for extracting field access patterns.
51pub const FIELD_QUERY: &str = r#"
52(field_expression
53  value: (_) @object
54  field: (field_identifier) @field) @field_access
55"#;
56
57/// Extract function name from a function node.
58pub fn extract_function_name(node: &Node, source: &str, _query_name: &str) -> Option<String> {
59    if node.kind() != "function_item" {
60        return None;
61    }
62    node.child_by_field_name("name").and_then(|n| {
63        let start = n.start_byte();
64        let end = n.end_byte();
65        if end <= source.len() {
66            Some(source[start..end].to_string())
67        } else {
68            None
69        }
70    })
71}
72
73/// Find method name for a receiver type.
74pub fn find_method_for_receiver(
75    node: &Node,
76    source: &str,
77    _depth: Option<usize>,
78) -> Option<String> {
79    if node.kind() != "method_item" && node.kind() != "function_item" {
80        return None;
81    }
82    node.child_by_field_name("name").and_then(|n| {
83        let start = n.start_byte();
84        let end = n.end_byte();
85        if end <= source.len() {
86            Some(source[start..end].to_string())
87        } else {
88            None
89        }
90    })
91}
92
93/// Find receiver type for a method.
94pub fn find_receiver_type(node: &Node, source: &str) -> Option<String> {
95    if node.kind() != "impl_item" {
96        return None;
97    }
98    node.child_by_field_name("type").and_then(|n| {
99        let start = n.start_byte();
100        let end = n.end_byte();
101        if end <= source.len() {
102            Some(source[start..end].to_string())
103        } else {
104            None
105        }
106    })
107}
108
109/// Extract inheritance information from a Rust class node.
110/// Rust class nodes (struct_item, enum_item, trait_item) have no syntactic inheritance.
111/// Inheritance is via impl blocks, not on the type declaration itself.
112pub fn extract_inheritance(_node: &Node, _source: &str) -> Vec<String> {
113    Vec::new()
114}