deepwiki-rs 1.2.6

deepwiki-rs(also known as Litho) is a high-performance automatic generation engine for C4 architecture documentation, developed using Rust. It can intelligently analyze project structures, identify core components, parse dependency relationships, and leverage large language models (LLMs) to automatically generate professional architecture documentation.
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
use super::{Dependency, LanguageProcessor};
use crate::types::code::{InterfaceInfo, ParameterInfo};
use regex::Regex;
use std::path::Path;

#[derive(Debug)]
pub struct CSharpProcessor {
    using_regex: Regex,
    namespace_regex: Regex,
    method_regex: Regex,
    class_regex: Regex,
    interface_regex: Regex,
    enum_regex: Regex,
    struct_regex: Regex,
    property_regex: Regex,
    constructor_regex: Regex,
}

impl CSharpProcessor {
    pub fn new() -> Self {
        Self {
            using_regex: Regex::new(r"^\s*using\s+([^;]+);").unwrap(),
            namespace_regex: Regex::new(r"^\s*namespace\s+([^;\{]+)").unwrap(),
            method_regex: Regex::new(r"^\s*(public|private|protected|internal)?\s*(static)?\s*(virtual|override|abstract|sealed)?\s*(async)?\s*(\w+)\s+(\w+)\s*\(([^)]*)\)").unwrap(),
            class_regex: Regex::new(r"^\s*(public|private|protected|internal)?\s*(static)?\s*(abstract)?\s*(sealed)?\s*(partial)?\s*class\s+(\w+)").unwrap(),
            interface_regex: Regex::new(r"^\s*(public|private|protected|internal)?\s*(partial)?\s*interface\s+(\w+)").unwrap(),
            enum_regex: Regex::new(r"^\s*(public|private|protected|internal)?\s*enum\s+(\w+)").unwrap(),
            struct_regex: Regex::new(r"^\s*(public|private|protected|internal)?\s*(readonly)?\s*(partial)?\s*struct\s+(\w+)").unwrap(),
            property_regex: Regex::new(r"^\s*(public|private|protected|internal)?\s*(static)?\s*(virtual|override|abstract)?\s*(\w+)\s+(\w+)\s*\{\s*(get|set)").unwrap(),
            constructor_regex: Regex::new(r"^\s*(public|private|protected|internal)?\s*(\w+)\s*\(([^)]*)\)").unwrap(),
        }
    }
}

