Skip to content

Commit 4a0e424

Browse files
committed
add apply ssr assist
1 parent 3fdf26a commit 4a0e424

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

crates/ide/src/lib.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ use ide_db::{
5959
symbol_index::{self, FileSymbol},
6060
LineIndexDatabase,
6161
};
62-
use syntax::SourceFile;
62+
use syntax::{
63+
ast::{self, AstNode, AstToken},
64+
SourceFile,
65+
};
6366

6467
use crate::display::ToNav;
6568

@@ -504,7 +507,11 @@ impl Analysis {
504507
resolve: bool,
505508
frange: FileRange,
506509
) -> Cancelable<Vec<Assist>> {
507-
self.with_db(|db| Assist::get(db, config, resolve, frange))
510+
self.with_db(|db| {
511+
let mut acc = Assist::get(db, config, resolve, frange);
512+
add_ssr_assist(db, &mut acc, resolve, frange);
513+
acc
514+
})
508515
}
509516

510517
/// Computes the set of diagnostics for the given file.
@@ -579,6 +586,33 @@ impl Analysis {
579586
}
580587
}
581588

589+
fn add_ssr_assist(
590+
db: &RootDatabase,
591+
base: &mut Vec<Assist>,
592+
resolve: bool,
593+
frange: FileRange,
594+
) -> Option<()> {
595+
let comment = {
596+
let file = db.parse(frange.file_id);
597+
file.tree().syntax().token_at_offset(frange.range.start()).find_map(ast::Comment::cast)
598+
}?;
599+
let match_finder = ide_ssr::ssr_from_comment(db, frange, &comment).ok()?;
600+
601+
let mut assist = Assist {
602+
id: AssistId("ssr", AssistKind::RefactorRewrite),
603+
label: Label::new("Apply SSR"),
604+
group: None,
605+
target: comment.syntax().text_range(),
606+
source_change: None,
607+
};
608+
if resolve {
609+
assist.source_change = Some(SourceChange::from_edits(match_finder.edits(), Vec::new()));
610+
}
611+
612+
base.push(assist);
613+
Some(())
614+
}
615+
582616
#[test]
583617
fn analysis_is_send() {
584618
fn is_send<T: Send>() {}

crates/ide_ssr/src/from_comment.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! This module allows building an SSR MatchFinder by parsing the SSR rule
2+
//! from a comment.
3+
4+
use ide_db::{
5+
base_db::{FilePosition, FileRange},
6+
RootDatabase,
7+
};
8+
use syntax::ast::{self, AstToken};
9+
10+
use crate::{MatchFinder, SsrError};
11+
12+
pub fn ssr_from_comment<'db>(
13+
db: &'db RootDatabase,
14+
frange: FileRange,
15+
comment: &ast::Comment,
16+
) -> Result<MatchFinder<'db>, SsrError> {
17+
let comment_text_without_prefix = comment.text().strip_prefix(comment.prefix()).unwrap();
18+
let ssr_rule = comment_text_without_prefix.parse()?;
19+
20+
let lookup_context = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
21+
22+
let mut match_finder = MatchFinder::in_context(db, lookup_context, vec![]);
23+
match_finder.add_rule(ssr_rule)?;
24+
25+
Ok(match_finder)
26+
}

crates/ide_ssr/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
// | VS Code | **Rust Analyzer: Structural Search Replace**
5959
// |===
6060

61+
mod from_comment;
6162
mod matching;
6263
mod nester;
6364
mod parsing;
@@ -71,6 +72,7 @@ mod tests;
7172

7273
use crate::errors::bail;
7374
pub use crate::errors::SsrError;
75+
pub use crate::from_comment::ssr_from_comment;
7476
pub use crate::matching::Match;
7577
use crate::matching::MatchFailureReason;
7678
use hir::Semantics;

0 commit comments

Comments
 (0)