1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
//! Tree-sitter based test detection for accurate AST parsing
//!
//! This module uses tree-sitter-rust to properly parse Rust code and detect:
//! - Test modules (#[cfg(test)] mod tests {})
//! - Test functions (#[test] fn test_name())
//! - Module boundaries and nesting
//! - Cursor position relative to AST nodes
use crate::{EntryPoint, EntryPointType, Position, RazError, RazResult};
use regex::Regex;
#[cfg(feature = "tree-sitter-support")]
use tree_sitter::{Node, Parser};
#[cfg(feature = "tree-sitter-support")]
pub struct TreeSitterTestDetector {
parser: Parser,
}
#[cfg(feature = "tree-sitter-support")]
impl TreeSitterTestDetector {
pub fn new() -> RazResult<Self> {
let mut parser = Parser::new();
let language = tree_sitter_rust::LANGUAGE;
parser
.set_language(&language.into())
.map_err(|e| RazError::analysis(format!("Failed to set tree-sitter language: {e}")))?;
Ok(Self { parser })
}
/// Detect all entry points in the file (tests, main, benchmarks, etc.)
pub fn detect_entry_points(
&mut self,
source: &str,
cursor: Option<Position>,
) -> RazResult<Vec<EntryPoint>> {
let tree = self
.parser
.parse(source, None)
.ok_or_else(|| RazError::analysis("Failed to parse source code".to_string()))?;
let mut entry_points = Vec::new();
let mut module_stack: Vec<(String, u32, u32)> = Vec::new(); // (name, start_line, end_line)
// Walk the tree to find test modules and functions
self.walk_tree(
tree.root_node(),
source,
cursor,
&mut entry_points,
&mut module_stack,
)?;
// Also detect doctests
self.detect_doctests(tree.root_node(), source, &mut entry_points)?;
Ok(entry_points)
}
/// Detect doctest blocks in the source code using tree-sitter
pub fn detect_doctests(
&self,
node: Node,
source: &str,
entry_points: &mut Vec<EntryPoint>,
) -> RazResult<()> {
self.walk_tree_for_doctests(node, source, entry_points, "")?;
Ok(())
}
/// Recursively walk the tree to find doc comments with code blocks
/// Since tree-sitter doesn't include comments in the AST, we need to use source positions
fn walk_tree_for_doctests(
&self,
node: Node,
source: &str,
entry_points: &mut Vec<EntryPoint>,
_parent_name: &str,
) -> RazResult<()> {
match node.kind() {
"function_item" | "struct_item" | "enum_item" | "impl_item" => {
// Get the name of this item
let item_name = self.extract_item_name(&node, source).unwrap_or_default();
// Since tree-sitter doesn't include comments, we need to search the source text
// Look for doc comments preceding this item's position
let item_start_line = node.start_position().row;
if let Some((doc_start, doc_end)) =
self.find_doc_comment_before_line(source, item_start_line)
{
// Check if the doc comment contains code blocks
let lines: Vec<&str> = source.lines().collect();
let doc_lines = &lines[doc_start..=doc_end];
let doc_text = doc_lines.join("\n");
if self.contains_rust_code_blocks(&doc_text) {
let start_line = doc_start as u32 + 1; // Convert to 1-based
let end_line = doc_end as u32 + 1;
let doctest_name = if item_name.is_empty() {
format!("doctest_{start_line}_{end_line}")
} else {
item_name.clone()
};
entry_points.push(EntryPoint {
name: doctest_name,
entry_type: EntryPointType::DocTest,
line: start_line,
column: 0,
line_range: (start_line, end_line),
full_path: Some(item_name.clone()),
});
}
}
// Recursively process children with current item name as parent
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.walk_tree_for_doctests(child, source, entry_points, &item_name)?;
}
}
_ => {
// For other node types, just recurse
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.walk_tree_for_doctests(child, source, entry_points, _parent_name)?;
}
}
}
Ok(())
}
/// Find doc comment lines that precede the given line
/// Returns (start_line, end_line) in 0-based indexing
fn find_doc_comment_before_line(
&self,
source: &str,
item_line: usize,
) -> Option<(usize, usize)> {
let lines: Vec<&str> = source.lines().collect();
if item_line == 0 || item_line >= lines.len() {
return None;
}
// Walk backwards from the item line to find doc comments
let mut end_line = None;
let mut start_line = None;
for i in (0..item_line).rev() {
let line = lines[i].trim();
// Check if this is a doc comment
if line.starts_with("///") || line.starts_with("//!") {
if end_line.is_none() {
end_line = Some(i);
}
start_line = Some(i);
} else if line.is_empty() && start_line.is_some() {
// Empty line within doc comment block - continue
continue;
} else if start_line.is_some() {
// Hit a non-doc-comment line - stop
break;
} else {
// Haven't found any doc comments yet and hit non-doc line - continue searching
continue;
}
}
if let (Some(start), Some(end)) = (start_line, end_line) {
Some((start, end))
} else {
None
}
}
/// Check if the doc comment text contains Rust code blocks
fn contains_rust_code_blocks(&self, doc_text: &str) -> bool {
// Look for ``` blocks (with or without rust specifier)
let has_code_blocks = doc_text.contains("```");
if !has_code_blocks {
return false;
}
// Simple heuristic: if it contains ``` and common Rust patterns, it's likely a doctest
let rust_patterns = [
"assert_eq!",
"assert!",
"panic!",
"println!",
"fn ",
"let ",
"use ",
"::new",
"#[",
"impl ",
"struct ",
"enum ",
"trait ",
"mod ",
"pub ",
];
rust_patterns
.iter()
.any(|pattern| doc_text.contains(pattern))
}
/// Extract the name of an item (function, struct, etc.)
fn extract_item_name(&self, node: &Node, source: &str) -> Option<String> {
match node.kind() {
"function_item" => self.get_function_name(node, source).ok(),
"struct_item" => self.get_struct_name(node, source).ok(),
"enum_item" => self.get_enum_name(node, source).ok(),
"impl_item" => self.get_impl_target_name(node, source).ok(),
_ => None,
}
}
/// Get struct name from struct_item node
fn get_struct_name(&self, node: &Node, source: &str) -> RazResult<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "type_identifier" {
if let Ok(text) = child.utf8_text(source.as_bytes()) {
return Ok(text.to_string());
}
}
}
Err(RazError::analysis("Struct name not found".to_string()))
}
/// Get enum name from enum_item node
fn get_enum_name(&self, node: &Node, source: &str) -> RazResult<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "type_identifier" {
if let Ok(text) = child.utf8_text(source.as_bytes()) {
return Ok(text.to_string());
}
}
}
Err(RazError::analysis("Enum name not found".to_string()))
}
/// Get impl target name from impl_item node
fn get_impl_target_name(&self, node: &Node, source: &str) -> RazResult<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "type_identifier" {
if let Ok(text) = child.utf8_text(source.as_bytes()) {
return Ok(text.to_string());
}
}
}
Err(RazError::analysis("Impl target name not found".to_string()))
}
/// Recursively walk the AST to find test-related nodes
fn walk_tree(
&self,
node: Node,
source: &str,
_cursor: Option<Position>,
entry_points: &mut Vec<EntryPoint>,
module_stack: &mut Vec<(String, u32, u32)>,
) -> RazResult<()> {
// Debug output
// #[cfg(debug_assertions)]
// {
// let indent = " ".repeat(module_stack.len());
// eprintln!("{}Walking node: {} at line {}", indent, node.kind(), node.start_position().row + 1);
// }
match node.kind() {
"mod_item" => {
// Get module name first
let module_name = self.get_module_name(&node, source)?;
let start_line = node.start_position().row as u32 + 1;
let end_line = node.end_position().row as u32 + 1;
// Check if this is a test module or if we're inside a test context
let is_test_mod = self.is_test_module(&node, source);
let in_test_context = module_stack.iter().any(|(name, _, _)| name == "tests");
if is_test_mod || in_test_context {
// Build full module path
let full_path = if module_stack.is_empty() {
module_name.clone()
} else {
format!(
"{}::{}",
module_stack
.iter()
.map(|(name, _, _)| name.as_str())
.collect::<Vec<_>>()
.join("::"),
module_name
)
};
// Add module entry point
entry_points.push(EntryPoint {
name: module_name.clone(),
entry_type: EntryPointType::TestModule,
line: start_line,
column: 0,
line_range: (start_line, end_line),
full_path: Some(full_path.clone()),
});
}
// Push to stack for nested tracking (even if not a test module)
module_stack.push((module_name, start_line, end_line));
// Look specifically for declaration_list which contains the module body
let mut mod_cursor = node.walk();
for child in node.children(&mut mod_cursor) {
if child.kind() == "declaration_list" {
// Process all items in the module body
let mut body_cursor = child.walk();
for body_item in child.children(&mut body_cursor) {
self.walk_tree(body_item, source, _cursor, entry_points, module_stack)?;
}
}
}
// Pop module from stack
module_stack.pop();
// Return early to avoid double-processing
return Ok(());
}
"function_item" => {
let fn_name = self.get_function_name(&node, source)?;
let start_line = node.start_position().row as u32 + 1;
let end_line = node.end_position().row as u32 + 1;
// Check if this is a test function
if self.is_test_function(&node, source) {
// Build full path including module hierarchy
let full_path = if module_stack.is_empty() {
fn_name.clone()
} else {
format!(
"{}::{}",
module_stack
.iter()
.map(|(name, _, _)| name.as_str())
.collect::<Vec<_>>()
.join("::"),
fn_name
)
};
entry_points.push(EntryPoint {
name: fn_name,
entry_type: EntryPointType::Test,
line: start_line,
column: node.start_position().column as u32,
line_range: (start_line, end_line),
full_path: Some(full_path),
});
} else if fn_name == "main" {
// Detect main function with proper line range
entry_points.push(EntryPoint {
name: "main".to_string(),
entry_type: EntryPointType::Main,
line: start_line,
column: node.start_position().column as u32,
line_range: (start_line, end_line),
full_path: None,
});
} else if self.is_bench_function(&node, source) {
// Detect benchmark function with proper line range
entry_points.push(EntryPoint {
name: fn_name,
entry_type: EntryPointType::Benchmark,
line: start_line,
column: node.start_position().column as u32,
line_range: (start_line, end_line),
full_path: None,
});
}
}
_ => {}
}
// Store the current module stack size before processing children
let initial_stack_size = module_stack.len();
// Recursively process children
let mut child_cursor = node.walk();
for child in node.children(&mut child_cursor) {
self.walk_tree(child, source, _cursor, entry_points, module_stack)?;
}
// Pop any modules that were added for this node
while module_stack.len() > initial_stack_size {
module_stack.pop();
}
Ok(())
}
/// Check if a module node is a test module (#[cfg(test)])
fn is_test_module(&self, node: &Node, source: &str) -> bool {
// Look for #[cfg(test)] attribute
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "attribute_item" {
let attr_text = child.utf8_text(source.as_bytes()).unwrap_or("");
if attr_text.contains("cfg(test)") {
return true;
}
}
}
// Also check if module name contains "test"
if let Ok(name) = self.get_module_name(node, source) {
return name.contains("test");
}
false
}
/// Check if a function node is a test function (#[test])
fn is_test_function(&self, node: &Node, source: &str) -> bool {
// Check previous sibling for test attributes
if let Some(prev_sibling) = node.prev_sibling() {
if prev_sibling.kind() == "attribute_item" {
let attr_text = prev_sibling.utf8_text(source.as_bytes());
if let Ok(text) = attr_text {
// Match various test attributes
if text.contains("test") && !text.contains("bench") {
return true;
}
}
}
}
false
}
/// Check if a function node is a benchmark function (#[bench])
fn is_bench_function(&self, node: &Node, source: &str) -> bool {
// Check previous sibling for bench attributes
if let Some(prev_sibling) = node.prev_sibling() {
if prev_sibling.kind() == "attribute_item" {
let attr_text = prev_sibling.utf8_text(source.as_bytes());
if let Ok(text) = attr_text {
if text.contains("bench") {
return true;
}
}
}
}
false
}
/// Extract module name from a mod_item node
fn get_module_name(&self, node: &Node, source: &str) -> RazResult<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "identifier" {
if let Ok(text) = child.utf8_text(source.as_bytes()) {
return Ok(text.to_string());
}
}
}
Err(RazError::analysis("Module name not found".to_string()))
}
/// Extract function name from a function_item node
fn get_function_name(&self, node: &Node, source: &str) -> RazResult<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "identifier" {
if let Ok(text) = child.utf8_text(source.as_bytes()) {
return Ok(text.to_string());
}
}
}
Err(RazError::analysis("Function name not found".to_string()))
}
/// Find what test context the cursor is in
pub fn find_test_context_at_cursor(
&mut self,
source: &str,
cursor: Position,
) -> RazResult<Option<TestContext>> {
let tree = self
.parser
.parse(source, None)
.ok_or_else(|| RazError::analysis("Failed to parse source code".to_string()))?;
let cursor_byte = self.position_to_byte_offset(source, cursor);
let mut context = TestContext::default();
self.find_context_recursive(
tree.root_node(),
source,
cursor,
cursor_byte,
&mut context,
&mut Vec::new(),
)?;
if context.in_test_function.is_some() || context.in_test_module.is_some() {
Ok(Some(context))
} else {
Ok(None)
}
}
/// Find the doctest associated with the cursor position
/// This uses tree-sitter AST to properly associate functions with their doctests
pub fn find_doctest_at_cursor(
&mut self,
source: &str,
cursor: Position,
) -> RazResult<Option<EntryPoint>> {
let tree = self
.parser
.parse(source, None)
.ok_or_else(|| RazError::analysis("Failed to parse source code".to_string()))?;
// First check if cursor is in a test context - if so, don't associate with doctests
if let Some(test_context) = self.find_test_context_at_cursor(source, cursor)? {
if test_context.in_test_function.is_some() || test_context.in_test_module.is_some() {
return Ok(None);
}
}
let cursor_byte = self.position_to_byte_offset(source, cursor);
// Walk the tree to find what item the cursor is in or near
if let Some(associated_item) =
self.find_item_at_cursor(tree.root_node(), source, cursor_byte)?
{
// Now find the doctest for this item
let mut entry_points = Vec::new();
self.detect_doctests(tree.root_node(), source, &mut entry_points)?;
// Find the doctest that matches this item
for ep in entry_points {
if ep.entry_type == EntryPointType::DocTest {
if let Some(full_path) = &ep.full_path {
if full_path == &associated_item {
return Ok(Some(ep));
}
}
}
}
}
// Fallback: check if cursor is directly in a doc comment
self.find_doctest_containing_cursor(tree.root_node(), source, cursor_byte)
}
/// Find the item (function, struct, etc.) that the cursor is in or near
fn find_item_at_cursor(
&self,
node: Node,
source: &str,
cursor_byte: usize,
) -> RazResult<Option<String>> {
// Check if cursor is within this node
if cursor_byte >= node.start_byte() && cursor_byte <= node.end_byte() {
match node.kind() {
"function_item" => {
if let Ok(name) = self.get_function_name(&node, source) {
return Ok(Some(name));
}
}
"struct_item" => {
if let Ok(name) = self.get_struct_name(&node, source) {
return Ok(Some(name));
}
}
"enum_item" => {
if let Ok(name) = self.get_enum_name(&node, source) {
return Ok(Some(name));
}
}
"impl_item" => {
if let Ok(name) = self.get_impl_target_name(&node, source) {
return Ok(Some(name));
}
}
_ => {}
}
// Check children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(item_name) = self.find_item_at_cursor(child, source, cursor_byte)? {
return Ok(Some(item_name));
}
}
}
Ok(None)
}
/// Find doctest that contains the cursor position
/// Since comments aren't in the AST, we check the source directly
fn find_doctest_containing_cursor(
&self,
_node: Node,
source: &str,
cursor_byte: usize,
) -> RazResult<Option<EntryPoint>> {
// Convert byte offset to line/column
let cursor_line = self.byte_offset_to_line(source, cursor_byte);
// Check if the cursor line is within a doc comment that has code blocks
let lines: Vec<&str> = source.lines().collect();
if cursor_line >= lines.len() {
return Ok(None);
}
let current_line = lines[cursor_line].trim();
// Check if cursor is on a doc comment line
if current_line.starts_with("///") || current_line.starts_with("//!") {
// Find the full doc comment block containing this line
if let Some((doc_start, doc_end)) =
self.find_doc_comment_block_containing_line(source, cursor_line)
{
let doc_lines = &lines[doc_start..=doc_end];
let doc_text = doc_lines.join("\n");
if self.contains_rust_code_blocks(&doc_text) {
// Find the associated item that follows this doc comment
let associated_item = self
.find_item_name_after_line(source, doc_end)
.unwrap_or_else(|| format!("doctest_{}_{}", doc_start + 1, doc_end + 1));
return Ok(Some(EntryPoint {
name: associated_item.clone(),
entry_type: EntryPointType::DocTest,
line: doc_start as u32 + 1,
column: 0,
line_range: (doc_start as u32 + 1, doc_end as u32 + 1),
full_path: Some(associated_item),
}));
}
}
}
Ok(None)
}
/// Convert byte offset to line number (0-based)
fn byte_offset_to_line(&self, source: &str, byte_offset: usize) -> usize {
let mut current_byte = 0;
for (line_num, line) in source.lines().enumerate() {
let line_end = current_byte + line.len() + 1; // +1 for newline
if byte_offset <= line_end {
return line_num;
}
current_byte = line_end;
}
// If we reach here, cursor is at end of file
source.lines().count().saturating_sub(1)
}
/// Find the complete doc comment block that contains the given line
fn find_doc_comment_block_containing_line(
&self,
source: &str,
line_num: usize,
) -> Option<(usize, usize)> {
let lines: Vec<&str> = source.lines().collect();
if line_num >= lines.len() {
return None;
}
// Find the start of the doc comment block
let mut start_line = line_num;
for i in (0..=line_num).rev() {
let line = lines[i].trim();
if line.starts_with("///") || line.starts_with("//!") {
start_line = i;
} else if line.is_empty() {
// Empty lines are allowed within doc comments
continue;
} else {
// Hit a non-doc-comment line
break;
}
}
// Find the end of the doc comment block
let mut end_line = line_num;
for (i, line) in lines.iter().enumerate().skip(line_num) {
let line = line.trim();
if line.starts_with("///") || line.starts_with("//!") {
end_line = i;
} else if line.is_empty() {
// Empty lines are allowed within doc comments
continue;
} else {
// Hit a non-doc-comment line
break;
}
}
// Verify this is actually a doc comment
if lines[start_line].trim().starts_with("///")
|| lines[start_line].trim().starts_with("//!")
{
Some((start_line, end_line))
} else {
None
}
}
/// Find the name of the item that follows the given line
fn find_item_name_after_line(&self, source: &str, after_line: usize) -> Option<String> {
let lines: Vec<&str> = source.lines().collect();
// Look for function/struct/enum/impl declarations after the doc comment
for line in lines.iter().skip(after_line + 1) {
let line = line.trim();
if line.is_empty() || line.starts_with("///") || line.starts_with("//!") {
continue;
}
// Try to extract item name from various patterns
if let Some(name) = self.extract_name_from_declaration(line) {
return Some(name);
}
// If we hit a non-empty, non-comment line without extracting a name, stop
break;
}
None
}
/// Extract item name from a declaration line
fn extract_name_from_declaration(&self, line: &str) -> Option<String> {
// Use lazy_static for regex patterns to avoid recompilation
use once_cell::sync::Lazy;
static FN_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"fn\s+(\w+)").unwrap());
static STRUCT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"struct\s+(\w+)").unwrap());
static ENUM_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"enum\s+(\w+)").unwrap());
static IMPL_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"impl\s+(?:\w+\s+for\s+)?(\w+)").unwrap());
// Function: pub fn name(...) or fn name(...)
if let Some(captures) = FN_REGEX.captures(line) {
return captures.get(1).map(|m| m.as_str().to_string());
}
// Struct: pub struct Name or struct Name
if let Some(captures) = STRUCT_REGEX.captures(line) {
return captures.get(1).map(|m| m.as_str().to_string());
}
// Enum: pub enum Name or enum Name
if let Some(captures) = ENUM_REGEX.captures(line) {
return captures.get(1).map(|m| m.as_str().to_string());
}
// Impl: impl Name or impl Trait for Name
if let Some(captures) = IMPL_REGEX.captures(line) {
return captures.get(1).map(|m| m.as_str().to_string());
}
None
}
/// Recursively find the test context at cursor position
fn find_context_recursive(
&self,
node: Node,
source: &str,
_cursor: Position,
cursor_byte: usize,
context: &mut TestContext,
module_stack: &mut Vec<String>,
) -> RazResult<()> {
let node_start = node.start_byte();
let node_end = node.end_byte();
// Check if cursor is within this node
if cursor_byte >= node_start && cursor_byte <= node_end {
let initial_stack_size = module_stack.len();
match node.kind() {
"mod_item" => {
let module_name = self.get_module_name(&node, source)?;
let is_test_mod = self.is_test_module(&node, source);
// Always push to stack for path tracking, regardless of test status
module_stack.push(module_name.clone());
// Only set context if it's a test module
if is_test_mod {
context.in_test_module = Some(TestModule {
name: module_name,
full_path: module_stack.join("::"),
line_range: (
node.start_position().row as u32 + 1,
node.end_position().row as u32 + 1,
),
});
}
}
"function_item" => {
if self.is_test_function(&node, source) {
let fn_name = self.get_function_name(&node, source)?;
let full_path = if module_stack.is_empty() {
fn_name.clone()
} else {
format!("{}::{}", module_stack.join("::"), fn_name)
};
context.in_test_function = Some(TestFunction {
name: fn_name,
full_path,
line: node.start_position().row as u32 + 1,
});
}
}
_ => {}
}
// Check children
let mut tree_cursor = node.walk();
for child in node.children(&mut tree_cursor) {
self.find_context_recursive(
child,
source,
_cursor,
cursor_byte,
context,
module_stack,
)?;
}
// Restore module stack to original size
module_stack.truncate(initial_stack_size);
}
Ok(())
}
/// Convert Position to byte offset in source
fn position_to_byte_offset(&self, source: &str, position: Position) -> usize {
let mut current_line = 0;
let mut line_start_byte = 0;
for (byte_pos, ch) in source.char_indices() {
if current_line == position.line {
// We're on the target line, add column offset
for (current_column, (col_byte_pos, _)) in
source[line_start_byte..].char_indices().enumerate()
{
if current_column >= position.column as usize {
return line_start_byte + col_byte_pos;
}
}
// If we reach end of line, return end of line
return byte_pos;
}
if ch == '\n' {
current_line += 1;
line_start_byte = byte_pos + 1;
if current_line > position.line {
return line_start_byte;
}
}
}
source.len() // Return end of file if position is beyond
}
}
#[derive(Debug, Default)]
pub struct TestContext {
pub in_test_module: Option<TestModule>,
pub in_test_function: Option<TestFunction>,
}
#[derive(Debug)]
pub struct TestModule {
pub name: String,
pub full_path: String,
pub line_range: (u32, u32),
}
#[derive(Debug)]
pub struct TestFunction {
pub name: String,
pub full_path: String,
pub line: u32,
}
// Fallback implementation when tree-sitter is not available
#[cfg(not(feature = "tree-sitter-support"))]
pub struct TreeSitterTestDetector;
#[cfg(not(feature = "tree-sitter-support"))]
impl TreeSitterTestDetector {
pub fn new() -> RazResult<Self> {
Ok(Self)
}
pub fn detect_test_entry_points(
&mut self,
_source: &str,
_cursor: Option<Position>,
) -> RazResult<Vec<EntryPoint>> {
Err(RazError::analysis(
"Tree-sitter support not enabled. Enable the 'advanced-analysis' feature.".to_string(),
))
}
pub fn find_test_context_at_cursor(
&mut self,
_source: &str,
_cursor: Position,
) -> RazResult<Option<TestContext>> {
Err(RazError::analysis(
"Tree-sitter support not enabled. Enable the 'advanced-analysis' feature.".to_string(),
))
}
}
#[cfg(not(feature = "tree-sitter-support"))]
#[derive(Debug, Default)]
pub struct TestContext;