impl LanguageProcessor for CSharpProcessor {
    fn supported_extensions(&self) -> Vec<&'static str> {
        vec!["cs", "csproj", "sln"]
    }
    
    fn extract_dependencies(&self, content: &str, file_path: &Path) -> Vec<Dependency> {
        let mut dependencies = Vec::new();
        let source_file = file_path.to_string_lossy().to_string();
        
        // Handle .csproj files
        if file_path.extension().and_then(|e| e.to_str()) == Some("csproj") {
            return self.extract_csproj_dependencies(content, &source_file);
        }
        
        // Handle .sln files
        if file_path.extension().and_then(|e| e.to_str()) == Some("sln") {
            return self.extract_sln_dependencies(content, &source_file);
        }
        
        // Handle .cs files
        for (line_num, line) in content.lines().enumerate() {
            // Extract using statements
            if let Some(captures) = self.using_regex.captures(line) {
                if let Some(using_path) = captures.get(1) {
                    let using_str = using_path.as_str().trim();
                    
                    // Skip using static and using alias
                    if using_str.starts_with("static ") || using_str.contains(" = ") {
                        continue;
                    }
                    
                    let is_external = using_str.starts_with("System") || 
                                    using_str.starts_with("Microsoft") ||
                                    !using_str.contains(".");
                    
                    // Parse dependency name
                    let dependency_name = self.extract_dependency_name(using_str);
                    
                    dependencies.push(Dependency {
                        name: dependency_name,
                        path: Some(source_file.clone()),
                        is_external,
                        line_number: Some(line_num + 1),
                        dependency_type: "using".to_string(),
                        version: None,
                    });
                }
            }
            
            // Extract namespace statement
            if let Some(captures) = self.namespace_regex.captures(line) {
                if let Some(namespace_name) = captures.get(1) {
                    dependencies.push(Dependency {
                        name: namespace_name.as_str().trim().to_string(),
                        path: Some(source_file.clone()),
                        is_external: false,
                        line_number: Some(line_num + 1),
                        dependency_type: "namespace".to_string(),
                        version: None,
                    });
                }
            }
        }
        
        dependencies
    }
    
    fn determine_component_type(&self, file_path: &Path, content: &str) -> String {
        let file_name = file_path.file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("");
        
        // Check for project files
        if file_name.ends_with(".csproj") {
            // Determine project type from SDK or OutputType
            if content.contains("Microsoft.NET.Sdk.Web") {
                return "csharp_web_project".to_string();
            } else if content.contains("<OutputType>Exe</OutputType>") {
                return "csharp_console_project".to_string();
            } else if content.contains("<OutputType>Library</OutputType>") || content.contains("Microsoft.NET.Sdk") {
                return "csharp_library_project".to_string();
            } else if content.contains("Microsoft.NET.Test.Sdk") || file_name.contains("Test") {
                return "csharp_test_project".to_string();
            }
            return "csharp_project".to_string();
        }
        
        // Check for solution files
        if file_name.ends_with(".sln") {
            return "csharp_solution".to_string();
        }
        
        // Check for test files
        if file_name.ends_with("Test.cs") || file_name.ends_with("Tests.cs") ||
           content.contains("[Test]") || content.contains("[TestMethod]") {
            return "csharp_test".to_string();
        }
        
        // Check for common patterns
        if content.contains("interface ") {
            "csharp_interface".to_string()
        } else if content.contains("enum ") {
            "csharp_enum".to_string()
        } else if content.contains("struct ") {
            "csharp_struct".to_string()
        } else if content.contains("abstract class") {
            "csharp_abstract_class".to_string()
        } else if content.contains("static class") {
            "csharp_static_class".to_string()
        } else if content.contains("sealed class") {
            "csharp_sealed_class".to_string()
        } else if content.contains("partial class") {
            "csharp_partial_class".to_string()
        } else if content.contains("class ") {
            "csharp_class".to_string()
        } else {
            "csharp_file".to_string()
        }
    }
    
    fn is_important_line(&self, line: &str) -> bool {
        let trimmed = line.trim();
        
        // Type declarations
        if trimmed.starts_with("public class ") || trimmed.starts_with("class ") ||
           trimmed.starts_with("interface ") || trimmed.starts_with("enum ") ||
           trimmed.starts_with("struct ") || trimmed.starts_with("public ") || 
           trimmed.starts_with("private ") || trimmed.starts_with("protected ") ||
           trimmed.starts_with("internal ") || trimmed.starts_with("using ") ||
           trimmed.starts_with("namespace ") {
            return true;
        }
        
        // Attributes
        if trimmed.starts_with('[') && trimmed.contains(']') {
            return true;
        }
        
        // Important comments
        if trimmed.contains("TODO") || trimmed.contains("FIXME") || 
           trimmed.contains("NOTE") || trimmed.contains("HACK") {
            return true;
        }
        
        false
    }
    
    fn language_name(&self) -> &'static str {
        "C#"
    }

    fn extract_interfaces(&self, content: &str, _file_path: &Path) -> Vec<InterfaceInfo> {
        let mut interfaces = Vec::new();
        let lines: Vec<&str> = content.lines().collect();
        
        for (i, line) in lines.iter().enumerate() {
            // Extract class definitions
            if let Some(captures) = self.class_regex.captures(line) {
                let visibility = captures.get(1).map(|m| m.as_str()).unwrap_or("private");
                let is_static = captures.get(2).is_some();
                let is_abstract = captures.get(3).is_some();
                let is_sealed = captures.get(4).is_some();
                let is_partial = captures.get(5).is_some();
                let name = captures.get(6).map(|m| m.as_str()).unwrap_or("").to_string();
                
                let mut interface_type = "class".to_string();
                if is_static {
                    interface_type = "static_class".to_string();
                } else if is_abstract {
                    interface_type = "abstract_class".to_string();
                } else if is_sealed {
                    interface_type = "sealed_class".to_string();
                } else if is_partial {
                    interface_type = "partial_class".to_string();
                }
                
                interfaces.push(InterfaceInfo {
                    name,
                    interface_type,
                    visibility: visibility.to_string(),
                    parameters: Vec::new(),
                    return_type: None,
                    description: self.extract_xml_doc(&lines, i),
                });
            }
            
            // Extract interface definitions
            if let Some(captures) = self.interface_regex.captures(line) {
                let visibility = captures.get(1).map(|m| m.as_str()).unwrap_or("private");
                let is_partial = captures.get(2).is_some();
                let name = captures.get(3).map(|m| m.as_str()).unwrap_or("").to_string();
                
                let interface_type = if is_partial {
                    "partial_interface".to_string()
                } else {
                    "interface".to_string()
                };
                
                interfaces.push(InterfaceInfo {
                    name,
                    interface_type,
                    visibility: visibility.to_string(),
                    parameters: Vec::new(),
                    return_type: None,
                    description: self.extract_xml_doc(&lines, i),
                });
            }
            
            // Extract struct definitions
            if let Some(captures) = self.struct_regex.captures(line) {
                let visibility = captures.get(1).map(|m| m.as_str()).unwrap_or("private");
                let is_readonly = captures.get(2).is_some();
                let is_partial = captures.get(3).is_some();
                let name = captures.get(4).map(|m| m.as_str()).unwrap_or("").to_string();
                
                let mut interface_type = "struct".to_string();
                if is_readonly {
                    interface_type = "readonly_struct".to_string();
                } else if is_partial {
                    interface_type = "partial_struct".to_string();
                }
                
                interfaces.push(InterfaceInfo {
                    name,
                    interface_type,
                    visibility: visibility.to_string(),
                    parameters: Vec::new(),
                    return_type: None,
                    description: self.extract_xml_doc(&lines, i),
                });
            }
            
            // Extract enum definitions
            if let Some(captures) = self.enum_regex.captures(line) {
                let visibility = captures.get(1).map(|m| m.as_str()).unwrap_or("private");
                let name = captures.get(2).map(|m| m.as_str()).unwrap_or("").to_string();
                
                interfaces.push(InterfaceInfo {
                    name,
                    interface_type: "enum".to_string(),
                    visibility: visibility.to_string(),
                    parameters: Vec::new(),
                    return_type: None,
                    description: self.extract_xml_doc(&lines, i),
                });
            }
            
            // Extract property definitions
            if let Some(captures) = self.property_regex.captures(line) {
                let visibility = captures.get(1).map(|m| m.as_str()).unwrap_or("private");
                let is_static = captures.get(2).is_some();
                let modifier = captures.get(3).map(|m| m.as_str()).unwrap_or("");
                let return_type = captures.get(4).map(|m| m.as_str()).unwrap_or("").to_string();
                let name = captures.get(5).map(|m| m.as_str()).unwrap_or("").to_string();
                
                let mut interface_type = "property".to_string();
                if is_static {
                    interface_type = "static_property".to_string();
                } else if modifier == "virtual" {
                    interface_type = "virtual_property".to_string();
                } else if modifier == "override" {
                    interface_type = "override_property".to_string();
                } else if modifier == "abstract" {
                    interface_type = "abstract_property".to_string();
                }
                
                interfaces.push(InterfaceInfo {
                    name,
                    interface_type,
                    visibility: visibility.to_string(),
                    parameters: Vec::new(),
                    return_type: Some(return_type),
                    description: self.extract_xml_doc(&lines, i),
                });
            }
            
            // Extract method definitions
            if let Some(captures) = self.method_regex.captures(line) {
                let visibility = captures.get(1).map(|m| m.as_str()).unwrap_or("private");
                let is_static = captures.get(2).is_some();
                let modifier = captures.get(3).map(|m| m.as_str()).unwrap_or("");
                let is_async = captures.get(4).is_some();
                let return_type = captures.get(5).map(|m| m.as_str()).unwrap_or("").to_string();
                let name = captures.get(6).map(|m| m.as_str()).unwrap_or("").to_string();
                let params_str = captures.get(7).map(|m| m.as_str()).unwrap_or("");
                
                // Skip C# keywords
                if return_type == "if" || return_type == "for" || return_type == "while" || 
                   return_type == "foreach" || return_type == "switch" || return_type == "try" ||
                   return_type == "using" || return_type == "lock" {
                    continue;
                }
                
                let parameters = self.parse_csharp_parameters(params_str);
                let mut interface_type = "method".to_string();
                if is_static {
                    interface_type = "static_method".to_string();
                } else if is_async {
                    interface_type = "async_method".to_string();
                } else if modifier == "virtual" {
                    interface_type = "virtual_method".to_string();
                } else if modifier == "override" {
                    interface_type = "override_method".to_string();
                } else if modifier == "abstract" {
                    interface_type = "abstract_method".to_string();
                } else if modifier == "sealed" {
                    interface_type = "sealed_method".to_string();
                }
                
                interfaces.push(InterfaceInfo {
                    name,
                    interface_type,
                    visibility: visibility.to_string(),
                    parameters,
                    return_type: Some(return_type),
                    description: self.extract_xml_doc(&lines, i),
                });
            }
            
            // Extract constructors
            if let Some(captures) = self.constructor_regex.captures(line) {
                let visibility = captures.get(1).map(|m| m.as_str()).unwrap_or("private");
                let name = captures.get(2).map(|m| m.as_str()).unwrap_or("").to_string();
                let params_str = captures.get(3).map(|m| m.as_str()).unwrap_or("");
                
                // Simple check if it's a constructor (name starts with uppercase)
                if name.chars().next().map_or(false, |c| c.is_uppercase()) {
                    let parameters = self.parse_csharp_parameters(params_str);
                    
                    interfaces.push(InterfaceInfo {
                        name,
                        interface_type: "constructor".to_string(),
                        visibility: visibility.to_string(),
                        parameters,
                        return_type: None,
                        description: self.extract_xml_doc(&lines, i),
                    });
                }
            }
        }
        
        interfaces
    }
}

