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
// Rust analyzer module structure
mod imports;
mod naming;
mod preprocess;
mod tauri;
mod usages;
// Re-export public items
pub use imports::CrateModuleMap;
// Imports from submodules
use imports::parse_rust_brace_names;
use naming::exposed_command_name;
use preprocess::{
find_balanced_bracket, strip_cfg_attributes, strip_cfg_test_modules, strip_function_body_uses,
};
use tauri::{extract_plugin_identifier, extract_plugin_name};
use usages::{
collect_identifier_mentions, collect_rust_signature_uses, extract_bare_function_calls,
extract_function_arguments, extract_identifier_usages, extract_path_qualified_calls,
extract_struct_field_types, extract_type_alias_qualified_paths,
};
// External imports
use super::offset_to_line;
use super::regexes::{
regex_custom_command_fn, regex_event_const_rust, regex_event_emit_rust,
regex_event_listen_rust, regex_rust_async_main_attr, regex_rust_fn_main, regex_rust_mod_decl,
regex_rust_pub_use, regex_rust_use, regex_tauri_command_fn, regex_tauri_generate_handler,
rust_pub_const_regexes, rust_pub_decl_regexes,
};
use crate::types::{
CommandRef, EventRef, ExportSymbol, FileAnalysis, ImportEntry, ImportKind, LocalSymbol,
ParamInfo, ReexportEntry, ReexportKind, SymbolUsage,
};
use regex::Regex;
use std::collections::HashSet;
use std::sync::OnceLock;
/// Extract params from content starting at a given position after function name.
/// Looks for `(...)` and parses the params inside.
fn extract_rust_fn_params(content: &str, after_name_pos: usize) -> Vec<ParamInfo> {
// Find opening paren
let rest = &content[after_name_pos..];
let Some(paren_start) = rest.find('(') else {
return Vec::new();
};
// Find matching closing paren (handle nested generics)
let params_start = after_name_pos + paren_start + 1;
let mut depth = 1;
let mut end_pos = params_start;
for (i, ch) in content[params_start..].char_indices() {
match ch {
'(' | '<' | '[' | '{' => depth += 1,
')' | '>' | ']' | '}' => {
depth -= 1;
if depth == 0 {
end_pos = params_start + i;
break;
}
}
_ => {}
}
}
if depth != 0 {
return Vec::new();
}
let params_text = &content[params_start..end_pos];
parse_rust_params(params_text)
}
/// Parse Rust function params like `x: i32, y: &str, z: Option<T>`.
/// Skips `self`, `&self`, `&mut self`.
fn parse_rust_params(params_text: &str) -> Vec<ParamInfo> {
let mut params = Vec::new();
let mut current = String::new();
let mut depth: usize = 0;
for ch in params_text.chars() {
match ch {
'<' | '(' | '[' | '{' => {
depth += 1;
current.push(ch);
}
'>' | ')' | ']' | '}' => {
depth = depth.saturating_sub(1);
current.push(ch);
}
',' if depth == 0 => {
if let Some(p) = parse_single_rust_param(current.trim()) {
params.push(p);
}
current.clear();
}
_ => current.push(ch),
}
}
// Last param
if !current.trim().is_empty()
&& let Some(p) = parse_single_rust_param(current.trim())
{
params.push(p);
}
params
}
/// Parse a single Rust param like `name: Type`.
fn parse_single_rust_param(param: &str) -> Option<ParamInfo> {
let param = param.trim();
if param.is_empty() {
return None;
}
// Skip self variants
if param == "self"
|| param == "&self"
|| param == "&mut self"
|| param == "mut self"
|| param.starts_with("self:")
{
return None;
}
// Parse `name: Type` or `mut name: Type`
let param = param.strip_prefix("mut ").unwrap_or(param);
if let Some((name, type_ann)) = param.split_once(':') {
Some(ParamInfo {
name: name.trim().to_string(),
type_annotation: Some(type_ann.trim().to_string()),
has_default: false, // Rust doesn't have default params
})
} else {
// Just a name without type annotation (rare in Rust)
Some(ParamInfo {
name: param.to_string(),
type_annotation: None,
has_default: false,
})
}
}
fn rust_local_decl_regexes() -> &'static Vec<(&'static str, Regex)> {
static RE: OnceLock<Vec<(&'static str, Regex)>> = OnceLock::new();
RE.get_or_init(|| {
vec![
(
"function",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?(?:async|const|unsafe\s+)*fn\s+([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust fn regex"),
),
(
"struct",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?struct\s+([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust struct regex"),
),
(
"enum",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?enum\s+([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust enum regex"),
),
(
"trait",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?trait\s+([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust trait regex"),
),
(
"type",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?type\s+([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust type regex"),
),
(
"union",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?union\s+([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust union regex"),
),
(
"const",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?const\s+([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust const regex"),
),
(
"static",
Regex::new(
r#"(?m)^\s*(?:pub\s*(?:\([^)]*\)\s*)?)?static\s+(?:mut\s+)?(?:ref\s+)?([A-Za-z_][A-Za-z0-9_]*)"#,
)
.expect("valid rust static regex"),
),
]
})
}
fn rust_line_context(lines: &[&str], line: usize) -> String {
if line == 0 {
return String::new();
}
lines
.get(line.saturating_sub(1))
.map(|l| l.trim().to_string())
.unwrap_or_default()
}
fn collect_rust_local_symbols(content: &str, exported_names: &HashSet<String>) -> Vec<LocalSymbol> {
let lines: Vec<&str> = content.lines().collect();
let mut locals = Vec::new();
const SKIP_NAMES: &[&str] = &["fn"];
for (kind, re) in rust_local_decl_regexes() {
for caps in re.captures_iter(content) {
let Some(name) = caps.get(1) else { continue };
let name_str = name.as_str().to_string();
if exported_names.contains(&name_str) {
continue;
}
if SKIP_NAMES.contains(&name_str.as_str()) {
continue;
}
let line = offset_to_line(content, name.start());
locals.push(LocalSymbol {
name: name_str,
kind: (*kind).to_string(),
line: Some(line),
context: rust_line_context(&lines, line),
is_exported: false,
});
}
}
locals
}
fn collect_symbol_usages_from_lines(
lines: &[&str],
names: &HashSet<String>,
max: usize,
) -> Vec<SymbolUsage> {
let mut usages = Vec::new();
let mut seen: HashSet<(String, usize)> = HashSet::new();
for (idx, line) in lines.iter().enumerate() {
if usages.len() >= max {
break;
}
let mut start: Option<usize> = None;
for (i, ch) in line.char_indices() {
let is_ident = ch.is_ascii_alphanumeric() || ch == '_';
if is_ident {
if start.is_none() {
start = Some(i);
}
continue;
}
if let Some(begin) = start.take() {
let token = &line[begin..i];
let Some(first) = token.chars().next() else {
continue;
};
if !(first.is_ascii_alphabetic() || first == '_') {
continue;
}
if !names.contains(token) {
continue;
}
let line_num = idx + 1;
if seen.insert((token.to_string(), line_num)) {
usages.push(SymbolUsage {
name: token.to_string(),
line: line_num,
context: line.trim().to_string(),
});
if usages.len() >= max {
break;
}
}
}
}
if usages.len() >= max {
break;
}
if let Some(begin) = start.take() {
let token = &line[begin..];
let Some(first) = token.chars().next() else {
continue;
};
if !(first.is_ascii_alphabetic() || first == '_') {
continue;
}
if !names.contains(token) {
continue;
}
let line_num = idx + 1;
if seen.insert((token.to_string(), line_num)) {
usages.push(SymbolUsage {
name: token.to_string(),
line: line_num,
context: line.trim().to_string(),
});
}
}
}
usages
}
pub(crate) fn analyze_rust_file(
content: &str,
relative: String,
custom_command_macros: &[String],
) -> FileAnalysis {
let mut analysis = FileAnalysis::new(relative.clone());
let mut event_emits = Vec::new();
let mut event_listens = Vec::new();
// Extract plugin identifier for Tauri plugins
// Tries: 1) #![plugin(identifier = "...")] attribute
// 2) tauri-plugin-XXX in path
// 3) plugins/XXX/ in path
let plugin_identifier = extract_plugin_identifier(content, &relative);
// Strip #[cfg(test)] modules and inline function-body imports to avoid false positive cycles
let production_content = strip_function_body_uses(&strip_cfg_test_modules(content));
for caps in regex_rust_use().captures_iter(&production_content) {
let source = caps.get(1).map(|m| m.as_str()).unwrap_or("").trim();
if source.is_empty() {
continue;
}
let mut imp = ImportEntry::new(source.to_string(), ImportKind::Static);
imp.line = Some(offset_to_line(
&production_content,
caps.get(0).map(|m| m.start()).unwrap_or(0),
));
// Track crate-internal import patterns for dead code detection
imp.raw_path = source.to_string();
imp.is_crate_relative = source.starts_with("crate::");
imp.is_super_relative = source.starts_with("super::");
imp.is_self_relative = source.starts_with("self::");
// Parse symbols from use statements like `use foo::{Bar, Baz}`
if source.contains('{') && source.contains('}') {
let mut parts = source.splitn(2, '{');
let prefix = parts.next().unwrap_or("").trim().trim_end_matches("::");
let braces = parts.next().unwrap_or("").trim_end_matches('}').trim();
let names = parse_rust_brace_names(braces);
for (original, exported) in names {
imp.symbols.push(crate::types::ImportSymbol {
name: original.clone(),
alias: if original != exported {
Some(exported)
} else {
None
},
is_default: false,
});
}
// Set source to the prefix for better matching
imp.source = prefix.to_string();
} else {
// Single import like `use foo::Bar` or `use foo::*`
if let Some(last_segment) = source.rsplit("::").next() {
let last = last_segment.trim();
if last == "*" {
// Star import - add "*" as symbol to trigger star_used check
imp.symbols.push(crate::types::ImportSymbol {
name: "*".to_string(),
alias: None,
is_default: false,
});
// Also set source to the prefix path
if let Some(prefix) = source.rsplit_once("::") {
imp.source = prefix.0.to_string();
}
} else if !last.is_empty() && last != "self" {
imp.symbols.push(crate::types::ImportSymbol {
name: last.to_string(),
alias: None,
is_default: false,
});
}
}
}
analysis.imports.push(imp);
}
for caps in regex_rust_pub_use().captures_iter(content) {
let raw = caps.get(1).map(|m| m.as_str()).unwrap_or("").trim();
if raw.is_empty() {
continue;
}
if raw.contains('{') && raw.contains('}') {
let mut parts = raw.splitn(2, '{');
let _prefix = parts.next().unwrap_or("").trim().trim_end_matches("::");
let braces = parts.next().unwrap_or("").trim_end_matches('}').trim();
let names = parse_rust_brace_names(braces);
analysis.reexports.push(ReexportEntry {
source: raw.to_string(),
kind: ReexportKind::Named(names.clone()),
resolved: None,
});
for (_, exported) in names {
analysis
.exports
.push(ExportSymbol::new(exported, "reexport", "named", None));
}
} else if raw.ends_with("::*") {
analysis.reexports.push(ReexportEntry {
source: raw.to_string(),
kind: ReexportKind::Star,
resolved: None,
});
} else {
// pub use foo::bar as Baz;
let (path_part, original_name, export_name) =
if let Some((path, alias)) = raw.split_once(" as ") {
// Extract original name from path (last segment)
let orig = path.trim().rsplit("::").next().unwrap_or(path.trim());
(path.trim(), orig, alias.trim())
} else {
let mut segments = raw.rsplitn(2, "::");
let name = segments.next().unwrap_or(raw).trim();
let _ = segments.next();
(raw, name, name) // No alias - same name
};
analysis.reexports.push(ReexportEntry {
source: path_part.to_string(),
kind: ReexportKind::Named(vec![(
original_name.to_string(),
export_name.to_string(),
)]),
resolved: None,
});
analysis.exports.push(ExportSymbol::new(
export_name.to_string(),
"reexport",
"named",
None,
));
}
}
// Parse `mod foo;` declarations as imports
// This creates a dependency edge from the declaring file to the module file
for caps in regex_rust_mod_decl().captures_iter(&production_content) {
if let Some(mod_name) = caps.get(2) {
let mod_name = mod_name.as_str();
// Check for #[path = "..."] attribute (group 1)
let custom_path = caps.get(1).map(|m| m.as_str().to_string());
// Create import source in format: mod::name or mod::path::name for #[path]
let source = if let Some(path) = &custom_path {
// #[path = "foo.rs"] mod bar; -> mod::path:foo.rs
format!("mod::path:{}", path)
} else {
// Regular mod foo; -> mod::foo
format!("mod::{}", mod_name)
};
let mut imp = ImportEntry::new(source.clone(), ImportKind::Static);
imp.raw_path = source;
imp.is_crate_relative = false;
imp.is_super_relative = false;
imp.is_self_relative = false;
// Mark as mod declaration - this is NOT an import edge for cycle detection
imp.is_mod_declaration = true;
// Add the module name as an imported symbol
imp.symbols.push(crate::types::ImportSymbol {
name: mod_name.to_string(),
alias: None,
is_default: false,
});
analysis.imports.push(imp);
}
}
// public items - process with proper kind detection
// rust_pub_decl_regexes() returns [fn, struct, enum, trait, type, union] in order
let kinds = ["function", "struct", "enum", "trait", "type", "union"];
for (regex, kind) in rust_pub_decl_regexes().iter().zip(kinds.iter()) {
for caps in regex.captures_iter(content) {
if let Some(name) = caps.get(1) {
let line = offset_to_line(content, name.start());
let name_str = name.as_str().to_string();
// Extract params only for functions
let params = if *kind == "function" {
extract_rust_fn_params(content, name.end())
} else {
Vec::new()
};
analysis.exports.push(ExportSymbol::with_params(
name_str,
kind,
"named",
Some(line),
params,
));
}
}
}
for regex in rust_pub_const_regexes() {
for caps in regex.captures_iter(content) {
if let Some(name) = caps.get(1) {
let line = offset_to_line(content, name.start());
analysis.exports.push(ExportSymbol::new(
name.as_str().to_string(),
"decl",
"named",
Some(line),
));
}
}
}
let exported_names: HashSet<String> = analysis.exports.iter().map(|e| e.name.clone()).collect();
let locals = collect_rust_local_symbols(content, &exported_names);
if !locals.is_empty() {
analysis.local_symbols = locals;
}
collect_rust_signature_uses(&production_content, &mut analysis);
for caps in regex_event_const_rust().captures_iter(content) {
if let (Some(name), Some(val)) = (caps.get(1), caps.get(2)) {
analysis
.event_consts
.insert(name.as_str().to_string(), val.as_str().to_string());
}
}
// Check if a token looks like a valid Tauri event name (not a Rust literal/keyword).
// Returns true if the token should be filtered out (is NOT a valid event name).
let is_invalid_event_identifier = |token: &str| -> bool {
// Filter out Rust keywords and common literals
const RUST_KEYWORDS: &[&str] = &[
"true", "false", "None", "Some", "Ok", "Err", "self", "Self", "super", "crate",
];
if RUST_KEYWORDS.contains(&token) {
return true;
}
// Filter out tokens that look like module paths (contain ::)
// These are likely enum variants or associated items, not event names
if token.contains("::") {
return true;
}
// Filter out single lowercase words that look like crate/module names
// Valid event names typically use kebab-case, snake_case with underscores,
// or have mixed case. Single lowercase words without separators are
// more likely to be crate names (e.g., "gix", "tokio", "serde")
if token.chars().all(|c| c.is_ascii_lowercase()) && token.len() <= 8 {
return true;
}
// Filter out PascalCase identifiers without underscores or hyphens
// These are likely type names (Mode, AppState, etc.) not event names.
// Event names typically use kebab-case, snake_case, or SCREAMING_SNAKE_CASE.
// A single PascalCase word is almost never an event name.
if let Some(first) = token.chars().next()
&& first.is_ascii_uppercase()
{
// Check if it's a simple PascalCase identifier (no underscores/hyphens)
let has_separator = token.contains('_') || token.contains('-');
let is_all_caps = token.chars().all(|c| !c.is_ascii_lowercase());
// Filter out if it's PascalCase without separators and not all caps
if !has_separator && !is_all_caps {
return true;
}
}
false
};
let resolve_event = |token: &str| -> Option<(String, Option<String>, String, bool)> {
let trimmed = token.trim();
// Detect format! pattern - e.g., format!("event:{}", var) or &format!(...)
if trimmed.contains("format!") {
// Extract the format string pattern
if let Some(start) = trimmed.find("format!(\"") {
let after_paren = &trimmed[start + 9..]; // Skip 'format!("'
if let Some(end) = after_paren.find('"') {
let pattern = &after_paren[..end];
// Replace {} placeholders with * for pattern matching
let normalized = pattern.replace("{}", "*").replace("{:?}", "*");
return Some((
normalized.clone(),
Some(format!("format!(\"{}\")", pattern)),
"dynamic".to_string(),
true, // is_dynamic
));
}
}
// Fallback for complex format patterns
return Some((
"dynamic-event:*".to_string(),
Some(trimmed.to_string()),
"dynamic".to_string(),
true,
));
}
// String literals are always valid event names
if (trimmed.starts_with('"') && trimmed.ends_with('"'))
|| (trimmed.starts_with('\'') && trimmed.ends_with('\''))
{
let name = trimmed
.trim_start_matches(['"', '\''])
.trim_end_matches(['"', '\''])
.to_string();
return Some((
name,
Some(trimmed.to_string()),
"literal".to_string(),
false,
));
}
// Check if it's a known const
if let Some(val) = analysis.event_consts.get(trimmed) {
return Some((
val.clone(),
Some(trimmed.to_string()),
"const".to_string(),
false,
));
}
// For identifiers, apply filtering
if is_invalid_event_identifier(trimmed) {
return None;
}
Some((
trimmed.to_string(),
Some(trimmed.to_string()),
"ident".to_string(),
false,
))
};
for caps in regex_event_emit_rust().captures_iter(content) {
if let Some(target) = caps.name("target") {
// Skip if resolve_event filters out this identifier
if let Some((name, raw_name, source_kind, is_dynamic)) = resolve_event(target.as_str())
{
let payload = caps
.name("payload")
.map(|p| p.as_str().trim().trim_end_matches(')').trim().to_string())
.filter(|s| !s.is_empty());
let line = offset_to_line(content, caps.get(0).map(|m| m.start()).unwrap_or(0));
event_emits.push(EventRef {
raw_name,
name,
line,
kind: format!("emit_{}", source_kind),
awaited: false,
payload,
is_dynamic,
});
}
}
}
for caps in regex_event_listen_rust().captures_iter(content) {
if let Some(target) = caps.name("target") {
// Skip if resolve_event filters out this identifier
if let Some((name, raw_name, source_kind, is_dynamic)) = resolve_event(target.as_str())
{
let line = offset_to_line(content, caps.get(0).map(|m| m.start()).unwrap_or(0));
event_listens.push(EventRef {
raw_name,
name,
line,
kind: format!("listen_{}", source_kind),
awaited: false,
payload: None,
is_dynamic,
});
}
}
}
for caps in regex_tauri_command_fn().captures_iter(content) {
let attr_raw = caps.get(1).map(|m| m.as_str()).unwrap_or("").trim();
let name_match = caps.get(2);
let params = caps
.name("params")
.map(|p| p.as_str().trim().to_string())
.filter(|s| !s.is_empty());
if let Some(name) = name_match {
let fn_name = name.as_str().to_string();
let base_exposed_name = exposed_command_name(attr_raw, &fn_name);
// Check if this is a plugin command (has root = "crate" attribute)
let is_plugin_command = extract_plugin_name(attr_raw).is_some();
// exposed_name is just the command name (without plugin prefix)
// The plugin namespace is stored separately in plugin_name field
// This matches frontend behavior: invoke('plugin:window|cmd') parses to name="cmd", plugin_name="window"
let exposed_name = base_exposed_name;
let line = offset_to_line(content, name.start());
analysis.command_handlers.push(CommandRef {
name: fn_name,
exposed_name: Some(exposed_name),
line,
generic_type: None,
payload: params,
plugin_name: if is_plugin_command {
plugin_identifier.clone()
} else {
None
},
});
}
}
// Custom command macros (from .loctree/config.toml)
if let Some(custom_regex) = regex_custom_command_fn(custom_command_macros) {
for caps in custom_regex.captures_iter(content) {
let attr_raw = caps.get(1).map(|m| m.as_str()).unwrap_or("").trim();
let name_match = caps.get(2);
let params = caps
.name("params")
.map(|p| p.as_str().trim().to_string())
.filter(|s| !s.is_empty());
if let Some(name) = name_match {
let fn_name = name.as_str().to_string();
// Avoid duplicates if both #[tauri::command] and custom macro are used
if analysis.command_handlers.iter().any(|c| c.name == fn_name) {
continue;
}
let base_exposed_name = exposed_command_name(attr_raw, &fn_name);
let is_plugin_command = extract_plugin_name(attr_raw).is_some();
// exposed_name is just the command name (without plugin prefix)
// The plugin namespace is stored separately in plugin_name field
let exposed_name = base_exposed_name;
let line = offset_to_line(content, name.start());
analysis.command_handlers.push(CommandRef {
name: fn_name,
exposed_name: Some(exposed_name),
line,
generic_type: None,
payload: params,
plugin_name: if is_plugin_command {
plugin_identifier.clone()
} else {
None
},
});
}
}
}
// Tauri generate_handler! registrations
// The generate_handler! macro may span multiple lines and contain #[cfg(...)] attributes.
// We need to handle nested brackets by finding balanced pairs.
for caps in regex_tauri_generate_handler().captures_iter(content) {
if let Some(list_match) = caps.get(1) {
let start_pos = list_match.start();
// Find the actual end by matching balanced brackets from the start
let remaining = &content[start_pos..];
let balanced_end = find_balanced_bracket(remaining);
let raw = if balanced_end > 0 {
&remaining[..balanced_end]
} else {
list_match.as_str()
};
// Strip #[...] attributes from the handler list
let cleaned = strip_cfg_attributes(raw);
for part in cleaned.split(',') {
let ident = part.trim();
if ident.is_empty() {
continue;
}
// Strip potential trailing generics or module qualifiers (foo::<T>, module::foo)
// Use .last() to get the function name from paths like commands::foo::bar
let base = ident
.split(|c: char| c == ':' || c.is_whitespace() || c == '<')
.rfind(|s| !s.is_empty())
.unwrap_or("")
.trim();
if base.is_empty() {
continue;
}
// Basic Rust identifier check: starts with letter or '_', rest alphanumeric or '_'
let mut chars = base.chars();
if let Some(first) = chars.next() {
if !(first.is_ascii_alphabetic() || first == '_') {
continue;
}
if chars.any(|ch| !(ch.is_ascii_alphanumeric() || ch == '_')) {
continue;
}
if !analysis
.tauri_registered_handlers
.contains(&base.to_string())
{
analysis.tauri_registered_handlers.push(base.to_string());
}
}
}
}
}
analysis.event_emits = event_emits;
analysis.event_listens = event_listens;
// Detect Rust entry points using proper regex (not contains - avoids false positives in comments/strings)
if regex_rust_fn_main().is_match(content) {
analysis.entry_points.push("main".to_string());
}
if regex_rust_async_main_attr().is_match(content)
&& !analysis.entry_points.contains(&"async_main".to_string())
{
analysis.entry_points.push("async_main".to_string());
}
// Detect path-qualified calls like `module::function()` or `Type::method()`
// These are function calls via module path without explicit `use` import.
// Pattern: `::<identifier>(` or `::<Identifier>{` or `::<Identifier><`
// This catches: command::branch::handle(), OutputChannel::new(), etc.
extract_path_qualified_calls(&production_content, &mut analysis.local_uses);
// Detect type alias qualified paths like `io::Result`, `fs::File`, etc.
// This handles cases where a module is imported but types from that module
// are used via qualified paths (e.g., `use std::io; fn foo() -> io::Result<()>`)
// This reduces false positives by ~15% for Rust codebases
extract_type_alias_qualified_paths(content, &analysis.imports, &mut analysis.local_uses);
// Detect bare function calls like `func_name(...)` in the same file
// This catches local function calls without path qualification
extract_bare_function_calls(&production_content, &mut analysis.local_uses);
// Detect type names used in struct/enum field definitions
// This catches types like Vec<DiffEdge>, Option<HubFile>, etc. that are used
// as field types within the same file - they count as "local uses" of those types
extract_struct_field_types(content, &mut analysis.local_uses);
// Detect identifiers used in expressions and variable declarations
// This catches const/static usage like `create_buffer::<BUFFER_SIZE>()`
// and type usage in let bindings like `let x: SomeType = ...`
// NOTE: Use full `content` here, not `production_content`, because we need to
// scan function bodies for usages of exported symbols (constants, types, etc.)
extract_identifier_usages(content, &mut analysis.local_uses);
// Detect identifiers used as function arguments like `func(CONST_NAME)`
// This catches const/static usage passed as arguments to functions
extract_function_arguments(content, &mut analysis.local_uses);
// Fallback: treat any identifier mention (excluding keywords) as a local use.
// This plugs gaps where complex patterns (const tables, enum variants, nested types)
// might not be caught by the structured extractors above.
collect_identifier_mentions(content, &mut analysis.local_uses);
// Remove standard library/common types from local uses to avoid false positives
// in same-file usage checks.
const SKIP_STD_TYPES: &[&str] = &[
"Vec", "Option", "Result", "String", "HashMap", "Box", "Arc", "Rc",
];
analysis
.local_uses
.retain(|u| !SKIP_STD_TYPES.contains(&u.as_str()));
if !analysis.local_uses.is_empty() {
let usage_names: HashSet<String> = analysis.local_uses.iter().cloned().collect();
let lines: Vec<&str> = content.lines().collect();
const MAX_USAGES_PER_FILE: usize = 1500;
let usages = collect_symbol_usages_from_lines(&lines, &usage_names, MAX_USAGES_PER_FILE);
if !usages.is_empty() {
analysis.symbol_usages = usages;
}
}
analysis
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rust_local_symbols_and_usages() {
let content = r#"
fn helper() {}
struct LocalType;
pub fn public_fn() {
helper();
}
fn call_local() {
helper();
let _x = LocalType;
}
"#;
let analysis = analyze_rust_file(content, "sample.rs".to_string(), &[]);
assert!(
analysis.local_symbols.iter().any(|s| s.name == "helper"),
"helper should be in local_symbols"
);
assert!(
analysis.local_symbols.iter().any(|s| s.name == "LocalType"),
"LocalType should be in local_symbols"
);
assert!(
!analysis.local_symbols.iter().any(|s| s.name == "public_fn"),
"public_fn should be exported, not local"
);
assert!(
analysis.symbol_usages.iter().any(|u| u.name == "helper"),
"helper should appear in symbol_usages"
);
}
}