|
| 1 | +use ide_db::base_db::FilePosition; |
| 2 | +use syntax::{ast, AstToken}; |
| 3 | + |
| 4 | +use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; |
| 5 | + |
| 6 | +/// Assist: apply_ssr |
| 7 | +/// |
| 8 | +/// Applies structural search and replace |
| 9 | +/// |
| 10 | +/// ``` |
| 11 | +/// fn foo() {} |
| 12 | +/// fn bar() {} |
| 13 | +/// |
| 14 | +/// fn baz() { foo(); } |
| 15 | +/// |
| 16 | +/// // foo() ==>> bar() $0 |
| 17 | +/// ``` |
| 18 | +/// |
| 19 | +/// ``` |
| 20 | +/// fn foo() {} |
| 21 | +/// fn bar() {} |
| 22 | +/// |
| 23 | +/// fn baz() { bar(); } |
| 24 | +/// |
| 25 | +/// // foo() ==>> bar() |
| 26 | +/// ``` |
| 27 | +pub(crate) fn apply_ssr(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
| 28 | + let comment = ctx.find_token_at_offset::<ast::Comment>()?; |
| 29 | + let target = comment.syntax().text_range(); |
| 30 | + |
| 31 | + let lookup_context = FilePosition { file_id: ctx.frange.file_id, offset: ctx.offset() }; |
| 32 | + |
| 33 | + let comment_text_without_prefix = comment.text().strip_prefix(comment.prefix()).unwrap(); |
| 34 | + // Attempt to parse the comment line as an SSR rule. If the parsing fails |
| 35 | + // this assist is not applicable. |
| 36 | + let ssr_rule = comment_text_without_prefix.parse().ok()?; |
| 37 | + |
| 38 | + let mut match_finder = ide_ssr::MatchFinder::in_context(ctx.sema.db, lookup_context, vec![]); |
| 39 | + match_finder.add_rule(ssr_rule).ok()?; |
| 40 | + |
| 41 | + let group = GroupLabel("Apply SSR".into()); |
| 42 | + |
| 43 | + acc.add_group( |
| 44 | + &group, |
| 45 | + AssistId("ssr", AssistKind::RefactorRewrite), |
| 46 | + "Apply SSR in workspace", |
| 47 | + target, |
| 48 | + |assist_builder| { |
| 49 | + let edits = match_finder.edits(); |
| 50 | + |
| 51 | + for (file, edit) in edits { |
| 52 | + assist_builder.edit_file(file); |
| 53 | + for indel in edit { |
| 54 | + assist_builder.replace(indel.delete, indel.insert); |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + ); |
| 59 | + |
| 60 | + acc.add_group( |
| 61 | + &group, |
| 62 | + AssistId("ssr", AssistKind::RefactorRewrite), |
| 63 | + "Apply SSR in file", |
| 64 | + target, |
| 65 | + |assist_builder| { |
| 66 | + let edits = match_finder.edits(); |
| 67 | + |
| 68 | + for (file, edit) in edits { |
| 69 | + if file != ctx.frange.file_id { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + assist_builder.edit_file(file); |
| 74 | + for indel in edit { |
| 75 | + assist_builder.replace(indel.delete, indel.insert); |
| 76 | + } |
| 77 | + } |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + Some(()) |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use crate::tests::check_assist_by_label; |
| 87 | + |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn in_file() { |
| 92 | + check_assist_by_label( |
| 93 | + apply_ssr, |
| 94 | + r#" |
| 95 | +fn foo() {} |
| 96 | +fn bar() {} |
| 97 | +
|
| 98 | +// foo() ==>>$0 bar() |
| 99 | +
|
| 100 | +fn main() { |
| 101 | + foo(); |
| 102 | +} |
| 103 | +"#, |
| 104 | + r#" |
| 105 | +fn foo() {} |
| 106 | +fn bar() {} |
| 107 | +
|
| 108 | +// foo() ==>> bar() |
| 109 | +
|
| 110 | +fn main() { |
| 111 | + bar(); |
| 112 | +} |
| 113 | +"#, |
| 114 | + "Apply SSR in file", |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments