Skip to main content

codelens_engine/
rename.rs

1use crate::project::{ProjectRoot, collect_files};
2use crate::symbols::{SymbolInfo, get_symbols_overview};
3use anyhow::{Result, bail};
4use regex::Regex;
5use serde::Serialize;
6use std::collections::HashMap;
7use std::fs;
8use std::sync::LazyLock;
9
10static IDENTIFIER_RE: LazyLock<Regex> =
11    LazyLock::new(|| Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap());
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
14#[serde(rename_all = "snake_case")]
15pub enum RenameScope {
16    File,
17    Project,
18}
19
20#[derive(Debug, Clone, Serialize)]
21pub struct RenameEdit {
22    pub file_path: String,
23    pub line: usize,
24    pub column: usize,
25    pub old_text: String,
26    pub new_text: String,
27}
28
29#[derive(Debug, Clone, Serialize)]
30pub struct RenameResult {
31    pub success: bool,
32    pub message: String,
33    pub modified_files: usize,
34    pub total_replacements: usize,
35    pub edits: Vec<RenameEdit>,
36}
37
38/// Rename a symbol across one file or the entire project.
39///
40/// - `file_path`: the file containing the symbol declaration
41/// - `symbol_name`: current name of the symbol
42/// - `new_name`: desired new name
43/// - `name_path`: optional qualified name path (e.g. "Service/run")
44/// - `scope`: File (declaration scope only) or Project (all references)
45/// - `dry_run`: if true, returns edits without modifying files
46pub fn rename_symbol(
47    project: &ProjectRoot,
48    file_path: &str,
49    symbol_name: &str,
50    new_name: &str,
51    name_path: Option<&str>,
52    scope: RenameScope,
53    dry_run: bool,
54) -> Result<RenameResult> {
55    validate_identifier(new_name)?;
56
57    if symbol_name == new_name {
58        return Ok(RenameResult {
59            success: true,
60            message: "Symbol name unchanged".to_string(),
61            modified_files: 0,
62            total_replacements: 0,
63            edits: vec![],
64        });
65    }
66
67    let edits = match scope {
68        RenameScope::File => {
69            collect_file_scope_edits(project, file_path, symbol_name, new_name, name_path)?
70        }
71        RenameScope::Project => {
72            collect_project_scope_edits(project, file_path, symbol_name, new_name, name_path)?
73        }
74    };
75
76    let modified_files = edits
77        .iter()
78        .map(|e| &e.file_path)
79        .collect::<std::collections::HashSet<_>>()
80        .len();
81    let total_replacements = edits.len();
82
83    if !dry_run {
84        apply_edits(project, &edits)?;
85    }
86
87    Ok(RenameResult {
88        success: true,
89        message: format!(
90            "{} {} replacement(s) in {} file(s)",
91            if dry_run { "Would make" } else { "Made" },
92            total_replacements,
93            modified_files
94        ),
95        modified_files,
96        total_replacements,
97        edits,
98    })
99}
100
101fn validate_identifier(name: &str) -> Result<()> {
102    if !IDENTIFIER_RE.is_match(name) {
103        bail!("invalid identifier: '{name}' — must match [a-zA-Z_][a-zA-Z0-9_]*");
104    }
105    Ok(())
106}
107
108/// FILE scope: only rename within the declaration's body range.
109fn collect_file_scope_edits(
110    project: &ProjectRoot,
111    file_path: &str,
112    symbol_name: &str,
113    new_name: &str,
114    name_path: Option<&str>,
115) -> Result<Vec<RenameEdit>> {
116    let resolved = project.resolve(file_path)?;
117    let source = fs::read_to_string(&resolved)?;
118    let lines: Vec<&str> = source.lines().collect();
119
120    // Find symbol to get its line range
121    let (start_line, end_line) =
122        find_symbol_line_range(project, file_path, symbol_name, name_path)?;
123
124    let word_re = Regex::new(&format!(r"\b{}\b", regex::escape(symbol_name)))?;
125    let mut edits = Vec::new();
126
127    for (line_idx, line) in lines
128        .iter()
129        .enumerate()
130        .take(end_line.min(lines.len()))
131        .skip(start_line.saturating_sub(1))
132    {
133        for mat in word_re.find_iter(line) {
134            edits.push(RenameEdit {
135                file_path: file_path.to_string(),
136                line: line_idx + 1,
137                column: mat.start() + 1,
138                old_text: symbol_name.to_string(),
139                new_text: new_name.to_string(),
140            });
141        }
142    }
143
144    Ok(edits)
145}
146
147/// PROJECT scope: rename in declaration file + all referencing files.
148fn collect_project_scope_edits(
149    project: &ProjectRoot,
150    file_path: &str,
151    symbol_name: &str,
152    new_name: &str,
153    name_path: Option<&str>,
154) -> Result<Vec<RenameEdit>> {
155    // Step 1: Find ALL word-boundary matches across project (handles multiple per line)
156    let all_matches = find_all_word_matches(project, symbol_name)?;
157
158    // Step 2: Get files that have their own declaration of the same name (shadowing)
159    let shadow_files =
160        find_shadowing_files(project, file_path, symbol_name, name_path, &all_matches)?;
161
162    // Step 3: Build edits, skipping files with shadowed declarations
163    let mut edits = Vec::new();
164    for (match_file, line, column) in &all_matches {
165        if match_file != file_path && shadow_files.contains(match_file) {
166            continue;
167        }
168        edits.push(RenameEdit {
169            file_path: match_file.clone(),
170            line: *line,
171            column: *column,
172            old_text: symbol_name.to_string(),
173            new_text: new_name.to_string(),
174        });
175    }
176
177    Ok(edits)
178}
179
180/// Find ALL word-boundary matches of `symbol_name` across the project.
181/// Unlike search_for_pattern, this returns multiple matches per line via find_iter.
182pub fn find_all_word_matches(
183    project: &ProjectRoot,
184    symbol_name: &str,
185) -> Result<Vec<(String, usize, usize)>> {
186    let candidate_files = collect_candidate_files(project)?;
187
188    if candidate_files.is_empty() {
189        return Ok(Vec::new());
190    }
191
192    // Fast path: use indexed file list only when it fully covers the current
193    // project. Partial or empty DBs must not suppress project-wide rename hits.
194    let db_path = crate::db::index_db_path(project.as_path());
195    if db_path.exists()
196        && let Ok(db) = crate::db::IndexDb::open(&db_path)
197        && let Ok(indexed_files) = db.all_file_paths()
198        && indexed_files.len() >= candidate_files.len()
199    {
200        let indexed_set: std::collections::HashSet<&str> =
201            indexed_files.iter().map(String::as_str).collect();
202        if candidate_files
203            .iter()
204            .all(|path| indexed_set.contains(path.as_str()))
205        {
206            return find_word_matches_in_files(project, symbol_name, &indexed_files);
207        }
208    }
209
210    find_word_matches_in_files(project, symbol_name, &candidate_files)
211}
212
213fn collect_candidate_files(project: &ProjectRoot) -> Result<Vec<String>> {
214    Ok(collect_files(project.as_path(), |path| {
215        crate::lang_config::language_for_path(path).is_some()
216    })?
217    .into_iter()
218    .map(|path| project.to_relative(path))
219    .collect())
220}
221
222/// Fast path: scan only indexed files (from DB).
223/// Filters out matches inside comments and string literals using tree-sitter.
224fn find_word_matches_in_files(
225    project: &ProjectRoot,
226    symbol_name: &str,
227    files: &[String],
228) -> Result<Vec<(String, usize, usize)>> {
229    let word_re = Regex::new(&format!(r"\b{}\b", regex::escape(symbol_name)))?;
230    let mut results = Vec::new();
231    let mut non_code_cache: HashMap<std::path::PathBuf, Vec<(usize, usize)>> = HashMap::new();
232    for rel in files {
233        let abs = project.as_path().join(rel);
234        let content = match fs::read_to_string(&abs) {
235            Ok(c) => c,
236            Err(_) => continue,
237        };
238        // Build non-code byte ranges with per-file cache
239        let non_code = non_code_cache
240            .entry(abs.clone())
241            .or_insert_with(|| build_non_code_ranges(&abs, content.as_bytes()));
242
243        let mut byte_offset = 0usize;
244        for (line_idx, raw_line) in content.split_inclusive('\n').enumerate() {
245            let line = raw_line.strip_suffix('\n').unwrap_or(raw_line);
246            let line = line.strip_suffix('\r').unwrap_or(line);
247            for mat in word_re.find_iter(line) {
248                let abs_start = byte_offset + mat.start();
249                if !is_in_ranges(non_code, abs_start) {
250                    results.push((rel.clone(), line_idx + 1, mat.start() + 1));
251                }
252            }
253            byte_offset += raw_line.len();
254        }
255    }
256    Ok(results)
257}
258
259/// Node kinds that represent comments or string literals across languages.
260const NON_CODE_KINDS: &[&str] = &[
261    "comment",
262    "line_comment",
263    "block_comment",
264    "string",
265    "string_literal",
266    "raw_string_literal",
267    "template_string",
268    "string_content",
269    "interpreted_string_literal",
270    "heredoc_body",
271    "regex_literal",
272];
273
274/// Build byte ranges of non-code nodes (comments + strings) using tree-sitter.
275fn build_non_code_ranges(path: &std::path::Path, source: &[u8]) -> Vec<(usize, usize)> {
276    let Some(config) = crate::lang_config::language_for_path(path) else {
277        return Vec::new();
278    };
279    let mut parser = tree_sitter::Parser::new();
280    if parser.set_language(&config.language).is_err() {
281        return Vec::new();
282    }
283    let Some(tree) = parser.parse(source, None) else {
284        return Vec::new();
285    };
286    let mut ranges = Vec::new();
287    collect_non_code_ranges(&tree.root_node(), &mut ranges);
288    ranges
289}
290
291fn collect_non_code_ranges(node: &tree_sitter::Node, ranges: &mut Vec<(usize, usize)>) {
292    if NON_CODE_KINDS.contains(&node.kind()) {
293        ranges.push((node.start_byte(), node.end_byte()));
294        return; // don't recurse into children
295    }
296    let mut cursor = node.walk();
297    for child in node.children(&mut cursor) {
298        collect_non_code_ranges(&child, ranges);
299    }
300}
301
302fn is_in_ranges(ranges: &[(usize, usize)], offset: usize) -> bool {
303    // Binary search: ranges are sorted by start_byte from tree-sitter DFS
304    ranges
305        .binary_search_by(|&(start, end)| {
306            if offset < start {
307                std::cmp::Ordering::Greater
308            } else if offset >= end {
309                std::cmp::Ordering::Less
310            } else {
311                std::cmp::Ordering::Equal
312            }
313        })
314        .is_ok()
315}
316
317/// Find files (other than the declaration file) that declare a symbol with the same name.
318fn find_shadowing_files(
319    project: &ProjectRoot,
320    declaration_file: &str,
321    symbol_name: &str,
322    _name_path: Option<&str>,
323    all_matches: &[(String, usize, usize)],
324) -> Result<std::collections::HashSet<String>> {
325    let mut shadow_files = std::collections::HashSet::new();
326
327    let files_with_matches: Vec<&str> = all_matches
328        .iter()
329        .map(|(f, _, _)| f.as_str())
330        .filter(|f| *f != declaration_file)
331        .collect();
332
333    if files_with_matches.is_empty() {
334        return Ok(shadow_files);
335    }
336
337    // Try DB-based batch lookup first (avoids per-file tree-sitter re-parse)
338    let db_path = crate::db::index_db_path(project.as_path());
339    if let Ok(db) = crate::db::IndexDb::open(&db_path)
340        && let Ok(symbols) = db.symbols_for_files(&files_with_matches)
341        && !symbols.is_empty()
342    {
343        for sym in &symbols {
344            if sym.name == symbol_name && sym.file_path != declaration_file {
345                shadow_files.insert(sym.file_path.clone());
346            }
347        }
348        return Ok(shadow_files);
349    }
350
351    // Fallback: per-file tree-sitter parse
352    for fp in files_with_matches {
353        if let Ok(symbols) = get_symbols_overview(project, fp, 3)
354            && has_declaration(&symbols, symbol_name)
355        {
356            shadow_files.insert(fp.to_owned());
357        }
358    }
359
360    Ok(shadow_files)
361}
362
363fn has_declaration(symbols: &[SymbolInfo], name: &str) -> bool {
364    symbols
365        .iter()
366        .any(|s| s.name == name || has_declaration(&s.children, name))
367}
368
369/// Find the line range of a symbol using tree-sitter.
370fn find_symbol_line_range(
371    project: &ProjectRoot,
372    file_path: &str,
373    symbol_name: &str,
374    name_path: Option<&str>,
375) -> Result<(usize, usize)> {
376    let symbols = get_symbols_overview(project, file_path, 0)?;
377    let flat = flatten_symbol_infos(symbols);
378
379    let candidate = if let Some(np) = name_path {
380        flat.iter().find(|s| s.name_path == np)
381    } else {
382        flat.iter().find(|s| s.name == symbol_name)
383    };
384
385    match candidate {
386        Some(sym) => {
387            // Estimate end line from body or use heuristic
388            let end_line = if let Some(body) = &sym.body {
389                sym.line + body.lines().count()
390            } else {
391                // Read the file to get body via find_symbol_range
392                let (_start_byte, end_byte) =
393                    crate::symbols::find_symbol_range(project, file_path, symbol_name, name_path)?;
394                let resolved = project.resolve(file_path)?;
395                let source = fs::read_to_string(&resolved)?;
396
397                source[..end_byte].lines().count()
398            };
399            Ok((sym.line, end_line))
400        }
401        None => bail!("symbol '{}' not found in {}", symbol_name, file_path),
402    }
403}
404
405fn flatten_symbol_infos(symbols: Vec<SymbolInfo>) -> Vec<SymbolInfo> {
406    let mut flat = Vec::new();
407    for mut s in symbols {
408        let children = std::mem::take(&mut s.children);
409        flat.push(s);
410        flat.extend(flatten_symbol_infos(children));
411    }
412    flat
413}
414
415/// Apply edits to files on disk. Edits are sorted (line desc, col desc) per file
416/// and applied back-to-front to preserve offsets.
417pub fn apply_edits(project: &ProjectRoot, edits: &[RenameEdit]) -> Result<()> {
418    // Group by file
419    let mut by_file: HashMap<String, Vec<&RenameEdit>> = HashMap::new();
420    for edit in edits {
421        by_file
422            .entry(edit.file_path.clone())
423            .or_default()
424            .push(edit);
425    }
426
427    for (file_path, mut file_edits) in by_file {
428        let resolved = project.resolve(&file_path)?;
429        let content = fs::read_to_string(&resolved)?;
430        let mut lines: Vec<String> = content.lines().map(String::from).collect();
431
432        // Sort by line desc, then column desc — apply from end to preserve earlier offsets
433        file_edits.sort_by(|a, b| b.line.cmp(&a.line).then(b.column.cmp(&a.column)));
434
435        for edit in &file_edits {
436            let line_idx = edit.line - 1;
437            if line_idx >= lines.len() {
438                continue;
439            }
440            let line = &mut lines[line_idx];
441            let col_idx = edit.column - 1;
442            let old_len = edit.old_text.len();
443            if col_idx + old_len <= line.len() && line[col_idx..col_idx + old_len] == edit.old_text
444            {
445                line.replace_range(col_idx..col_idx + old_len, &edit.new_text);
446            }
447        }
448
449        let mut result = lines.join("\n");
450        if content.ends_with('\n') {
451            result.push('\n');
452        }
453        fs::write(&resolved, &result)?;
454    }
455
456    Ok(())
457}
458
459#[cfg(test)]
460mod tests {
461    use super::*;
462    use crate::ProjectRoot;
463    use std::fs;
464
465    fn make_fixture() -> (std::path::PathBuf, ProjectRoot) {
466        let dir = std::env::temp_dir().join(format!(
467            "codelens-rename-fixture-{}",
468            std::time::SystemTime::now()
469                .duration_since(std::time::UNIX_EPOCH)
470                .unwrap()
471                .as_nanos()
472        ));
473        fs::create_dir_all(dir.join("src")).unwrap();
474        fs::write(
475            dir.join("src/service.py"),
476            "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",
477        )
478        .unwrap();
479        fs::write(
480            dir.join("src/main.py"),
481            "from service import UserService\n\nsvc = UserService()\nresult = svc.get_user(1)\n",
482        )
483        .unwrap();
484        fs::write(
485            dir.join("src/other.py"),
486            "class OtherService:\n    def get_user(self):\n        return None\n",
487        )
488        .unwrap();
489        let project = ProjectRoot::new(&dir).unwrap();
490        (dir, project)
491    }
492
493    #[test]
494    fn validates_identifier() {
495        assert!(validate_identifier("newName").is_ok());
496        assert!(validate_identifier("_private").is_ok());
497        assert!(validate_identifier("123bad").is_err());
498        assert!(validate_identifier("has-dash").is_err());
499        assert!(validate_identifier("").is_err());
500    }
501
502    #[test]
503    fn file_scope_renames_within_symbol_body() {
504        let (_dir, project) = make_fixture();
505        let result = rename_symbol(
506            &project,
507            "src/service.py",
508            "get_user",
509            "fetch_user",
510            Some("UserService/get_user"),
511            RenameScope::File,
512            false,
513        )
514        .unwrap();
515        assert!(result.success);
516        assert!(result.total_replacements >= 1);
517        // Verify the file was modified
518        let content = fs::read_to_string(project.resolve("src/service.py").unwrap()).unwrap();
519        assert!(content.contains("fetch_user"));
520        // The call to self.get_user in delete_user should NOT be renamed (outside symbol body)
521        // But it depends on the symbol's line range — get_user is a standalone method
522    }
523
524    #[test]
525    fn project_scope_renames_across_files() {
526        let (_dir, project) = make_fixture();
527        let result = rename_symbol(
528            &project,
529            "src/service.py",
530            "UserService",
531            "AccountService",
532            None,
533            RenameScope::Project,
534            false,
535        )
536        .unwrap();
537        assert!(result.success);
538        assert!(result.modified_files >= 2); // service.py + main.py
539        let main_content = fs::read_to_string(project.resolve("src/main.py").unwrap()).unwrap();
540        assert!(main_content.contains("AccountService"));
541        assert!(!main_content.contains("UserService"));
542    }
543
544    #[test]
545    fn project_scope_falls_back_when_symbol_db_is_empty() {
546        let (dir, project) = make_fixture();
547        let db_dir = dir.join(".codelens/index");
548        fs::create_dir_all(&db_dir).unwrap();
549        let _db = crate::db::IndexDb::open(&db_dir.join("symbols.db")).unwrap();
550
551        let result = rename_symbol(
552            &project,
553            "src/service.py",
554            "UserService",
555            "AccountService",
556            None,
557            RenameScope::Project,
558            true,
559        )
560        .unwrap();
561
562        assert!(result.success);
563        assert!(result.modified_files >= 2);
564        assert!(result.total_replacements >= 3);
565    }
566
567    #[test]
568    fn dry_run_does_not_modify_files() {
569        let (_dir, project) = make_fixture();
570        let original = fs::read_to_string(project.resolve("src/service.py").unwrap()).unwrap();
571        let result = rename_symbol(
572            &project,
573            "src/service.py",
574            "UserService",
575            "AccountService",
576            None,
577            RenameScope::Project,
578            true,
579        )
580        .unwrap();
581        assert!(result.success);
582        assert!(!result.edits.is_empty());
583        let after = fs::read_to_string(project.resolve("src/service.py").unwrap()).unwrap();
584        assert_eq!(original, after);
585    }
586
587    #[test]
588    fn shadowing_skips_other_declarations() {
589        let (_dir, project) = make_fixture();
590        // other.py has its own get_user — should not be renamed
591        let result = rename_symbol(
592            &project,
593            "src/service.py",
594            "get_user",
595            "fetch_user",
596            Some("UserService/get_user"),
597            RenameScope::Project,
598            true,
599        )
600        .unwrap();
601        // Check no edits target other.py
602        let other_edits: Vec<_> = result
603            .edits
604            .iter()
605            .filter(|e| e.file_path == "src/other.py")
606            .collect();
607        assert!(
608            other_edits.is_empty(),
609            "should skip other.py due to shadowing"
610        );
611    }
612
613    #[test]
614    fn same_name_returns_no_changes() {
615        let (_dir, project) = make_fixture();
616        let result = rename_symbol(
617            &project,
618            "src/service.py",
619            "UserService",
620            "UserService",
621            None,
622            RenameScope::Project,
623            false,
624        )
625        .unwrap();
626        assert!(result.success);
627        assert_eq!(result.total_replacements, 0);
628    }
629
630    #[test]
631    fn column_precise_replacement() {
632        let dir = std::env::temp_dir().join(format!(
633            "codelens-rename-col-{}",
634            std::time::SystemTime::now()
635                .duration_since(std::time::UNIX_EPOCH)
636                .unwrap()
637                .as_nanos()
638        ));
639        fs::create_dir_all(&dir).unwrap();
640        // "foo" appears twice on the same line
641        fs::write(dir.join("test.py"), "x = foo + foo\n").unwrap();
642        let project = ProjectRoot::new(&dir).unwrap();
643        let result = rename_symbol(
644            &project,
645            "test.py",
646            "foo",
647            "bar",
648            None,
649            RenameScope::Project,
650            false,
651        )
652        .unwrap();
653        assert!(result.success);
654        let content = fs::read_to_string(project.resolve("test.py").unwrap()).unwrap();
655        assert_eq!(content.trim(), "x = bar + bar");
656        assert_eq!(result.total_replacements, 2);
657    }
658
659    #[test]
660    fn find_all_word_matches_skips_crlf_string_literals() {
661        let dir = std::env::temp_dir().join(format!(
662            "codelens-rename-crlf-{}",
663            std::time::SystemTime::now()
664                .duration_since(std::time::UNIX_EPOCH)
665                .unwrap()
666                .as_nanos()
667        ));
668        fs::create_dir_all(dir.join("src")).unwrap();
669        fs::write(
670            dir.join("src/main.py"),
671            "label = \"PatternMatch\"\r\nPatternMatch()\r\n",
672        )
673        .unwrap();
674
675        let project = ProjectRoot::new(&dir).unwrap();
676        let matches = find_all_word_matches(&project, "PatternMatch").unwrap();
677
678        assert_eq!(matches, vec![("src/main.py".to_string(), 2, 1)]);
679    }
680}