|
| 1 | +//! structural search replace |
| 2 | +
|
| 3 | +use ra_syntax::ast::make::expr_from_text; |
| 4 | +use ra_syntax::AstNode; |
| 5 | +use ra_syntax::SyntaxElement; |
| 6 | +use ra_syntax::SyntaxNode; |
| 7 | +use ra_text_edit::{TextEdit, TextEditBuilder}; |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +#[derive(Debug, PartialEq)] |
| 11 | +pub enum SsrError { |
| 12 | + ParseError(String), |
| 13 | +} |
| 14 | + |
| 15 | +impl std::fmt::Display for SsrError { |
| 16 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 17 | + match self { |
| 18 | + SsrError::ParseError(s) => write!(f, "Parse error: {}", s), |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl std::error::Error for SsrError {} |
| 24 | + |
| 25 | +#[derive(Debug, PartialEq)] |
| 26 | +pub struct SsrPattern { |
| 27 | + pattern: SyntaxNode, |
| 28 | + names: Vec<String>, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, PartialEq)] |
| 32 | +pub struct SsrTemplate { |
| 33 | + template: SyntaxNode, |
| 34 | +} |
| 35 | + |
| 36 | +type Binding = HashMap<String, SyntaxNode>; |
| 37 | + |
| 38 | +#[derive(Debug)] |
| 39 | +pub struct Match { |
| 40 | + place: SyntaxNode, |
| 41 | + binding: Binding, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Debug)] |
| 45 | +pub struct SsrMatches { |
| 46 | + matches: Vec<Match>, |
| 47 | +} |
| 48 | + |
| 49 | +impl SsrMatches { |
| 50 | + pub(crate) fn is_empty(&self) -> bool { |
| 51 | + self.matches.is_empty() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +pub(crate) fn parse(query: &str) -> Result<(SsrPattern, SsrTemplate), SsrError> { |
| 56 | + let mut it = query.split("==>>"); |
| 57 | + let pattern = it.next().expect("at least empty string").trim(); |
| 58 | + let mut template = it |
| 59 | + .next() |
| 60 | + .ok_or(SsrError::ParseError("Cannot find delemiter `==>>`".into()))? |
| 61 | + .trim() |
| 62 | + .to_string(); |
| 63 | + if it.next().is_some() { |
| 64 | + return Err(SsrError::ParseError("More than one delimiter found".into())); |
| 65 | + } |
| 66 | + let mut names = vec![]; |
| 67 | + let mut it = pattern.split('$'); |
| 68 | + let mut pattern = it.next().expect("something").to_string(); |
| 69 | + |
| 70 | + for part in it.map(split_by_binding) { |
| 71 | + let (binding_name, binding_type, remainder) = part?; |
| 72 | + is_expr(binding_type)?; |
| 73 | + let new_binding_name = create_name(binding_name, &mut names)?; |
| 74 | + pattern.push_str(new_binding_name); |
| 75 | + pattern.push_str(remainder); |
| 76 | + template = replace_in_template(template, binding_name, new_binding_name); |
| 77 | + } |
| 78 | + Ok(( |
| 79 | + SsrPattern { pattern: expr_from_text(&pattern).syntax().clone(), names }, |
| 80 | + SsrTemplate { template: expr_from_text(&template).syntax().clone() }, |
| 81 | + )) |
| 82 | +} |
| 83 | + |
| 84 | +fn split_by_binding(s: &str) -> Result<(&str, &str, &str), SsrError> { |
| 85 | + let end_of_name = s.find(":").ok_or(SsrError::ParseError("Use $<name>:expr".into()))?; |
| 86 | + let name = &s[0..end_of_name]; |
| 87 | + is_name(name)?; |
| 88 | + let type_begin = end_of_name + 1; |
| 89 | + let type_length = s[type_begin..].find(|c| !char::is_ascii_alphanumeric(&c)).unwrap_or(s.len()); |
| 90 | + let type_name = &s[type_begin..type_begin + type_length]; |
| 91 | + Ok((name, type_name, &s[type_begin + type_length..])) |
| 92 | +} |
| 93 | + |
| 94 | +fn is_name(s: &str) -> Result<(), SsrError> { |
| 95 | + if s.chars().all(|c| char::is_ascii_alphanumeric(&c) || c == '_') { |
| 96 | + Ok(()) |
| 97 | + } else { |
| 98 | + Err(SsrError::ParseError("Name can contain only alphanumerics and _".into())) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn is_expr(s: &str) -> Result<(), SsrError> { |
| 103 | + if s == "expr" { |
| 104 | + Ok(()) |
| 105 | + } else { |
| 106 | + Err(SsrError::ParseError("Only $<name>:expr is supported".into())) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +fn replace_in_template(template: String, name: &str, new_name: &str) -> String { |
| 111 | + let name = format!("${}", name); |
| 112 | + template.replace(&name, new_name) |
| 113 | +} |
| 114 | + |
| 115 | +fn create_name<'a>(name: &str, binding_names: &'a mut Vec<String>) -> Result<&'a str, SsrError> { |
| 116 | + let sanitized_name = format!("__search_pattern_{}", name); |
| 117 | + if binding_names.iter().any(|a| a == &sanitized_name) { |
| 118 | + return Err(SsrError::ParseError(format!("Name `{}` repeats more than once", name))); |
| 119 | + } |
| 120 | + binding_names.push(sanitized_name); |
| 121 | + Ok(binding_names.last().unwrap()) |
| 122 | +} |
| 123 | + |
| 124 | +pub(crate) fn find(pattern: &SsrPattern, code: &SyntaxNode) -> SsrMatches { |
| 125 | + fn check( |
| 126 | + pattern: &SyntaxElement, |
| 127 | + code: &SyntaxElement, |
| 128 | + placeholders: &[String], |
| 129 | + m: &mut Match, |
| 130 | + ) -> bool { |
| 131 | + match (pattern, code) { |
| 132 | + (SyntaxElement::Token(ref pattern), SyntaxElement::Token(ref code)) => { |
| 133 | + pattern.text() == code.text() |
| 134 | + } |
| 135 | + (SyntaxElement::Node(ref pattern), SyntaxElement::Node(ref code)) => { |
| 136 | + if placeholders.iter().find(|&n| n.as_str() == pattern.text()).is_some() { |
| 137 | + m.binding.insert(pattern.text().to_string(), code.clone()); |
| 138 | + true |
| 139 | + } else { |
| 140 | + pattern.green().children().count() == code.green().children().count() |
| 141 | + && pattern |
| 142 | + .children_with_tokens() |
| 143 | + .zip(code.children_with_tokens()) |
| 144 | + .all(|(a, b)| check(&a, &b, placeholders, m)) |
| 145 | + } |
| 146 | + } |
| 147 | + _ => false, |
| 148 | + } |
| 149 | + } |
| 150 | + let kind = pattern.pattern.kind(); |
| 151 | + let matches = code |
| 152 | + .descendants_with_tokens() |
| 153 | + .filter(|n| n.kind() == kind) |
| 154 | + .filter_map(|code| { |
| 155 | + let mut m = Match { place: code.as_node().unwrap().clone(), binding: HashMap::new() }; |
| 156 | + if check(&SyntaxElement::from(pattern.pattern.clone()), &code, &pattern.names, &mut m) { |
| 157 | + Some(m) |
| 158 | + } else { |
| 159 | + None |
| 160 | + } |
| 161 | + }) |
| 162 | + .collect(); |
| 163 | + SsrMatches { matches } |
| 164 | +} |
| 165 | + |
| 166 | +pub(crate) fn replace(matches: &SsrMatches, template: &SsrTemplate) -> TextEdit { |
| 167 | + let mut builder = TextEditBuilder::default(); |
| 168 | + for m in &matches.matches { |
| 169 | + builder.replace(m.place.text_range(), render(&m.binding, template)); |
| 170 | + } |
| 171 | + builder.finish() |
| 172 | +} |
| 173 | + |
| 174 | +fn render(binding: &Binding, template: &SsrTemplate) -> String { |
| 175 | + fn replace(p: &SyntaxNode, binding: &Binding, builder: &mut TextEditBuilder) { |
| 176 | + if let Some(name) = binding.keys().find(|&n| n.as_str() == p.text()) { |
| 177 | + builder.replace(p.text_range(), binding[name].text().to_string()) |
| 178 | + } else { |
| 179 | + for ref child in p.children() { |
| 180 | + replace(child, binding, builder); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + let mut builder = TextEditBuilder::default(); |
| 185 | + replace(&template.template, binding, &mut builder); |
| 186 | + builder.finish().apply(&template.template.text().to_string()) //FIXME |
| 187 | +} |
| 188 | + |
| 189 | +#[cfg(test)] |
| 190 | +mod tests { |
| 191 | + use super::*; |
| 192 | + use ra_syntax::SourceFile; |
| 193 | + #[test] |
| 194 | + fn parser_happy_case() { |
| 195 | + let result = parse("foo($a:expr, $b:expr) ==>> bar($b, $a)").unwrap(); |
| 196 | + assert_eq!(&result.0.pattern.text(), "foo(__search_pattern_a, __search_pattern_b)"); |
| 197 | + assert_eq!( |
| 198 | + result.0.names, |
| 199 | + vec!["__search_pattern_a".to_string(), "__search_pattern_b".to_string()] |
| 200 | + ); |
| 201 | + assert_eq!(&result.1.template.text(), "bar(__search_pattern_b, __search_pattern_a)"); |
| 202 | + } |
| 203 | + |
| 204 | + #[test] |
| 205 | + fn parser_empty_query() { |
| 206 | + assert_eq!( |
| 207 | + parse("").unwrap_err(), |
| 208 | + SsrError::ParseError("Cannot find delemiter `==>>`".into()) |
| 209 | + ); |
| 210 | + } |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn parser_no_delimiter() { |
| 214 | + assert_eq!( |
| 215 | + parse("foo()").unwrap_err(), |
| 216 | + SsrError::ParseError("Cannot find delemiter `==>>`".into()) |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | + #[test] |
| 221 | + fn parser_two_delimiters() { |
| 222 | + assert_eq!( |
| 223 | + parse("foo() ==>> a ==>> b ").unwrap_err(), |
| 224 | + SsrError::ParseError("More than one delimiter found".into()) |
| 225 | + ); |
| 226 | + } |
| 227 | + |
| 228 | + #[test] |
| 229 | + fn parser_no_pattern_type() { |
| 230 | + assert_eq!( |
| 231 | + parse("foo($a) ==>>").unwrap_err(), |
| 232 | + SsrError::ParseError("Use $<name>:expr".into()) |
| 233 | + ); |
| 234 | + } |
| 235 | + |
| 236 | + #[test] |
| 237 | + fn parser_invalid_name() { |
| 238 | + assert_eq!( |
| 239 | + parse("foo($a+:expr) ==>>").unwrap_err(), |
| 240 | + SsrError::ParseError("Name can contain only alphanumerics and _".into()) |
| 241 | + ); |
| 242 | + } |
| 243 | + |
| 244 | + #[test] |
| 245 | + fn parser_invalid_type() { |
| 246 | + assert_eq!( |
| 247 | + parse("foo($a:ident) ==>>").unwrap_err(), |
| 248 | + SsrError::ParseError("Only $<name>:expr is supported".into()) |
| 249 | + ); |
| 250 | + } |
| 251 | + |
| 252 | + #[test] |
| 253 | + fn parser_repeated_name() { |
| 254 | + assert_eq!( |
| 255 | + parse("foo($a:expr, $a:expr) ==>>").unwrap_err(), |
| 256 | + SsrError::ParseError("Name `a` repeats more than once".into()) |
| 257 | + ); |
| 258 | + } |
| 259 | + |
| 260 | + #[test] |
| 261 | + fn parse_match_replace() { |
| 262 | + let (p, template) = parse("foo($x:expr) ==>> bar($x)").unwrap(); |
| 263 | + let input = &"fn main() { foo(1+2); }"; |
| 264 | + |
| 265 | + let code = SourceFile::parse(input).tree(); |
| 266 | + let matches = find(&p, code.syntax()); |
| 267 | + assert_eq!(matches.matches.len(), 1); |
| 268 | + assert_eq!(matches.matches[0].place.text(), "foo(1+2)"); |
| 269 | + assert_eq!(matches.matches[0].binding.len(), 1); |
| 270 | + assert_eq!(matches.matches[0].binding["__search_pattern_x"].text(), "1+2"); |
| 271 | + |
| 272 | + let edit = replace(&matches, &template); |
| 273 | + assert_eq!(edit.apply(input), "fn main() { bar(1+2); }"); |
| 274 | + } |
| 275 | +} |
0 commit comments