codelens-engine 1.9.37

Harness-native Rust MCP server for code intelligence — 107 tools, 25 languages, tree-sitter + hybrid semantic search, 6.1x fewer tokens than rg+cat on agent tasks
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use crate::project::{ProjectRoot, collect_files};
use crate::symbols::{SymbolInfo, get_symbols_overview};
use anyhow::{Result, bail};
use regex::Regex;
use serde::Serialize;
use std::collections::HashMap;
use std::fs;
use std::sync::LazyLock;

static IDENTIFIER_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap());

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RenameScope {
    File,
    Project,
}

#[derive(Debug, Clone, Serialize)]
pub struct RenameEdit {
    pub file_path: String,
    pub line: usize,
    pub column: usize,
    pub old_text: String,
    pub new_text: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct RenameResult {
    pub success: bool,
    pub message: String,
    pub modified_files: usize,
    pub total_replacements: usize,
    pub edits: Vec<RenameEdit>,
}

/// Rename a symbol across one file or the entire project.
///
/// - `file_path`: the file containing the symbol declaration
/// - `symbol_name`: current name of the symbol
/// - `new_name`: desired new name
/// - `name_path`: optional qualified name path (e.g. "Service/run")
/// - `scope`: File (declaration scope only) or Project (all references)
/// - `dry_run`: if true, returns edits without modifying files
pub fn rename_symbol(
    project: &ProjectRoot,
    file_path: &str,
    symbol_name: &str,
    new_name: &str,
    name_path: Option<&str>,
    scope: RenameScope,
    dry_run: bool,
) -> Result<RenameResult> {
    validate_identifier(new_name)?;

    if symbol_name == new_name {
        return Ok(RenameResult {
            success: true,
            message: "Symbol name unchanged".to_string(),
            modified_files: 0,
            total_replacements: 0,
            edits: vec![],
        });
    }

    let edits = match scope {
        RenameScope::File => {
            collect_file_scope_edits(project, file_path, symbol_name, new_name, name_path)?
        }
        RenameScope::Project => {
            collect_project_scope_edits(project, file_path, symbol_name, new_name, name_path)?
        }
    };

    let modified_files = edits
        .iter()
        .map(|e| &e.file_path)
        .collect::<std::collections::HashSet<_>>()
        .len();
    let total_replacements = edits.len();

    if !dry_run {
        apply_edits(project, &edits)?;
    }

    Ok(RenameResult {
        success: true,
        message: format!(
            "{} {} replacement(s) in {} file(s)",
            if dry_run { "Would make" } else { "Made" },
            total_replacements,
            modified_files
        ),
        modified_files,
        total_replacements,
        edits,
    })
}

fn validate_identifier(name: &str) -> Result<()> {
    if !IDENTIFIER_RE.is_match(name) {
        bail!("invalid identifier: '{name}' — must match [a-zA-Z_][a-zA-Z0-9_]*");
    }
    Ok(())
}

/// FILE scope: only rename within the declaration's body range.
fn collect_file_scope_edits(
    project: &ProjectRoot,
    file_path: &str,
    symbol_name: &str,
    new_name: &str,
    name_path: Option<&str>,
) -> Result<Vec<RenameEdit>> {
    let resolved = project.resolve(file_path)?;
    let source = fs::read_to_string(&resolved)?;
    let lines: Vec<&str> = source.lines().collect();

    // Find symbol to get its line range
    let (start_line, end_line) =
        find_symbol_line_range(project, file_path, symbol_name, name_path)?;

    let word_re = Regex::new(&format!(r"\b{}\b", regex::escape(symbol_name)))?;
    let mut edits = Vec::new();

    for (line_idx, line) in lines
        .iter()
        .enumerate()
        .take(end_line.min(lines.len()))
        .skip(start_line.saturating_sub(1))
    {
        for mat in word_re.find_iter(line) {
            edits.push(RenameEdit {
                file_path: file_path.to_string(),
                line: line_idx + 1,
                column: mat.start() + 1,
                old_text: symbol_name.to_string(),
                new_text: new_name.to_string(),
            });
        }
    }

    Ok(edits)
}

/// PROJECT scope: rename in declaration file + all referencing files.
fn collect_project_scope_edits(
    project: &ProjectRoot,
    file_path: &str,
    symbol_name: &str,
    new_name: &str,
    name_path: Option<&str>,
) -> Result<Vec<RenameEdit>> {
    // Step 1: Find ALL word-boundary matches across project (handles multiple per line)
    let all_matches = find_all_word_matches(project, symbol_name)?;

    // Step 2: Get files that have their own declaration of the same name (shadowing)
    let shadow_files =
        find_shadowing_files(project, file_path, symbol_name, name_path, &all_matches)?;

    // Step 3: Build edits, skipping files with shadowed declarations
    let mut edits = Vec::new();
    for (match_file, line, column) in &all_matches {
        if match_file != file_path && shadow_files.contains(match_file) {
            continue;
        }
        edits.push(RenameEdit {
            file_path: match_file.clone(),
            line: *line,
            column: *column,
            old_text: symbol_name.to_string(),
            new_text: new_name.to_string(),
        });
    }

    Ok(edits)
}

/// Find ALL word-boundary matches of `symbol_name` across the project.
/// Unlike search_for_pattern, this returns multiple matches per line via find_iter.
pub fn find_all_word_matches(
    project: &ProjectRoot,
    symbol_name: &str,
) -> Result<Vec<(String, usize, usize)>> {
    let candidate_files = collect_candidate_files(project)?;

    if candidate_files.is_empty() {
        return Ok(Vec::new());
    }

    // Fast path: use indexed file list only when it fully covers the current
    // project. Partial or empty DBs must not suppress project-wide rename hits.
    let db_path = crate::db::index_db_path(project.as_path());
    if db_path.exists()
        && let Ok(db) = crate::db::IndexDb::open(&db_path)
        && let Ok(indexed_files) = db.all_file_paths()
        && indexed_files.len() >= candidate_files.len()
    {
        let indexed_set: std::collections::HashSet<&str> =
            indexed_files.iter().map(String::as_str).collect();
        if candidate_files
            .iter()
            .all(|path| indexed_set.contains(path.as_str()))
        {
            return find_word_matches_in_files(project, symbol_name, &indexed_files);
        }
    }

    find_word_matches_in_files(project, symbol_name, &candidate_files)
}

fn collect_candidate_files(project: &ProjectRoot) -> Result<Vec<String>> {
    Ok(collect_files(project.as_path(), |path| {
        crate::lang_config::language_for_path(path).is_some()
    })?
    .into_iter()
    .map(|path| project.to_relative(path))
    .collect())
}

/// Fast path: scan only indexed files (from DB).
/// Filters out matches inside comments and string literals using tree-sitter.
fn find_word_matches_in_files(
    project: &ProjectRoot,
    symbol_name: &str,
    files: &[String],
) -> Result<Vec<(String, usize, usize)>> {
    let word_re = Regex::new(&format!(r"\b{}\b", regex::escape(symbol_name)))?;
    let mut results = Vec::new();
    let mut non_code_cache: HashMap<std::path::PathBuf, Vec<(usize, usize)>> = HashMap::new();
    for rel in files {
        let abs = project.as_path().join(rel);
        let content = match fs::read_to_string(&abs) {
            Ok(c) => c,
            Err(_) => continue,
        };
        // Build non-code byte ranges with per-file cache
        let non_code = non_code_cache
            .entry(abs.clone())
            .or_insert_with(|| build_non_code_ranges(&abs, content.as_bytes()));

        let mut byte_offset = 0usize;
        for (line_idx, raw_line) in content.split_inclusive('\n').enumerate() {
            let line = raw_line.strip_suffix('\n').unwrap_or(raw_line);
            let line = line.strip_suffix('\r').unwrap_or(line);
            for mat in word_re.find_iter(line) {
                let abs_start = byte_offset + mat.start();
                if !is_in_ranges(non_code, abs_start) {
                    results.push((rel.clone(), line_idx + 1, mat.start() + 1));
                }
            }
            byte_offset += raw_line.len();
        }
    }
    Ok(results)
}

/// Node kinds that represent comments or string literals across languages.
const NON_CODE_KINDS: &[&str] = &[
    "comment",
    "line_comment",
    "block_comment",
    "string",
    "string_literal",
    "raw_string_literal",
    "template_string",
    "string_content",
    "interpreted_string_literal",
    "heredoc_body",
    "regex_literal",
];

/// Build byte ranges of non-code nodes (comments + strings) using tree-sitter.
fn build_non_code_ranges(path: &std::path::Path, source: &[u8]) -> Vec<(usize, usize)> {
    let Some(config) = crate::lang_config::language_for_path(path) else {
        return Vec::new();
    };
    let mut parser = tree_sitter::Parser::new();
    if parser.set_language(&config.language).is_err() {
        return Vec::new();
    }
    let Some(tree) = parser.parse(source, None) else {
        return Vec::new();
    };
    let mut ranges = Vec::new();
    collect_non_code_ranges(&tree.root_node(), &mut ranges);
    ranges
}

fn collect_non_code_ranges(node: &tree_sitter::Node, ranges: &mut Vec<(usize, usize)>) {
    if NON_CODE_KINDS.contains(&node.kind()) {
        ranges.push((node.start_byte(), node.end_byte()));
        return; // don't recurse into children
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        collect_non_code_ranges(&child, ranges);
    }
}

fn is_in_ranges(ranges: &[(usize, usize)], offset: usize) -> bool {
    // Binary search: ranges are sorted by start_byte from tree-sitter DFS
    ranges
        .binary_search_by(|&(start, end)| {
            if offset < start {
                std::cmp::Ordering::Greater
            } else if offset >= end {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Equal
            }
        })
        .is_ok()
}

/// Find files (other than the declaration file) that declare a symbol with the same name.
fn find_shadowing_files(
    project: &ProjectRoot,
    declaration_file: &str,
    symbol_name: &str,
    _name_path: Option<&str>,
    all_matches: &[(String, usize, usize)],
) -> Result<std::collections::HashSet<String>> {
    let mut shadow_files = std::collections::HashSet::new();

    let files_with_matches: Vec<&str> = all_matches
        .iter()
        .map(|(f, _, _)| f.as_str())
        .filter(|f| *f != declaration_file)
        .collect();

    if files_with_matches.is_empty() {
        return Ok(shadow_files);
    }

    // Try DB-based batch lookup first (avoids per-file tree-sitter re-parse)
    let db_path = crate::db::index_db_path(project.as_path());
    if let Ok(db) = crate::db::IndexDb::open(&db_path)
        && let Ok(symbols) = db.symbols_for_files(&files_with_matches)
        && !symbols.is_empty()
    {
        for sym in &symbols {
            if sym.name == symbol_name && sym.file_path != declaration_file {
                shadow_files.insert(sym.file_path.clone());
            }
        }
        return Ok(shadow_files);
    }

    // Fallback: per-file tree-sitter parse
    for fp in files_with_matches {
        if let Ok(symbols) = get_symbols_overview(project, fp, 3)
            && has_declaration(&symbols, symbol_name)
        {
            shadow_files.insert(fp.to_owned());
        }
    }

    Ok(shadow_files)
}

fn has_declaration(symbols: &[SymbolInfo], name: &str) -> bool {
    symbols
        .iter()
        .any(|s| s.name == name || has_declaration(&s.children, name))
}

/// Find the line range of a symbol using tree-sitter.
fn find_symbol_line_range(
    project: &ProjectRoot,
    file_path: &str,
    symbol_name: &str,
    name_path: Option<&str>,
) -> Result<(usize, usize)> {
    let symbols = get_symbols_overview(project, file_path, 0)?;
    let flat = flatten_symbol_infos(symbols);

    let candidate = if let Some(np) = name_path {
        flat.iter().find(|s| s.name_path == np)
    } else {
        flat.iter().find(|s| s.name == symbol_name)
    };

    match candidate {
        Some(sym) => {
            // Estimate end line from body or use heuristic
            let end_line = if let Some(body) = &sym.body {
                sym.line + body.lines().count()
            } else {
                // Read the file to get body via find_symbol_range
                let (_start_byte, end_byte) =
                    crate::symbols::find_symbol_range(project, file_path, symbol_name, name_path)?;
                let resolved = project.resolve(file_path)?;
                let source = fs::read_to_string(&resolved)?;

                source[..end_byte].lines().count()
            };
            Ok((sym.line, end_line))
        }
        None => bail!("symbol '{}' not found in {}", symbol_name, file_path),
    }
}

fn flatten_symbol_infos(symbols: Vec<SymbolInfo>) -> Vec<SymbolInfo> {
    let mut flat = Vec::new();
    for mut s in symbols {
        let children = std::mem::take(&mut s.children);
        flat.push(s);
        flat.extend(flatten_symbol_infos(children));
    }
    flat
}

/// Apply edits to files on disk. Edits are sorted (line desc, col desc) per file
/// and applied back-to-front to preserve offsets.
pub fn apply_edits(project: &ProjectRoot, edits: &[RenameEdit]) -> Result<()> {
    // Group by file
    let mut by_file: HashMap<String, Vec<&RenameEdit>> = HashMap::new();
    for edit in edits {
        by_file
            .entry(edit.file_path.clone())
            .or_default()
            .push(edit);
    }

    for (file_path, mut file_edits) in by_file {
        let resolved = project.resolve(&file_path)?;
        let content = fs::read_to_string(&resolved)?;
        let mut lines: Vec<String> = content.lines().map(String::from).collect();

        // Sort by line desc, then column desc — apply from end to preserve earlier offsets
        file_edits.sort_by(|a, b| b.line.cmp(&a.line).then(b.column.cmp(&a.column)));

        for edit in &file_edits {
            let line_idx = edit.line - 1;
            if line_idx >= lines.len() {
                continue;
            }
            let line = &mut lines[line_idx];
            let col_idx = edit.column - 1;
            let old_len = edit.old_text.len();
            if col_idx + old_len <= line.len() && line[col_idx..col_idx + old_len] == edit.old_text
            {
                line.replace_range(col_idx..col_idx + old_len, &edit.new_text);
            }
        }

        let mut result = lines.join("\n");
        if content.ends_with('\n') {
            result.push('\n');
        }
        fs::write(&resolved, &result)?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ProjectRoot;
    use std::fs;

    fn make_fixture() -> (std::path::PathBuf, ProjectRoot) {
        let dir = std::env::temp_dir().join(format!(
            "codelens-rename-fixture-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        fs::create_dir_all(dir.join("src")).unwrap();
        fs::write(
            dir.join("src/service.py"),
            "class UserService:\n    def get_user(self, user_id):\n        return self.db.find(user_id)\n\n    def delete_user(self, user_id):\n        user = self.get_user(user_id)\n        return self.db.delete(user)\n",
        )
        .unwrap();
        fs::write(
            dir.join("src/main.py"),
            "from service import UserService\n\nsvc = UserService()\nresult = svc.get_user(1)\n",
        )
        .unwrap();
        fs::write(
            dir.join("src/other.py"),
            "class OtherService:\n    def get_user(self):\n        return None\n",
        )
        .unwrap();
        let project = ProjectRoot::new(&dir).unwrap();
        (dir, project)
    }

    #[test]
    fn validates_identifier() {
        assert!(validate_identifier("newName").is_ok());
        assert!(validate_identifier("_private").is_ok());
        assert!(validate_identifier("123bad").is_err());
        assert!(validate_identifier("has-dash").is_err());
        assert!(validate_identifier("").is_err());
    }

    #[test]
    fn file_scope_renames_within_symbol_body() {
        let (_dir, project) = make_fixture();
        let result = rename_symbol(
            &project,
            "src/service.py",
            "get_user",
            "fetch_user",
            Some("UserService/get_user"),
            RenameScope::File,
            false,
        )
        .unwrap();
        assert!(result.success);
        assert!(result.total_replacements >= 1);
        // Verify the file was modified
        let content = fs::read_to_string(project.resolve("src/service.py").unwrap()).unwrap();
        assert!(content.contains("fetch_user"));
        // The call to self.get_user in delete_user should NOT be renamed (outside symbol body)
        // But it depends on the symbol's line range — get_user is a standalone method
    }

    #[test]
    fn project_scope_renames_across_files() {
        let (_dir, project) = make_fixture();
        let result = rename_symbol(
            &project,
            "src/service.py",
            "UserService",
            "AccountService",
            None,
            RenameScope::Project,
            false,
        )
        .unwrap();
        assert!(result.success);
        assert!(result.modified_files >= 2); // service.py + main.py
        let main_content = fs::read_to_string(project.resolve("src/main.py").unwrap()).unwrap();
        assert!(main_content.contains("AccountService"));
        assert!(!main_content.contains("UserService"));
    }

    #[test]
    fn project_scope_falls_back_when_symbol_db_is_empty() {
        let (dir, project) = make_fixture();
        let db_dir = dir.join(".codelens/index");
        fs::create_dir_all(&db_dir).unwrap();
        let _db = crate::db::IndexDb::open(&db_dir.join("symbols.db")).unwrap();

        let result = rename_symbol(
            &project,
            "src/service.py",
            "UserService",
            "AccountService",
            None,
            RenameScope::Project,
            true,
        )
        .unwrap();

        assert!(result.success);
        assert!(result.modified_files >= 2);
        assert!(result.total_replacements >= 3);
    }

    #[test]
    fn dry_run_does_not_modify_files() {
        let (_dir, project) = make_fixture();
        let original = fs::read_to_string(project.resolve("src/service.py").unwrap()).unwrap();
        let result = rename_symbol(
            &project,
            "src/service.py",
            "UserService",
            "AccountService",
            None,
            RenameScope::Project,
            true,
        )
        .unwrap();
        assert!(result.success);
        assert!(!result.edits.is_empty());
        let after = fs::read_to_string(project.resolve("src/service.py").unwrap()).unwrap();
        assert_eq!(original, after);
    }

    #[test]
    fn shadowing_skips_other_declarations() {
        let (_dir, project) = make_fixture();
        // other.py has its own get_user — should not be renamed
        let result = rename_symbol(
            &project,
            "src/service.py",
            "get_user",
            "fetch_user",
            Some("UserService/get_user"),
            RenameScope::Project,
            true,
        )
        .unwrap();
        // Check no edits target other.py
        let other_edits: Vec<_> = result
            .edits
            .iter()
            .filter(|e| e.file_path == "src/other.py")
            .collect();
        assert!(
            other_edits.is_empty(),
            "should skip other.py due to shadowing"
        );
    }

    #[test]
    fn same_name_returns_no_changes() {
        let (_dir, project) = make_fixture();
        let result = rename_symbol(
            &project,
            "src/service.py",
            "UserService",
            "UserService",
            None,
            RenameScope::Project,
            false,
        )
        .unwrap();
        assert!(result.success);
        assert_eq!(result.total_replacements, 0);
    }

    #[test]
    fn column_precise_replacement() {
        let dir = std::env::temp_dir().join(format!(
            "codelens-rename-col-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        fs::create_dir_all(&dir).unwrap();
        // "foo" appears twice on the same line
        fs::write(dir.join("test.py"), "x = foo + foo\n").unwrap();
        let project = ProjectRoot::new(&dir).unwrap();
        let result = rename_symbol(
            &project,
            "test.py",
            "foo",
            "bar",
            None,
            RenameScope::Project,
            false,
        )
        .unwrap();
        assert!(result.success);
        let content = fs::read_to_string(project.resolve("test.py").unwrap()).unwrap();
        assert_eq!(content.trim(), "x = bar + bar");
        assert_eq!(result.total_replacements, 2);
    }

    #[test]
    fn find_all_word_matches_skips_crlf_string_literals() {
        let dir = std::env::temp_dir().join(format!(
            "codelens-rename-crlf-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        fs::create_dir_all(dir.join("src")).unwrap();
        fs::write(
            dir.join("src/main.py"),
            "label = \"PatternMatch\"\r\nPatternMatch()\r\n",
        )
        .unwrap();

        let project = ProjectRoot::new(&dir).unwrap();
        let matches = find_all_word_matches(&project, "PatternMatch").unwrap();

        assert_eq!(matches, vec![("src/main.py".to_string(), 2, 1)]);
    }
}