impl CSharpProcessor {
    /// Extract dependencies from .csproj files (NuGet packages and project references)
    fn extract_csproj_dependencies(&self, content: &str, source_file: &str) -> Vec<Dependency> {
        let mut dependencies = Vec::new();
        
        for (line_num, line) in content.lines().enumerate() {
            let trimmed = line.trim();
            
            // Extract NuGet package references: <PackageReference Include="Package.Name" Version="1.0.0" />
            if trimmed.starts_with("<PackageReference") && trimmed.contains("Include=") {
                if let Some(start) = trimmed.find("Include=\"") {
                    let after_include = &trimmed[start + 9..];
                    if let Some(end) = after_include.find('"') {
                        let package_name = &after_include[..end];
                        
                        // Extract version if present
                        let version = if let Some(ver_start) = trimmed.find("Version=\"") {
                            let after_version = &trimmed[ver_start + 9..];
                            after_version.find('"').map(|ver_end| after_version[..ver_end].to_string())
                        } else {
                            None
                        };
                        
                        dependencies.push(Dependency {
                            name: package_name.to_string(),
                            path: Some(source_file.to_string()),
                            is_external: true,
                            line_number: Some(line_num + 1),
                            dependency_type: "nuget_package".to_string(),
                            version,
                        });
                    }
                }
            }
            
            // Extract project references: <ProjectReference Include="..\Other.Project\Other.Project.csproj" />
            if trimmed.starts_with("<ProjectReference") && trimmed.contains("Include=") {
                if let Some(start) = trimmed.find("Include=\"") {
                    let after_include = &trimmed[start + 9..];
                    if let Some(end) = after_include.find('"') {
                        let project_path = &after_include[..end];
                        
                        // Extract project name from path
                        let project_name = project_path
                            .split(['/', '\\'])
                            .last()
                            .unwrap_or(project_path)
                            .trim_end_matches(".csproj")
                            .to_string();
                        
                        dependencies.push(Dependency {
                            name: project_name,
                            path: Some(source_file.to_string()),
                            is_external: false,
                            line_number: Some(line_num + 1),
                            dependency_type: "project_reference".to_string(),
                            version: None,
                        });
                    }
                }
            }
            
            // Extract framework references: <FrameworkReference Include="Microsoft.AspNetCore.App" />
            if trimmed.starts_with("<FrameworkReference") && trimmed.contains("Include=") {
                if let Some(start) = trimmed.find("Include=\"") {
                    let after_include = &trimmed[start + 9..];
                    if let Some(end) = after_include.find('"') {
                        let framework_name = &after_include[..end];
                        
                        dependencies.push(Dependency {
                            name: framework_name.to_string(),
                            path: Some(source_file.to_string()),
                            is_external: true,
                            line_number: Some(line_num + 1),
                            dependency_type: "framework_reference".to_string(),
                            version: None,
                        });
                    }
                }
            }
        }
        
        dependencies
    }
    
