diff options
Diffstat (limited to 'crates')
23 files changed, 382 insertions, 225 deletions
diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index 44f6a1dae..1cd532e80 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs | |||
@@ -47,8 +47,7 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> { | |||
47 | return None; | 47 | return None; |
48 | } | 48 | } |
49 | (vis_offset(&parent), keyword.text_range()) | 49 | (vis_offset(&parent), keyword.text_range()) |
50 | } else { | 50 | } else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() { |
51 | let field_name: ast::Name = ctx.find_node_at_offset()?; | ||
52 | let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; | 51 | let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; |
53 | if field.name()? != field_name { | 52 | if field.name()? != field_name { |
54 | tested_by!(change_visibility_field_false_positive); | 53 | tested_by!(change_visibility_field_false_positive); |
@@ -58,6 +57,13 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> { | |||
58 | return None; | 57 | return None; |
59 | } | 58 | } |
60 | (vis_offset(field.syntax()), field_name.syntax().text_range()) | 59 | (vis_offset(field.syntax()), field_name.syntax().text_range()) |
60 | } else if let Some(field) = ctx.find_node_at_offset::<ast::TupleFieldDef>() { | ||
61 | if field.visibility().is_some() { | ||
62 | return None; | ||
63 | } | ||
64 | (vis_offset(field.syntax()), field.syntax().text_range()) | ||
65 | } else { | ||
66 | return None; | ||
61 | }; | 67 | }; |
62 | 68 | ||
63 | ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| { | 69 | ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| { |
@@ -129,7 +135,8 @@ mod tests { | |||
129 | change_visibility, | 135 | change_visibility, |
130 | r"struct S { <|>field: u32 }", | 136 | r"struct S { <|>field: u32 }", |
131 | r"struct S { <|>pub(crate) field: u32 }", | 137 | r"struct S { <|>pub(crate) field: u32 }", |
132 | ) | 138 | ); |
139 | check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( <|>pub(crate) u32 )"); | ||
133 | } | 140 | } |
134 | 141 | ||
135 | #[test] | 142 | #[test] |
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index 58649c47e..859c70ad8 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use crate::{Assist, AssistCtx, AssistId}; | 1 | use crate::{Assist, AssistCtx, AssistId}; |
2 | 2 | ||
3 | use ast::{BlockExpr, Expr, ForExpr, IfExpr, LoopBodyOwner, LoopExpr, WhileExpr}; | 3 | use ast::LoopBodyOwner; |
4 | use ra_fmt::unwrap_trivial_block; | 4 | use ra_fmt::unwrap_trivial_block; |
5 | use ra_syntax::{ast, AstNode, TextRange, T}; | 5 | use ra_syntax::{ast, match_ast, AstNode, TextRange, T}; |
6 | 6 | ||
7 | // Assist: unwrap_block | 7 | // Assist: unwrap_block |
8 | // | 8 | // |
@@ -23,39 +23,40 @@ use ra_syntax::{ast, AstNode, TextRange, T}; | |||
23 | // ``` | 23 | // ``` |
24 | pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { | 24 | pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { |
25 | let l_curly_token = ctx.find_token_at_offset(T!['{'])?; | 25 | let l_curly_token = ctx.find_token_at_offset(T!['{'])?; |
26 | 26 | let block = ast::BlockExpr::cast(l_curly_token.parent())?; | |
27 | let res = if let Some(if_expr) = l_curly_token.ancestors().find_map(IfExpr::cast) { | 27 | let parent = block.syntax().parent()?; |
28 | // if expression | 28 | let (expr, expr_to_unwrap) = match_ast! { |
29 | let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); | 29 | match parent { |
30 | let expr_to_unwrap = expr_to_unwrap?; | 30 | ast::IfExpr(if_expr) => { |
31 | // Find if we are in a else if block | 31 | let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); |
32 | let ancestor = if_expr.syntax().ancestors().skip(1).find_map(ast::IfExpr::cast); | 32 | let expr_to_unwrap = expr_to_unwrap?; |
33 | 33 | // Find if we are in a else if block | |
34 | if let Some(ancestor) = ancestor { | 34 | let ancestor = if_expr.syntax().parent().and_then(ast::IfExpr::cast); |
35 | Some((ast::Expr::IfExpr(ancestor), expr_to_unwrap)) | 35 | |
36 | } else { | 36 | match ancestor { |
37 | Some((ast::Expr::IfExpr(if_expr), expr_to_unwrap)) | 37 | None => (ast::Expr::IfExpr(if_expr), expr_to_unwrap), |
38 | Some(ancestor) => (ast::Expr::IfExpr(ancestor), expr_to_unwrap), | ||
39 | } | ||
40 | }, | ||
41 | ast::ForExpr(for_expr) => { | ||
42 | let block_expr = for_expr.loop_body()?; | ||
43 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
44 | (ast::Expr::ForExpr(for_expr), expr_to_unwrap) | ||
45 | }, | ||
46 | ast::WhileExpr(while_expr) => { | ||
47 | let block_expr = while_expr.loop_body()?; | ||
48 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
49 | (ast::Expr::WhileExpr(while_expr), expr_to_unwrap) | ||
50 | }, | ||
51 | ast::LoopExpr(loop_expr) => { | ||
52 | let block_expr = loop_expr.loop_body()?; | ||
53 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
54 | (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap) | ||
55 | }, | ||
56 | _ => return None, | ||
38 | } | 57 | } |
39 | } else if let Some(for_expr) = l_curly_token.ancestors().find_map(ForExpr::cast) { | ||
40 | // for expression | ||
41 | let block_expr = for_expr.loop_body()?; | ||
42 | extract_expr(ctx.frange.range, block_expr) | ||
43 | .map(|expr_to_unwrap| (ast::Expr::ForExpr(for_expr), expr_to_unwrap)) | ||
44 | } else if let Some(while_expr) = l_curly_token.ancestors().find_map(WhileExpr::cast) { | ||
45 | // while expression | ||
46 | let block_expr = while_expr.loop_body()?; | ||
47 | extract_expr(ctx.frange.range, block_expr) | ||
48 | .map(|expr_to_unwrap| (ast::Expr::WhileExpr(while_expr), expr_to_unwrap)) | ||
49 | } else if let Some(loop_expr) = l_curly_token.ancestors().find_map(LoopExpr::cast) { | ||
50 | // loop expression | ||
51 | let block_expr = loop_expr.loop_body()?; | ||
52 | extract_expr(ctx.frange.range, block_expr) | ||
53 | .map(|expr_to_unwrap| (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap)) | ||
54 | } else { | ||
55 | None | ||
56 | }; | 58 | }; |
57 | 59 | ||
58 | let (expr, expr_to_unwrap) = res?; | ||
59 | ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { | 60 | ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { |
60 | edit.set_cursor(expr.syntax().text_range().start()); | 61 | edit.set_cursor(expr.syntax().text_range().start()); |
61 | edit.target(expr_to_unwrap.syntax().text_range()); | 62 | edit.target(expr_to_unwrap.syntax().text_range()); |
@@ -76,7 +77,7 @@ pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { | |||
76 | }) | 77 | }) |
77 | } | 78 | } |
78 | 79 | ||
79 | fn extract_expr(cursor_range: TextRange, block: BlockExpr) -> Option<Expr> { | 80 | fn extract_expr(cursor_range: TextRange, block: ast::BlockExpr) -> Option<ast::Expr> { |
80 | let cursor_in_range = block.l_curly_token()?.text_range().contains_range(cursor_range); | 81 | let cursor_in_range = block.l_curly_token()?.text_range().contains_range(cursor_range); |
81 | 82 | ||
82 | if cursor_in_range { | 83 | if cursor_in_range { |
diff --git a/crates/ra_flycheck/Cargo.toml b/crates/ra_flycheck/Cargo.toml index 324c33d9d..3d5093264 100644 --- a/crates/ra_flycheck/Cargo.toml +++ b/crates/ra_flycheck/Cargo.toml | |||
@@ -4,6 +4,9 @@ name = "ra_flycheck" | |||
4 | version = "0.1.0" | 4 | version = "0.1.0" |
5 | authors = ["rust-analyzer developers"] | 5 | authors = ["rust-analyzer developers"] |
6 | 6 | ||
7 | [lib] | ||
8 | doctest = false | ||
9 | |||
7 | [dependencies] | 10 | [dependencies] |
8 | crossbeam-channel = "0.4.0" | 11 | crossbeam-channel = "0.4.0" |
9 | lsp-types = { version = "0.74.0", features = ["proposed"] } | 12 | lsp-types = { version = "0.74.0", features = ["proposed"] } |
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 8eef51828..2bc34d449 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs | |||
@@ -117,7 +117,14 @@ fn lower_enum( | |||
117 | ast: &InFile<ast::EnumDef>, | 117 | ast: &InFile<ast::EnumDef>, |
118 | module_id: ModuleId, | 118 | module_id: ModuleId, |
119 | ) { | 119 | ) { |
120 | for var in ast.value.variant_list().into_iter().flat_map(|it| it.variants()) { | 120 | let expander = CfgExpander::new(db, ast.file_id, module_id.krate); |
121 | let variants = ast | ||
122 | .value | ||
123 | .variant_list() | ||
124 | .into_iter() | ||
125 | .flat_map(|it| it.variants()) | ||
126 | .filter(|var| expander.is_cfg_enabled(var)); | ||
127 | for var in variants { | ||
121 | trace.alloc( | 128 | trace.alloc( |
122 | || var.clone(), | 129 | || var.clone(), |
123 | || EnumVariantData { | 130 | || EnumVariantData { |
@@ -209,8 +216,7 @@ fn lower_struct( | |||
209 | match &ast.value { | 216 | match &ast.value { |
210 | ast::StructKind::Tuple(fl) => { | 217 | ast::StructKind::Tuple(fl) => { |
211 | for (i, fd) in fl.fields().enumerate() { | 218 | for (i, fd) in fl.fields().enumerate() { |
212 | let attrs = expander.parse_attrs(&fd); | 219 | if !expander.is_cfg_enabled(&fd) { |
213 | if !expander.is_cfg_enabled(&attrs) { | ||
214 | continue; | 220 | continue; |
215 | } | 221 | } |
216 | 222 | ||
@@ -227,8 +233,7 @@ fn lower_struct( | |||
227 | } | 233 | } |
228 | ast::StructKind::Record(fl) => { | 234 | ast::StructKind::Record(fl) => { |
229 | for fd in fl.fields() { | 235 | for fd in fl.fields() { |
230 | let attrs = expander.parse_attrs(&fd); | 236 | if !expander.is_cfg_enabled(&fd) { |
231 | if !expander.is_cfg_enabled(&attrs) { | ||
232 | continue; | 237 | continue; |
233 | } | 238 | } |
234 | 239 | ||
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 4edaad960..f5a7305dc 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -60,7 +60,8 @@ impl CfgExpander { | |||
60 | Attrs::new(owner, &self.hygiene) | 60 | Attrs::new(owner, &self.hygiene) |
61 | } | 61 | } |
62 | 62 | ||
63 | pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { | 63 | pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool { |
64 | let attrs = self.parse_attrs(owner); | ||
64 | attrs.is_cfg_enabled(&self.cfg_options) | 65 | attrs.is_cfg_enabled(&self.cfg_options) |
65 | } | 66 | } |
66 | } | 67 | } |
@@ -141,12 +142,8 @@ impl Expander { | |||
141 | InFile { file_id: self.current_file_id, value } | 142 | InFile { file_id: self.current_file_id, value } |
142 | } | 143 | } |
143 | 144 | ||
144 | pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs { | 145 | pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool { |
145 | self.cfg_expander.parse_attrs(owner) | 146 | self.cfg_expander.is_cfg_enabled(owner) |
146 | } | ||
147 | |||
148 | pub(crate) fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { | ||
149 | self.cfg_expander.is_cfg_enabled(attrs) | ||
150 | } | 147 | } |
151 | 148 | ||
152 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { | 149 | fn parse_path(&mut self, path: ast::Path) -> Option<Path> { |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 4671b72e9..443b057ab 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -162,8 +162,7 @@ impl ExprCollector<'_> { | |||
162 | 162 | ||
163 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { | 163 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { |
164 | let syntax_ptr = AstPtr::new(&expr); | 164 | let syntax_ptr = AstPtr::new(&expr); |
165 | let attrs = self.expander.parse_attrs(&expr); | 165 | if !self.expander.is_cfg_enabled(&expr) { |
166 | if !self.expander.is_cfg_enabled(&attrs) { | ||
167 | return self.missing_expr(); | 166 | return self.missing_expr(); |
168 | } | 167 | } |
169 | match expr { | 168 | match expr { |
@@ -329,8 +328,7 @@ impl ExprCollector<'_> { | |||
329 | .fields() | 328 | .fields() |
330 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) | 329 | .inspect(|field| field_ptrs.push(AstPtr::new(field))) |
331 | .filter_map(|field| { | 330 | .filter_map(|field| { |
332 | let attrs = self.expander.parse_attrs(&field); | 331 | if !self.expander.is_cfg_enabled(&field) { |
333 | if !self.expander.is_cfg_enabled(&attrs) { | ||
334 | return None; | 332 | return None; |
335 | } | 333 | } |
336 | let name = field.field_name()?.as_name(); | 334 | let name = field.field_name()?.as_name(); |
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 7a2067e49..2dbae04d3 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -150,51 +150,31 @@ pub struct TraitData { | |||
150 | 150 | ||
151 | impl TraitData { | 151 | impl TraitData { |
152 | pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> { | 152 | pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> { |
153 | let src = tr.lookup(db).source(db); | 153 | let tr_loc = tr.lookup(db); |
154 | let src = tr_loc.source(db); | ||
154 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 155 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
155 | let auto = src.value.auto_token().is_some(); | 156 | let auto = src.value.auto_token().is_some(); |
156 | let ast_id_map = db.ast_id_map(src.file_id); | 157 | let module_id = tr_loc.container.module(db); |
157 | 158 | ||
158 | let container = AssocContainerId::TraitId(tr); | 159 | let container = AssocContainerId::TraitId(tr); |
159 | let items = if let Some(item_list) = src.value.item_list() { | 160 | let mut items = Vec::new(); |
160 | item_list | 161 | |
161 | .impl_items() | 162 | if let Some(item_list) = src.value.item_list() { |
162 | .map(|item_node| match item_node { | 163 | let mut expander = Expander::new(db, tr_loc.ast_id.file_id, module_id); |
163 | ast::ImplItem::FnDef(it) => { | 164 | items.extend(collect_items( |
164 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); | 165 | db, |
165 | let def = FunctionLoc { | 166 | &mut expander, |
166 | container, | 167 | item_list.impl_items(), |
167 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | 168 | src.file_id, |
168 | } | 169 | container, |
169 | .intern(db) | 170 | )); |
170 | .into(); | 171 | items.extend(collect_items_in_macros( |
171 | (name, def) | 172 | db, |
172 | } | 173 | &mut expander, |
173 | ast::ImplItem::ConstDef(it) => { | 174 | &src.with_value(item_list), |
174 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); | 175 | container, |
175 | let def = ConstLoc { | 176 | )); |
176 | container, | 177 | } |
177 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
178 | } | ||
179 | .intern(db) | ||
180 | .into(); | ||
181 | (name, def) | ||
182 | } | ||
183 | ast::ImplItem::TypeAliasDef(it) => { | ||
184 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); | ||
185 | let def = TypeAliasLoc { | ||
186 | container, | ||
187 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
188 | } | ||
189 | .intern(db) | ||
190 | .into(); | ||
191 | (name, def) | ||
192 | } | ||
193 | }) | ||
194 | .collect() | ||
195 | } else { | ||
196 | Vec::new() | ||
197 | }; | ||
198 | Arc::new(TraitData { name, items, auto }) | 178 | Arc::new(TraitData { name, items, auto }) |
199 | } | 179 | } |
200 | 180 | ||
@@ -232,24 +212,22 @@ impl ImplData { | |||
232 | let target_type = TypeRef::from_ast_opt(&lower_ctx, src.value.target_type()); | 212 | let target_type = TypeRef::from_ast_opt(&lower_ctx, src.value.target_type()); |
233 | let is_negative = src.value.excl_token().is_some(); | 213 | let is_negative = src.value.excl_token().is_some(); |
234 | let module_id = impl_loc.container.module(db); | 214 | let module_id = impl_loc.container.module(db); |
215 | let container = AssocContainerId::ImplId(id); | ||
235 | 216 | ||
236 | let mut items = Vec::new(); | 217 | let mut items: Vec<AssocItemId> = Vec::new(); |
237 | 218 | ||
238 | if let Some(item_list) = src.value.item_list() { | 219 | if let Some(item_list) = src.value.item_list() { |
239 | let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); | 220 | let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); |
240 | items.extend(collect_impl_items( | 221 | items.extend( |
241 | db, | 222 | collect_items(db, &mut expander, item_list.impl_items(), src.file_id, container) |
242 | &mut expander, | 223 | .into_iter() |
243 | item_list.impl_items(), | 224 | .map(|(_, item)| item), |
244 | src.file_id, | 225 | ); |
245 | id, | 226 | items.extend( |
246 | )); | 227 | collect_items_in_macros(db, &mut expander, &src.with_value(item_list), container) |
247 | items.extend(collect_impl_items_in_macros( | 228 | .into_iter() |
248 | db, | 229 | .map(|(_, item)| item), |
249 | &mut expander, | 230 | ); |
250 | &src.with_value(item_list), | ||
251 | id, | ||
252 | )); | ||
253 | } | 231 | } |
254 | 232 | ||
255 | let res = ImplData { target_trait, target_type, items, is_negative }; | 233 | let res = ImplData { target_trait, target_type, items, is_negative }; |
@@ -292,49 +270,50 @@ impl ConstData { | |||
292 | } | 270 | } |
293 | } | 271 | } |
294 | 272 | ||
295 | fn collect_impl_items_in_macros( | 273 | fn collect_items_in_macros( |
296 | db: &dyn DefDatabase, | 274 | db: &dyn DefDatabase, |
297 | expander: &mut Expander, | 275 | expander: &mut Expander, |
298 | impl_def: &InFile<ast::ItemList>, | 276 | impl_def: &InFile<ast::ItemList>, |
299 | id: ImplId, | 277 | container: AssocContainerId, |
300 | ) -> Vec<AssocItemId> { | 278 | ) -> Vec<(Name, AssocItemId)> { |
301 | let mut res = Vec::new(); | 279 | let mut res = Vec::new(); |
302 | 280 | ||
303 | // We set a limit to protect against infinite recursion | 281 | // We set a limit to protect against infinite recursion |
304 | let limit = 100; | 282 | let limit = 100; |
305 | 283 | ||
306 | for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { | 284 | for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { |
307 | res.extend(collect_impl_items_in_macro(db, expander, m, id, limit)) | 285 | res.extend(collect_items_in_macro(db, expander, m, container, limit)) |
308 | } | 286 | } |
309 | 287 | ||
310 | res | 288 | res |
311 | } | 289 | } |
312 | 290 | ||
313 | fn collect_impl_items_in_macro( | 291 | fn collect_items_in_macro( |
314 | db: &dyn DefDatabase, | 292 | db: &dyn DefDatabase, |
315 | expander: &mut Expander, | 293 | expander: &mut Expander, |
316 | m: ast::MacroCall, | 294 | m: ast::MacroCall, |
317 | id: ImplId, | 295 | container: AssocContainerId, |
318 | limit: usize, | 296 | limit: usize, |
319 | ) -> Vec<AssocItemId> { | 297 | ) -> Vec<(Name, AssocItemId)> { |
320 | if limit == 0 { | 298 | if limit == 0 { |
321 | return Vec::new(); | 299 | return Vec::new(); |
322 | } | 300 | } |
323 | 301 | ||
324 | if let Some((mark, items)) = expander.enter_expand(db, None, m) { | 302 | if let Some((mark, items)) = expander.enter_expand(db, None, m) { |
325 | let items: InFile<ast::MacroItems> = expander.to_source(items); | 303 | let items: InFile<ast::MacroItems> = expander.to_source(items); |
326 | let mut res = collect_impl_items( | 304 | let mut res = collect_items( |
327 | db, | 305 | db, |
328 | expander, | 306 | expander, |
329 | items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())), | 307 | items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())), |
330 | items.file_id, | 308 | items.file_id, |
331 | id, | 309 | container, |
332 | ); | 310 | ); |
311 | |||
333 | // Recursive collect macros | 312 | // Recursive collect macros |
334 | // Note that ast::ModuleItem do not include ast::MacroCall | 313 | // Note that ast::ModuleItem do not include ast::MacroCall |
335 | // We cannot use ModuleItemOwner::items here | 314 | // We cannot use ModuleItemOwner::items here |
336 | for it in items.value.syntax().children().filter_map(ast::MacroCall::cast) { | 315 | for it in items.value.syntax().children().filter_map(ast::MacroCall::cast) { |
337 | res.extend(collect_impl_items_in_macro(db, expander, it, id, limit - 1)) | 316 | res.extend(collect_items_in_macro(db, expander, it, container, limit - 1)) |
338 | } | 317 | } |
339 | expander.exit(db, mark); | 318 | expander.exit(db, mark); |
340 | res | 319 | res |
@@ -343,44 +322,38 @@ fn collect_impl_items_in_macro( | |||
343 | } | 322 | } |
344 | } | 323 | } |
345 | 324 | ||
346 | fn collect_impl_items( | 325 | fn collect_items( |
347 | db: &dyn DefDatabase, | 326 | db: &dyn DefDatabase, |
348 | expander: &mut Expander, | 327 | expander: &mut Expander, |
349 | impl_items: impl Iterator<Item = ImplItem>, | 328 | impl_items: impl Iterator<Item = ImplItem>, |
350 | file_id: crate::HirFileId, | 329 | file_id: crate::HirFileId, |
351 | id: ImplId, | 330 | container: AssocContainerId, |
352 | ) -> Vec<AssocItemId> { | 331 | ) -> Vec<(Name, AssocItemId)> { |
353 | let items = db.ast_id_map(file_id); | 332 | let items = db.ast_id_map(file_id); |
354 | 333 | ||
355 | impl_items | 334 | impl_items |
356 | .filter_map(|item_node| match item_node { | 335 | .filter_map(|item_node| match item_node { |
357 | ast::ImplItem::FnDef(it) => { | 336 | ast::ImplItem::FnDef(it) => { |
358 | let attrs = expander.parse_attrs(&it); | 337 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); |
359 | if !expander.is_cfg_enabled(&attrs) { | 338 | if !expander.is_cfg_enabled(&it) { |
360 | return None; | 339 | return None; |
361 | } | 340 | } |
362 | let def = FunctionLoc { | 341 | let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } |
363 | container: AssocContainerId::ImplId(id), | 342 | .intern(db); |
364 | ast_id: AstId::new(file_id, items.ast_id(&it)), | 343 | Some((name, def.into())) |
365 | } | ||
366 | .intern(db); | ||
367 | Some(def.into()) | ||
368 | } | 344 | } |
369 | ast::ImplItem::ConstDef(it) => { | 345 | ast::ImplItem::ConstDef(it) => { |
370 | let def = ConstLoc { | 346 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); |
371 | container: AssocContainerId::ImplId(id), | 347 | let def = ConstLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } |
372 | ast_id: AstId::new(file_id, items.ast_id(&it)), | 348 | .intern(db); |
373 | } | 349 | Some((name, def.into())) |
374 | .intern(db); | ||
375 | Some(def.into()) | ||
376 | } | 350 | } |
377 | ast::ImplItem::TypeAliasDef(it) => { | 351 | ast::ImplItem::TypeAliasDef(it) => { |
378 | let def = TypeAliasLoc { | 352 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); |
379 | container: AssocContainerId::ImplId(id), | 353 | let def = |
380 | ast_id: AstId::new(file_id, items.ast_id(&it)), | 354 | TypeAliasLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } |
381 | } | 355 | .intern(db); |
382 | .intern(db); | 356 | Some((name, def.into())) |
383 | Some(def.into()) | ||
384 | } | 357 | } |
385 | }) | 358 | }) |
386 | .collect() | 359 | .collect() |
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 588d81282..d60732e19 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs | |||
@@ -361,6 +361,33 @@ fn no_such_field_with_feature_flag_diagnostics() { | |||
361 | } | 361 | } |
362 | 362 | ||
363 | #[test] | 363 | #[test] |
364 | fn no_such_field_enum_with_feature_flag_diagnostics() { | ||
365 | let diagnostics = TestDB::with_files( | ||
366 | r#" | ||
367 | //- /lib.rs crate:foo cfg:feature=foo | ||
368 | enum Foo { | ||
369 | #[cfg(not(feature = "foo"))] | ||
370 | Buz, | ||
371 | #[cfg(feature = "foo")] | ||
372 | Bar, | ||
373 | Baz | ||
374 | } | ||
375 | |||
376 | fn test_fn(f: Foo) { | ||
377 | match f { | ||
378 | Foo::Bar => {}, | ||
379 | Foo::Baz => {}, | ||
380 | } | ||
381 | } | ||
382 | "#, | ||
383 | ) | ||
384 | .diagnostics() | ||
385 | .0; | ||
386 | |||
387 | assert_snapshot!(diagnostics, @r###""###); | ||
388 | } | ||
389 | |||
390 | #[test] | ||
364 | fn no_such_field_with_feature_flag_diagnostics_on_struct_lit() { | 391 | fn no_such_field_with_feature_flag_diagnostics_on_struct_lit() { |
365 | let diagnostics = TestDB::with_files( | 392 | let diagnostics = TestDB::with_files( |
366 | r#" | 393 | r#" |
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index e555c879a..9d32cbc7a 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs | |||
@@ -2055,7 +2055,7 @@ fn test<I: Iterator<Item: Iterator<Item = u32>>>() { | |||
2055 | #[test] | 2055 | #[test] |
2056 | fn proc_macro_server_types() { | 2056 | fn proc_macro_server_types() { |
2057 | assert_snapshot!( | 2057 | assert_snapshot!( |
2058 | infer_with_mismatches(r#" | 2058 | infer(r#" |
2059 | macro_rules! with_api { | 2059 | macro_rules! with_api { |
2060 | ($S:ident, $self:ident, $m:ident) => { | 2060 | ($S:ident, $self:ident, $m:ident) => { |
2061 | $m! { | 2061 | $m! { |
@@ -2069,9 +2069,9 @@ macro_rules! with_api { | |||
2069 | } | 2069 | } |
2070 | macro_rules! associated_item { | 2070 | macro_rules! associated_item { |
2071 | (type TokenStream) => | 2071 | (type TokenStream) => |
2072 | (type TokenStream: 'static + Clone;); | 2072 | (type TokenStream: 'static;); |
2073 | (type Group) => | 2073 | (type Group) => |
2074 | (type Group: 'static + Clone;); | 2074 | (type Group: 'static;); |
2075 | ($($item:tt)*) => ($($item)*;) | 2075 | ($($item:tt)*) => ($($item)*;) |
2076 | } | 2076 | } |
2077 | macro_rules! declare_server_traits { | 2077 | macro_rules! declare_server_traits { |
@@ -2083,21 +2083,23 @@ macro_rules! declare_server_traits { | |||
2083 | } | 2083 | } |
2084 | 2084 | ||
2085 | $(pub trait $name: Types { | 2085 | $(pub trait $name: Types { |
2086 | $(associated_item!(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?);)* | 2086 | $(associated_item!(fn $method($($arg: $arg_ty),*) $(-> $ret_ty)?);)* |
2087 | })* | 2087 | })* |
2088 | 2088 | ||
2089 | pub trait Server: Types $(+ $name)* {} | 2089 | pub trait Server: Types $(+ $name)* {} |
2090 | impl<S: Types $(+ $name)*> Server for S {} | 2090 | impl<S: Types $(+ $name)*> Server for S {} |
2091 | } | 2091 | } |
2092 | } | 2092 | } |
2093 | |||
2093 | with_api!(Self, self_, declare_server_traits); | 2094 | with_api!(Self, self_, declare_server_traits); |
2094 | struct Group {} | 2095 | struct G {} |
2095 | struct TokenStream {} | 2096 | struct T {} |
2096 | struct Rustc; | 2097 | struct Rustc; |
2097 | impl Types for Rustc { | 2098 | impl Types for Rustc { |
2098 | type TokenStream = TokenStream; | 2099 | type TokenStream = T; |
2099 | type Group = Group; | 2100 | type Group = G; |
2100 | } | 2101 | } |
2102 | |||
2101 | fn make<T>() -> T { loop {} } | 2103 | fn make<T>() -> T { loop {} } |
2102 | impl TokenStream for Rustc { | 2104 | impl TokenStream for Rustc { |
2103 | fn new() -> Self::TokenStream { | 2105 | fn new() -> Self::TokenStream { |
@@ -2105,17 +2107,17 @@ impl TokenStream for Rustc { | |||
2105 | make() | 2107 | make() |
2106 | } | 2108 | } |
2107 | } | 2109 | } |
2108 | "#, true), | 2110 | "#), |
2109 | @r###" | 2111 | @r###" |
2110 | 1115..1126 '{ loop {} }': T | 2112 | 1062..1073 '{ loop {} }': T |
2111 | 1117..1124 'loop {}': ! | 2113 | 1064..1071 'loop {}': ! |
2112 | 1122..1124 '{}': () | 2114 | 1069..1071 '{}': () |
2113 | 1190..1253 '{ ... }': {unknown} | 2115 | 1137..1200 '{ ... }': T |
2114 | 1204..1209 'group': {unknown} | 2116 | 1151..1156 'group': G |
2115 | 1225..1229 'make': fn make<{unknown}>() -> {unknown} | 2117 | 1172..1176 'make': fn make<G>() -> G |
2116 | 1225..1231 'make()': {unknown} | 2118 | 1172..1178 'make()': G |
2117 | 1241..1245 'make': fn make<{unknown}>() -> {unknown} | 2119 | 1188..1192 'make': fn make<T>() -> T |
2118 | 1241..1247 'make()': {unknown} | 2120 | 1188..1194 'make()': T |
2119 | "### | 2121 | "### |
2120 | ); | 2122 | ); |
2121 | } | 2123 | } |
diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index a0e06faa2..8bdc43b1a 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs | |||
@@ -65,23 +65,20 @@ 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 | if ctx.attribute_under_caret.is_some() { | 68 | complete_attribute::complete_attribute(&mut acc, &ctx); |
69 | complete_attribute::complete_attribute(&mut acc, &ctx); | 69 | complete_fn_param::complete_fn_param(&mut acc, &ctx); |
70 | } else { | 70 | complete_keyword::complete_expr_keyword(&mut acc, &ctx); |
71 | complete_fn_param::complete_fn_param(&mut acc, &ctx); | 71 | complete_keyword::complete_use_tree_keyword(&mut acc, &ctx); |
72 | complete_keyword::complete_expr_keyword(&mut acc, &ctx); | 72 | complete_snippet::complete_expr_snippet(&mut acc, &ctx); |
73 | complete_keyword::complete_use_tree_keyword(&mut acc, &ctx); | 73 | complete_snippet::complete_item_snippet(&mut acc, &ctx); |
74 | complete_snippet::complete_expr_snippet(&mut acc, &ctx); | 74 | complete_qualified_path::complete_qualified_path(&mut acc, &ctx); |
75 | complete_snippet::complete_item_snippet(&mut acc, &ctx); | 75 | complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx); |
76 | complete_qualified_path::complete_qualified_path(&mut acc, &ctx); | 76 | complete_dot::complete_dot(&mut acc, &ctx); |
77 | complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx); | 77 | complete_record::complete_record(&mut acc, &ctx); |
78 | complete_dot::complete_dot(&mut acc, &ctx); | 78 | complete_pattern::complete_pattern(&mut acc, &ctx); |
79 | complete_record::complete_record(&mut acc, &ctx); | 79 | complete_postfix::complete_postfix(&mut acc, &ctx); |
80 | complete_pattern::complete_pattern(&mut acc, &ctx); | 80 | complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); |
81 | complete_postfix::complete_postfix(&mut acc, &ctx); | 81 | complete_trait_impl::complete_trait_impl(&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 | } | ||
85 | 82 | ||
86 | Some(acc) | 83 | Some(acc) |
87 | } | 84 | } |
diff --git a/crates/ra_ide/src/completion/complete_attribute.rs b/crates/ra_ide/src/completion/complete_attribute.rs index 20e6edc17..f17266221 100644 --- a/crates/ra_ide/src/completion/complete_attribute.rs +++ b/crates/ra_ide/src/completion/complete_attribute.rs | |||
@@ -3,20 +3,21 @@ | |||
3 | //! This module uses a bit of static metadata to provide completions | 3 | //! This module uses a bit of static metadata to provide completions |
4 | //! for built-in attributes. | 4 | //! for built-in attributes. |
5 | 5 | ||
6 | use super::completion_context::CompletionContext; | 6 | use ra_syntax::{ast, AstNode, SyntaxKind}; |
7 | use super::completion_item::{CompletionItem, CompletionItemKind, CompletionKind, Completions}; | ||
8 | use ast::AttrInput; | ||
9 | use ra_syntax::{ | ||
10 | ast::{self, AttrKind}, | ||
11 | AstNode, SyntaxKind, | ||
12 | }; | ||
13 | use rustc_hash::FxHashSet; | 7 | use rustc_hash::FxHashSet; |
14 | 8 | ||
9 | use crate::completion::{ | ||
10 | completion_context::CompletionContext, | ||
11 | completion_item::{CompletionItem, CompletionItemKind, CompletionKind, Completions}, | ||
12 | }; | ||
13 | |||
15 | pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | 14 | pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { |
16 | let attribute = ctx.attribute_under_caret.as_ref()?; | 15 | let attribute = ctx.attribute_under_caret.as_ref()?; |
17 | 16 | ||
18 | match (attribute.path(), attribute.input()) { | 17 | match (attribute.path(), attribute.input()) { |
19 | (Some(path), Some(AttrInput::TokenTree(token_tree))) if path.to_string() == "derive" => { | 18 | (Some(path), Some(ast::AttrInput::TokenTree(token_tree))) |
19 | if path.to_string() == "derive" => | ||
20 | { | ||
20 | complete_derive(acc, ctx, token_tree) | 21 | complete_derive(acc, ctx, token_tree) |
21 | } | 22 | } |
22 | _ => complete_attribute_start(acc, ctx, attribute), | 23 | _ => complete_attribute_start(acc, ctx, attribute), |
@@ -40,7 +41,7 @@ fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attr | |||
40 | _ => {} | 41 | _ => {} |
41 | } | 42 | } |
42 | 43 | ||
43 | if attribute.kind() == AttrKind::Inner || !attr_completion.should_be_inner { | 44 | if attribute.kind() == ast::AttrKind::Inner || !attr_completion.should_be_inner { |
44 | acc.add(item); | 45 | acc.add(item); |
45 | } | 46 | } |
46 | } | 47 | } |
diff --git a/crates/ra_ide/src/completion/complete_qualified_path.rs b/crates/ra_ide/src/completion/complete_qualified_path.rs index aa56a5cd8..d9ea92ef8 100644 --- a/crates/ra_ide/src/completion/complete_qualified_path.rs +++ b/crates/ra_ide/src/completion/complete_qualified_path.rs | |||
@@ -2,16 +2,21 @@ | |||
2 | 2 | ||
3 | use hir::{Adt, HasVisibility, PathResolution, ScopeDef}; | 3 | use hir::{Adt, HasVisibility, PathResolution, ScopeDef}; |
4 | use ra_syntax::AstNode; | 4 | use ra_syntax::AstNode; |
5 | use rustc_hash::FxHashSet; | ||
5 | use test_utils::tested_by; | 6 | use test_utils::tested_by; |
6 | 7 | ||
7 | use crate::completion::{CompletionContext, Completions}; | 8 | use crate::completion::{CompletionContext, Completions}; |
8 | use rustc_hash::FxHashSet; | ||
9 | 9 | ||
10 | pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { | 10 | pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { |
11 | let path = match &ctx.path_prefix { | 11 | let path = match &ctx.path_prefix { |
12 | Some(path) => path.clone(), | 12 | Some(path) => path.clone(), |
13 | _ => return, | 13 | None => return, |
14 | }; | 14 | }; |
15 | |||
16 | if ctx.attribute_under_caret.is_some() { | ||
17 | return; | ||
18 | } | ||
19 | |||
15 | let scope = ctx.scope(); | 20 | let scope = ctx.scope(); |
16 | let context_module = scope.module(); | 21 | let context_module = scope.module(); |
17 | 22 | ||
@@ -1325,4 +1330,18 @@ mod tests { | |||
1325 | "### | 1330 | "### |
1326 | ); | 1331 | ); |
1327 | } | 1332 | } |
1333 | |||
1334 | #[test] | ||
1335 | fn dont_complete_attr() { | ||
1336 | assert_debug_snapshot!( | ||
1337 | do_reference_completion( | ||
1338 | r" | ||
1339 | mod foo { pub struct Foo; } | ||
1340 | #[foo::<|>] | ||
1341 | fn f() {} | ||
1342 | " | ||
1343 | ), | ||
1344 | @r###"[]"### | ||
1345 | ) | ||
1346 | } | ||
1328 | } | 1347 | } |
diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs index a6a5568de..bd40af1cb 100644 --- a/crates/ra_ide/src/completion/complete_unqualified_path.rs +++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs | |||
@@ -8,9 +8,12 @@ use hir::{Adt, ModuleDef, Type}; | |||
8 | use ra_syntax::AstNode; | 8 | use ra_syntax::AstNode; |
9 | 9 | ||
10 | pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { | 10 | pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { |
11 | if (!ctx.is_trivial_path && !ctx.is_pat_binding_or_const) | 11 | if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) { |
12 | || ctx.record_lit_syntax.is_some() | 12 | return; |
13 | } | ||
14 | if ctx.record_lit_syntax.is_some() | ||
13 | || ctx.record_pat_syntax.is_some() | 15 | || ctx.record_pat_syntax.is_some() |
16 | || ctx.attribute_under_caret.is_some() | ||
14 | { | 17 | { |
15 | return; | 18 | return; |
16 | } | 19 | } |
@@ -1369,4 +1372,18 @@ mod tests { | |||
1369 | "### | 1372 | "### |
1370 | ) | 1373 | ) |
1371 | } | 1374 | } |
1375 | |||
1376 | #[test] | ||
1377 | fn dont_complete_attr() { | ||
1378 | assert_debug_snapshot!( | ||
1379 | do_reference_completion( | ||
1380 | r" | ||
1381 | struct Foo; | ||
1382 | #[<|>] | ||
1383 | fn f() {} | ||
1384 | " | ||
1385 | ), | ||
1386 | @r###"[]"### | ||
1387 | ) | ||
1388 | } | ||
1372 | } | 1389 | } |
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 { | |||
376 | impl ToNav for hir::TypeParam { | 376 | impl 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 1dfca819d..150895abb 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs | |||
@@ -244,6 +244,39 @@ mod tests { | |||
244 | } | 244 | } |
245 | 245 | ||
246 | #[test] | 246 | #[test] |
247 | fn goto_def_for_use_alias() { | ||
248 | covers!(ra_ide_db::goto_def_for_use_alias); | ||
249 | check_goto( | ||
250 | " | ||
251 | //- /lib.rs | ||
252 | use foo as bar<|>; | ||
253 | |||
254 | |||
255 | //- /foo/lib.rs | ||
256 | #[macro_export] | ||
257 | macro_rules! foo { () => { () } }", | ||
258 | "SOURCE_FILE FileId(2) 0..50", | ||
259 | "#[macro_export]\nmacro_rules! foo { () => { () } }\n", | ||
260 | ); | ||
261 | } | ||
262 | |||
263 | #[test] | ||
264 | fn goto_def_for_use_alias_foo_macro() { | ||
265 | check_goto( | ||
266 | " | ||
267 | //- /lib.rs | ||
268 | use foo::foo as bar<|>; | ||
269 | |||
270 | //- /foo/lib.rs | ||
271 | #[macro_export] | ||
272 | macro_rules! foo { () => { () } } | ||
273 | ", | ||
274 | "foo MACRO_CALL FileId(2) 0..49 29..32", | ||
275 | "#[macro_export]\nmacro_rules! foo { () => { () } }|foo", | ||
276 | ); | ||
277 | } | ||
278 | |||
279 | #[test] | ||
247 | fn goto_def_for_macros_in_use_tree() { | 280 | fn goto_def_for_macros_in_use_tree() { |
248 | check_goto( | 281 | check_goto( |
249 | " | 282 | " |
@@ -754,14 +787,14 @@ mod tests { | |||
754 | #[test] | 787 | #[test] |
755 | fn goto_for_type_param() { | 788 | fn goto_for_type_param() { |
756 | check_goto( | 789 | check_goto( |
757 | " | 790 | r#" |
758 | //- /lib.rs | 791 | //- /lib.rs |
759 | struct Foo<T> { | 792 | struct Foo<T: Clone> { |
760 | t: <|>T, | 793 | t: <|>T, |
761 | } | 794 | } |
762 | ", | 795 | "#, |
763 | "T TYPE_PARAM FileId(1) 11..12", | 796 | "T TYPE_PARAM FileId(1) 11..19 11..12", |
764 | "T", | 797 | "T: Clone|T", |
765 | ); | 798 | ); |
766 | } | 799 | } |
767 | 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 | ||
11 | use crate::{FileId, FunctionSignature}; | 11 | use crate::{FileId, FunctionSignature}; |
12 | use stdx::to_lower_snake_case; | ||
12 | 13 | ||
13 | #[derive(Clone, Debug, PartialEq, Eq)] | 14 | #[derive(Clone, Debug, PartialEq, Eq)] |
14 | pub struct InlayHintsConfig { | 15 | pub 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 | ||
182 | fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ty: &Type) -> bool { | 183 | fn 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 | ||
233 | fn should_show_param_hint( | 234 | fn 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 | ||
257 | fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool { | 259 | fn 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(¶m_name) || argument_string.ends_with(¶m_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 | |||
276 | fn 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 | ||
264 | fn remove_ref(expr: ast::Expr) -> ast::Expr { | 287 | fn 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 | ||
273 | fn is_obvious_param(param_name: &str) -> bool { | 297 | fn 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 | ||
1100 | impl TestVarContainer { | ||
1101 | fn test_var(&self) -> i32 { | ||
1102 | self.test_var | ||
1103 | } | ||
1104 | } | ||
1105 | |||
1076 | struct Test {} | 1106 | struct Test {} |
1077 | 1107 | ||
1078 | impl Test { | 1108 | impl Test { |
@@ -1098,10 +1128,15 @@ struct Param {} | |||
1098 | fn different_order(param: &Param) {} | 1128 | fn different_order(param: &Param) {} |
1099 | fn different_order_mut(param: &mut Param) {} | 1129 | fn different_order_mut(param: &mut Param) {} |
1100 | fn has_underscore(_param: bool) {} | 1130 | fn has_underscore(_param: bool) {} |
1131 | fn enum_matches_param_name(completion_kind: CompletionKind) {} | ||
1101 | 1132 | ||
1102 | fn twiddle(twiddle: bool) {} | 1133 | fn twiddle(twiddle: bool) {} |
1103 | fn doo(_doo: bool) {} | 1134 | fn doo(_doo: bool) {} |
1104 | 1135 | ||
1136 | enum CompletionKind { | ||
1137 | Keyword, | ||
1138 | } | ||
1139 | |||
1105 | fn main() { | 1140 | fn 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(¶m_begin); | 1159 | different_order(¶m_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 | }; |
9 | use ra_text_edit::TextEdit; | 9 | use ra_text_edit::TextEdit; |
10 | use test_utils::tested_by; | ||
10 | 11 | ||
11 | use crate::{ | 12 | use 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 | ||
16 | use super::find_all_refs; | ||
17 | |||
18 | pub(crate) fn rename( | 17 | pub(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( | |||
147 | mod tests { | 148 | mod 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 { |
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index 7cd2384e9..40d0e77b5 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs | |||
@@ -119,6 +119,16 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti | |||
119 | 119 | ||
120 | match_ast! { | 120 | match_ast! { |
121 | match parent { | 121 | match parent { |
122 | ast::Alias(it) => { | ||
123 | tested_by!(goto_def_for_use_alias; force); | ||
124 | let use_tree = it.syntax().parent().and_then(ast::UseTree::cast)?; | ||
125 | let path = use_tree.path()?; | ||
126 | let path_segment = path.segment()?; | ||
127 | let name_ref = path_segment.name_ref()?; | ||
128 | let name_ref_class = classify_name_ref(sema, &name_ref)?; | ||
129 | |||
130 | Some(name_ref_class.definition()) | ||
131 | }, | ||
122 | ast::BindPat(it) => { | 132 | ast::BindPat(it) => { |
123 | let local = sema.to_def(&it)?; | 133 | let local = sema.to_def(&it)?; |
124 | Some(Definition::Local(local)) | 134 | Some(Definition::Local(local)) |
diff --git a/crates/ra_ide_db/src/marks.rs b/crates/ra_ide_db/src/marks.rs index 03b4be21c..386fe605c 100644 --- a/crates/ra_ide_db/src/marks.rs +++ b/crates/ra_ide_db/src/marks.rs | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | test_utils::marks![ | 3 | test_utils::marks![ |
4 | goto_def_for_macros | 4 | goto_def_for_macros |
5 | goto_def_for_use_alias | ||
5 | goto_def_for_methods | 6 | goto_def_for_methods |
6 | goto_def_for_fields | 7 | goto_def_for_fields |
7 | goto_def_for_record_fields | 8 | goto_def_for_record_fields |
diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs index 44222d8bd..110c9a442 100644 --- a/crates/rust-analyzer/src/caps.rs +++ b/crates/rust-analyzer/src/caps.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | //! Advertizes the capabilities of the LSP Server. | 1 | //! Advertizes the capabilities of the LSP Server. |
2 | use std::env; | ||
2 | 3 | ||
3 | use crate::semantic_tokens; | 4 | use crate::semantic_tokens; |
4 | 5 | ||
@@ -16,7 +17,11 @@ pub fn server_capabilities() -> ServerCapabilities { | |||
16 | ServerCapabilities { | 17 | ServerCapabilities { |
17 | text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions { | 18 | text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions { |
18 | open_close: Some(true), | 19 | open_close: Some(true), |
19 | change: Some(TextDocumentSyncKind::Incremental), | 20 | change: Some(if env::var("RA_NO_INCREMENTAL_SYNC").is_ok() { |
21 | TextDocumentSyncKind::Full | ||
22 | } else { | ||
23 | TextDocumentSyncKind::Incremental | ||
24 | }), | ||
20 | will_save: None, | 25 | will_save: None, |
21 | will_save_wait_until: None, | 26 | will_save_wait_until: None, |
22 | save: Some(SaveOptions::default()), | 27 | save: Some(SaveOptions::default()), |
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 401fae755..b163ea848 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -676,13 +676,13 @@ fn apply_document_changes( | |||
676 | // remember the last valid line in the index and only rebuild it if needed. | 676 | // remember the last valid line in the index and only rebuild it if needed. |
677 | enum IndexValid { | 677 | enum IndexValid { |
678 | All, | 678 | All, |
679 | UpToLine(u64), | 679 | UpToLineExclusive(u64), |
680 | } | 680 | } |
681 | 681 | ||
682 | impl IndexValid { | 682 | impl IndexValid { |
683 | fn covers(&self, line: u64) -> bool { | 683 | fn covers(&self, line: u64) -> bool { |
684 | match *self { | 684 | match *self { |
685 | IndexValid::UpToLine(to) => to >= line, | 685 | IndexValid::UpToLineExclusive(to) => to > line, |
686 | _ => true, | 686 | _ => true, |
687 | } | 687 | } |
688 | } | 688 | } |
@@ -692,10 +692,10 @@ fn apply_document_changes( | |||
692 | for change in content_changes { | 692 | for change in content_changes { |
693 | match change.range { | 693 | match change.range { |
694 | Some(range) => { | 694 | Some(range) => { |
695 | if !index_valid.covers(range.start.line) { | 695 | if !index_valid.covers(range.end.line) { |
696 | line_index = Cow::Owned(LineIndex::new(&old_text)); | 696 | line_index = Cow::Owned(LineIndex::new(&old_text)); |
697 | } | 697 | } |
698 | index_valid = IndexValid::UpToLine(range.start.line); | 698 | index_valid = IndexValid::UpToLineExclusive(range.start.line); |
699 | let range = range.conv_with(&line_index); | 699 | let range = range.conv_with(&line_index); |
700 | let mut text = old_text.to_owned(); | 700 | let mut text = old_text.to_owned(); |
701 | match std::panic::catch_unwind(move || { | 701 | match std::panic::catch_unwind(move || { |
@@ -713,7 +713,7 @@ fn apply_document_changes( | |||
713 | } | 713 | } |
714 | None => { | 714 | None => { |
715 | *old_text = change.text; | 715 | *old_text = change.text; |
716 | index_valid = IndexValid::UpToLine(0); | 716 | index_valid = IndexValid::UpToLineExclusive(0); |
717 | } | 717 | } |
718 | } | 718 | } |
719 | } | 719 | } |
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 01cdf452c..0f34ce70e 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -102,3 +102,17 @@ pub fn timeit(label: &'static str) -> impl Drop { | |||
102 | 102 | ||
103 | Guard { label, start: Instant::now() } | 103 | Guard { label, start: Instant::now() } |
104 | } | 104 | } |
105 | |||
106 | pub fn to_lower_snake_case(s: &str) -> String { | ||
107 | let mut buf = String::with_capacity(s.len()); | ||
108 | let mut prev = false; | ||
109 | for c in s.chars() { | ||
110 | if c.is_ascii_uppercase() && prev { | ||
111 | buf.push('_') | ||
112 | } | ||
113 | prev = true; | ||
114 | |||
115 | buf.push(c.to_ascii_lowercase()); | ||
116 | } | ||
117 | buf | ||
118 | } | ||