gobby-code 1.3.3

Fast Rust CLI for Gobby's code index — AST-aware search, symbol navigation, and dependency graph
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
use super::*;

/// Directory roots acting as the repository's top decomposition units.
/// A top-level directory that holds no direct files is a container — like
/// `crates/` in a Cargo workspace — and expands one level so decomposition
/// starts at meaningful units (the individual crates) instead of the
/// container. Root-level files belong to no subsystem.
pub(crate) fn subsystem_roots(files: &[String]) -> BTreeSet<String> {
    let mut top_level = BTreeSet::new();
    let mut has_direct_files = BTreeSet::new();
    let mut children: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    for file in files {
        let module = module_for_file(file);
        let mut parts = module.split('/').filter(|part| !part.is_empty());
        let Some(top) = parts.next() else {
            continue;
        };
        top_level.insert(top.to_string());
        match parts.next() {
            None => {
                has_direct_files.insert(top.to_string());
            }
            Some(child) => {
                children
                    .entry(top.to_string())
                    .or_default()
                    .insert(format!("{top}/{child}"));
            }
        }
    }

    let mut roots = BTreeSet::new();
    for top in top_level {
        let expandable = !has_direct_files.contains(&top)
            && children.get(&top).is_some_and(|kids| !kids.is_empty());
        if expandable {
            roots.extend(children.remove(&top).unwrap_or_default());
        } else {
            roots.insert(top);
        }
    }
    roots
}

/// The subsystem root that owns `file`, when one exists.
pub(crate) fn subsystem_root_for_file<'a>(
    file: &str,
    roots: &'a BTreeSet<String>,
) -> Option<&'a str> {
    let module = module_for_file(file);
    roots
        .iter()
        .map(String::as_str)
        .find(|root| module_is_within(&module, root))
}

fn module_is_within(module: &str, root: &str) -> bool {
    module
        .strip_prefix(root)
        .is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
}

pub(crate) fn cluster_file_modules(
    files: &[String],
    symbols_by_file: &BTreeMap<String, Vec<Symbol>>,
    graph_edges: &[CodewikiGraphEdge],
) -> HashMap<String, String> {
    let mut components_to_file = HashMap::new();
    for (file, symbols) in symbols_by_file {
        for symbol in symbols {
            components_to_file.insert(symbol.id.clone(), file.clone());
        }
    }

    let roots = subsystem_roots(files);
    let mut parents = files
        .iter()
        .map(|file| (file.clone(), file.clone()))
        .collect::<HashMap<_, _>>();
    let mut ranks = files
        .iter()
        .map(|file| (file.clone(), 0_usize))
        .collect::<HashMap<_, _>>();
    for edge in graph_edges
        .iter()
        .filter(|edge| edge.kind == CodewikiGraphEdgeKind::Call)
    {
        let Some(source_file) = components_to_file.get(&edge.source_component_id) else {
            continue;
        };
        let Some(target_file) = components_to_file.get(&edge.target_component_id) else {
            continue;
        };
        // Call edges never merge clusters across subsystem roots; otherwise
        // one cross-subsystem call collapses the decomposition to the common
        // container (`crates`) and every page above it loses its structure.
        if subsystem_root_for_file(source_file, &roots)
            != subsystem_root_for_file(target_file, &roots)
        {
            continue;
        }
        union_files(&mut parents, &mut ranks, source_file, target_file);
    }

    let mut grouped: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for file in files {
        let root = find_file_root(&mut parents, file);
        grouped.entry(root).or_default().push(file.clone());
    }

    let mut modules = HashMap::new();
    for grouped_files in grouped.values() {
        let module = if grouped_files.len() > 1 {
            common_module_for_files(grouped_files)
        } else {
            module_for_file(&grouped_files[0])
        };
        for file in grouped_files {
            modules.insert(file.clone(), module.clone());
        }
    }
    modules
}