    /// Extract project references from .sln files
    fn extract_sln_dependencies(&self, content: &str, source_file: &str) -> Vec<Dependency> {
        let mut dependencies = Vec::new();
        
        for (line_num, line) in content.lines().enumerate() {
            let trimmed = line.trim();
            
            // Extract project entries: Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectName", "Path\ProjectName.csproj", "{GUID}"
            if trimmed.starts_with("Project(") && trimmed.contains(".csproj") {
                // Extract project name (between first pair of quotes after =)
                if let Some(name_start) = trimmed.find("= \"") {
                    let after_equals = &trimmed[name_start + 3..];
                    if let Some(name_end) = after_equals.find('"') {
                        let project_name = &after_equals[..name_end];
                        
                        dependencies.push(Dependency {
                            name: project_name.to_string(),
                            path: Some(source_file.to_string()),
                            is_external: false,
                            line_number: Some(line_num + 1),
                            dependency_type: "solution_project".to_string(),
                            version: None,
                        });
                    }
                }
            }
        }
        
        dependencies
    }

    /// Parse C# method parameters
    fn parse_csharp_parameters(&self, params_str: &str) -> Vec<ParameterInfo> {
        let mut parameters = Vec::new();
        
        if params_str.trim().is_empty() {
            return parameters;
        }
        
        // Simple parameter parsing, handling basic cases
        for param in params_str.split(',') {
            let param = param.trim();
            if param.is_empty() {
                continue;
            }
            
            // Parse parameter format: Type name, ref Type name, out Type name, params Type[] name, Type name = default
            let parts: Vec<&str> = param.split_whitespace().collect();
            if parts.len() >= 2 {
                let (param_type, name, is_optional) = if parts[0] == "ref" || parts[0] == "out" || parts[0] == "in" || parts[0] == "params" {
                    if parts.len() >= 3 {
                        (parts[1].to_string(), parts[2].to_string(), false)
                    } else {
                        continue;
                    }
                } else {
                    // Check for default value (optional parameter)
                    let has_default = param.contains('=');
                    let name = parts[1].split('=').next().unwrap_or(parts[1]).to_string();
                    (parts[0].to_string(), name, has_default)
                };
                
                // Handle generic types and nullable types
                let clean_type = if param_type.contains('<') || param_type.contains('?') {
                    param_type
                } else {
                    param_type
                };
                
                parameters.push(ParameterInfo {
                    name,
                    param_type: clean_type,
                    is_optional,
                    description: None,
                });
            }
        }
        
        parameters
    }
    
