aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/inline_local_variable.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-01-11 23:05:07 +0000
committerLukas Wirth <[email protected]>2021-01-12 00:03:04 +0000
commitfbdb32adfc49e0d69b7fd8e44135bea59382d2cb (patch)
tree5cffb6d3f0a0a168bfa48d7adcd6fa233e30b3db /crates/assists/src/handlers/inline_local_variable.rs
parent52fa926f005890f07dffc789c84c2be57a6bdccc (diff)
Group references by FileId
Diffstat (limited to 'crates/assists/src/handlers/inline_local_variable.rs')
-rw-r--r--crates/assists/src/handlers/inline_local_variable.rs95
1 files changed, 48 insertions, 47 deletions
diff --git a/crates/assists/src/handlers/inline_local_variable.rs b/crates/assists/src/handlers/inline_local_variable.rs
index d559be9cb..928df6825 100644
--- a/crates/assists/src/handlers/inline_local_variable.rs
+++ b/crates/assists/src/handlers/inline_local_variable.rs
@@ -1,4 +1,7 @@
1use ide_db::{defs::Definition, search::ReferenceKind}; 1use ide_db::{
2 defs::Definition,
3 search::{FileReference, ReferenceKind},
4};
2use syntax::{ 5use syntax::{
3 ast::{self, AstNode, AstToken}, 6 ast::{self, AstNode, AstToken},
4 TextRange, 7 TextRange,
@@ -63,48 +66,44 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
63 let_stmt.syntax().text_range() 66 let_stmt.syntax().text_range()
64 }; 67 };
65 68
66 let mut wrap_in_parens = vec![true; refs.len()]; 69 let wrap_in_parens = refs
67 70 .iter()
68 for (i, desc) in refs.iter().enumerate() { 71 .flat_map(|refs| &refs.references)
69 let usage_node = ctx 72 .map(|&FileReference { range, .. }| {
70 .covering_node_for_range(desc.file_range.range) 73 let usage_node =
71 .ancestors() 74 ctx.covering_node_for_range(range).ancestors().find_map(ast::PathExpr::cast)?;
72 .find_map(ast::PathExpr::cast)?; 75 let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast);
73 let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast); 76 let usage_parent = match usage_parent_option {
74 let usage_parent = match usage_parent_option { 77 Some(u) => u,
75 Some(u) => u, 78 None => return Ok(false),
76 None => { 79 };
77 wrap_in_parens[i] = false; 80
78 continue; 81 Ok(!matches!((&initializer_expr, usage_parent),
79 } 82 (ast::Expr::CallExpr(_), _)
80 }; 83 | (ast::Expr::IndexExpr(_), _)
81 84 | (ast::Expr::MethodCallExpr(_), _)
82 wrap_in_parens[i] = match (&initializer_expr, usage_parent) { 85 | (ast::Expr::FieldExpr(_), _)
83 (ast::Expr::CallExpr(_), _) 86 | (ast::Expr::TryExpr(_), _)
84 | (ast::Expr::IndexExpr(_), _) 87 | (ast::Expr::RefExpr(_), _)
85 | (ast::Expr::MethodCallExpr(_), _) 88 | (ast::Expr::Literal(_), _)
86 | (ast::Expr::FieldExpr(_), _) 89 | (ast::Expr::TupleExpr(_), _)
87 | (ast::Expr::TryExpr(_), _) 90 | (ast::Expr::ArrayExpr(_), _)
88 | (ast::Expr::RefExpr(_), _) 91 | (ast::Expr::ParenExpr(_), _)
89 | (ast::Expr::Literal(_), _) 92 | (ast::Expr::PathExpr(_), _)
90 | (ast::Expr::TupleExpr(_), _) 93 | (ast::Expr::BlockExpr(_), _)
91 | (ast::Expr::ArrayExpr(_), _) 94 | (ast::Expr::EffectExpr(_), _)
92 | (ast::Expr::ParenExpr(_), _) 95 | (_, ast::Expr::CallExpr(_))
93 | (ast::Expr::PathExpr(_), _) 96 | (_, ast::Expr::TupleExpr(_))
94 | (ast::Expr::BlockExpr(_), _) 97 | (_, ast::Expr::ArrayExpr(_))
95 | (ast::Expr::EffectExpr(_), _) 98 | (_, ast::Expr::ParenExpr(_))
96 | (_, ast::Expr::CallExpr(_)) 99 | (_, ast::Expr::ForExpr(_))
97 | (_, ast::Expr::TupleExpr(_)) 100 | (_, ast::Expr::WhileExpr(_))
98 | (_, ast::Expr::ArrayExpr(_)) 101 | (_, ast::Expr::BreakExpr(_))
99 | (_, ast::Expr::ParenExpr(_)) 102 | (_, ast::Expr::ReturnExpr(_))
100 | (_, ast::Expr::ForExpr(_)) 103 | (_, ast::Expr::MatchExpr(_))
101 | (_, ast::Expr::WhileExpr(_)) 104 ))
102 | (_, ast::Expr::BreakExpr(_)) 105 })
103 | (_, ast::Expr::ReturnExpr(_)) 106 .collect::<Result<Vec<_>, _>>()?;
104 | (_, ast::Expr::MatchExpr(_)) => false,
105 _ => true,
106 };
107 }
108 107
109 let init_str = initializer_expr.syntax().text().to_string(); 108 let init_str = initializer_expr.syntax().text().to_string();
110 let init_in_paren = format!("({})", &init_str); 109 let init_in_paren = format!("({})", &init_str);
@@ -116,15 +115,17 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
116 target, 115 target,
117 move |builder| { 116 move |builder| {
118 builder.delete(delete_range); 117 builder.delete(delete_range);
119 for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { 118 for (reference, should_wrap) in
119 refs.iter().flat_map(|refs| &refs.references).zip(wrap_in_parens)
120 {
120 let replacement = 121 let replacement =
121 if should_wrap { init_in_paren.clone() } else { init_str.clone() }; 122 if should_wrap { init_in_paren.clone() } else { init_str.clone() };
122 match desc.kind { 123 match reference.kind {
123 ReferenceKind::FieldShorthandForLocal => { 124 ReferenceKind::FieldShorthandForLocal => {
124 mark::hit!(inline_field_shorthand); 125 mark::hit!(inline_field_shorthand);
125 builder.insert(desc.file_range.range.end(), format!(": {}", replacement)) 126 builder.insert(reference.range.end(), format!(": {}", replacement))
126 } 127 }
127 _ => builder.replace(desc.file_range.range, replacement), 128 _ => builder.replace(reference.range, replacement),
128 } 129 }
129 } 130 }
130 }, 131 },