@@ -27,11 +27,11 @@ impl std::error::Error for SsrError {}
27
27
//
28
28
// Search and replace with named wildcards that will match any expression.
29
29
// The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`.
30
- // A `$<name>:expr ` placeholder in the search pattern will match any expression and `$<name>` will reference it in the replacement.
30
+ // A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement.
31
31
// Available via the command `rust-analyzer.ssr`.
32
32
//
33
33
// ```rust
34
- // // Using structural search replace command [foo($a:expr , $b:expr ) ==>> ($a).foo($b)]
34
+ // // Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)]
35
35
//
36
36
// // BEFORE
37
37
// String::from(foo(y + 5, z))
@@ -79,7 +79,7 @@ struct SsrPattern {
79
79
vars : Vec < Var > ,
80
80
}
81
81
82
- /// represents an `$var` in an SSR query
82
+ /// Represents a `$var` in an SSR query.
83
83
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
84
84
struct Var ( String ) ;
85
85
@@ -122,8 +122,7 @@ impl FromStr for SsrQuery {
122
122
let mut pattern = it. next ( ) . expect ( "something" ) . to_string ( ) ;
123
123
124
124
for part in it. map ( split_by_var) {
125
- let ( var, var_type, remainder) = part?;
126
- is_expr ( var_type) ?;
125
+ let ( var, remainder) = part?;
127
126
let new_var = create_name ( var, & mut vars) ?;
128
127
pattern. push_str ( new_var) ;
129
128
pattern. push_str ( remainder) ;
@@ -166,15 +165,11 @@ fn traverse(node: &SyntaxNode, go: &mut impl FnMut(&SyntaxNode) -> bool) {
166
165
}
167
166
}
168
167
169
- fn split_by_var ( s : & str ) -> Result < ( & str , & str , & str ) , SsrError > {
170
- let end_of_name = s. find ( ':' ) . ok_or_else ( || SsrError ( "Use $<name>:expr" . into ( ) ) ) ? ;
171
- let name = & s[ 0 ..end_of_name] ;
168
+ fn split_by_var ( s : & str ) -> Result < ( & str , & str ) , SsrError > {
169
+ let end_of_name = s. find ( |c| !char :: is_ascii_alphanumeric ( & c ) ) . unwrap_or_else ( || s . len ( ) ) ;
170
+ let name = & s[ ..end_of_name] ;
172
171
is_name ( name) ?;
173
- let type_begin = end_of_name + 1 ;
174
- let type_length =
175
- s[ type_begin..] . find ( |c| !char:: is_ascii_alphanumeric ( & c) ) . unwrap_or_else ( || s. len ( ) ) ;
176
- let type_name = & s[ type_begin..type_begin + type_length] ;
177
- Ok ( ( name, type_name, & s[ type_begin + type_length..] ) )
172
+ Ok ( ( name, & s[ end_of_name..] ) )
178
173
}
179
174
180
175
fn is_name ( s : & str ) -> Result < ( ) , SsrError > {
@@ -185,14 +180,6 @@ fn is_name(s: &str) -> Result<(), SsrError> {
185
180
}
186
181
}
187
182
188
- fn is_expr ( s : & str ) -> Result < ( ) , SsrError > {
189
- if s == "expr" {
190
- Ok ( ( ) )
191
- } else {
192
- Err ( SsrError ( "Only $<name>:expr is supported" . into ( ) ) )
193
- }
194
- }
195
-
196
183
fn replace_in_template ( template : String , var : & str , new_var : & str ) -> String {
197
184
let name = format ! ( "${}" , var) ;
198
185
template. replace ( & name, new_var)
@@ -450,7 +437,7 @@ mod tests {
450
437
451
438
#[ test]
452
439
fn parser_happy_case ( ) {
453
- let result: SsrQuery = "foo($a:expr , $b:expr ) ==>> bar($b, $a)" . parse ( ) . unwrap ( ) ;
440
+ let result: SsrQuery = "foo($a, $b) ==>> bar($b, $a)" . parse ( ) . unwrap ( ) ;
454
441
assert_eq ! ( & result. pattern. pattern. text( ) , "foo(__search_pattern_a, __search_pattern_b)" ) ;
455
442
assert_eq ! ( result. pattern. vars. len( ) , 2 ) ;
456
443
assert_eq ! ( result. pattern. vars[ 0 ] . 0 , "__search_pattern_a" ) ;
@@ -476,31 +463,10 @@ mod tests {
476
463
) ;
477
464
}
478
465
479
- #[ test]
480
- fn parser_no_pattern_type ( ) {
481
- assert_eq ! ( parse_error_text( "foo($a) ==>>" ) , "Parse error: Use $<name>:expr" ) ;
482
- }
483
-
484
- #[ test]
485
- fn parser_invalid_name ( ) {
486
- assert_eq ! (
487
- parse_error_text( "foo($a+:expr) ==>>" ) ,
488
- "Parse error: Name can contain only alphanumerics and _"
489
- ) ;
490
- }
491
-
492
- #[ test]
493
- fn parser_invalid_type ( ) {
494
- assert_eq ! (
495
- parse_error_text( "foo($a:ident) ==>>" ) ,
496
- "Parse error: Only $<name>:expr is supported"
497
- ) ;
498
- }
499
-
500
466
#[ test]
501
467
fn parser_repeated_name ( ) {
502
468
assert_eq ! (
503
- parse_error_text( "foo($a:expr , $a:expr ) ==>>" ) ,
469
+ parse_error_text( "foo($a, $a) ==>>" ) ,
504
470
"Parse error: Name `a` repeats more than once"
505
471
) ;
506
472
}
@@ -517,7 +483,7 @@ mod tests {
517
483
518
484
#[ test]
519
485
fn parse_match_replace ( ) {
520
- let query: SsrQuery = "foo($x:expr ) ==>> bar($x)" . parse ( ) . unwrap ( ) ;
486
+ let query: SsrQuery = "foo($x) ==>> bar($x)" . parse ( ) . unwrap ( ) ;
521
487
let input = "fn main() { foo(1+2); }" ;
522
488
523
489
let code = SourceFile :: parse ( input) . tree ( ) ;
@@ -549,7 +515,7 @@ mod tests {
549
515
#[ test]
550
516
fn ssr_function_to_method ( ) {
551
517
assert_ssr_transform (
552
- "my_function($a:expr , $b:expr ) ==>> ($a).my_method($b)" ,
518
+ "my_function($a, $b) ==>> ($a).my_method($b)" ,
553
519
"loop { my_function( other_func(x, y), z + w) }" ,
554
520
"loop { (other_func(x, y)).my_method(z + w) }" ,
555
521
)
@@ -558,7 +524,7 @@ mod tests {
558
524
#[ test]
559
525
fn ssr_nested_function ( ) {
560
526
assert_ssr_transform (
561
- "foo($a:expr , $b:expr , $c:expr ) ==>> bar($c, baz($a, $b))" ,
527
+ "foo($a, $b, $c) ==>> bar($c, baz($a, $b))" ,
562
528
"fn main { foo (x + value.method(b), x+y-z, true && false) }" ,
563
529
"fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }" ,
564
530
)
@@ -567,7 +533,7 @@ mod tests {
567
533
#[ test]
568
534
fn ssr_expected_spacing ( ) {
569
535
assert_ssr_transform (
570
- "foo($x:expr ) + bar() ==>> bar($x)" ,
536
+ "foo($x) + bar() ==>> bar($x)" ,
571
537
"fn main() { foo(5) + bar() }" ,
572
538
"fn main() { bar(5) }" ,
573
539
) ;
@@ -576,7 +542,7 @@ mod tests {
576
542
#[ test]
577
543
fn ssr_with_extra_space ( ) {
578
544
assert_ssr_transform (
579
- "foo($x:expr ) + bar() ==>> bar($x)" ,
545
+ "foo($x ) + bar() ==>> bar($x)" ,
580
546
"fn main() { foo( 5 ) +bar( ) }" ,
581
547
"fn main() { bar(5) }" ,
582
548
) ;
@@ -585,7 +551,7 @@ mod tests {
585
551
#[ test]
586
552
fn ssr_keeps_nested_comment ( ) {
587
553
assert_ssr_transform (
588
- "foo($x:expr ) ==>> bar($x)" ,
554
+ "foo($x) ==>> bar($x)" ,
589
555
"fn main() { foo(other(5 /* using 5 */)) }" ,
590
556
"fn main() { bar(other(5 /* using 5 */)) }" ,
591
557
)
@@ -594,7 +560,7 @@ mod tests {
594
560
#[ test]
595
561
fn ssr_keeps_comment ( ) {
596
562
assert_ssr_transform (
597
- "foo($x:expr ) ==>> bar($x)" ,
563
+ "foo($x) ==>> bar($x)" ,
598
564
"fn main() { foo(5 /* using 5 */) }" ,
599
565
"fn main() { bar(5)/* using 5 */ }" ,
600
566
)
@@ -603,7 +569,7 @@ mod tests {
603
569
#[ test]
604
570
fn ssr_struct_lit ( ) {
605
571
assert_ssr_transform (
606
- "foo{a: $a:expr , b: $b:expr } ==>> foo::new($a, $b)" ,
572
+ "foo{a: $a, b: $b} ==>> foo::new($a, $b)" ,
607
573
"fn main() { foo{b:2, a:1} }" ,
608
574
"fn main() { foo::new(1, 2) }" ,
609
575
)
@@ -612,7 +578,7 @@ mod tests {
612
578
#[ test]
613
579
fn ssr_call_and_method_call ( ) {
614
580
assert_ssr_transform (
615
- "foo::<'a>($a:expr , $b:expr )) ==>> foo2($a, $b)" ,
581
+ "foo::<'a>($a, $b)) ==>> foo2($a, $b)" ,
616
582
"fn main() { get().bar.foo::<'a>(1); }" ,
617
583
"fn main() { foo2(get().bar, 1); }" ,
618
584
)
@@ -621,7 +587,7 @@ mod tests {
621
587
#[ test]
622
588
fn ssr_method_call_and_call ( ) {
623
589
assert_ssr_transform (
624
- "$o:expr .foo::<i32>($a:expr )) ==>> $o.foo2($a)" ,
590
+ "$o.foo::<i32>($a)) ==>> $o.foo2($a)" ,
625
591
"fn main() { X::foo::<i32>(x, 1); }" ,
626
592
"fn main() { x.foo2(1); }" ,
627
593
)
0 commit comments