leankg 0.16.7

Lightweight Knowledge Graph for AI-Assisted Development
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
use crate::db::models::{CodeElement, Relationship};
use regex;

pub struct JetpackNavExtractor<'a> {
    source: &'a [u8],
    file_path: &'a str,
}

impl<'a> JetpackNavExtractor<'a> {
    pub fn new(source: &'a [u8], file_path: &'a str) -> Self {
        Self { source, file_path }
    }

    /// Parse Android XML navigation graph files (`res/navigation/*.xml`).
    pub fn extract_xml(&self) -> (Vec<CodeElement>, Vec<Relationship>) {
        let content = match std::str::from_utf8(self.source) {
            Ok(s) => s,
            Err(_) => return (Vec::new(), Vec::new()),
        };

        // Ensure the android namespace is declared — real-world nav XMLs always
        // have it, but some test fixtures omit it.  Inject it when missing so
        // roxmltree (which is strict about undeclared prefixes) can parse the doc.
        let injected;
        let content: &str = if content.contains("android:") && !content.contains("xmlns:android") {
            injected = content.replacen(
                "<navigation",
                "<navigation xmlns:android=\"http://schemas.android.com/apk/res/android\"",
                1,
            );
            &injected
        } else {
            content
        };

        let doc = match roxmltree::Document::parse(content) {
            Ok(d) => d,
            Err(_) => return (Vec::new(), Vec::new()),
        };

        let mut elements: Vec<CodeElement> = Vec::new();
        let mut relationships: Vec<Relationship> = Vec::new();

        let root = doc.root_element();

        // Only process <navigation> root elements
        if root.tag_name().name() != "navigation" {
            return (Vec::new(), Vec::new());
        }

        let graph_id = android_id(&root).unwrap_or_else(|| "unknown".to_string());
        let graph_qn = format!("{}::nav_graph::{}", self.file_path, graph_id);

        let start_dest_raw = root
            .attributes()
            .find(|a| {
                a.name() == "startDestination"
                    && a.namespace() == Some("http://schemas.android.com/apk/res-auto")
            })
            .map(|a| a.value().to_string());
        let start_dest_id = start_dest_raw.as_deref().map(strip_id_prefix);

        // nav_graph element
        elements.push(CodeElement {
            qualified_name: graph_qn.clone(),
            element_type: "nav_graph".to_string(),
            name: graph_id.clone(),
            file_path: self.file_path.to_string(),
            line_start: root.range().start as u32,
            line_end: root.range().end as u32,
            language: "xml".to_string(),
            metadata: serde_json::json!({
                "graph_id": graph_id,
                "start_destination": start_dest_id,
            }),
            ..Default::default()
        });

        // Destination tags
        const DEST_TAGS: &[&str] = &["fragment", "activity", "dialog"];

        for child in root.children().filter(|n| n.is_element()) {
            let tag = child.tag_name().name();
            if !DEST_TAGS.contains(&tag) {
                continue;
            }

            let dest_id = match android_id(&child) {
                Some(id) => id,
                None => continue,
            };
            let dest_qn = format!("{}::{}", graph_qn, dest_id);
            let is_start = start_dest_id.map(|s| s == dest_id).unwrap_or(false);

            let class_name = android_attr(&child, "name");

            elements.push(CodeElement {
                qualified_name: dest_qn.clone(),
                element_type: "nav_destination".to_string(),
                name: dest_id.clone(),
                file_path: self.file_path.to_string(),
                line_start: child.range().start as u32,
                line_end: child.range().end as u32,
                language: "xml".to_string(),
                parent_qualified: Some(graph_qn.clone()),
                metadata: serde_json::json!({
                    "destination_id": dest_id,
                    "dest_type": tag,
                    "class_name": class_name,
                    "start_destination": is_start,
                }),
                ..Default::default()
            });

            // Children: action, argument, deepLink
            for sub in child.children().filter(|n| n.is_element()) {
                match sub.tag_name().name() {
                    "action" => {
                        let action_id = android_id(&sub);
                        let target_raw = app_attr(&sub, "destination");
                        let target_id = target_raw.as_deref().map(strip_id_prefix);
                        let pop_up_to = app_attr(&sub, "popUpTo");

                        if let Some(target) = target_id {
                            let target_qn = format!("{}::{}", graph_qn, target);
                            relationships.push(Relationship {
                                id: None,
                                source_qualified: dest_qn.clone(),
                                target_qualified: target_qn,
                                rel_type: "nav_action".to_string(),
                                confidence: 1.0,
                                metadata: serde_json::json!({
                                    "action_id": action_id,
                                    "pop_up_to": pop_up_to,
                                }),
                            });
                        }
                    }
                    "argument" => {
                        let arg_name = match android_attr(&sub, "name") {
                            Some(n) => n,
                            None => continue,
                        };
                        let arg_qn = format!("{}::arg::{}", dest_qn, arg_name);
                        let arg_type =
                            app_attr(&sub, "argType").unwrap_or_else(|| "string".to_string());
                        let nullable = app_attr(&sub, "nullable")
                            .map(|v| v == "true")
                            .unwrap_or(false);

                        elements.push(CodeElement {
                            qualified_name: arg_qn.clone(),
                            element_type: "nav_argument".to_string(),
                            name: arg_name.clone(),
                            file_path: self.file_path.to_string(),
                            line_start: sub.range().start as u32,
                            line_end: sub.range().end as u32,
                            language: "xml".to_string(),
                            parent_qualified: Some(dest_qn.clone()),
                            metadata: serde_json::json!({
                                "arg_type": arg_type,
                                "nullable": nullable,
                            }),
                            ..Default::default()
                        });

                        relationships.push(Relationship {
                            id: None,
                            source_qualified: dest_qn.clone(),
                            target_qualified: arg_qn,
                            rel_type: "requires_arg".to_string(),
                            confidence: 1.0,
                            metadata: serde_json::json!({
                                "arg_name": arg_name,
                            }),
                        });
                    }
                    "deepLink" => {
                        let uri = match app_attr(&sub, "uri") {
                            Some(u) => u,
                            None => continue,
                        };
                        let dl_qn = format!("{}::deeplink::{}", dest_qn, uri);

                        elements.push(CodeElement {
                            qualified_name: dl_qn.clone(),
                            element_type: "nav_deep_link".to_string(),
                            name: uri.clone(),
                            file_path: self.file_path.to_string(),
                            line_start: sub.range().start as u32,
                            line_end: sub.range().end as u32,
                            language: "xml".to_string(),
                            parent_qualified: Some(dest_qn.clone()),
                            metadata: serde_json::json!({ "uri": uri }),
                            ..Default::default()
                        });

                        relationships.push(Relationship {
                            id: None,
                            source_qualified: dl_qn,
                            target_qualified: dest_qn.clone(),
                            rel_type: "deep_link".to_string(),
                            confidence: 1.0,
                            metadata: serde_json::Value::Object(serde_json::Map::new()),
                        });
                    }
                    _ => {}
                }
            }
        }

        (elements, relationships)
    }