pub(crate) fn union_files(
    parents: &mut HashMap<String, String>,
    ranks: &mut HashMap<String, usize>,
    left: &str,
    right: &str,
) {
    let left_root = find_file_root(parents, left);
    let right_root = find_file_root(parents, right);
    if left_root == right_root {
        return;
    }

    let left_rank = *ranks.entry(left_root.clone()).or_default();
    let right_rank = *ranks.entry(right_root.clone()).or_default();
    let (parent, child, increment_rank) = match left_rank.cmp(&right_rank) {
        std::cmp::Ordering::Greater => (left_root, right_root, false),
        std::cmp::Ordering::Less => (right_root, left_root, false),
        std::cmp::Ordering::Equal if left_root <= right_root => (left_root, right_root, true),
        std::cmp::Ordering::Equal => (right_root, left_root, true),
    };
    parents.insert(child, parent.clone());
    if increment_rank {
        *ranks.entry(parent).or_default() += 1;
    }
}

/// Find the canonical root for a file in the module-union parent map.
///
/// The parent map is expected to represent a disjoint-set forest where roots
/// point to themselves. If a cycle slips in, choose the lexicographically
/// smallest file in the discovered cycle/path as the stable root. Every visited
/// node is then path-compressed to that root so future lookups keep the forest
/// invariant.
pub(crate) fn find_file_root(parents: &mut HashMap<String, String>, file: &str) -> String {
    let mut current = file.to_string();
    let mut path = Vec::new();
    let mut seen = HashSet::new();

    let root = loop {
        if !seen.insert(current.clone()) {
            path.push(current.clone());
            let root = path
                .iter()
                .min()
                .cloned()
                .unwrap_or_else(|| current.clone());
            debug_assert!(
                path.iter().any(|node| node == &current),
                "codewiki file union parent cycle detected while resolving `{file}`"
            );
            log::debug!(
                "codewiki file union parent cycle detected while resolving `{}`; compressing to `{}`",
                file,
                root
            );
            break root;
        }

        let parent = parents
            .get(&current)
            .cloned()
            .unwrap_or_else(|| current.clone());
        if parent == current {
            break parent;
        }

        path.push(current);
        current = parent;
    };

    for node in path {
        parents.insert(node, root.clone());
    }
    root
}

pub(crate) fn common_module_for_files(files: &[String]) -> String {
    if files.is_empty() {
        return String::new();
    }

    let mut common = module_for_file(&files[0])
        .split('/')
        .filter(|part| !part.is_empty())
        .map(str::to_string)
        .collect::<Vec<_>>();
    for file in &files[1..] {
        let parts = module_for_file(file)
            .split('/')
            .filter(|part| !part.is_empty())
            .map(str::to_string)
            .collect::<Vec<_>>();
        let keep = common
            .iter()
            .zip(parts.iter())
            .take_while(|(left, right)| left == right)
            .count();
        common.truncate(keep);
    }
    common.join("/")
}

pub(crate) fn symbols_by_file_component(symbols: &[Symbol]) -> BTreeMap<String, Vec<String>> {
    let mut out: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for symbol in symbols {
        if is_core_file(&symbol.file_path) {
            out.entry(symbol.file_path.clone())
                .or_default()
                .push(symbol.id.clone());
        }
    }
    out
}

pub(crate) fn first_component_for_file(
    symbols_by_file: &BTreeMap<String, Vec<String>>,
    file: &str,
) -> Option<String> {
    symbols_by_file
        .get(file)
        .and_then(|components| components.first())
        .cloned()
}

pub(crate) fn files_for_import_target<'a>(
    files: &'a [String],
    target_module: &str,
) -> Vec<&'a str> {
    let target = module_components(target_module);
    if target.is_empty() {
        return Vec::new();
    }
    files
        .iter()
        .map(String::as_str)
        .filter(|file| {
            contains_component_sequence(&path_components(file), &target)
                || contains_component_sequence(&module_components(&module_for_file(file)), &target)
        })
        .collect()
}

fn module_components(value: &str) -> Vec<String> {
    value
        .replace("::", "/")
        .replace(['.', '\\'], "/")
        .split('/')
        .filter(|part| !part.is_empty())
        .map(str::to_string)
        .collect()
}

