@@ -85,7 +85,9 @@ struct BoolNodeData {
85
85
86
86
/// Attempts to find an appropriate node to apply the action to.
87
87
fn find_bool_node ( ctx : & AssistContext < ' _ > ) -> Option < BoolNodeData > {
88
- if let Some ( let_stmt) = ctx. find_node_at_offset :: < ast:: LetStmt > ( ) {
88
+ let name: ast:: Name = ctx. find_node_at_offset ( ) ?;
89
+
90
+ if let Some ( let_stmt) = name. syntax ( ) . ancestors ( ) . find_map ( ast:: LetStmt :: cast) {
89
91
let bind_pat = match let_stmt. pat ( ) ? {
90
92
ast:: Pat :: IdentPat ( pat) => pat,
91
93
_ => {
@@ -101,12 +103,12 @@ fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
101
103
102
104
Some ( BoolNodeData {
103
105
target_node : let_stmt. syntax ( ) . clone ( ) ,
104
- name : bind_pat . name ( ) ? ,
106
+ name,
105
107
ty_annotation : let_stmt. ty ( ) ,
106
108
initializer : let_stmt. initializer ( ) ,
107
109
definition : Definition :: Local ( def) ,
108
110
} )
109
- } else if let Some ( const_) = ctx . find_node_at_offset :: < ast:: Const > ( ) {
111
+ } else if let Some ( const_) = name . syntax ( ) . ancestors ( ) . find_map ( ast:: Const :: cast ) {
110
112
let def = ctx. sema . to_def ( & const_) ?;
111
113
if !def. ty ( ctx. db ( ) ) . is_bool ( ) {
112
114
cov_mark:: hit!( not_applicable_non_bool_const) ;
@@ -115,12 +117,12 @@ fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
115
117
116
118
Some ( BoolNodeData {
117
119
target_node : const_. syntax ( ) . clone ( ) ,
118
- name : const_ . name ( ) ? ,
120
+ name,
119
121
ty_annotation : const_. ty ( ) ,
120
122
initializer : const_. body ( ) ,
121
123
definition : Definition :: Const ( def) ,
122
124
} )
123
- } else if let Some ( static_) = ctx . find_node_at_offset :: < ast:: Static > ( ) {
125
+ } else if let Some ( static_) = name . syntax ( ) . ancestors ( ) . find_map ( ast:: Static :: cast ) {
124
126
let def = ctx. sema . to_def ( & static_) ?;
125
127
if !def. ty ( ctx. db ( ) ) . is_bool ( ) {
126
128
cov_mark:: hit!( not_applicable_non_bool_static) ;
@@ -129,14 +131,14 @@ fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
129
131
130
132
Some ( BoolNodeData {
131
133
target_node : static_. syntax ( ) . clone ( ) ,
132
- name : static_ . name ( ) ? ,
134
+ name,
133
135
ty_annotation : static_. ty ( ) ,
134
136
initializer : static_. body ( ) ,
135
137
definition : Definition :: Static ( def) ,
136
138
} )
137
- } else if let Some ( field_name ) = ctx . find_node_at_offset :: < ast :: Name > ( ) {
138
- let field = field_name . syntax ( ) . ancestors ( ) . find_map ( ast:: RecordField :: cast) ?;
139
- if field. name ( ) ? != field_name {
139
+ } else {
140
+ let field = name . syntax ( ) . ancestors ( ) . find_map ( ast:: RecordField :: cast) ?;
141
+ if field. name ( ) ? != name {
140
142
return None ;
141
143
}
142
144
@@ -148,13 +150,11 @@ fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
148
150
}
149
151
Some ( BoolNodeData {
150
152
target_node : strukt. syntax ( ) . clone ( ) ,
151
- name : field_name ,
153
+ name,
152
154
ty_annotation : field. ty ( ) ,
153
155
initializer : None ,
154
156
definition : Definition :: Field ( def) ,
155
157
} )
156
- } else {
157
- None
158
158
}
159
159
}
160
160
@@ -528,6 +528,18 @@ fn main() {
528
528
)
529
529
}
530
530
531
+ #[ test]
532
+ fn local_variable_cursor_not_on_ident ( ) {
533
+ check_assist_not_applicable (
534
+ bool_to_enum,
535
+ r#"
536
+ fn main() {
537
+ let foo = $0true;
538
+ }
539
+ "# ,
540
+ )
541
+ }
542
+
531
543
#[ test]
532
544
fn local_variable_non_ident_pat ( ) {
533
545
cov_mark:: check!( not_applicable_in_non_ident_pat) ;
@@ -762,4 +774,9 @@ fn main() {
762
774
"# ,
763
775
)
764
776
}
777
+
778
+ #[ test]
779
+ fn not_applicable_to_other_names ( ) {
780
+ check_assist_not_applicable ( bool_to_enum, "fn $0main() {}" )
781
+ }
765
782
}
0 commit comments