1use std::ops::Range;
2use tree_sitter::{Query, QueryMatch};
3
4use crate::{MigrationPatterns, patterns::SETTINGS_DUPLICATED_AGENT_PATTERN};
5
6pub const SETTINGS_PATTERNS: MigrationPatterns =
7 &[(SETTINGS_DUPLICATED_AGENT_PATTERN, comment_duplicated_agent)];
8
9fn comment_duplicated_agent(
10 contents: &str,
11 mat: &QueryMatch,
12 query: &Query,
13) -> Option<(Range<usize>, String)> {
14 let pair_ix = query.capture_index_for_name("pair1")?;
15 let mut range = mat.nodes_for_capture_index(pair_ix).next()?.byte_range();
16
17 // Include the comma into the commented region
18 let rtext = &contents[range.end..];
19 if let Some(comma_index) = rtext.find(',') {
20 range.end += comma_index + 1;
21 }
22
23 let value = contents[range.clone()].to_string();
24 let commented_value = format!("/* Duplicated key auto-commented: {value} */");
25 Some((range, commented_value))
26}