@@ -4,7 +4,10 @@ use ide_db::{
4
4
search:: { FileReference , SearchScope , UsageSearchResult } ,
5
5
} ;
6
6
use itertools:: Itertools ;
7
- use syntax:: { TextRange , ast:: { self , AstNode , IdentPat , NameOwner } } ;
7
+ use syntax:: {
8
+ ast:: { self , AstNode , IdentPat , NameOwner } ,
9
+ TextRange ,
10
+ } ;
8
11
9
12
use crate :: assist_context:: { AssistBuilder , AssistContext , Assists } ;
10
13
@@ -49,7 +52,11 @@ pub(crate) fn destructure_tuple_binding(acc: &mut Assists, ctx: &AssistContext)
49
52
destructure_tuple_binding_impl ( acc, ctx, false )
50
53
}
51
54
52
- pub ( crate ) fn destructure_tuple_binding_impl ( acc : & mut Assists , ctx : & AssistContext , with_sub_pattern : bool ) -> Option < ( ) > {
55
+ pub ( crate ) fn destructure_tuple_binding_impl (
56
+ acc : & mut Assists ,
57
+ ctx : & AssistContext ,
58
+ with_sub_pattern : bool ,
59
+ ) -> Option < ( ) > {
53
60
let ident_pat = ctx. find_node_at_offset :: < ast:: IdentPat > ( ) ?;
54
61
let data = collect_data ( ident_pat, ctx) ?;
55
62
@@ -121,7 +128,7 @@ fn generate_name(
121
128
_usages : & Option < UsageSearchResult > ,
122
129
_ctx : & AssistContext ,
123
130
) -> String {
124
- //TODO : detect if name already used
131
+ // FIXME : detect if name already used
125
132
format ! ( "_{}" , index)
126
133
}
127
134
@@ -133,16 +140,19 @@ struct TupleData {
133
140
// field_types: Vec<Type>,
134
141
usages : Option < UsageSearchResult > ,
135
142
}
136
- fn edit_tuple_assignment ( data : & TupleData , builder : & mut AssistBuilder , ctx : & AssistContext , in_sub_pattern : bool ) {
143
+ fn edit_tuple_assignment (
144
+ data : & TupleData ,
145
+ builder : & mut AssistBuilder ,
146
+ ctx : & AssistContext ,
147
+ in_sub_pattern : bool ,
148
+ ) {
137
149
let tuple_pat = {
138
150
let original = & data. ident_pat ;
139
151
let is_ref = original. ref_token ( ) . is_some ( ) ;
140
152
let is_mut = original. mut_token ( ) . is_some ( ) ;
141
- let fields =
142
- data
143
- . field_names
144
- . iter ( )
145
- . map ( |name| ast:: Pat :: from ( ast:: make:: ident_pat ( is_ref, is_mut, ast:: make:: name ( name) ) ) ) ;
153
+ let fields = data. field_names . iter ( ) . map ( |name| {
154
+ ast:: Pat :: from ( ast:: make:: ident_pat ( is_ref, is_mut, ast:: make:: name ( name) ) )
155
+ } ) ;
146
156
ast:: make:: tuple_pat ( fields)
147
157
} ;
148
158
@@ -231,17 +241,17 @@ fn detect_tuple_index(usage: &FileReference, data: &TupleData) -> Option<TupleIn
231
241
// PAREN_EXRP*
232
242
// FIELD_EXPR
233
243
234
- let node =
235
- usage. name . syntax ( )
244
+ let node = usage
245
+ . name
246
+ . syntax ( )
236
247
. ancestors ( )
237
248
. skip_while ( |s| !ast:: PathExpr :: can_cast ( s. kind ( ) ) )
238
249
. skip ( 1 ) // PATH_EXPR
239
- . find ( |s| !ast:: ParenExpr :: can_cast ( s. kind ( ) ) ) ?; // skip parentheses
250
+ . find ( |s| !ast:: ParenExpr :: can_cast ( s. kind ( ) ) ) ?; // skip parentheses
240
251
241
252
if let Some ( field_expr) = ast:: FieldExpr :: cast ( node) {
242
253
let idx = field_expr. name_ref ( ) ?. as_tuple_field ( ) ?;
243
254
if idx < data. field_names . len ( ) {
244
-
245
255
// special case: in macro call -> range of `field_expr` in applied macro, NOT range in actual file!
246
256
if field_expr. syntax ( ) . ancestors ( ) . any ( |a| ast:: MacroStmts :: can_cast ( a. kind ( ) ) ) {
247
257
cov_mark:: hit!( destructure_tuple_macro_call) ;
@@ -260,10 +270,7 @@ fn detect_tuple_index(usage: &FileReference, data: &TupleData) -> Option<TupleIn
260
270
return None ;
261
271
}
262
272
263
- Some ( TupleIndex {
264
- index : idx,
265
- range : field_expr. syntax ( ) . text_range ( ) ,
266
- } )
273
+ Some ( TupleIndex { index : idx, range : field_expr. syntax ( ) . text_range ( ) } )
267
274
} else {
268
275
// tuple index out of range
269
276
None
@@ -279,7 +286,7 @@ mod tests {
279
286
280
287
use crate :: tests:: { check_assist, check_assist_not_applicable} ;
281
288
282
- // Tests for direct tuple destructure:
289
+ // Tests for direct tuple destructure:
283
290
// `let $0t = (1,2);` -> `let (_0, _1) = (1,2);`
284
291
285
292
fn assist ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
@@ -459,13 +466,13 @@ fn main() {
459
466
r#"
460
467
fn main() {
461
468
let $0t = ("3.14", 0);
462
- let pi: f32 = t.0.parse().unwrap( );
469
+ let pi: f32 = t.0.parse().unwrap_or(0.0 );
463
470
}
464
471
"# ,
465
472
r#"
466
473
fn main() {
467
474
let ($0_0, _1) = ("3.14", 0);
468
- let pi: f32 = _0.parse().unwrap( );
475
+ let pi: f32 = _0.parse().unwrap_or(0.0 );
469
476
}
470
477
"# ,
471
478
)
@@ -849,7 +856,6 @@ fn f(o: Option<(usize, usize)>) {
849
856
)
850
857
}
851
858
852
-
853
859
#[ test]
854
860
fn in_match ( ) {
855
861
check_assist (
@@ -1019,7 +1025,7 @@ fn main() {
1019
1025
assist,
1020
1026
r#"
1021
1027
fn main {
1022
- let $0t =
1028
+ let $0t =
1023
1029
if 1 > 2 {
1024
1030
(1,2)
1025
1031
} else {
@@ -1036,7 +1042,7 @@ fn main {
1036
1042
"# ,
1037
1043
r#"
1038
1044
fn main {
1039
- let ($0_0, _1) =
1045
+ let ($0_0, _1) =
1040
1046
if 1 > 2 {
1041
1047
(1,2)
1042
1048
} else {
@@ -1062,28 +1068,21 @@ fn main {
1062
1068
destructure_tuple_binding_impl ( acc, ctx, true )
1063
1069
}
1064
1070
1065
- pub ( crate ) fn check_in_place_assist (
1066
-
1067
- ra_fixture_before : & str ,
1068
- ra_fixture_after : & str ,
1069
- ) {
1071
+ pub ( crate ) fn check_in_place_assist ( ra_fixture_before : & str , ra_fixture_after : & str ) {
1070
1072
check_assist_by_label (
1071
- assist,
1072
- ra_fixture_before,
1073
- ra_fixture_after,
1074
- "Destructure tuple in place"
1073
+ assist,
1074
+ ra_fixture_before,
1075
+ ra_fixture_after,
1076
+ "Destructure tuple in place" ,
1075
1077
) ;
1076
1078
}
1077
1079
1078
- pub ( crate ) fn check_sub_pattern_assist (
1079
- ra_fixture_before : & str ,
1080
- ra_fixture_after : & str ,
1081
- ) {
1080
+ pub ( crate ) fn check_sub_pattern_assist ( ra_fixture_before : & str , ra_fixture_after : & str ) {
1082
1081
check_assist_by_label (
1083
- assist,
1084
- ra_fixture_before,
1085
- ra_fixture_after,
1086
- "Destructure tuple in sub-pattern"
1082
+ assist,
1083
+ ra_fixture_before,
1084
+ ra_fixture_after,
1085
+ "Destructure tuple in sub-pattern" ,
1087
1086
) ;
1088
1087
}
1089
1088
@@ -1097,11 +1096,11 @@ fn main {
1097
1096
}
1098
1097
}
1099
1098
1100
- /// Tests for destructure of tuple in sub-pattern:
1099
+ /// Tests for destructure of tuple in sub-pattern:
1101
1100
/// `let $0t = (1,2);` -> `let t @ (_0, _1) = (1,2);`
1102
1101
mod sub_pattern {
1103
- use super :: * ;
1104
1102
use super :: assist:: * ;
1103
+ use super :: * ;
1105
1104
use crate :: tests:: check_assist_by_label;
1106
1105
1107
1106
#[ test]
@@ -1123,7 +1122,7 @@ fn main() {
1123
1122
"# ,
1124
1123
)
1125
1124
}
1126
-
1125
+
1127
1126
#[ test]
1128
1127
fn trigger_both_destructure_tuple_assists ( ) {
1129
1128
fn assist ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
@@ -1135,23 +1134,23 @@ fn main() {
1135
1134
}
1136
1135
"# ;
1137
1136
check_assist_by_label (
1138
- assist,
1139
- text,
1137
+ assist,
1138
+ text,
1140
1139
r#"
1141
1140
fn main() {
1142
1141
let ($0_0, _1) = (1,2);
1143
1142
}
1144
- "# ,
1143
+ "# ,
1145
1144
"Destructure tuple in place" ,
1146
1145
) ;
1147
1146
check_assist_by_label (
1148
- assist,
1149
- text,
1147
+ assist,
1148
+ text,
1150
1149
r#"
1151
1150
fn main() {
1152
1151
let t @ ($0_0, _1) = (1,2);
1153
1152
}
1154
- "# ,
1153
+ "# ,
1155
1154
"Destructure tuple in sub-pattern" ,
1156
1155
) ;
1157
1156
}
@@ -1175,7 +1174,7 @@ fn main() {
1175
1174
"# ,
1176
1175
)
1177
1176
}
1178
-
1177
+
1179
1178
#[ test]
1180
1179
fn keep_function_call ( ) {
1181
1180
cov_mark:: check!( destructure_tuple_call_with_subpattern) ;
@@ -1194,7 +1193,7 @@ fn main() {
1194
1193
"# ,
1195
1194
)
1196
1195
}
1197
-
1196
+
1198
1197
#[ test]
1199
1198
fn keep_type ( ) {
1200
1199
check_sub_pattern_assist (
@@ -1293,11 +1292,10 @@ fn main() {
1293
1292
}
1294
1293
1295
1294
/// Tests for tuple usage in macro call:
1296
- /// `dbg!( t.0)`
1297
- mod in_macro_call {
1295
+ /// `println!("{}", t.0)`
1296
+ mod in_macro_call {
1298
1297
use super :: assist:: * ;
1299
1298
1300
-
1301
1299
#[ test]
1302
1300
fn detect_macro_call ( ) {
1303
1301
cov_mark:: check!( destructure_tuple_macro_call) ;
@@ -1539,7 +1537,6 @@ fn main() {
1539
1537
}
1540
1538
"# ,
1541
1539
)
1542
-
1543
1540
}
1544
1541
}
1545
1542
}
0 commit comments