    /// Parse Compose Navigation DSL in Kotlin source files.
    pub fn extract_kotlin_dsl(&self) -> (Vec<CodeElement>, Vec<Relationship>) {
        let content = match std::str::from_utf8(self.source) {
            Ok(s) => s,
            Err(_) => return (Vec::new(), Vec::new()),
        };

        let mut elements: Vec<CodeElement> = Vec::new();
        let mut relationships: Vec<Relationship> = Vec::new();

        let graph_id = "compose_nav".to_string();
        let graph_qn = format!("{}::nav_graph::{}", self.file_path, graph_id);

        // Create a root nav_graph element for the compose DSL
        elements.push(CodeElement {
            qualified_name: graph_qn.clone(),
            element_type: "nav_graph".to_string(),
            name: graph_id.clone(),
            file_path: self.file_path.to_string(),
            line_start: 0,
            line_end: 0,
            language: "kotlin".to_string(),
            metadata: serde_json::json!({
                "graph_id": graph_id,
                "dsl_type": "compose",
            }),
            ..Default::default()
        });

        // Extract composable() route definitions: composable(route = "...")
        // Regex: composable\s*\(\s*route\s*=\s*"([^"]+)"
        let composable_re = regex::Regex::new(r#"composable\s*\(\s*route\s*=\s*"([^"]+)"#)
            .unwrap_or_else(|_| regex::Regex::new(r"^\x00$").unwrap());

        for cap in composable_re.captures_iter(content) {
            if let Some(route_match) = cap.get(1) {
                let route = route_match.as_str();
                let dest_id = route.to_string();
                let dest_qn = format!("{}::{}", graph_qn, dest_id);

                elements.push(CodeElement {
                    qualified_name: dest_qn.clone(),
                    element_type: "nav_destination".to_string(),
                    name: dest_id.clone(),
                    file_path: self.file_path.to_string(),
                    line_start: 0,
                    line_end: 0,
                    language: "kotlin".to_string(),
                    parent_qualified: Some(graph_qn.clone()),
                    metadata: serde_json::json!({
                        "destination_id": dest_id,
                        "dest_type": "composable",
                        "route": route,
                    }),
                    ..Default::default()
                });
            }
        }

        // Extract navigation() blocks: navigation(route = "...", startDestination = "...")
        let nav_re = regex::Regex::new(
            r#"navigation\s*\(\s*route\s*=\s*"([^"]+)"\s*,\s*startDestination\s*=\s*"([^"]+)""#,
        )
        .unwrap_or_else(|_| regex::Regex::new(r"^\x00$").unwrap());

        for cap in nav_re.captures_iter(content) {
            if let (Some(route_match), Some(start_match)) = (cap.get(1), cap.get(2)) {
                let route = route_match.as_str();
                let start_dest = start_match.as_str();
                let dest_id = route.to_string();
                let dest_qn = format!("{}::{}", graph_qn, dest_id);

                elements.push(CodeElement {
                    qualified_name: dest_qn.clone(),
                    element_type: "nav_destination".to_string(),
                    name: dest_id.clone(),
                    file_path: self.file_path.to_string(),
                    line_start: 0,
                    line_end: 0,
                    language: "kotlin".to_string(),
                    parent_qualified: Some(graph_qn.clone()),
                    metadata: serde_json::json!({
                        "destination_id": dest_id,
                        "dest_type": "navigation",
                        "route": route,
                        "start_destination": start_dest,
                    }),
                    ..Default::default()
                });
            }
        }

        // Extract argument definitions: argument(name = "...")
        // Regex: argument\s*\(\s*name\s*=\s*"([^"]+)"
        let arg_re = regex::Regex::new(r#"argument\s*\(\s*name\s*=\s*"([^"]+)"#)
            .unwrap_or_else(|_| regex::Regex::new(r"^\x00$").unwrap());

        for cap in arg_re.captures_iter(content) {
            if let Some(arg_match) = cap.get(1) {
                let arg_name = arg_match.as_str();
                // For simplicity, assume argument belongs to the first composable found
                if let Some(first_dest_qn) = elements
                    .iter()
                    .find(|e| e.element_type == "nav_destination")
                    .map(|e| e.qualified_name.clone())
                {
                    let arg_qn = format!("{}::arg::{}", first_dest_qn, arg_name);
                    let arg_type = "string".to_string();
                    let nullable = false;

                    elements.push(CodeElement {
                        qualified_name: arg_qn.clone(),
                        element_type: "nav_argument".to_string(),
                        name: arg_name.to_string(),
                        file_path: self.file_path.to_string(),
                        line_start: 0,
                        line_end: 0,
                        language: "kotlin".to_string(),
                        parent_qualified: Some(first_dest_qn.clone()),
                        metadata: serde_json::json!({
                            "arg_type": arg_type,
                            "nullable": nullable,
                        }),
                        ..Default::default()
                    });

                    relationships.push(Relationship {
                        id: None,
                        source_qualified: first_dest_qn.clone(),
                        target_qualified: arg_qn,
                        rel_type: "requires_arg".to_string(),
                        confidence: 0.85,
                        metadata: serde_json::json!({
                            "arg_name": arg_name,
                        }),
                    });
                }
            }
        }

        // Extract navigate() calls to infer navigation actions
        // Regex: navigate\s*\(\s*"([^"]+)"
        let navigate_re = regex::Regex::new(r#"navigate\s*\(\s*"([^"]+)""#)
            .unwrap_or_else(|_| regex::Regex::new(r"^\x00$").unwrap());

        let mut seen_nav = std::collections::HashSet::new();
        for cap in navigate_re.captures_iter(content) {
            if let Some(dest_match) = cap.get(1) {
                let target_route = dest_match.as_str();
                let key = target_route.to_string();
                if !seen_nav.contains(&key) && !elements.is_empty() {
                    seen_nav.insert(key);
                    // Create implicit action from first destination to the target
                    if let Some(source_dest) = elements
                        .iter()
                        .find(|e| e.element_type == "nav_destination")
                    {
                        let target_qn = format!("{}::{}", graph_qn, target_route);
                        relationships.push(Relationship {
                            id: None,
                            source_qualified: source_dest.qualified_name.clone(),
                            target_qualified: target_qn,
                            rel_type: "nav_action".to_string(),
                            confidence: 0.75,
                            metadata: serde_json::json!({
                                "action_id": None::<String>,
                            }),
                        });
                    }
                }
            }
        }

        (elements, relationships)
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

const NS_ANDROID: &str = "http://schemas.android.com/apk/res/android";
const NS_APP: &str = "http://schemas.android.com/apk/res-auto";

/// Get the value of `android:<attr_name>` on a node.
fn android_attr(node: &roxmltree::Node, attr_name: &str) -> Option<String> {
    node.attributes()
        .find(|a| a.name() == attr_name && a.namespace() == Some(NS_ANDROID))
        .map(|a| a.value().to_string())
}

/// Get the value of `app:<attr_name>` on a node.
fn app_attr(node: &roxmltree::Node, attr_name: &str) -> Option<String> {
    node.attributes()
        .find(|a| a.name() == attr_name && a.namespace() == Some(NS_APP))
        .map(|a| a.value().to_string())
}

/// Get the stripped `android:id` value (strips `@+id/` / `@id/` prefix).
fn android_id(node: &roxmltree::Node) -> Option<String> {
    android_attr(node, "id").map(|v| strip_id_prefix(&v).to_string())
}

/// Strip `@+id/` or `@id/` prefix from an Android resource reference.
fn strip_id_prefix(s: &str) -> &str {
    if let Some(rest) = s.strip_prefix("@+id/") {
        rest
    } else if let Some(rest) = s.strip_prefix("@id/") {
        rest
    } else {
        s
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_xml_nav_graph_destinations() {
        let xml = r#"<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.HomeFragment">
        <action
            android:id="@+id/action_home_to_detail"
            app:destination="@id/detailFragment" />
        <argument
            android:name="userId"
            app:argType="string"
            app:nullable="true" />
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:name="com.example.DetailFragment">
        <deepLink app:uri="example://detail/{id}" />
    </fragment>
</navigation>"#;

        let extractor = JetpackNavExtractor::new(xml.as_bytes(), "res/navigation/nav_graph.xml");
        let (elements, relationships) = extractor.extract_xml();

        let destinations: Vec<_> = elements
            .iter()
            .filter(|e| e.element_type == "nav_destination")
            .collect();
        assert_eq!(destinations.len(), 2, "Should find 2 destinations");
        assert!(destinations.iter().any(|e| e.name == "homeFragment"));
        assert!(destinations.iter().any(|e| e.name == "detailFragment"));

        let actions: Vec<_> = relationships
            .iter()
            .filter(|r| r.rel_type == "nav_action")
            .collect();
        assert_eq!(actions.len(), 1, "Should find 1 action");

        let args: Vec<_> = elements
            .iter()
            .filter(|e| e.element_type == "nav_argument")
            .collect();
        assert_eq!(args.len(), 1, "Should find 1 argument (userId)");

        let deep_links: Vec<_> = relationships
            .iter()
            .filter(|r| r.rel_type == "deep_link")
            .collect();
        assert_eq!(deep_links.len(), 1, "Should find 1 deep link");
    }

    #[test]
    fn test_xml_nav_start_destination() {
        let xml = r#"<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_main"
    app:startDestination="@id/loginFragment">
    <fragment android:id="@+id/loginFragment" android:name="com.example.LoginFragment" />
    <fragment android:id="@+id/dashboardFragment" android:name="com.example.DashboardFragment" />
</navigation>"#;

        let extractor = JetpackNavExtractor::new(xml.as_bytes(), "res/navigation/nav_main.xml");
        let (elements, _) = extractor.extract_xml();

        let nav_graph = elements.iter().find(|e| e.element_type == "nav_graph");
        assert!(nav_graph.is_some(), "Should have a nav_graph element");

        let start = elements
            .iter()
            .find(|e| e.element_type == "nav_destination" && e.name == "loginFragment");
        assert!(start.is_some());
        assert_eq!(
            start
                .unwrap()
                .metadata
                .get("start_destination")
                .and_then(|v| v.as_bool()),
            Some(true),
            "loginFragment should be marked as start destination"
        );
    }
}