code_analyze_mcp/languages/
rust.rs1use tree_sitter::Node;
2
3pub 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
13pub 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
20pub const REFERENCE_QUERY: &str = r"
22(type_identifier) @type_ref
23";
24
25pub const IMPORT_QUERY: &str = r"
27(use_declaration argument: (_) @import_path) @import
28";
29
30pub const IMPL_TRAIT_QUERY: &str = r"
35(impl_item
36 trait: (type_identifier) @trait_name
37 type: (type_identifier) @impl_type)
38";
39
40pub 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#[must_use]
52pub fn extract_function_name(node: &Node, source: &str, _query_name: &str) -> Option<String> {
53 if node.kind() != "function_item" {
54 return None;
55 }
56 node.child_by_field_name("name").and_then(|n| {
57 let start = n.start_byte();
58 let end = n.end_byte();
59 if end <= source.len() {
60 Some(source[start..end].to_string())
61 } else {
62 None
63 }
64 })
65}
66
67#[must_use]
69pub fn find_method_for_receiver(
70 node: &Node,
71 source: &str,
72 _depth: Option<usize>,
73) -> Option<String> {
74 if node.kind() != "method_item" && node.kind() != "function_item" {
75 return None;
76 }
77 node.child_by_field_name("name").and_then(|n| {
78 let start = n.start_byte();
79 let end = n.end_byte();
80 if end <= source.len() {
81 Some(source[start..end].to_string())
82 } else {
83 None
84 }
85 })
86}
87
88#[must_use]
90pub fn find_receiver_type(node: &Node, source: &str) -> Option<String> {
91 if node.kind() != "impl_item" {
92 return None;
93 }
94 node.child_by_field_name("type").and_then(|n| {
95 let start = n.start_byte();
96 let end = n.end_byte();
97 if end <= source.len() {
98 Some(source[start..end].to_string())
99 } else {
100 None
101 }
102 })
103}
104
105#[must_use]
109pub fn extract_inheritance(_node: &Node, _source: &str) -> Vec<String> {
110 Vec::new()
111}