Deduplicate a base name against existing names in the appropriate scope. For methods, checks against sibling method names in the class. For functions, checks against `function ` patterns in the file.
(base: &str, content: &str, ctx: &EnclosingContext)
| 763 | /// For methods, checks against sibling method names in the class. |
| 764 | /// For functions, checks against `function <name>` patterns in the file. |
| 765 | fn deduplicate_name(base: &str, content: &str, ctx: &EnclosingContext) -> String { |
| 766 | let mut name = base.to_string(); |
| 767 | let mut counter = 1u32; |
| 768 | |
| 769 | match ctx.target { |
| 770 | ExtractionTarget::Method => { |
| 771 | // Check against sibling method names in the class. |
| 772 | loop { |
| 773 | if !ctx.sibling_method_names.contains(&name) { |
| 774 | break; |
| 775 | } |
| 776 | counter += 1; |
| 777 | name = format!("{}{}", base, counter); |
| 778 | } |
| 779 | } |
| 780 | ExtractionTarget::Function => { |
| 781 | // Check against function declarations in the file. |
| 782 | loop { |
| 783 | let pattern_fn = format!("function {}", name); |
| 784 | if !content.contains(&pattern_fn) { |
| 785 | break; |
| 786 | } |
| 787 | counter += 1; |
| 788 | name = format!("{}{}", base, counter); |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | name |
| 794 | } |
| 795 | |
| 796 | // ─── Selection trimming ──────────────────────────────────────────────────── |
| 797 |
no test coverage detected