Skip to content

Commit a35809b

Browse files
committed
add apply ssr assist
1 parent b0fba24 commit a35809b

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide_assists/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ syntax = { path = "../syntax", version = "0.0.0" }
1919
text_edit = { path = "../text_edit", version = "0.0.0" }
2020
profile = { path = "../profile", version = "0.0.0" }
2121
ide_db = { path = "../ide_db", version = "0.0.0" }
22+
ide_ssr = { path = "../ide_ssr", version = "0.0.0" }
2223
hir = { path = "../hir", version = "0.0.0" }
2324
test_utils = { path = "../test_utils", version = "0.0.0" }
2425

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

crates/ide_assists/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ mod handlers {
112112
mod add_missing_impl_members;
113113
mod add_turbo_fish;
114114
mod apply_demorgan;
115+
mod apply_ssr;
115116
mod auto_import;
116117
mod change_visibility;
117118
mod convert_integer_literal;
@@ -176,6 +177,7 @@ mod handlers {
176177
add_lifetime_to_type::add_lifetime_to_type,
177178
add_turbo_fish::add_turbo_fish,
178179
apply_demorgan::apply_demorgan,
180+
apply_ssr::apply_ssr,
179181
auto_import::auto_import,
180182
change_visibility::change_visibility,
181183
convert_integer_literal::convert_integer_literal,

0 commit comments

Comments
 (0)