aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/completion.rs32
-rw-r--r--crates/ra_ide/src/completion/complete_attribute.rs293
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs6
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs10
-rw-r--r--crates/ra_ide/src/goto_definition.rs10
-rw-r--r--crates/ra_ide/src/inlay_hints.rs74
-rw-r--r--crates/ra_ide/src/marks.rs2
-rw-r--r--crates/ra_ide/src/references/rename.rs13
8 files changed, 378 insertions, 62 deletions
diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs
index 4ca0fdf4f..a0e06faa2 100644
--- a/crates/ra_ide/src/completion.rs
+++ b/crates/ra_ide/src/completion.rs
@@ -65,21 +65,23 @@ pub(crate) fn completions(
65 let ctx = CompletionContext::new(db, position, config)?; 65 let ctx = CompletionContext::new(db, position, config)?;
66 66
67 let mut acc = Completions::default(); 67 let mut acc = Completions::default();
68 68 if ctx.attribute_under_caret.is_some() {
69 complete_fn_param::complete_fn_param(&mut acc, &ctx); 69 complete_attribute::complete_attribute(&mut acc, &ctx);
70 complete_keyword::complete_expr_keyword(&mut acc, &ctx); 70 } else {
71 complete_keyword::complete_use_tree_keyword(&mut acc, &ctx); 71 complete_fn_param::complete_fn_param(&mut acc, &ctx);
72 complete_snippet::complete_expr_snippet(&mut acc, &ctx); 72 complete_keyword::complete_expr_keyword(&mut acc, &ctx);
73 complete_snippet::complete_item_snippet(&mut acc, &ctx); 73 complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
74 complete_qualified_path::complete_qualified_path(&mut acc, &ctx); 74 complete_snippet::complete_expr_snippet(&mut acc, &ctx);
75 complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx); 75 complete_snippet::complete_item_snippet(&mut acc, &ctx);
76 complete_dot::complete_dot(&mut acc, &ctx); 76 complete_qualified_path::complete_qualified_path(&mut acc, &ctx);
77 complete_record::complete_record(&mut acc, &ctx); 77 complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx);
78 complete_pattern::complete_pattern(&mut acc, &ctx); 78 complete_dot::complete_dot(&mut acc, &ctx);
79 complete_postfix::complete_postfix(&mut acc, &ctx); 79 complete_record::complete_record(&mut acc, &ctx);
80 complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); 80 complete_pattern::complete_pattern(&mut acc, &ctx);
81 complete_trait_impl::complete_trait_impl(&mut acc, &ctx); 81 complete_postfix::complete_postfix(&mut acc, &ctx);
82 complete_attribute::complete_attribute(&mut acc, &ctx); 82 complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
83 complete_trait_impl::complete_trait_impl(&mut acc, &ctx);
84 }
83 85
84 Some(acc) 86 Some(acc)
85} 87}
diff --git a/crates/ra_ide/src/completion/complete_attribute.rs b/crates/ra_ide/src/completion/complete_attribute.rs
index 8bf952798..20e6edc17 100644
--- a/crates/ra_ide/src/completion/complete_attribute.rs
+++ b/crates/ra_ide/src/completion/complete_attribute.rs
@@ -5,23 +5,26 @@
5 5
6use super::completion_context::CompletionContext; 6use super::completion_context::CompletionContext;
7use super::completion_item::{CompletionItem, CompletionItemKind, CompletionKind, Completions}; 7use super::completion_item::{CompletionItem, CompletionItemKind, CompletionKind, Completions};
8use ast::AttrInput;
8use ra_syntax::{ 9use ra_syntax::{
9 ast::{Attr, AttrKind}, 10 ast::{self, AttrKind},
10 AstNode, 11 AstNode, SyntaxKind,
11}; 12};
13use rustc_hash::FxHashSet;
12 14
13pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) { 15pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
14 if !ctx.is_attribute { 16 let attribute = ctx.attribute_under_caret.as_ref()?;
15 return;
16 }
17 17
18 let is_inner = ctx 18 match (attribute.path(), attribute.input()) {
19 .original_token 19 (Some(path), Some(AttrInput::TokenTree(token_tree))) if path.to_string() == "derive" => {
20 .ancestors() 20 complete_derive(acc, ctx, token_tree)
21 .find_map(Attr::cast) 21 }
22 .map(|attr| attr.kind() == AttrKind::Inner) 22 _ => complete_attribute_start(acc, ctx, attribute),
23 .unwrap_or(false); 23 }
24 Some(())
25}
24 26
27fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
25 for attr_completion in ATTRIBUTES { 28 for attr_completion in ATTRIBUTES {
26 let mut item = CompletionItem::new( 29 let mut item = CompletionItem::new(
27 CompletionKind::Attribute, 30 CompletionKind::Attribute,
@@ -37,7 +40,7 @@ pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
37 _ => {} 40 _ => {}
38 } 41 }
39 42
40 if is_inner || !attr_completion.should_be_inner { 43 if attribute.kind() == AttrKind::Inner || !attr_completion.should_be_inner {
41 acc.add(item); 44 acc.add(item);
42 } 45 }
43 } 46 }
@@ -126,6 +129,106 @@ const ATTRIBUTES: &[AttrCompletion] = &[
126 }, 129 },
127]; 130];
128 131
132fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input: ast::TokenTree) {
133 if let Ok(existing_derives) = parse_derive_input(derive_input) {
134 for derive_completion in DEFAULT_DERIVE_COMPLETIONS
135 .into_iter()
136 .filter(|completion| !existing_derives.contains(completion.label))
137 {
138 let mut label = derive_completion.label.to_owned();
139 for dependency in derive_completion
140 .dependencies
141 .into_iter()
142 .filter(|&&dependency| !existing_derives.contains(dependency))
143 {
144 label.push_str(", ");
145 label.push_str(dependency);
146 }
147 acc.add(
148 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
149 .kind(CompletionItemKind::Attribute),
150 );
151 }
152
153 for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) {
154 acc.add(
155 CompletionItem::new(
156 CompletionKind::Attribute,
157 ctx.source_range(),
158 custom_derive_name,
159 )
160 .kind(CompletionItemKind::Attribute),
161 );
162 }
163 }
164}
165
166fn parse_derive_input(derive_input: ast::TokenTree) -> Result<FxHashSet<String>, ()> {
167 match (derive_input.left_delimiter_token(), derive_input.right_delimiter_token()) {
168 (Some(left_paren), Some(right_paren))
169 if left_paren.kind() == SyntaxKind::L_PAREN
170 && right_paren.kind() == SyntaxKind::R_PAREN =>
171 {
172 let mut input_derives = FxHashSet::default();
173 let mut current_derive = String::new();
174 for token in derive_input
175 .syntax()
176 .children_with_tokens()
177 .filter_map(|token| token.into_token())
178 .skip_while(|token| token != &left_paren)
179 .skip(1)
180 .take_while(|token| token != &right_paren)
181 {
182 if SyntaxKind::COMMA == token.kind() {
183 if !current_derive.is_empty() {
184 input_derives.insert(current_derive);
185 current_derive = String::new();
186 }
187 } else {
188 current_derive.push_str(token.to_string().trim());
189 }
190 }
191
192 if !current_derive.is_empty() {
193 input_derives.insert(current_derive);
194 }
195 Ok(input_derives)
196 }
197 _ => Err(()),
198 }
199}
200
201fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
202 let mut result = FxHashSet::default();
203 ctx.scope().process_all_names(&mut |name, scope_def| {
204 if let hir::ScopeDef::MacroDef(mac) = scope_def {
205 if mac.is_derive_macro() {
206 result.insert(name.to_string());
207 }
208 }
209 });
210 result
211}
212
213struct DeriveCompletion {
214 label: &'static str,
215 dependencies: &'static [&'static str],
216}
217
218/// Standard Rust derives and the information about their dependencies
219/// (the dependencies are needed so that the main derive don't break the compilation when added)
220const DEFAULT_DERIVE_COMPLETIONS: &[DeriveCompletion] = &[
221 DeriveCompletion { label: "Clone", dependencies: &[] },
222 DeriveCompletion { label: "Copy", dependencies: &["Clone"] },
223 DeriveCompletion { label: "Debug", dependencies: &[] },
224 DeriveCompletion { label: "Default", dependencies: &[] },
225 DeriveCompletion { label: "Hash", dependencies: &[] },
226 DeriveCompletion { label: "PartialEq", dependencies: &[] },
227 DeriveCompletion { label: "Eq", dependencies: &["PartialEq"] },
228 DeriveCompletion { label: "PartialOrd", dependencies: &["PartialEq"] },
229 DeriveCompletion { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
230];
231
129#[cfg(test)] 232#[cfg(test)]
130mod tests { 233mod tests {
131 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; 234 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
@@ -136,6 +239,170 @@ mod tests {
136 } 239 }
137 240
138 #[test] 241 #[test]
242 fn empty_derive_completion() {
243 assert_debug_snapshot!(
244 do_attr_completion(
245 r"
246 #[derive(<|>)]
247 struct Test {}
248 ",
249 ),
250 @r###"
251 [
252 CompletionItem {
253 label: "Clone",
254 source_range: 30..30,
255 delete: 30..30,
256 insert: "Clone",
257 kind: Attribute,
258 },
259 CompletionItem {
260 label: "Copy, Clone",
261 source_range: 30..30,
262 delete: 30..30,
263 insert: "Copy, Clone",
264 kind: Attribute,
265 },
266 CompletionItem {
267 label: "Debug",
268 source_range: 30..30,
269 delete: 30..30,
270 insert: "Debug",
271 kind: Attribute,
272 },
273 CompletionItem {
274 label: "Default",
275 source_range: 30..30,
276 delete: 30..30,
277 insert: "Default",
278 kind: Attribute,
279 },
280 CompletionItem {
281 label: "Eq, PartialEq",
282 source_range: 30..30,
283 delete: 30..30,
284 insert: "Eq, PartialEq",
285 kind: Attribute,
286 },
287 CompletionItem {
288 label: "Hash",
289 source_range: 30..30,
290 delete: 30..30,
291 insert: "Hash",
292 kind: Attribute,
293 },
294 CompletionItem {
295 label: "Ord, PartialOrd, Eq, PartialEq",
296 source_range: 30..30,
297 delete: 30..30,
298 insert: "Ord, PartialOrd, Eq, PartialEq",
299 kind: Attribute,
300 },
301 CompletionItem {
302 label: "PartialEq",
303 source_range: 30..30,
304 delete: 30..30,
305 insert: "PartialEq",
306 kind: Attribute,
307 },
308 CompletionItem {
309 label: "PartialOrd, PartialEq",
310 source_range: 30..30,
311 delete: 30..30,
312 insert: "PartialOrd, PartialEq",
313 kind: Attribute,
314 },
315 ]
316 "###
317 );
318 }
319
320 #[test]
321 fn no_completion_for_incorrect_derive() {
322 assert_debug_snapshot!(
323 do_attr_completion(
324 r"
325 #[derive{<|>)]
326 struct Test {}
327 ",
328 ),
329 @"[]"
330 );
331 }
332
333 #[test]
334 fn derive_with_input_completion() {
335 assert_debug_snapshot!(
336 do_attr_completion(
337 r"
338 #[derive(serde::Serialize, PartialEq, <|>)]
339 struct Test {}
340 ",
341 ),
342 @r###"
343 [
344 CompletionItem {
345 label: "Clone",
346 source_range: 59..59,
347 delete: 59..59,
348 insert: "Clone",
349 kind: Attribute,
350 },
351 CompletionItem {
352 label: "Copy, Clone",
353 source_range: 59..59,
354 delete: 59..59,
355 insert: "Copy, Clone",
356 kind: Attribute,
357 },
358 CompletionItem {
359 label: "Debug",
360 source_range: 59..59,
361 delete: 59..59,
362 insert: "Debug",
363 kind: Attribute,
364 },
365 CompletionItem {
366 label: "Default",
367 source_range: 59..59,
368 delete: 59..59,
369 insert: "Default",
370 kind: Attribute,
371 },
372 CompletionItem {
373 label: "Eq",
374 source_range: 59..59,
375 delete: 59..59,
376 insert: "Eq",
377 kind: Attribute,
378 },
379 CompletionItem {
380 label: "Hash",
381 source_range: 59..59,
382 delete: 59..59,
383 insert: "Hash",
384 kind: Attribute,
385 },
386 CompletionItem {
387 label: "Ord, PartialOrd, Eq",
388 source_range: 59..59,
389 delete: 59..59,
390 insert: "Ord, PartialOrd, Eq",
391 kind: Attribute,
392 },
393 CompletionItem {
394 label: "PartialOrd",
395 source_range: 59..59,
396 delete: 59..59,
397 insert: "PartialOrd",
398 kind: Attribute,
399 },
400 ]
401 "###
402 );
403 }
404
405 #[test]
139 fn test_attribute_completion() { 406 fn test_attribute_completion() {
140 assert_debug_snapshot!( 407 assert_debug_snapshot!(
141 do_attr_completion( 408 do_attr_completion(
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs
index c529752d4..dd87bd119 100644
--- a/crates/ra_ide/src/completion/completion_context.rs
+++ b/crates/ra_ide/src/completion/completion_context.rs
@@ -58,7 +58,7 @@ pub(crate) struct CompletionContext<'a> {
58 pub(super) is_macro_call: bool, 58 pub(super) is_macro_call: bool,
59 pub(super) is_path_type: bool, 59 pub(super) is_path_type: bool,
60 pub(super) has_type_args: bool, 60 pub(super) has_type_args: bool,
61 pub(super) is_attribute: bool, 61 pub(super) attribute_under_caret: Option<ast::Attr>,
62} 62}
63 63
64impl<'a> CompletionContext<'a> { 64impl<'a> CompletionContext<'a> {
@@ -116,7 +116,7 @@ impl<'a> CompletionContext<'a> {
116 is_path_type: false, 116 is_path_type: false,
117 has_type_args: false, 117 has_type_args: false,
118 dot_receiver_is_ambiguous_float_literal: false, 118 dot_receiver_is_ambiguous_float_literal: false,
119 is_attribute: false, 119 attribute_under_caret: None,
120 }; 120 };
121 121
122 let mut original_file = original_file.syntax().clone(); 122 let mut original_file = original_file.syntax().clone();
@@ -200,6 +200,7 @@ impl<'a> CompletionContext<'a> {
200 Some(ty) 200 Some(ty)
201 }) 201 })
202 .flatten(); 202 .flatten();
203 self.attribute_under_caret = find_node_at_offset(&file_with_fake_ident, offset);
203 204
204 // First, let's try to complete a reference to some declaration. 205 // First, let's try to complete a reference to some declaration.
205 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&file_with_fake_ident, offset) { 206 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&file_with_fake_ident, offset) {
@@ -318,7 +319,6 @@ impl<'a> CompletionContext<'a> {
318 .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) 319 .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
319 .is_some(); 320 .is_some();
320 self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some(); 321 self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some();
321 self.is_attribute = path.syntax().parent().and_then(ast::Attr::cast).is_some();
322 322
323 self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); 323 self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
324 self.has_type_args = segment.type_arg_list().is_some(); 324 self.has_type_args = segment.type_arg_list().is_some();
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs
index 914a8b471..de35c6711 100644
--- a/crates/ra_ide/src/display/navigation_target.rs
+++ b/crates/ra_ide/src/display/navigation_target.rs
@@ -376,16 +376,20 @@ impl ToNav for hir::Local {
376impl ToNav for hir::TypeParam { 376impl ToNav for hir::TypeParam {
377 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { 377 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
378 let src = self.source(db); 378 let src = self.source(db);
379 let range = match src.value { 379 let full_range = match &src.value {
380 Either::Left(it) => it.syntax().text_range(), 380 Either::Left(it) => it.syntax().text_range(),
381 Either::Right(it) => it.syntax().text_range(), 381 Either::Right(it) => it.syntax().text_range(),
382 }; 382 };
383 let focus_range = match &src.value {
384 Either::Left(_) => None,
385 Either::Right(it) => it.name().map(|it| it.syntax().text_range()),
386 };
383 NavigationTarget { 387 NavigationTarget {
384 file_id: src.file_id.original_file(db), 388 file_id: src.file_id.original_file(db),
385 name: self.name(db).to_string().into(), 389 name: self.name(db).to_string().into(),
386 kind: TYPE_PARAM, 390 kind: TYPE_PARAM,
387 full_range: range, 391 full_range,
388 focus_range: None, 392 focus_range,
389 container_name: None, 393 container_name: None,
390 description: None, 394 description: None,
391 docs: None, 395 docs: None,
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs
index 5b9b3eef8..150895abb 100644
--- a/crates/ra_ide/src/goto_definition.rs
+++ b/crates/ra_ide/src/goto_definition.rs
@@ -787,14 +787,14 @@ mod tests {
787 #[test] 787 #[test]
788 fn goto_for_type_param() { 788 fn goto_for_type_param() {
789 check_goto( 789 check_goto(
790 " 790 r#"
791 //- /lib.rs 791 //- /lib.rs
792 struct Foo<T> { 792 struct Foo<T: Clone> {
793 t: <|>T, 793 t: <|>T,
794 } 794 }
795 ", 795 "#,
796 "T TYPE_PARAM FileId(1) 11..12", 796 "T TYPE_PARAM FileId(1) 11..19 11..12",
797 "T", 797 "T: Clone|T",
798 ); 798 );
799 } 799 }
800 800
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index 98483df32..b391f903a 100644
--- a/crates/ra_ide/src/inlay_hints.rs
+++ b/crates/ra_ide/src/inlay_hints.rs
@@ -9,6 +9,7 @@ use ra_syntax::{
9}; 9};
10 10
11use crate::{FileId, FunctionSignature}; 11use crate::{FileId, FunctionSignature};
12use stdx::to_lower_snake_case;
12 13
13#[derive(Clone, Debug, PartialEq, Eq)] 14#[derive(Clone, Debug, PartialEq, Eq)]
14pub struct InlayHintsConfig { 15pub struct InlayHintsConfig {
@@ -144,7 +145,7 @@ fn get_param_name_hints(
144 .iter() 145 .iter()
145 .skip(n_params_to_skip) 146 .skip(n_params_to_skip)
146 .zip(args) 147 .zip(args)
147 .filter(|(param, arg)| should_show_param_hint(&fn_signature, param, &arg)) 148 .filter(|(param, arg)| should_show_param_name_hint(sema, &fn_signature, param, &arg))
148 .map(|(param_name, arg)| InlayHint { 149 .map(|(param_name, arg)| InlayHint {
149 range: arg.syntax().text_range(), 150 range: arg.syntax().text_range(),
150 kind: InlayKind::ParameterHint, 151 kind: InlayKind::ParameterHint,
@@ -181,7 +182,7 @@ fn get_bind_pat_hints(
181 182
182fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ty: &Type) -> bool { 183fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ty: &Type) -> bool {
183 if let Some(Adt::Enum(enum_data)) = pat_ty.as_adt() { 184 if let Some(Adt::Enum(enum_data)) = pat_ty.as_adt() {
184 let pat_text = bind_pat.syntax().to_string(); 185 let pat_text = bind_pat.to_string();
185 enum_data 186 enum_data
186 .variants(db) 187 .variants(db)
187 .into_iter() 188 .into_iter()
@@ -198,7 +199,7 @@ fn should_not_display_type_hint(db: &RootDatabase, bind_pat: &ast::BindPat, pat_
198 } 199 }
199 200
200 if let Some(Adt::Struct(s)) = pat_ty.as_adt() { 201 if let Some(Adt::Struct(s)) = pat_ty.as_adt() {
201 if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.syntax().to_string() { 202 if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() {
202 return true; 203 return true;
203 } 204 }
204 } 205 }
@@ -230,15 +231,16 @@ fn should_not_display_type_hint(db: &RootDatabase, bind_pat: &ast::BindPat, pat_
230 false 231 false
231} 232}
232 233
233fn should_show_param_hint( 234fn should_show_param_name_hint(
235 sema: &Semantics<RootDatabase>,
234 fn_signature: &FunctionSignature, 236 fn_signature: &FunctionSignature,
235 param_name: &str, 237 param_name: &str,
236 argument: &ast::Expr, 238 argument: &ast::Expr,
237) -> bool { 239) -> bool {
240 let param_name = param_name.trim_start_matches('_');
238 if param_name.is_empty() 241 if param_name.is_empty()
239 || is_argument_similar_to_param(argument, param_name) 242 || Some(param_name) == fn_signature.name.as_ref().map(|s| s.trim_start_matches('_'))
240 || Some(param_name.trim_start_matches('_')) 243 || is_argument_similar_to_param_name(sema, argument, param_name)
241 == fn_signature.name.as_ref().map(|s| s.trim_start_matches('_'))
242 { 244 {
243 return false; 245 return false;
244 } 246 }
@@ -254,20 +256,42 @@ fn should_show_param_hint(
254 parameters_len != 1 || !is_obvious_param(param_name) 256 parameters_len != 1 || !is_obvious_param(param_name)
255} 257}
256 258
257fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool { 259fn is_argument_similar_to_param_name(
258 let argument_string = remove_ref(argument.clone()).syntax().to_string(); 260 sema: &Semantics<RootDatabase>,
259 let param_name = param_name.trim_start_matches('_'); 261 argument: &ast::Expr,
260 let argument_string = argument_string.trim_start_matches('_'); 262 param_name: &str,
261 argument_string.starts_with(&param_name) || argument_string.ends_with(&param_name) 263) -> bool {
264 if is_enum_name_similar_to_param_name(sema, argument, param_name) {
265 return true;
266 }
267 match get_string_representation(argument) {
268 None => false,
269 Some(repr) => {
270 let argument_string = repr.trim_start_matches('_');
271 argument_string.starts_with(param_name) || argument_string.ends_with(param_name)
272 }
273 }
274}
275
276fn is_enum_name_similar_to_param_name(
277 sema: &Semantics<RootDatabase>,
278 argument: &ast::Expr,
279 param_name: &str,
280) -> bool {
281 match sema.type_of_expr(argument).and_then(|t| t.as_adt()) {
282 Some(Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
283 _ => false,
284 }
262} 285}
263 286
264fn remove_ref(expr: ast::Expr) -> ast::Expr { 287fn get_string_representation(expr: &ast::Expr) -> Option<String> {
265 if let ast::Expr::RefExpr(ref_expr) = &expr { 288 match expr {
266 if let Some(inner) = ref_expr.expr() { 289 ast::Expr::MethodCallExpr(method_call_expr) => {
267 return inner; 290 Some(method_call_expr.name_ref()?.to_string())
268 } 291 }
292 ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?),
293 _ => Some(expr.to_string()),
269 } 294 }
270 expr
271} 295}
272 296
273fn is_obvious_param(param_name: &str) -> bool { 297fn is_obvious_param(param_name: &str) -> bool {
@@ -1073,6 +1097,12 @@ struct TestVarContainer {
1073 test_var: i32, 1097 test_var: i32,
1074} 1098}
1075 1099
1100impl TestVarContainer {
1101 fn test_var(&self) -> i32 {
1102 self.test_var
1103 }
1104}
1105
1076struct Test {} 1106struct Test {}
1077 1107
1078impl Test { 1108impl Test {
@@ -1098,10 +1128,15 @@ struct Param {}
1098fn different_order(param: &Param) {} 1128fn different_order(param: &Param) {}
1099fn different_order_mut(param: &mut Param) {} 1129fn different_order_mut(param: &mut Param) {}
1100fn has_underscore(_param: bool) {} 1130fn has_underscore(_param: bool) {}
1131fn enum_matches_param_name(completion_kind: CompletionKind) {}
1101 1132
1102fn twiddle(twiddle: bool) {} 1133fn twiddle(twiddle: bool) {}
1103fn doo(_doo: bool) {} 1134fn doo(_doo: bool) {}
1104 1135
1136enum CompletionKind {
1137 Keyword,
1138}
1139
1105fn main() { 1140fn main() {
1106 let container: TestVarContainer = TestVarContainer { test_var: 42 }; 1141 let container: TestVarContainer = TestVarContainer { test_var: 42 };
1107 let test: Test = Test {}; 1142 let test: Test = Test {};
@@ -1114,18 +1149,21 @@ fn main() {
1114 let test_var: i32 = 55; 1149 let test_var: i32 = 55;
1115 test_processed.no_hints_expected(22, test_var); 1150 test_processed.no_hints_expected(22, test_var);
1116 test_processed.no_hints_expected(33, container.test_var); 1151 test_processed.no_hints_expected(33, container.test_var);
1152 test_processed.no_hints_expected(44, container.test_var());
1117 test_processed.frob(false); 1153 test_processed.frob(false);
1118 1154
1119 twiddle(true); 1155 twiddle(true);
1120 doo(true); 1156 doo(true);
1121 1157
1122 let param_begin: Param = Param {}; 1158 let mut param_begin: Param = Param {};
1123 different_order(&param_begin); 1159 different_order(&param_begin);
1124 different_order(&mut param_begin); 1160 different_order(&mut param_begin);
1125 1161
1126 let param: bool = true; 1162 let param: bool = true;
1127 has_underscore(param); 1163 has_underscore(param);
1128 1164
1165 enum_matches_param_name(CompletionKind::Keyword);
1166
1129 let a: f64 = 7.0; 1167 let a: f64 = 7.0;
1130 let b: f64 = 4.0; 1168 let b: f64 = 4.0;
1131 let _: f64 = a.div_euclid(b); 1169 let _: f64 = a.div_euclid(b);
diff --git a/crates/ra_ide/src/marks.rs b/crates/ra_ide/src/marks.rs
index bea30fe2a..51ca4dde3 100644
--- a/crates/ra_ide/src/marks.rs
+++ b/crates/ra_ide/src/marks.rs
@@ -11,4 +11,6 @@ test_utils::marks!(
11 self_fulfilling_completion 11 self_fulfilling_completion
12 test_struct_field_completion_in_func_call 12 test_struct_field_completion_in_func_call
13 test_struct_field_completion_in_record_lit 13 test_struct_field_completion_in_record_lit
14 test_rename_struct_field_for_shorthand
15 test_rename_local_for_field_shorthand
14); 16);
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index fd17bc9f2..916edaef2 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -7,14 +7,13 @@ use ra_syntax::{
7 algo::find_node_at_offset, ast, lex_single_valid_syntax_kind, AstNode, SyntaxKind, SyntaxNode, 7 algo::find_node_at_offset, ast, lex_single_valid_syntax_kind, AstNode, SyntaxKind, SyntaxNode,
8}; 8};
9use ra_text_edit::TextEdit; 9use ra_text_edit::TextEdit;
10use test_utils::tested_by;
10 11
11use crate::{ 12use crate::{
12 FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, SourceChange, 13 references::find_all_refs, FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind,
13 SourceFileEdit, TextRange, 14 SourceChange, SourceFileEdit, TextRange,
14}; 15};
15 16
16use super::find_all_refs;
17
18pub(crate) fn rename( 17pub(crate) fn rename(
19 db: &RootDatabase, 18 db: &RootDatabase,
20 position: FilePosition, 19 position: FilePosition,
@@ -52,11 +51,13 @@ fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFil
52 let file_id = reference.file_range.file_id; 51 let file_id = reference.file_range.file_id;
53 let range = match reference.kind { 52 let range = match reference.kind {
54 ReferenceKind::FieldShorthandForField => { 53 ReferenceKind::FieldShorthandForField => {
54 tested_by!(test_rename_struct_field_for_shorthand);
55 replacement_text.push_str(new_name); 55 replacement_text.push_str(new_name);
56 replacement_text.push_str(": "); 56 replacement_text.push_str(": ");
57 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start()) 57 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start())
58 } 58 }
59 ReferenceKind::FieldShorthandForLocal => { 59 ReferenceKind::FieldShorthandForLocal => {
60 tested_by!(test_rename_local_for_field_shorthand);
60 replacement_text.push_str(": "); 61 replacement_text.push_str(": ");
61 replacement_text.push_str(new_name); 62 replacement_text.push_str(new_name);
62 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) 63 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())
@@ -147,7 +148,7 @@ fn rename_reference(
147mod tests { 148mod tests {
148 use insta::assert_debug_snapshot; 149 use insta::assert_debug_snapshot;
149 use ra_text_edit::TextEditBuilder; 150 use ra_text_edit::TextEditBuilder;
150 use test_utils::assert_eq_text; 151 use test_utils::{assert_eq_text, covers};
151 152
152 use crate::{ 153 use crate::{
153 mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId, 154 mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId,
@@ -379,6 +380,7 @@ mod tests {
379 380
380 #[test] 381 #[test]
381 fn test_rename_struct_field_for_shorthand() { 382 fn test_rename_struct_field_for_shorthand() {
383 covers!(test_rename_struct_field_for_shorthand);
382 test_rename( 384 test_rename(
383 r#" 385 r#"
384 struct Foo { 386 struct Foo {
@@ -408,6 +410,7 @@ mod tests {
408 410
409 #[test] 411 #[test]
410 fn test_rename_local_for_field_shorthand() { 412 fn test_rename_local_for_field_shorthand() {
413 covers!(test_rename_local_for_field_shorthand);
411 test_rename( 414 test_rename(
412 r#" 415 r#"
413 struct Foo { 416 struct Foo {