2020-06-17 06:53:51 +00:00
|
|
|
//! Code for applying replacement templates for matches that have previously been found.
|
|
|
|
|
|
|
|
use crate::matching::Var;
|
2020-07-22 06:46:29 +00:00
|
|
|
use crate::{resolving::ResolvedRule, Match, SsrMatches};
|
|
|
|
use ra_syntax::ast::{self, AstToken};
|
|
|
|
use ra_syntax::{SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextSize};
|
2020-06-17 06:53:51 +00:00
|
|
|
use ra_text_edit::TextEdit;
|
|
|
|
|
|
|
|
/// Returns a text edit that will replace each match in `matches` with its corresponding replacement
|
|
|
|
/// template. Placeholders in the template will have been substituted with whatever they matched to
|
|
|
|
/// in the original code.
|
2020-07-22 09:15:19 +00:00
|
|
|
pub(crate) fn matches_to_edit(
|
|
|
|
matches: &SsrMatches,
|
|
|
|
file_src: &str,
|
2020-07-22 06:46:29 +00:00
|
|
|
rules: &[ResolvedRule],
|
2020-07-22 09:15:19 +00:00
|
|
|
) -> TextEdit {
|
|
|
|
matches_to_edit_at_offset(matches, file_src, 0.into(), rules)
|
2020-06-17 06:53:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 10:34:21 +00:00
|
|
|
fn matches_to_edit_at_offset(
|
|
|
|
matches: &SsrMatches,
|
|
|
|
file_src: &str,
|
|
|
|
relative_start: TextSize,
|
2020-07-22 06:46:29 +00:00
|
|
|
rules: &[ResolvedRule],
|
2020-06-27 10:34:21 +00:00
|
|
|
) -> TextEdit {
|
2020-06-17 06:53:51 +00:00
|
|
|
let mut edit_builder = ra_text_edit::TextEditBuilder::default();
|
|
|
|
for m in &matches.matches {
|
2020-06-30 05:55:20 +00:00
|
|
|
edit_builder.replace(
|
|
|
|
m.range.range.checked_sub(relative_start).unwrap(),
|
2020-07-22 09:15:19 +00:00
|
|
|
render_replace(m, file_src, rules),
|
2020-06-30 05:55:20 +00:00
|
|
|
);
|
2020-06-17 06:53:51 +00:00
|
|
|
}
|
|
|
|
edit_builder.finish()
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:15:19 +00:00
|
|
|
struct ReplacementRenderer<'a> {
|
|
|
|
match_info: &'a Match,
|
|
|
|
file_src: &'a str,
|
2020-07-22 06:46:29 +00:00
|
|
|
rules: &'a [ResolvedRule],
|
|
|
|
rule: &'a ResolvedRule,
|
2020-07-22 09:15:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 06:46:29 +00:00
|
|
|
fn render_replace(match_info: &Match, file_src: &str, rules: &[ResolvedRule]) -> String {
|
2020-06-17 06:53:51 +00:00
|
|
|
let mut out = String::new();
|
2020-07-22 09:15:19 +00:00
|
|
|
let rule = &rules[match_info.rule_index];
|
|
|
|
let template = rule
|
2020-07-03 02:57:17 +00:00
|
|
|
.template
|
|
|
|
.as_ref()
|
|
|
|
.expect("You called MatchFinder::edits after calling MatchFinder::add_search_pattern");
|
2020-07-22 09:15:19 +00:00
|
|
|
let renderer = ReplacementRenderer { match_info, file_src, rules, rule };
|
2020-07-22 06:46:29 +00:00
|
|
|
renderer.render_node(&template.node, &mut out);
|
2020-06-17 06:53:51 +00:00
|
|
|
for comment in &match_info.ignored_comments {
|
|
|
|
out.push_str(&comment.syntax().to_string());
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
2020-07-22 09:15:19 +00:00
|
|
|
|
|
|
|
impl ReplacementRenderer<'_> {
|
|
|
|
fn render_node_children(&self, node: &SyntaxNode, out: &mut String) {
|
|
|
|
for node_or_token in node.children_with_tokens() {
|
|
|
|
self.render_node_or_token(&node_or_token, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_node_or_token(&self, node_or_token: &SyntaxElement, out: &mut String) {
|
|
|
|
match node_or_token {
|
|
|
|
SyntaxElement::Token(token) => {
|
|
|
|
self.render_token(&token, out);
|
|
|
|
}
|
|
|
|
SyntaxElement::Node(child_node) => {
|
2020-07-22 06:46:29 +00:00
|
|
|
self.render_node(&child_node, out);
|
2020-07-22 09:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 06:46:29 +00:00
|
|
|
fn render_node(&self, node: &SyntaxNode, out: &mut String) {
|
|
|
|
use ra_syntax::ast::AstNode;
|
|
|
|
if let Some(mod_path) = self.match_info.rendered_template_paths.get(&node) {
|
|
|
|
out.push_str(&mod_path.to_string());
|
|
|
|
// Emit everything except for the segment's name-ref, since we already effectively
|
|
|
|
// emitted that as part of `mod_path`.
|
|
|
|
if let Some(path) = ast::Path::cast(node.clone()) {
|
|
|
|
if let Some(segment) = path.segment() {
|
|
|
|
for node_or_token in segment.syntax().children_with_tokens() {
|
|
|
|
if node_or_token.kind() != SyntaxKind::NAME_REF {
|
|
|
|
self.render_node_or_token(&node_or_token, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.render_node_children(&node, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:15:19 +00:00
|
|
|
fn render_token(&self, token: &SyntaxToken, out: &mut String) {
|
|
|
|
if let Some(placeholder) = self.rule.get_placeholder(&token) {
|
|
|
|
if let Some(placeholder_value) =
|
|
|
|
self.match_info.placeholder_values.get(&Var(placeholder.ident.to_string()))
|
|
|
|
{
|
|
|
|
let range = &placeholder_value.range.range;
|
|
|
|
let mut matched_text =
|
|
|
|
self.file_src[usize::from(range.start())..usize::from(range.end())].to_owned();
|
|
|
|
let edit = matches_to_edit_at_offset(
|
|
|
|
&placeholder_value.inner_matches,
|
|
|
|
self.file_src,
|
|
|
|
range.start(),
|
|
|
|
self.rules,
|
|
|
|
);
|
|
|
|
edit.apply(&mut matched_text);
|
|
|
|
out.push_str(&matched_text);
|
|
|
|
} else {
|
|
|
|
// We validated that all placeholder references were valid before we
|
|
|
|
// started, so this shouldn't happen.
|
|
|
|
panic!(
|
|
|
|
"Internal error: replacement referenced unknown placeholder {}",
|
|
|
|
placeholder.ident
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out.push_str(token.text().as_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|