deepwiki-rs 1.0.0

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
use super::{Dependency, LanguageProcessor};
use crate::types::code::InterfaceInfo;
use regex::Regex;
use std::path::Path;

#[derive(Debug)]
pub struct KotlinProcessor {
    import_regex: Regex,
    package_regex: Regex,
}

impl KotlinProcessor {
    pub fn new() -> Self {
        Self {
            import_regex: Regex::new(r"^\s*import\s+([^\s]+)").unwrap(),
            package_regex: Regex::new(r"^\s*package\s+([^\s]+)").unwrap(),
        }
    }
}

impl LanguageProcessor for KotlinProcessor {
    fn supported_extensions(&self) -> Vec<&'static str> {
        vec!["kt"]
    }

    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();

        for (line_num, line) in content.lines().enumerate() {
            // 提取import语句
            if let Some(captures) = self.import_regex.captures(line) {
                if let Some(import_path) = captures.get(1) {
                    let import_str = import_path.as_str();
                    let is_external = import_str.starts_with("android.")
                        || import_str.starts_with("androidx.")
                        || import_str.starts_with("kotlin.")
                        || import_str.starts_with("java.")
                        || !import_str.contains(".");

                    dependencies.push(Dependency {
                        name: source_file.clone(),
                        path: Some(import_str.to_string()),
                        is_external,
                        line_number: Some(line_num + 1),
                        dependency_type: "import".to_string(),
                        version: None,
                    });
                }
            }

            // 提取package语句
            if let Some(captures) = self.package_regex.captures(line) {
                if let Some(package_name) = captures.get(1) {
                    dependencies.push(Dependency {
                        name: source_file.clone(),
                        path: Some(package_name.as_str().to_string()),
                        is_external: false,
                        line_number: Some(line_num + 1),
                        dependency_type: "package".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("");

        // 检查特殊文件名模式
        if file_name.ends_with("Activity.kt") {
            return "android_activity".to_string();
        }

        if file_name.ends_with("Fragment.kt") {
            return "android_fragment".to_string();
        }

        if file_name.ends_with("Service.kt") {
            return "android_service".to_string();
        }

        if file_name.ends_with("Repository.kt") {
            return "kotlin_repository".to_string();
        }

        if file_name.ends_with("ViewModel.kt") {
            return "kotlin_viewmodel".to_string();
        }

        if file_name.ends_with("Model.kt") || file_name.ends_with("Entity.kt") {
            return "kotlin_model".to_string();
        }

        if file_name.ends_with("Utils.kt") || file_name.ends_with("Helper.kt") {
            return "kotlin_utility".to_string();
        }

        // 检查内容模式
        if content.contains("class ") && content.contains(": Activity") {
            "android_activity".to_string()
        } else if content.contains("class ") && content.contains(": Fragment") {
            "android_fragment".to_string()
        } else if content.contains("class ") && content.contains(": Service") {
            "android_service".to_string()
        } else if content.contains("class ") && content.contains(": ViewModel") {
            "kotlin_viewmodel".to_string()
        } else if content.contains("interface ") {
            "kotlin_interface".to_string()
        } else if content.contains("object ") {
            "kotlin_object".to_string()
        } else if content.contains("enum class") {
            "kotlin_enum".to_string()
        } else if content.contains("data class") {
            "kotlin_data_class".to_string()
        } else if content.contains("class ") {
            "kotlin_class".to_string()
        } else {
            "kotlin_file".to_string()
        }
    }

    fn is_important_line(&self, line: &str) -> bool {
        let trimmed = line.trim();

        // 类、接口、对象定义
        if trimmed.starts_with("class ")
            || trimmed.starts_with("interface ")
            || trimmed.starts_with("object ")
            || trimmed.starts_with("enum class ")
            || trimmed.starts_with("data class ")
            || trimmed.starts_with("sealed class ")
        {
            return true;
        }

        // 函数定义
        if trimmed.starts_with("fun ")
            || trimmed.starts_with("suspend fun ")
            || trimmed.starts_with("inline fun ")
            || trimmed.starts_with("private fun ")
            || trimmed.starts_with("public fun ")
            || trimmed.starts_with("internal fun ")
        {
            return true;
        }

        // 属性定义
        if trimmed.starts_with("val ")
            || trimmed.starts_with("var ")
            || trimmed.starts_with("const val ")
            || trimmed.starts_with("lateinit var ")
        {
            return true;
        }

        // 注解
        if trimmed.starts_with("@") {
            return true;
        }

        // 导入和包声明
        if trimmed.starts_with("import ") || trimmed.starts_with("package ") {
            return true;
        }

        // 重要注释
        if trimmed.contains("TODO")
            || trimmed.contains("FIXME")
            || trimmed.contains("NOTE")
            || trimmed.contains("HACK")
        {
            return true;
        }

        false
    }

    fn language_name(&self) -> &'static str {
        "Kotlin"
    }

    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() {
            let trimmed = line.trim();

            // 提取函数定义
            if trimmed.starts_with("fun ") || trimmed.contains(" fun ") {
                if let Some(func_name) = self.extract_kotlin_function(trimmed) {
                    let visibility = self.extract_kotlin_visibility(trimmed);
                    let is_suspend = trimmed.contains("suspend");
                    let interface_type = if is_suspend {
                        "suspend_function"
                    } else {
                        "function"
                    };

                    interfaces.push(InterfaceInfo {
                        name: func_name,
                        interface_type: interface_type.to_string(),
                        visibility,
                        parameters: Vec::new(),
                        return_type: self.extract_kotlin_return_type(trimmed),
                        description: self.extract_kotlin_comment(&lines, i),
                    });
                }
            }

            // 提取类定义
            if trimmed.starts_with("class ") || trimmed.contains(" class ") {
                if let Some(class_name) = self.extract_kotlin_class(trimmed) {
                    let visibility = self.extract_kotlin_visibility(trimmed);
                    let is_data = trimmed.contains("data class");
                    let is_sealed = trimmed.contains("sealed class");
                    let interface_type = if is_data {
                        "data_class"
                    } else if is_sealed {
                        "sealed_class"
                    } else {
                        "class"
                    };

                    interfaces.push(InterfaceInfo {
                        name: class_name,
                        interface_type: interface_type.to_string(),
                        visibility,
                        parameters: Vec::new(),
                        return_type: None,
                        description: self.extract_kotlin_comment(&lines, i),
                    });
                }
            }

            // 提取接口定义
            if trimmed.starts_with("interface ") || trimmed.contains(" interface ") {
                if let Some(interface_name) = self.extract_kotlin_interface(trimmed) {
                    let visibility = self.extract_kotlin_visibility(trimmed);

                    interfaces.push(InterfaceInfo {
                        name: interface_name,
                        interface_type: "interface".to_string(),
                        visibility,
                        parameters: Vec::new(),
                        return_type: None,
                        description: self.extract_kotlin_comment(&lines, i),
                    });
                }
            }

            // 提取对象定义
            if trimmed.starts_with("object ") || trimmed.contains(" object ") {
                if let Some(object_name) = self.extract_kotlin_object(trimmed) {
                    let visibility = self.extract_kotlin_visibility(trimmed);

                    interfaces.push(InterfaceInfo {
                        name: object_name,
                        interface_type: "object".to_string(),
                        visibility,
                        parameters: Vec::new(),
                        return_type: None,
                        description: self.extract_kotlin_comment(&lines, i),
                    });
                }
            }
        }

        interfaces
    }
}

impl KotlinProcessor {
    /// 提取Kotlin函数名称
    fn extract_kotlin_function(&self, line: &str) -> Option<String> {
        if let Some(fun_pos) = line.find("fun ") {
            let after_fun = &line[fun_pos + 4..];
            if let Some(paren_pos) = after_fun.find('(') {
                let func_name = after_fun[..paren_pos].trim();
                if !func_name.is_empty() {
                    return Some(func_name.to_string());
                }
            }
        }
        None
    }

    /// 提取Kotlin类名称
    fn extract_kotlin_class(&self, line: &str) -> Option<String> {
        if let Some(class_pos) = line.find("class ") {
            let after_class = &line[class_pos + 6..];
            let class_name = if let Some(space_pos) = after_class.find(' ') {
                after_class[..space_pos].trim()
            } else if let Some(paren_pos) = after_class.find('(') {
                after_class[..paren_pos].trim()
            } else if let Some(brace_pos) = after_class.find('{') {
                after_class[..brace_pos].trim()
            } else {
                after_class.trim()
            };

            if !class_name.is_empty() {
                return Some(class_name.to_string());
            }
        }
        None
    }

    /// 提取Kotlin接口名称
    fn extract_kotlin_interface(&self, line: &str) -> Option<String> {
        if let Some(interface_pos) = line.find("interface ") {
            let after_interface = &line[interface_pos + 10..];
            let interface_name = if let Some(space_pos) = after_interface.find(' ') {
                after_interface[..space_pos].trim()
            } else if let Some(brace_pos) = after_interface.find('{') {
                after_interface[..brace_pos].trim()
            } else {
                after_interface.trim()
            };

            if !interface_name.is_empty() {
                return Some(interface_name.to_string());
            }
        }
        None
    }

    /// 提取Kotlin对象名称
    fn extract_kotlin_object(&self, line: &str) -> Option<String> {
        if let Some(object_pos) = line.find("object ") {
            let after_object = &line[object_pos + 7..];
            let object_name = if let Some(space_pos) = after_object.find(' ') {
                after_object[..space_pos].trim()
            } else if let Some(brace_pos) = after_object.find('{') {
                after_object[..brace_pos].trim()
            } else {
                after_object.trim()
            };

            if !object_name.is_empty() {
                return Some(object_name.to_string());
            }
        }
        None
    }

    /// 提取Kotlin可见性修饰符
    fn extract_kotlin_visibility(&self, line: &str) -> String {
        if line.contains("private ") {
            "private".to_string()
        } else if line.contains("protected ") {
            "protected".to_string()
        } else if line.contains("internal ") {
            "internal".to_string()
        } else {
            "public".to_string()
        }
    }

    /// 提取Kotlin返回类型
    fn extract_kotlin_return_type(&self, line: &str) -> Option<String> {
        if let Some(colon_pos) = line.find(": ") {
            let after_colon = &line[colon_pos + 2..];
            if let Some(brace_pos) = after_colon.find('{') {
                let return_type = after_colon[..brace_pos].trim();
                if !return_type.is_empty() {
                    return Some(return_type.to_string());
                }
            } else if let Some(eq_pos) = after_colon.find('=') {
                let return_type = after_colon[..eq_pos].trim();
                if !return_type.is_empty() {
                    return Some(return_type.to_string());
                }
            }
        }
        None
    }

    /// 提取Kotlin注释
    fn extract_kotlin_comment(&self, lines: &[&str], current_line: usize) -> Option<String> {
        let mut doc_lines = Vec::new();

        // 向上查找注释
        for i in (0..current_line).rev() {
            let line = lines[i].trim();

            if line.starts_with("//") {
                doc_lines.insert(0, line.trim_start_matches("//").trim().to_string());
            } else if line.starts_with("/*") && line.ends_with("*/") {
                let content = line.trim_start_matches("/*").trim_end_matches("*/").trim();
                doc_lines.insert(0, content.to_string());
                break;
            } else if !line.is_empty() {
                break;
            }
        }

        if doc_lines.is_empty() {
            None
        } else {
            Some(doc_lines.join(" "))
        }
    }
}