    /// Extract XML documentation comments
    fn extract_xml_doc(&self, lines: &[&str], current_line: usize) -> Option<String> {
        let mut doc_lines = Vec::new();
        
        // Search upward for XML doc comments
        for i in (0..current_line).rev() {
            let line = lines[i].trim();
            
            if line.starts_with("///") {
                let content = line.trim_start_matches("///").trim();
                // Extract content from <summary> tags
                if content.starts_with("<summary>") {
                    let text = content.trim_start_matches("<summary>").trim_end_matches("</summary>").trim();
                    if !text.is_empty() {
                        doc_lines.insert(0, text.to_string());
                    }
                } else if content.ends_with("</summary>") {
                    let text = content.trim_end_matches("</summary>").trim();
                    if !text.is_empty() {
                        doc_lines.insert(0, text.to_string());
                    }
                } else if !content.is_empty() && !content.starts_with('<') && !content.ends_with('>') {
                    doc_lines.insert(0, content.to_string());
                }
            } else if !line.is_empty() && !line.starts_with('[') {
                break;
            }
        }
        
        if doc_lines.is_empty() {
            None
        } else {
            Some(doc_lines.join(" "))
        }
    }

    /// Extract dependency name from C# using path
    fn extract_dependency_name(&self, using_path: &str) -> String {
        // For System.Collections.Generic, return Generic
        if let Some(namespace_name) = using_path.split('.').last() {
            namespace_name.to_string()
        } else {
            using_path.to_string()
        }
    }
}