fn path_components(file: &str) -> Vec<String> {
    Path::new(file)
        .components()
        .filter_map(|component| match component {
            std::path::Component::Normal(part) => {
                let part = part.to_string_lossy();
                let component = if Path::new(part.as_ref()).extension().is_some() {
                    Path::new(part.as_ref())
                        .file_stem()
                        .map(|stem| stem.to_string_lossy().into_owned())
                } else {
                    Some(part.into_owned())
                };
                component.filter(|part| !part.is_empty())
            }
            _ => None,
        })
        .collect()
}

fn contains_component_sequence(components: &[String], target: &[String]) -> bool {
    target.len() <= components.len()
        && components
            .windows(target.len())
            .any(|window| window.iter().zip(target).all(|(left, right)| left == right))
}

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

    fn paths(values: &[&str]) -> Vec<String> {
        values.iter().map(|value| (*value).to_string()).collect()
    }

    #[test]
    fn subsystem_roots_expand_container_directories_one_level() {
        let files = paths(&[
            "crates/gcode/src/main.rs",
            "crates/gcore/src/lib.rs",
            "docs/guides/guide.md",
            "scripts/install.sh",
            "README.md",
        ]);

        let roots = subsystem_roots(&files);

        assert!(roots.contains("crates/gcode"), "{roots:?}");
        assert!(roots.contains("crates/gcore"), "{roots:?}");
        assert!(!roots.contains("crates"), "container expands: {roots:?}");
        assert!(roots.contains("docs/guides"), "{roots:?}");
        assert!(roots.contains("scripts"), "{roots:?}");
    }

    #[test]
    fn subsystem_roots_keep_top_level_directories_with_direct_files() {
        let files = paths(&["docs/readme.md", "docs/guides/guide.md"]);
        let roots = subsystem_roots(&files);
        assert_eq!(roots, BTreeSet::from(["docs".to_string()]), "{roots:?}");
    }

    #[test]
    fn subsystem_root_for_file_matches_whole_components_only() {
        let roots = BTreeSet::from(["crates/gcode".to_string()]);
        assert_eq!(
            subsystem_root_for_file("crates/gcode/src/main.rs", &roots),
            Some("crates/gcode")
        );
        assert_eq!(
            subsystem_root_for_file("crates/gcodex/src/main.rs", &roots),
            None
        );
        assert_eq!(subsystem_root_for_file("README.md", &roots), None);
    }

    #[test]
    fn call_edges_never_merge_clusters_across_subsystem_roots() {
        let files = paths(&["crates/gcode/src/main.rs", "crates/gcore/src/lib.rs"]);
        let main_symbol = Symbol::make_id(
            "project-1",
            "crates/gcode/src/main.rs",
            "main",
            "function",
            0,
        );
        let lib_symbol = Symbol::make_id(
            "project-1",
            "crates/gcore/src/lib.rs",
            "helper",
            "function",
            0,
        );
        let mut symbols_by_file: BTreeMap<String, Vec<Symbol>> = BTreeMap::new();
        for (file, name, id) in [
            ("crates/gcode/src/main.rs", "main", &main_symbol),
            ("crates/gcore/src/lib.rs", "helper", &lib_symbol),
        ] {
            let symbol = Symbol {
                id: id.clone(),
                project_id: "project-1".to_string(),
                file_path: file.to_string(),
                name: name.to_string(),
                qualified_name: name.to_string(),
                kind: "function".to_string(),
                language: "rust".to_string(),
                byte_start: 0,
                byte_end: 0,
                line_start: 1,
                line_end: 1,
                signature: None,
                docstring: None,
                parent_symbol_id: None,
                content_hash: String::new(),
                summary: None,
                created_at: String::new(),
                updated_at: String::new(),
            };
            symbols_by_file.insert(file.to_string(), vec![symbol]);
        }
        let edges = vec![CodewikiGraphEdge::call(
            main_symbol.clone(),
            lib_symbol.clone(),
        )];

        let modules = cluster_file_modules(&files, &symbols_by_file, &edges);

        assert_eq!(
            modules.get("crates/gcode/src/main.rs").map(String::as_str),
            Some("crates/gcode/src"),
            "cross-root call must not collapse modules to `crates`: {modules:?}"
        );
        assert_eq!(
            modules.get("crates/gcore/src/lib.rs").map(String::as_str),
            Some("crates/gcore/src"),
            "{modules:?}"
        );
    }
}