Skip to main content

lean_ctx/tools/
ctx_repomap.rs

1//! Tool handler for `ctx_repomap` — PageRank-based repo map.
2
3use crate::core::repomap;
4
5/// Handle a repo map request.
6///
7/// - `project_root`: project root path
8/// - `max_tokens`: token budget (default 2048)
9/// - `focus_files`: optional list of files to boost in ranking
10/// - `session_files`: files from the active session (injected by MCP wrapper)
11pub fn handle(
12    project_root: &str,
13    max_tokens: usize,
14    focus_files: &[String],
15    session_files: &[String],
16) -> String {
17    let graph = repomap::RepoGraph::build(project_root);
18
19    if graph.files.is_empty() {
20        return format!(
21            "No indexable files found in '{project_root}'. \
22             Ensure it contains source files (.rs, .ts, .py, etc.)."
23        );
24    }
25
26    let ranked = repomap::rank_symbols(&graph, session_files, focus_files);
27
28    if ranked.is_empty() {
29        return format!(
30            "No symbols extracted from {} files in '{project_root}'.",
31            graph.files.len()
32        );
33    }
34
35    repomap::fit_to_budget(&ranked, max_tokens)
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn nonexistent_root_returns_helpful_message() {
44        let result = handle("/tmp/nonexistent_repo_map_test_dir", 2048, &[], &[]);
45        assert!(
46            result.contains("No indexable files") || result.contains("No symbols"),
47            "unexpected: {result}"
48        );
49    }
50}