diff options
author | Lukas Wirth <[email protected]> | 2021-02-07 17:38:12 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-02-12 17:58:28 +0000 |
commit | d644728d82df10b034d0ea736590c781afa2ba15 (patch) | |
tree | 4591c44955de42387165e7a59789d7e636284bc6 /crates | |
parent | 43ccbf4360271d5da2fd3688a04b34c66357e0b6 (diff) |
Refactor reference searching to work with the ast
Diffstat (limited to 'crates')
-rw-r--r-- | crates/assists/src/handlers/inline_local_variable.rs | 110 | ||||
-rw-r--r-- | crates/ide/src/call_hierarchy.rs | 8 | ||||
-rw-r--r-- | crates/ide/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/references.rs | 357 | ||||
-rw-r--r-- | crates/ide/src/references/rename.rs | 197 | ||||
-rw-r--r-- | crates/ide_db/src/search.rs | 140 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 49 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 30 |
8 files changed, 489 insertions, 406 deletions
diff --git a/crates/assists/src/handlers/inline_local_variable.rs b/crates/assists/src/handlers/inline_local_variable.rs index 0e63a60e8..e4f984713 100644 --- a/crates/assists/src/handlers/inline_local_variable.rs +++ b/crates/assists/src/handlers/inline_local_variable.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | use ide_db::{ | 1 | use std::collections::HashMap; |
2 | defs::Definition, | 2 | |
3 | search::{FileReference, ReferenceKind}, | 3 | use ide_db::{defs::Definition, search::FileReference}; |
4 | }; | ||
5 | use syntax::{ | 4 | use syntax::{ |
6 | ast::{self, AstNode, AstToken}, | 5 | ast::{self, AstNode, AstToken}, |
7 | TextRange, | 6 | TextRange, |
@@ -68,44 +67,51 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O | |||
68 | 67 | ||
69 | let wrap_in_parens = usages | 68 | let wrap_in_parens = usages |
70 | .references | 69 | .references |
71 | .values() | 70 | .iter() |
72 | .flatten() | 71 | .map(|(&file_id, refs)| { |
73 | .map(|&FileReference { range, .. }| { | 72 | refs.iter() |
74 | let usage_node = | 73 | .map(|&FileReference { range, .. }| { |
75 | ctx.covering_node_for_range(range).ancestors().find_map(ast::PathExpr::cast)?; | 74 | let usage_node = ctx |
76 | let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast); | 75 | .covering_node_for_range(range) |
77 | let usage_parent = match usage_parent_option { | 76 | .ancestors() |
78 | Some(u) => u, | 77 | .find_map(ast::PathExpr::cast)?; |
79 | None => return Ok(false), | 78 | let usage_parent_option = |
80 | }; | 79 | usage_node.syntax().parent().and_then(ast::Expr::cast); |
81 | 80 | let usage_parent = match usage_parent_option { | |
82 | Ok(!matches!( | 81 | Some(u) => u, |
83 | (&initializer_expr, usage_parent), | 82 | None => return Ok(false), |
84 | (ast::Expr::CallExpr(_), _) | 83 | }; |
85 | | (ast::Expr::IndexExpr(_), _) | 84 | |
86 | | (ast::Expr::MethodCallExpr(_), _) | 85 | Ok(!matches!( |
87 | | (ast::Expr::FieldExpr(_), _) | 86 | (&initializer_expr, usage_parent), |
88 | | (ast::Expr::TryExpr(_), _) | 87 | (ast::Expr::CallExpr(_), _) |
89 | | (ast::Expr::RefExpr(_), _) | 88 | | (ast::Expr::IndexExpr(_), _) |
90 | | (ast::Expr::Literal(_), _) | 89 | | (ast::Expr::MethodCallExpr(_), _) |
91 | | (ast::Expr::TupleExpr(_), _) | 90 | | (ast::Expr::FieldExpr(_), _) |
92 | | (ast::Expr::ArrayExpr(_), _) | 91 | | (ast::Expr::TryExpr(_), _) |
93 | | (ast::Expr::ParenExpr(_), _) | 92 | | (ast::Expr::RefExpr(_), _) |
94 | | (ast::Expr::PathExpr(_), _) | 93 | | (ast::Expr::Literal(_), _) |
95 | | (ast::Expr::BlockExpr(_), _) | 94 | | (ast::Expr::TupleExpr(_), _) |
96 | | (ast::Expr::EffectExpr(_), _) | 95 | | (ast::Expr::ArrayExpr(_), _) |
97 | | (_, ast::Expr::CallExpr(_)) | 96 | | (ast::Expr::ParenExpr(_), _) |
98 | | (_, ast::Expr::TupleExpr(_)) | 97 | | (ast::Expr::PathExpr(_), _) |
99 | | (_, ast::Expr::ArrayExpr(_)) | 98 | | (ast::Expr::BlockExpr(_), _) |
100 | | (_, ast::Expr::ParenExpr(_)) | 99 | | (ast::Expr::EffectExpr(_), _) |
101 | | (_, ast::Expr::ForExpr(_)) | 100 | | (_, ast::Expr::CallExpr(_)) |
102 | | (_, ast::Expr::WhileExpr(_)) | 101 | | (_, ast::Expr::TupleExpr(_)) |
103 | | (_, ast::Expr::BreakExpr(_)) | 102 | | (_, ast::Expr::ArrayExpr(_)) |
104 | | (_, ast::Expr::ReturnExpr(_)) | 103 | | (_, ast::Expr::ParenExpr(_)) |
105 | | (_, ast::Expr::MatchExpr(_)) | 104 | | (_, ast::Expr::ForExpr(_)) |
106 | )) | 105 | | (_, ast::Expr::WhileExpr(_)) |
106 | | (_, ast::Expr::BreakExpr(_)) | ||
107 | | (_, ast::Expr::ReturnExpr(_)) | ||
108 | | (_, ast::Expr::MatchExpr(_)) | ||
109 | )) | ||
110 | }) | ||
111 | .collect::<Result<_, _>>() | ||
112 | .map(|b| (file_id, b)) | ||
107 | }) | 113 | }) |
108 | .collect::<Result<Vec<_>, _>>()?; | 114 | .collect::<Result<HashMap<_, Vec<_>>, _>>()?; |
109 | 115 | ||
110 | let init_str = initializer_expr.syntax().text().to_string(); | 116 | let init_str = initializer_expr.syntax().text().to_string(); |
111 | let init_in_paren = format!("({})", &init_str); | 117 | let init_in_paren = format!("({})", &init_str); |
@@ -117,16 +123,20 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O | |||
117 | target, | 123 | target, |
118 | move |builder| { | 124 | move |builder| { |
119 | builder.delete(delete_range); | 125 | builder.delete(delete_range); |
120 | for (reference, should_wrap) in usages.references.values().flatten().zip(wrap_in_parens) | 126 | for (file_id, references) in usages.references { |
121 | { | 127 | let root = ctx.sema.parse(file_id); |
122 | let replacement = | 128 | for (&should_wrap, reference) in wrap_in_parens[&file_id].iter().zip(references) { |
123 | if should_wrap { init_in_paren.clone() } else { init_str.clone() }; | 129 | let replacement = |
124 | match reference.kind { | 130 | if should_wrap { init_in_paren.clone() } else { init_str.clone() }; |
125 | ReferenceKind::FieldShorthandForLocal => { | 131 | match &reference.as_name_ref(root.syntax()) { |
126 | mark::hit!(inline_field_shorthand); | 132 | Some(name_ref) |
127 | builder.insert(reference.range.end(), format!(": {}", replacement)) | 133 | if ast::RecordExprField::for_field_name(name_ref).is_some() => |
134 | { | ||
135 | mark::hit!(inline_field_shorthand); | ||
136 | builder.insert(reference.range.end(), format!(": {}", replacement)); | ||
137 | } | ||
138 | _ => builder.replace(reference.range, replacement), | ||
128 | } | 139 | } |
129 | _ => builder.replace(reference.range, replacement), | ||
130 | } | 140 | } |
131 | } | 141 | } |
132 | }, | 142 | }, |
diff --git a/crates/ide/src/call_hierarchy.rs b/crates/ide/src/call_hierarchy.rs index b10a0a78b..b848945d7 100644 --- a/crates/ide/src/call_hierarchy.rs +++ b/crates/ide/src/call_hierarchy.rs | |||
@@ -47,11 +47,11 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio | |||
47 | 47 | ||
48 | let mut calls = CallLocations::default(); | 48 | let mut calls = CallLocations::default(); |
49 | 49 | ||
50 | for (&file_id, references) in refs.references().iter() { | 50 | for (file_id, references) in refs.references { |
51 | let file = sema.parse(file_id); | 51 | let file = sema.parse(file_id); |
52 | let file = file.syntax(); | 52 | let file = file.syntax(); |
53 | for reference in references { | 53 | for (r_range, _) in references { |
54 | let token = file.token_at_offset(reference.range.start()).next()?; | 54 | let token = file.token_at_offset(r_range.start()).next()?; |
55 | let token = sema.descend_into_macros(token); | 55 | let token = sema.descend_into_macros(token); |
56 | let syntax = token.parent(); | 56 | let syntax = token.parent(); |
57 | 57 | ||
@@ -61,7 +61,7 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio | |||
61 | let def = sema.to_def(&fn_)?; | 61 | let def = sema.to_def(&fn_)?; |
62 | def.try_to_nav(sema.db) | 62 | def.try_to_nav(sema.db) |
63 | }) { | 63 | }) { |
64 | let relative_range = reference.range; | 64 | let relative_range = r_range; |
65 | calls.add(&nav, relative_range); | 65 | calls.add(&nav, relative_range); |
66 | } | 66 | } |
67 | } | 67 | } |
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 989e94a31..a245d9341 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs | |||
@@ -73,7 +73,7 @@ pub use crate::{ | |||
73 | inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, | 73 | inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, |
74 | markup::Markup, | 74 | markup::Markup, |
75 | prime_caches::PrimeCachesProgress, | 75 | prime_caches::PrimeCachesProgress, |
76 | references::{rename::RenameError, Declaration, ReferenceSearchResult}, | 76 | references::{rename::RenameError, ReferenceSearchResult}, |
77 | runnables::{Runnable, RunnableKind, TestId}, | 77 | runnables::{Runnable, RunnableKind, TestId}, |
78 | syntax_highlighting::{ | 78 | syntax_highlighting::{ |
79 | tags::{Highlight, HlMod, HlMods, HlPunct, HlTag}, | 79 | tags::{Highlight, HlMod, HlMods, HlPunct, HlTag}, |
@@ -94,7 +94,7 @@ pub use ide_db::{ | |||
94 | call_info::CallInfo, | 94 | call_info::CallInfo, |
95 | label::Label, | 95 | label::Label, |
96 | line_index::{LineCol, LineIndex}, | 96 | line_index::{LineCol, LineIndex}, |
97 | search::{FileReference, ReferenceAccess, ReferenceKind, SearchScope}, | 97 | search::{FileReference, ReferenceAccess, SearchScope}, |
98 | source_change::{FileSystemEdit, SourceChange}, | 98 | source_change::{FileSystemEdit, SourceChange}, |
99 | symbol_index::Query, | 99 | symbol_index::Query, |
100 | RootDatabase, | 100 | RootDatabase, |
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index e8737dcfa..f96fac9c1 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs | |||
@@ -11,13 +11,14 @@ | |||
11 | 11 | ||
12 | pub(crate) mod rename; | 12 | pub(crate) mod rename; |
13 | 13 | ||
14 | use either::Either; | ||
15 | use hir::Semantics; | 14 | use hir::Semantics; |
16 | use ide_db::{ | 15 | use ide_db::{ |
16 | base_db::FileId, | ||
17 | defs::{Definition, NameClass, NameRefClass}, | 17 | defs::{Definition, NameClass, NameRefClass}, |
18 | search::{FileReference, ReferenceAccess, ReferenceKind, SearchScope, UsageSearchResult}, | 18 | search::{ReferenceAccess, SearchScope}, |
19 | RootDatabase, | 19 | RootDatabase, |
20 | }; | 20 | }; |
21 | use rustc_hash::FxHashMap; | ||
21 | use syntax::{ | 22 | use syntax::{ |
22 | algo::find_node_at_offset, | 23 | algo::find_node_at_offset, |
23 | ast::{self, NameOwner}, | 24 | ast::{self, NameOwner}, |
@@ -28,32 +29,14 @@ use crate::{display::TryToNav, FilePosition, NavigationTarget}; | |||
28 | 29 | ||
29 | #[derive(Debug, Clone)] | 30 | #[derive(Debug, Clone)] |
30 | pub struct ReferenceSearchResult { | 31 | pub struct ReferenceSearchResult { |
31 | declaration: Declaration, | 32 | pub declaration: Declaration, |
32 | references: UsageSearchResult, | 33 | pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>, |
33 | } | 34 | } |
34 | 35 | ||
35 | #[derive(Debug, Clone)] | 36 | #[derive(Debug, Clone)] |
36 | pub struct Declaration { | 37 | pub struct Declaration { |
37 | nav: NavigationTarget, | 38 | pub nav: NavigationTarget, |
38 | kind: ReferenceKind, | 39 | pub access: Option<ReferenceAccess>, |
39 | access: Option<ReferenceAccess>, | ||
40 | } | ||
41 | |||
42 | impl ReferenceSearchResult { | ||
43 | pub fn references(self) -> UsageSearchResult { | ||
44 | self.references | ||
45 | } | ||
46 | |||
47 | pub fn references_with_declaration(mut self) -> UsageSearchResult { | ||
48 | let decl_ref = FileReference { | ||
49 | range: self.declaration.nav.focus_or_full_range(), | ||
50 | kind: self.declaration.kind, | ||
51 | access: self.declaration.access, | ||
52 | }; | ||
53 | let file_id = self.declaration.nav.file_id; | ||
54 | self.references.references.entry(file_id).or_default().push(decl_ref); | ||
55 | self.references | ||
56 | } | ||
57 | } | 40 | } |
58 | 41 | ||
59 | pub(crate) fn find_all_refs( | 42 | pub(crate) fn find_all_refs( |
@@ -64,83 +47,76 @@ pub(crate) fn find_all_refs( | |||
64 | let _p = profile::span("find_all_refs"); | 47 | let _p = profile::span("find_all_refs"); |
65 | let syntax = sema.parse(position.file_id).syntax().clone(); | 48 | let syntax = sema.parse(position.file_id).syntax().clone(); |
66 | 49 | ||
67 | let (opt_name, search_kind) = if let Some(name) = | 50 | let (opt_name, ctor_filter): (_, Option<fn(&_) -> bool>) = if let Some(name) = |
68 | get_struct_def_name_for_struct_literal_search(&sema, &syntax, position) | 51 | get_struct_def_name_for_struct_literal_search(&sema, &syntax, position) |
69 | { | 52 | { |
70 | (Some(name), ReferenceKind::StructLiteral) | 53 | ( |
54 | Some(name), | ||
55 | Some(|name_ref| is_record_lit_name_ref(name_ref) || is_call_expr_name_ref(name_ref)), | ||
56 | ) | ||
71 | } else if let Some(name) = get_enum_def_name_for_struct_literal_search(&sema, &syntax, position) | 57 | } else if let Some(name) = get_enum_def_name_for_struct_literal_search(&sema, &syntax, position) |
72 | { | 58 | { |
73 | (Some(name), ReferenceKind::EnumLiteral) | 59 | (Some(name), Some(is_enum_lit_name_ref)) |
74 | } else { | 60 | } else { |
75 | ( | 61 | (sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, position.offset), None) |
76 | sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, position.offset), | ||
77 | ReferenceKind::Other, | ||
78 | ) | ||
79 | }; | 62 | }; |
80 | 63 | ||
81 | let def = find_name(&sema, &syntax, position, opt_name)?; | 64 | let def = find_def(&sema, &syntax, position, opt_name)?; |
82 | 65 | ||
83 | let mut usages = def.usages(sema).set_scope(search_scope).all(); | 66 | let mut usages = def.usages(sema).set_scope(search_scope).all(); |
84 | usages | 67 | if let Some(ctor_filter) = ctor_filter { |
85 | .references | 68 | // filter for constructor-literals |
86 | .values_mut() | 69 | usages.references.iter_mut().for_each(|(&file_id, it)| { |
87 | .for_each(|it| it.retain(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)); | 70 | let root = sema.parse(file_id); |
88 | usages.references.retain(|_, it| !it.is_empty()); | 71 | let root = root.syntax(); |
89 | 72 | it.retain(|reference| { | |
73 | reference.as_name_ref(root).map_or(false, |name_ref| ctor_filter(&name_ref)) | ||
74 | }) | ||
75 | }); | ||
76 | usages.references.retain(|_, it| !it.is_empty()); | ||
77 | } | ||
90 | let nav = def.try_to_nav(sema.db)?; | 78 | let nav = def.try_to_nav(sema.db)?; |
91 | let decl_range = nav.focus_or_full_range(); | 79 | let decl_range = nav.focus_or_full_range(); |
92 | 80 | ||
93 | let mut kind = ReferenceKind::Other; | 81 | let declaration = Declaration { nav, access: decl_access(&def, &syntax, decl_range) }; |
94 | if let Definition::Local(local) = def { | 82 | let references = usages |
95 | match local.source(sema.db).value { | 83 | .into_iter() |
96 | Either::Left(pat) => { | 84 | .map(|(file_id, refs)| { |
97 | if matches!( | 85 | (file_id, refs.into_iter().map(|file_ref| (file_ref.range, file_ref.access)).collect()) |
98 | pat.syntax().parent().and_then(ast::RecordPatField::cast), | 86 | }) |
99 | Some(pat_field) if pat_field.name_ref().is_none() | 87 | .collect(); |
100 | ) { | ||
101 | kind = ReferenceKind::FieldShorthandForLocal; | ||
102 | } | ||
103 | } | ||
104 | Either::Right(_) => kind = ReferenceKind::SelfParam, | ||
105 | } | ||
106 | } else if matches!( | ||
107 | def, | ||
108 | Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_) | ||
109 | ) { | ||
110 | kind = ReferenceKind::Lifetime; | ||
111 | }; | ||
112 | |||
113 | let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) }; | ||
114 | 88 | ||
115 | Some(ReferenceSearchResult { declaration, references: usages }) | 89 | Some(ReferenceSearchResult { declaration, references }) |
116 | } | 90 | } |
117 | 91 | ||
118 | fn find_name( | 92 | fn find_def( |
119 | sema: &Semantics<RootDatabase>, | 93 | sema: &Semantics<RootDatabase>, |
120 | syntax: &SyntaxNode, | 94 | syntax: &SyntaxNode, |
121 | position: FilePosition, | 95 | position: FilePosition, |
122 | opt_name: Option<ast::Name>, | 96 | opt_name: Option<ast::Name>, |
123 | ) -> Option<Definition> { | 97 | ) -> Option<Definition> { |
124 | let def = if let Some(name) = opt_name { | 98 | if let Some(name) = opt_name { |
125 | NameClass::classify(sema, &name)?.referenced_or_defined(sema.db) | 99 | let class = NameClass::classify(sema, &name)?; |
100 | Some(class.referenced_or_defined(sema.db)) | ||
126 | } else if let Some(lifetime) = | 101 | } else if let Some(lifetime) = |
127 | sema.find_node_at_offset_with_descend::<ast::Lifetime>(&syntax, position.offset) | 102 | sema.find_node_at_offset_with_descend::<ast::Lifetime>(&syntax, position.offset) |
128 | { | 103 | { |
129 | if let Some(def) = | 104 | let def = if let Some(def) = |
130 | NameRefClass::classify_lifetime(sema, &lifetime).map(|class| class.referenced(sema.db)) | 105 | NameRefClass::classify_lifetime(sema, &lifetime).map(|class| class.referenced(sema.db)) |
131 | { | 106 | { |
132 | def | 107 | def |
133 | } else { | 108 | } else { |
134 | NameClass::classify_lifetime(sema, &lifetime)?.referenced_or_defined(sema.db) | 109 | NameClass::classify_lifetime(sema, &lifetime)?.referenced_or_defined(sema.db) |
135 | } | 110 | }; |
111 | Some(def) | ||
136 | } else if let Some(name_ref) = | 112 | } else if let Some(name_ref) = |
137 | sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset) | 113 | sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset) |
138 | { | 114 | { |
139 | NameRefClass::classify(sema, &name_ref)?.referenced(sema.db) | 115 | let class = NameRefClass::classify(sema, &name_ref)?; |
116 | Some(class.referenced(sema.db)) | ||
140 | } else { | 117 | } else { |
141 | return None; | 118 | None |
142 | }; | 119 | } |
143 | Some(def) | ||
144 | } | 120 | } |
145 | 121 | ||
146 | fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> { | 122 | fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> { |
@@ -216,6 +192,43 @@ fn get_enum_def_name_for_struct_literal_search( | |||
216 | None | 192 | None |
217 | } | 193 | } |
218 | 194 | ||
195 | fn is_call_expr_name_ref(name_ref: &ast::NameRef) -> bool { | ||
196 | name_ref | ||
197 | .syntax() | ||
198 | .ancestors() | ||
199 | .find_map(ast::CallExpr::cast) | ||
200 | .and_then(|c| match c.expr()? { | ||
201 | ast::Expr::PathExpr(p) => { | ||
202 | Some(p.path()?.segment()?.name_ref().as_ref() == Some(name_ref)) | ||
203 | } | ||
204 | _ => None, | ||
205 | }) | ||
206 | .unwrap_or(false) | ||
207 | } | ||
208 | |||
209 | fn is_record_lit_name_ref(name_ref: &ast::NameRef) -> bool { | ||
210 | name_ref | ||
211 | .syntax() | ||
212 | .ancestors() | ||
213 | .find_map(ast::RecordExpr::cast) | ||
214 | .and_then(|l| l.path()) | ||
215 | .and_then(|p| p.segment()) | ||
216 | .map(|p| p.name_ref().as_ref() == Some(name_ref)) | ||
217 | .unwrap_or(false) | ||
218 | } | ||
219 | |||
220 | fn is_enum_lit_name_ref(name_ref: &ast::NameRef) -> bool { | ||
221 | name_ref | ||
222 | .syntax() | ||
223 | .ancestors() | ||
224 | .find_map(ast::PathExpr::cast) | ||
225 | .and_then(|p| p.path()) | ||
226 | .and_then(|p| p.qualifier()) | ||
227 | .and_then(|p| p.segment()) | ||
228 | .map(|p| p.name_ref().as_ref() == Some(name_ref)) | ||
229 | .unwrap_or(false) | ||
230 | } | ||
231 | |||
219 | #[cfg(test)] | 232 | #[cfg(test)] |
220 | mod tests { | 233 | mod tests { |
221 | use expect_test::{expect, Expect}; | 234 | use expect_test::{expect, Expect}; |
@@ -240,9 +253,9 @@ fn main() { | |||
240 | } | 253 | } |
241 | "#, | 254 | "#, |
242 | expect![[r#" | 255 | expect![[r#" |
243 | Foo Struct FileId(0) 0..26 7..10 Other | 256 | Foo Struct FileId(0) 0..26 7..10 |
244 | 257 | ||
245 | FileId(0) 101..104 StructLiteral | 258 | FileId(0) 101..104 |
246 | "#]], | 259 | "#]], |
247 | ); | 260 | ); |
248 | } | 261 | } |
@@ -258,10 +271,10 @@ struct Foo$0 {} | |||
258 | } | 271 | } |
259 | "#, | 272 | "#, |
260 | expect![[r#" | 273 | expect![[r#" |
261 | Foo Struct FileId(0) 0..13 7..10 Other | 274 | Foo Struct FileId(0) 0..13 7..10 |
262 | 275 | ||
263 | FileId(0) 41..44 Other | 276 | FileId(0) 41..44 |
264 | FileId(0) 54..57 StructLiteral | 277 | FileId(0) 54..57 |
265 | "#]], | 278 | "#]], |
266 | ); | 279 | ); |
267 | } | 280 | } |
@@ -277,9 +290,9 @@ struct Foo<T> $0{} | |||
277 | } | 290 | } |
278 | "#, | 291 | "#, |
279 | expect![[r#" | 292 | expect![[r#" |
280 | Foo Struct FileId(0) 0..16 7..10 Other | 293 | Foo Struct FileId(0) 0..16 7..10 |
281 | 294 | ||
282 | FileId(0) 64..67 StructLiteral | 295 | FileId(0) 64..67 |
283 | "#]], | 296 | "#]], |
284 | ); | 297 | ); |
285 | } | 298 | } |
@@ -296,9 +309,9 @@ fn main() { | |||
296 | } | 309 | } |
297 | "#, | 310 | "#, |
298 | expect![[r#" | 311 | expect![[r#" |
299 | Foo Struct FileId(0) 0..16 7..10 Other | 312 | Foo Struct FileId(0) 0..16 7..10 |
300 | 313 | ||
301 | FileId(0) 54..57 StructLiteral | 314 | FileId(0) 54..57 |
302 | "#]], | 315 | "#]], |
303 | ); | 316 | ); |
304 | } | 317 | } |
@@ -317,9 +330,9 @@ fn main() { | |||
317 | } | 330 | } |
318 | "#, | 331 | "#, |
319 | expect![[r#" | 332 | expect![[r#" |
320 | Foo Enum FileId(0) 0..26 5..8 Other | 333 | Foo Enum FileId(0) 0..26 5..8 |
321 | 334 | ||
322 | FileId(0) 63..66 EnumLiteral | 335 | FileId(0) 63..66 |
323 | "#]], | 336 | "#]], |
324 | ); | 337 | ); |
325 | } | 338 | } |
@@ -338,10 +351,10 @@ fn main() { | |||
338 | } | 351 | } |
339 | "#, | 352 | "#, |
340 | expect![[r#" | 353 | expect![[r#" |
341 | Foo Enum FileId(0) 0..26 5..8 Other | 354 | Foo Enum FileId(0) 0..26 5..8 |
342 | 355 | ||
343 | FileId(0) 50..53 Other | 356 | FileId(0) 50..53 |
344 | FileId(0) 63..66 EnumLiteral | 357 | FileId(0) 63..66 |
345 | "#]], | 358 | "#]], |
346 | ); | 359 | ); |
347 | } | 360 | } |
@@ -360,9 +373,9 @@ fn main() { | |||
360 | } | 373 | } |
361 | "#, | 374 | "#, |
362 | expect![[r#" | 375 | expect![[r#" |
363 | Foo Enum FileId(0) 0..32 5..8 Other | 376 | Foo Enum FileId(0) 0..32 5..8 |
364 | 377 | ||
365 | FileId(0) 73..76 EnumLiteral | 378 | FileId(0) 73..76 |
366 | "#]], | 379 | "#]], |
367 | ); | 380 | ); |
368 | } | 381 | } |
@@ -381,9 +394,9 @@ fn main() { | |||
381 | } | 394 | } |
382 | "#, | 395 | "#, |
383 | expect![[r#" | 396 | expect![[r#" |
384 | Foo Enum FileId(0) 0..33 5..8 Other | 397 | Foo Enum FileId(0) 0..33 5..8 |
385 | 398 | ||
386 | FileId(0) 70..73 EnumLiteral | 399 | FileId(0) 70..73 |
387 | "#]], | 400 | "#]], |
388 | ); | 401 | ); |
389 | } | 402 | } |
@@ -404,12 +417,12 @@ fn main() { | |||
404 | i = 5; | 417 | i = 5; |
405 | }"#, | 418 | }"#, |
406 | expect![[r#" | 419 | expect![[r#" |
407 | i Local FileId(0) 20..25 24..25 Other Write | 420 | i Local FileId(0) 20..25 24..25 Write |
408 | 421 | ||
409 | FileId(0) 50..51 Other Write | 422 | FileId(0) 50..51 Write |
410 | FileId(0) 54..55 Other Read | 423 | FileId(0) 54..55 Read |
411 | FileId(0) 76..77 Other Write | 424 | FileId(0) 76..77 Write |
412 | FileId(0) 94..95 Other Write | 425 | FileId(0) 94..95 Write |
413 | "#]], | 426 | "#]], |
414 | ); | 427 | ); |
415 | } | 428 | } |
@@ -428,10 +441,10 @@ fn bar() { | |||
428 | } | 441 | } |
429 | "#, | 442 | "#, |
430 | expect![[r#" | 443 | expect![[r#" |
431 | spam Local FileId(0) 19..23 19..23 Other | 444 | spam Local FileId(0) 19..23 19..23 |
432 | 445 | ||
433 | FileId(0) 34..38 Other Read | 446 | FileId(0) 34..38 Read |
434 | FileId(0) 41..45 Other Read | 447 | FileId(0) 41..45 Read |
435 | "#]], | 448 | "#]], |
436 | ); | 449 | ); |
437 | } | 450 | } |
@@ -443,9 +456,9 @@ fn bar() { | |||
443 | fn foo(i : u32) -> u32 { i$0 } | 456 | fn foo(i : u32) -> u32 { i$0 } |
444 | "#, | 457 | "#, |
445 | expect![[r#" | 458 | expect![[r#" |
446 | i ValueParam FileId(0) 7..8 7..8 Other | 459 | i ValueParam FileId(0) 7..8 7..8 |
447 | 460 | ||
448 | FileId(0) 25..26 Other Read | 461 | FileId(0) 25..26 Read |
449 | "#]], | 462 | "#]], |
450 | ); | 463 | ); |
451 | } | 464 | } |
@@ -457,9 +470,9 @@ fn foo(i : u32) -> u32 { i$0 } | |||
457 | fn foo(i$0 : u32) -> u32 { i } | 470 | fn foo(i$0 : u32) -> u32 { i } |
458 | "#, | 471 | "#, |
459 | expect![[r#" | 472 | expect![[r#" |
460 | i ValueParam FileId(0) 7..8 7..8 Other | 473 | i ValueParam FileId(0) 7..8 7..8 |
461 | 474 | ||
462 | FileId(0) 25..26 Other Read | 475 | FileId(0) 25..26 Read |
463 | "#]], | 476 | "#]], |
464 | ); | 477 | ); |
465 | } | 478 | } |
@@ -478,9 +491,9 @@ fn main(s: Foo) { | |||
478 | } | 491 | } |
479 | "#, | 492 | "#, |
480 | expect![[r#" | 493 | expect![[r#" |
481 | spam Field FileId(0) 17..30 21..25 Other | 494 | spam Field FileId(0) 17..30 21..25 |
482 | 495 | ||
483 | FileId(0) 67..71 Other Read | 496 | FileId(0) 67..71 Read |
484 | "#]], | 497 | "#]], |
485 | ); | 498 | ); |
486 | } | 499 | } |
@@ -495,7 +508,7 @@ impl Foo { | |||
495 | } | 508 | } |
496 | "#, | 509 | "#, |
497 | expect![[r#" | 510 | expect![[r#" |
498 | f Function FileId(0) 27..43 30..31 Other | 511 | f Function FileId(0) 27..43 30..31 |
499 | 512 | ||
500 | "#]], | 513 | "#]], |
501 | ); | 514 | ); |
@@ -512,7 +525,7 @@ enum Foo { | |||
512 | } | 525 | } |
513 | "#, | 526 | "#, |
514 | expect![[r#" | 527 | expect![[r#" |
515 | B Variant FileId(0) 22..23 22..23 Other | 528 | B Variant FileId(0) 22..23 22..23 |
516 | 529 | ||
517 | "#]], | 530 | "#]], |
518 | ); | 531 | ); |
@@ -529,7 +542,7 @@ enum Foo { | |||
529 | } | 542 | } |
530 | "#, | 543 | "#, |
531 | expect![[r#" | 544 | expect![[r#" |
532 | field Field FileId(0) 26..35 26..31 Other | 545 | field Field FileId(0) 26..35 26..31 |
533 | 546 | ||
534 | "#]], | 547 | "#]], |
535 | ); | 548 | ); |
@@ -570,10 +583,10 @@ fn f() { | |||
570 | } | 583 | } |
571 | "#, | 584 | "#, |
572 | expect![[r#" | 585 | expect![[r#" |
573 | Foo Struct FileId(1) 17..51 28..31 Other | 586 | Foo Struct FileId(1) 17..51 28..31 |
574 | 587 | ||
575 | FileId(0) 53..56 StructLiteral | 588 | FileId(0) 53..56 |
576 | FileId(2) 79..82 StructLiteral | 589 | FileId(2) 79..82 |
577 | "#]], | 590 | "#]], |
578 | ); | 591 | ); |
579 | } | 592 | } |
@@ -600,9 +613,9 @@ pub struct Foo { | |||
600 | } | 613 | } |
601 | "#, | 614 | "#, |
602 | expect![[r#" | 615 | expect![[r#" |
603 | foo Module FileId(1) 0..35 Other | 616 | foo Module FileId(1) 0..35 |
604 | 617 | ||
605 | FileId(0) 14..17 Other | 618 | FileId(0) 14..17 |
606 | "#]], | 619 | "#]], |
607 | ); | 620 | ); |
608 | } | 621 | } |
@@ -628,10 +641,10 @@ pub(super) struct Foo$0 { | |||
628 | } | 641 | } |
629 | "#, | 642 | "#, |
630 | expect![[r#" | 643 | expect![[r#" |
631 | Foo Struct FileId(2) 0..41 18..21 Other | 644 | Foo Struct FileId(2) 0..41 18..21 |
632 | 645 | ||
633 | FileId(1) 20..23 Other | 646 | FileId(1) 20..23 |
634 | FileId(1) 47..50 StructLiteral | 647 | FileId(1) 47..50 |
635 | "#]], | 648 | "#]], |
636 | ); | 649 | ); |
637 | } | 650 | } |
@@ -656,10 +669,10 @@ pub(super) struct Foo$0 { | |||
656 | code, | 669 | code, |
657 | None, | 670 | None, |
658 | expect![[r#" | 671 | expect![[r#" |
659 | quux Function FileId(0) 19..35 26..30 Other | 672 | quux Function FileId(0) 19..35 26..30 |
660 | 673 | ||
661 | FileId(1) 16..20 StructLiteral | 674 | FileId(1) 16..20 |
662 | FileId(2) 16..20 StructLiteral | 675 | FileId(2) 16..20 |
663 | "#]], | 676 | "#]], |
664 | ); | 677 | ); |
665 | 678 | ||
@@ -667,9 +680,9 @@ pub(super) struct Foo$0 { | |||
667 | code, | 680 | code, |
668 | Some(SearchScope::single_file(FileId(2))), | 681 | Some(SearchScope::single_file(FileId(2))), |
669 | expect![[r#" | 682 | expect![[r#" |
670 | quux Function FileId(0) 19..35 26..30 Other | 683 | quux Function FileId(0) 19..35 26..30 |
671 | 684 | ||
672 | FileId(2) 16..20 StructLiteral | 685 | FileId(2) 16..20 |
673 | "#]], | 686 | "#]], |
674 | ); | 687 | ); |
675 | } | 688 | } |
@@ -687,10 +700,10 @@ fn foo() { | |||
687 | } | 700 | } |
688 | "#, | 701 | "#, |
689 | expect![[r#" | 702 | expect![[r#" |
690 | m1 Macro FileId(0) 0..46 29..31 Other | 703 | m1 Macro FileId(0) 0..46 29..31 |
691 | 704 | ||
692 | FileId(0) 63..65 StructLiteral | 705 | FileId(0) 63..65 |
693 | FileId(0) 73..75 StructLiteral | 706 | FileId(0) 73..75 |
694 | "#]], | 707 | "#]], |
695 | ); | 708 | ); |
696 | } | 709 | } |
@@ -705,10 +718,10 @@ fn foo() { | |||
705 | } | 718 | } |
706 | "#, | 719 | "#, |
707 | expect![[r#" | 720 | expect![[r#" |
708 | i Local FileId(0) 19..24 23..24 Other Write | 721 | i Local FileId(0) 19..24 23..24 Write |
709 | 722 | ||
710 | FileId(0) 34..35 Other Write | 723 | FileId(0) 34..35 Write |
711 | FileId(0) 38..39 Other Read | 724 | FileId(0) 38..39 Read |
712 | "#]], | 725 | "#]], |
713 | ); | 726 | ); |
714 | } | 727 | } |
@@ -727,10 +740,10 @@ fn foo() { | |||
727 | } | 740 | } |
728 | "#, | 741 | "#, |
729 | expect![[r#" | 742 | expect![[r#" |
730 | f Field FileId(0) 15..21 15..16 Other | 743 | f Field FileId(0) 15..21 15..16 |
731 | 744 | ||
732 | FileId(0) 55..56 RecordFieldExprOrPat Read | 745 | FileId(0) 55..56 Read |
733 | FileId(0) 68..69 Other Write | 746 | FileId(0) 68..69 Write |
734 | "#]], | 747 | "#]], |
735 | ); | 748 | ); |
736 | } | 749 | } |
@@ -745,9 +758,9 @@ fn foo() { | |||
745 | } | 758 | } |
746 | "#, | 759 | "#, |
747 | expect![[r#" | 760 | expect![[r#" |
748 | i Local FileId(0) 19..20 19..20 Other | 761 | i Local FileId(0) 19..20 19..20 |
749 | 762 | ||
750 | FileId(0) 26..27 Other Write | 763 | FileId(0) 26..27 Write |
751 | "#]], | 764 | "#]], |
752 | ); | 765 | ); |
753 | } | 766 | } |
@@ -769,9 +782,9 @@ fn main() { | |||
769 | } | 782 | } |
770 | "#, | 783 | "#, |
771 | expect![[r#" | 784 | expect![[r#" |
772 | new Function FileId(0) 54..81 61..64 Other | 785 | new Function FileId(0) 54..81 61..64 |
773 | 786 | ||
774 | FileId(0) 126..129 StructLiteral | 787 | FileId(0) 126..129 |
775 | "#]], | 788 | "#]], |
776 | ); | 789 | ); |
777 | } | 790 | } |
@@ -791,10 +804,10 @@ use crate::f; | |||
791 | fn g() { f(); } | 804 | fn g() { f(); } |
792 | "#, | 805 | "#, |
793 | expect![[r#" | 806 | expect![[r#" |
794 | f Function FileId(0) 22..31 25..26 Other | 807 | f Function FileId(0) 22..31 25..26 |
795 | 808 | ||
796 | FileId(1) 11..12 Other | 809 | FileId(1) 11..12 |
797 | FileId(1) 24..25 StructLiteral | 810 | FileId(1) 24..25 |
798 | "#]], | 811 | "#]], |
799 | ); | 812 | ); |
800 | } | 813 | } |
@@ -814,9 +827,9 @@ fn f(s: S) { | |||
814 | } | 827 | } |
815 | "#, | 828 | "#, |
816 | expect![[r#" | 829 | expect![[r#" |
817 | field Field FileId(0) 15..24 15..20 Other | 830 | field Field FileId(0) 15..24 15..20 |
818 | 831 | ||
819 | FileId(0) 68..73 FieldShorthandForField Read | 832 | FileId(0) 68..73 Read |
820 | "#]], | 833 | "#]], |
821 | ); | 834 | ); |
822 | } | 835 | } |
@@ -838,9 +851,9 @@ fn f(e: En) { | |||
838 | } | 851 | } |
839 | "#, | 852 | "#, |
840 | expect![[r#" | 853 | expect![[r#" |
841 | field Field FileId(0) 32..41 32..37 Other | 854 | field Field FileId(0) 32..41 32..37 |
842 | 855 | ||
843 | FileId(0) 102..107 FieldShorthandForField Read | 856 | FileId(0) 102..107 Read |
844 | "#]], | 857 | "#]], |
845 | ); | 858 | ); |
846 | } | 859 | } |
@@ -862,9 +875,9 @@ fn f() -> m::En { | |||
862 | } | 875 | } |
863 | "#, | 876 | "#, |
864 | expect![[r#" | 877 | expect![[r#" |
865 | field Field FileId(0) 56..65 56..61 Other | 878 | field Field FileId(0) 56..65 56..61 |
866 | 879 | ||
867 | FileId(0) 125..130 RecordFieldExprOrPat Read | 880 | FileId(0) 125..130 Read |
868 | "#]], | 881 | "#]], |
869 | ); | 882 | ); |
870 | } | 883 | } |
@@ -887,10 +900,10 @@ impl Foo { | |||
887 | } | 900 | } |
888 | "#, | 901 | "#, |
889 | expect![[r#" | 902 | expect![[r#" |
890 | self SelfParam FileId(0) 47..51 47..51 SelfParam | 903 | self SelfParam FileId(0) 47..51 47..51 |
891 | 904 | ||
892 | FileId(0) 71..75 Other Read | 905 | FileId(0) 71..75 Read |
893 | FileId(0) 152..156 Other Read | 906 | FileId(0) 152..156 Read |
894 | "#]], | 907 | "#]], |
895 | ); | 908 | ); |
896 | } | 909 | } |
@@ -908,9 +921,9 @@ impl Foo { | |||
908 | } | 921 | } |
909 | "#, | 922 | "#, |
910 | expect![[r#" | 923 | expect![[r#" |
911 | self SelfParam FileId(0) 47..51 47..51 SelfParam | 924 | self SelfParam FileId(0) 47..51 47..51 |
912 | 925 | ||
913 | FileId(0) 63..67 Other Read | 926 | FileId(0) 63..67 Read |
914 | "#]], | 927 | "#]], |
915 | ); | 928 | ); |
916 | } | 929 | } |
@@ -926,7 +939,7 @@ impl Foo { | |||
926 | let mut actual = String::new(); | 939 | let mut actual = String::new(); |
927 | { | 940 | { |
928 | let decl = refs.declaration; | 941 | let decl = refs.declaration; |
929 | format_to!(actual, "{} {:?}", decl.nav.debug_render(), decl.kind); | 942 | format_to!(actual, "{}", decl.nav.debug_render()); |
930 | if let Some(access) = decl.access { | 943 | if let Some(access) = decl.access { |
931 | format_to!(actual, " {:?}", access) | 944 | format_to!(actual, " {:?}", access) |
932 | } | 945 | } |
@@ -934,9 +947,9 @@ impl Foo { | |||
934 | } | 947 | } |
935 | 948 | ||
936 | for (file_id, references) in refs.references { | 949 | for (file_id, references) in refs.references { |
937 | for r in references { | 950 | for (range, access) in references { |
938 | format_to!(actual, "{:?} {:?} {:?}", file_id, r.range, r.kind); | 951 | format_to!(actual, "{:?} {:?}", file_id, range); |
939 | if let Some(access) = r.access { | 952 | if let Some(access) = access { |
940 | format_to!(actual, " {:?}", access); | 953 | format_to!(actual, " {:?}", access); |
941 | } | 954 | } |
942 | actual += "\n"; | 955 | actual += "\n"; |
@@ -957,13 +970,13 @@ fn foo<'a, 'b: 'a>(x: &'a$0 ()) -> &'a () where &'a (): Foo<'a> { | |||
957 | } | 970 | } |
958 | "#, | 971 | "#, |
959 | expect![[r#" | 972 | expect![[r#" |
960 | 'a LifetimeParam FileId(0) 55..57 55..57 Lifetime | 973 | 'a LifetimeParam FileId(0) 55..57 55..57 |
961 | 974 | ||
962 | FileId(0) 63..65 Lifetime | 975 | FileId(0) 63..65 |
963 | FileId(0) 71..73 Lifetime | 976 | FileId(0) 71..73 |
964 | FileId(0) 82..84 Lifetime | 977 | FileId(0) 82..84 |
965 | FileId(0) 95..97 Lifetime | 978 | FileId(0) 95..97 |
966 | FileId(0) 106..108 Lifetime | 979 | FileId(0) 106..108 |
967 | "#]], | 980 | "#]], |
968 | ); | 981 | ); |
969 | } | 982 | } |
@@ -975,10 +988,10 @@ fn foo<'a, 'b: 'a>(x: &'a$0 ()) -> &'a () where &'a (): Foo<'a> { | |||
975 | type Foo<'a, T> where T: 'a$0 = &'a T; | 988 | type Foo<'a, T> where T: 'a$0 = &'a T; |
976 | "#, | 989 | "#, |
977 | expect![[r#" | 990 | expect![[r#" |
978 | 'a LifetimeParam FileId(0) 9..11 9..11 Lifetime | 991 | 'a LifetimeParam FileId(0) 9..11 9..11 |
979 | 992 | ||
980 | FileId(0) 25..27 Lifetime | 993 | FileId(0) 25..27 |
981 | FileId(0) 31..33 Lifetime | 994 | FileId(0) 31..33 |
982 | "#]], | 995 | "#]], |
983 | ); | 996 | ); |
984 | } | 997 | } |
@@ -997,11 +1010,11 @@ impl<'a> Foo<'a> for &'a () { | |||
997 | } | 1010 | } |
998 | "#, | 1011 | "#, |
999 | expect![[r#" | 1012 | expect![[r#" |
1000 | 'a LifetimeParam FileId(0) 47..49 47..49 Lifetime | 1013 | 'a LifetimeParam FileId(0) 47..49 47..49 |
1001 | 1014 | ||
1002 | FileId(0) 55..57 Lifetime | 1015 | FileId(0) 55..57 |
1003 | FileId(0) 64..66 Lifetime | 1016 | FileId(0) 64..66 |
1004 | FileId(0) 89..91 Lifetime | 1017 | FileId(0) 89..91 |
1005 | "#]], | 1018 | "#]], |
1006 | ); | 1019 | ); |
1007 | } | 1020 | } |
@@ -1017,9 +1030,9 @@ fn main() { | |||
1017 | } | 1030 | } |
1018 | "#, | 1031 | "#, |
1019 | expect![[r#" | 1032 | expect![[r#" |
1020 | a Local FileId(0) 59..60 59..60 Other | 1033 | a Local FileId(0) 59..60 59..60 |
1021 | 1034 | ||
1022 | FileId(0) 80..81 Other Read | 1035 | FileId(0) 80..81 Read |
1023 | "#]], | 1036 | "#]], |
1024 | ); | 1037 | ); |
1025 | } | 1038 | } |
@@ -1035,9 +1048,9 @@ fn main() { | |||
1035 | } | 1048 | } |
1036 | "#, | 1049 | "#, |
1037 | expect![[r#" | 1050 | expect![[r#" |
1038 | a Local FileId(0) 59..60 59..60 Other | 1051 | a Local FileId(0) 59..60 59..60 |
1039 | 1052 | ||
1040 | FileId(0) 80..81 Other Read | 1053 | FileId(0) 80..81 Read |
1041 | "#]], | 1054 | "#]], |
1042 | ); | 1055 | ); |
1043 | } | 1056 | } |
@@ -1056,10 +1069,10 @@ fn foo<'a>() -> &'a () { | |||
1056 | } | 1069 | } |
1057 | "#, | 1070 | "#, |
1058 | expect![[r#" | 1071 | expect![[r#" |
1059 | 'a Label FileId(0) 29..32 29..31 Lifetime | 1072 | 'a Label FileId(0) 29..32 29..31 |
1060 | 1073 | ||
1061 | FileId(0) 80..82 Lifetime | 1074 | FileId(0) 80..82 |
1062 | FileId(0) 108..110 Lifetime | 1075 | FileId(0) 108..110 |
1063 | "#]], | 1076 | "#]], |
1064 | ); | 1077 | ); |
1065 | } | 1078 | } |
@@ -1073,9 +1086,9 @@ fn foo<const FOO$0: usize>() -> usize { | |||
1073 | } | 1086 | } |
1074 | "#, | 1087 | "#, |
1075 | expect![[r#" | 1088 | expect![[r#" |
1076 | FOO ConstParam FileId(0) 7..23 13..16 Other | 1089 | FOO ConstParam FileId(0) 7..23 13..16 |
1077 | 1090 | ||
1078 | FileId(0) 42..45 Other | 1091 | FileId(0) 42..45 |
1079 | "#]], | 1092 | "#]], |
1080 | ); | 1093 | ); |
1081 | } | 1094 | } |
@@ -1089,9 +1102,9 @@ trait Foo { | |||
1089 | } | 1102 | } |
1090 | "#, | 1103 | "#, |
1091 | expect![[r#" | 1104 | expect![[r#" |
1092 | Self TypeParam FileId(0) 6..9 6..9 Other | 1105 | Self TypeParam FileId(0) 6..9 6..9 |
1093 | 1106 | ||
1094 | FileId(0) 26..30 Other | 1107 | FileId(0) 26..30 |
1095 | "#]], | 1108 | "#]], |
1096 | ); | 1109 | ); |
1097 | } | 1110 | } |
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index ebb1ce7dd..64992c72d 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -4,9 +4,9 @@ use std::fmt::{self, Display}; | |||
4 | use either::Either; | 4 | use either::Either; |
5 | use hir::{HasSource, InFile, Module, ModuleDef, ModuleSource, Semantics}; | 5 | use hir::{HasSource, InFile, Module, ModuleDef, ModuleSource, Semantics}; |
6 | use ide_db::{ | 6 | use ide_db::{ |
7 | base_db::{AnchoredPathBuf, FileId, FileRange}, | 7 | base_db::{AnchoredPathBuf, FileId}, |
8 | defs::{Definition, NameClass, NameRefClass}, | 8 | defs::{Definition, NameClass, NameRefClass}, |
9 | search::FileReference, | 9 | search::{FileReference, NameLike}, |
10 | RootDatabase, | 10 | RootDatabase, |
11 | }; | 11 | }; |
12 | use stdx::never; | 12 | use stdx::never; |
@@ -17,10 +17,7 @@ use syntax::{ | |||
17 | use test_utils::mark; | 17 | use test_utils::mark; |
18 | use text_edit::TextEdit; | 18 | use text_edit::TextEdit; |
19 | 19 | ||
20 | use crate::{ | 20 | use crate::{display::TryToNav, FilePosition, FileSystemEdit, RangeInfo, SourceChange, TextRange}; |
21 | display::TryToNav, FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, SourceChange, | ||
22 | TextRange, | ||
23 | }; | ||
24 | 21 | ||
25 | type RenameResult<T> = Result<T, RenameError>; | 22 | type RenameResult<T> = Result<T, RenameError>; |
26 | #[derive(Debug)] | 23 | #[derive(Debug)] |
@@ -41,6 +38,8 @@ macro_rules! bail { | |||
41 | ($($tokens:tt)*) => {return Err(format_err!($($tokens)*))} | 38 | ($($tokens:tt)*) => {return Err(format_err!($($tokens)*))} |
42 | } | 39 | } |
43 | 40 | ||
41 | /// Prepares a rename. The sole job of this function is to return the TextRange of the thing that is | ||
42 | /// being targeted for a rename. | ||
44 | pub(crate) fn prepare_rename( | 43 | pub(crate) fn prepare_rename( |
45 | db: &RootDatabase, | 44 | db: &RootDatabase, |
46 | position: FilePosition, | 45 | position: FilePosition, |
@@ -123,12 +122,6 @@ fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> { | |||
123 | } | 122 | } |
124 | } | 123 | } |
125 | 124 | ||
126 | enum NameLike { | ||
127 | Name(ast::Name), | ||
128 | NameRef(ast::NameRef), | ||
129 | Lifetime(ast::Lifetime), | ||
130 | } | ||
131 | |||
132 | fn find_name_like( | 125 | fn find_name_like( |
133 | sema: &Semantics<RootDatabase>, | 126 | sema: &Semantics<RootDatabase>, |
134 | syntax: &SyntaxNode, | 127 | syntax: &SyntaxNode, |
@@ -174,69 +167,96 @@ fn source_edit_from_references( | |||
174 | sema: &Semantics<RootDatabase>, | 167 | sema: &Semantics<RootDatabase>, |
175 | file_id: FileId, | 168 | file_id: FileId, |
176 | references: &[FileReference], | 169 | references: &[FileReference], |
170 | def: Definition, | ||
177 | new_name: &str, | 171 | new_name: &str, |
178 | ) -> (FileId, TextEdit) { | 172 | ) -> (FileId, TextEdit) { |
173 | let root = sema.parse(file_id); | ||
179 | let mut edit = TextEdit::builder(); | 174 | let mut edit = TextEdit::builder(); |
180 | for reference in references { | 175 | for reference in references { |
181 | let mut replacement_text = String::new(); | 176 | let (range, replacement) = match &reference.name_from_syntax(root.syntax()) { |
182 | let range = match reference.kind { | 177 | Some(NameLike::Name(_)) => (None, format!("{}", new_name)), |
183 | ReferenceKind::FieldShorthandForField => { | 178 | Some(NameLike::NameRef(name_ref)) => source_edit_from_name_ref(name_ref, new_name, def), |
184 | mark::hit!(test_rename_struct_field_for_shorthand); | 179 | Some(NameLike::Lifetime(_)) => (None, format!("{}", new_name)), |
185 | replacement_text.push_str(new_name); | 180 | None => (None, new_name.to_owned()), |
186 | replacement_text.push_str(": "); | ||
187 | TextRange::new(reference.range.start(), reference.range.start()) | ||
188 | } | ||
189 | ReferenceKind::FieldShorthandForLocal => { | ||
190 | mark::hit!(test_rename_local_for_field_shorthand); | ||
191 | replacement_text.push_str(": "); | ||
192 | replacement_text.push_str(new_name); | ||
193 | TextRange::new(reference.range.end(), reference.range.end()) | ||
194 | } | ||
195 | ReferenceKind::RecordFieldExprOrPat => { | ||
196 | mark::hit!(test_rename_field_expr_pat); | ||
197 | replacement_text.push_str(new_name); | ||
198 | edit_text_range_for_record_field_expr_or_pat( | ||
199 | sema, | ||
200 | FileRange { file_id, range: reference.range }, | ||
201 | new_name, | ||
202 | ) | ||
203 | } | ||
204 | _ => { | ||
205 | replacement_text.push_str(new_name); | ||
206 | reference.range | ||
207 | } | ||
208 | }; | 181 | }; |
209 | edit.replace(range, replacement_text); | 182 | // FIXME: Some(range) will be incorrect when we are inside macros |
183 | edit.replace(range.unwrap_or(reference.range), replacement); | ||
210 | } | 184 | } |
211 | (file_id, edit.finish()) | 185 | (file_id, edit.finish()) |
212 | } | 186 | } |
213 | 187 | ||
214 | fn edit_text_range_for_record_field_expr_or_pat( | 188 | fn source_edit_from_name_ref( |
215 | sema: &Semantics<RootDatabase>, | 189 | name_ref: &ast::NameRef, |
216 | file_range: FileRange, | ||
217 | new_name: &str, | 190 | new_name: &str, |
218 | ) -> TextRange { | 191 | def: Definition, |
219 | let source_file = sema.parse(file_range.file_id); | 192 | ) -> (Option<TextRange>, String) { |
220 | let file_syntax = source_file.syntax(); | 193 | if let Some(record_field) = ast::RecordExprField::for_name_ref(name_ref) { |
221 | let original_range = file_range.range; | 194 | let rcf_name_ref = record_field.name_ref(); |
222 | 195 | let rcf_expr = record_field.expr(); | |
223 | syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, original_range) | 196 | match (rcf_name_ref, rcf_expr.and_then(|it| it.name_ref())) { |
224 | .and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) { | 197 | // field: init-expr, check if we can use a field init shorthand |
225 | Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()), | 198 | (Some(field_name), Some(init)) => { |
226 | _ => None, | 199 | if field_name == *name_ref { |
227 | }) | 200 | if init.text() == new_name { |
228 | .or_else(|| { | 201 | mark::hit!(test_rename_field_put_init_shorthand); |
229 | syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, original_range) | 202 | // same names, we can use a shorthand here instead |
230 | .and_then(|field_pat| match field_pat.pat() { | 203 | // we do not want to erase attributes hence this range start |
231 | Some(ast::Pat::IdentPat(pat)) | 204 | let s = field_name.syntax().text_range().start(); |
232 | if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) => | 205 | let e = record_field.syntax().text_range().end(); |
233 | { | 206 | return (Some(TextRange::new(s, e)), format!("{}", new_name)); |
234 | Some(field_pat.syntax().text_range()) | ||
235 | } | 207 | } |
236 | _ => None, | 208 | } else if init == *name_ref { |
237 | }) | 209 | if field_name.text() == new_name { |
238 | }) | 210 | mark::hit!(test_rename_local_put_init_shorthand); |
239 | .unwrap_or(original_range) | 211 | // same names, we can use a shorthand here instead |
212 | // we do not want to erase attributes hence this range start | ||
213 | let s = field_name.syntax().text_range().start(); | ||
214 | let e = record_field.syntax().text_range().end(); | ||
215 | return (Some(TextRange::new(s, e)), format!("{}", new_name)); | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | // init shorthand | ||
220 | (None, Some(_)) => { | ||
221 | // FIXME: instead of splitting the shorthand, recursively trigger a rename of the | ||
222 | // other name https://github.com/rust-analyzer/rust-analyzer/issues/6547 | ||
223 | match def { | ||
224 | Definition::Field(_) => { | ||
225 | mark::hit!(test_rename_field_in_field_shorthand); | ||
226 | let s = name_ref.syntax().text_range().start(); | ||
227 | return (Some(TextRange::empty(s)), format!("{}: ", new_name)); | ||
228 | } | ||
229 | Definition::Local(_) => { | ||
230 | mark::hit!(test_rename_local_in_field_shorthand); | ||
231 | let s = name_ref.syntax().text_range().end(); | ||
232 | return (Some(TextRange::empty(s)), format!(": {}", new_name)); | ||
233 | } | ||
234 | _ => {} | ||
235 | } | ||
236 | } | ||
237 | _ => {} | ||
238 | } | ||
239 | } | ||
240 | if let Some(record_field) = ast::RecordPatField::for_field_name_ref(name_ref) { | ||
241 | let rcf_name_ref = record_field.name_ref(); | ||
242 | let rcf_pat = record_field.pat(); | ||
243 | match (rcf_name_ref, rcf_pat) { | ||
244 | // field: rename | ||
245 | (Some(field_name), Some(ast::Pat::IdentPat(pat))) if field_name == *name_ref => { | ||
246 | // field name is being renamed | ||
247 | if pat.name().map_or(false, |it| it.text() == new_name) { | ||
248 | mark::hit!(test_rename_field_put_init_shorthand_pat); | ||
249 | // same names, we can use a shorthand here instead | ||
250 | // we do not want to erase attributes hence this range start | ||
251 | let s = field_name.syntax().text_range().start(); | ||
252 | let e = record_field.syntax().text_range().end(); | ||
253 | return (Some(TextRange::new(s, e)), format!("{}", new_name)); | ||
254 | } | ||
255 | } | ||
256 | _ => {} | ||
257 | } | ||
258 | } | ||
259 | (None, format!("{}", new_name)) | ||
240 | } | 260 | } |
241 | 261 | ||
242 | fn rename_mod( | 262 | fn rename_mod( |
@@ -277,7 +297,7 @@ fn rename_mod( | |||
277 | let def = Definition::ModuleDef(ModuleDef::Module(module)); | 297 | let def = Definition::ModuleDef(ModuleDef::Module(module)); |
278 | let usages = def.usages(sema).all(); | 298 | let usages = def.usages(sema).all(); |
279 | let ref_edits = usages.iter().map(|(&file_id, references)| { | 299 | let ref_edits = usages.iter().map(|(&file_id, references)| { |
280 | source_edit_from_references(sema, file_id, references, new_name) | 300 | source_edit_from_references(sema, file_id, references, def, new_name) |
281 | }); | 301 | }); |
282 | source_change.extend(ref_edits); | 302 | source_change.extend(ref_edits); |
283 | 303 | ||
@@ -346,7 +366,7 @@ fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameRe | |||
346 | let usages = def.usages(sema).all(); | 366 | let usages = def.usages(sema).all(); |
347 | let mut source_change = SourceChange::default(); | 367 | let mut source_change = SourceChange::default(); |
348 | source_change.extend(usages.iter().map(|(&file_id, references)| { | 368 | source_change.extend(usages.iter().map(|(&file_id, references)| { |
349 | source_edit_from_references(sema, file_id, references, "self") | 369 | source_edit_from_references(sema, file_id, references, def, "self") |
350 | })); | 370 | })); |
351 | source_change.insert_source_edit( | 371 | source_change.insert_source_edit( |
352 | file_id.original_file(sema.db), | 372 | file_id.original_file(sema.db), |
@@ -403,7 +423,7 @@ fn rename_self_to_param( | |||
403 | let mut source_change = SourceChange::default(); | 423 | let mut source_change = SourceChange::default(); |
404 | source_change.insert_source_edit(file_id.original_file(sema.db), edit); | 424 | source_change.insert_source_edit(file_id.original_file(sema.db), edit); |
405 | source_change.extend(usages.iter().map(|(&file_id, references)| { | 425 | source_change.extend(usages.iter().map(|(&file_id, references)| { |
406 | source_edit_from_references(sema, file_id, &references, new_name) | 426 | source_edit_from_references(sema, file_id, &references, def, new_name) |
407 | })); | 427 | })); |
408 | Ok(source_change) | 428 | Ok(source_change) |
409 | } | 429 | } |
@@ -457,7 +477,7 @@ fn rename_reference( | |||
457 | } | 477 | } |
458 | let mut source_change = SourceChange::default(); | 478 | let mut source_change = SourceChange::default(); |
459 | source_change.extend(usages.iter().map(|(&file_id, references)| { | 479 | source_change.extend(usages.iter().map(|(&file_id, references)| { |
460 | source_edit_from_references(sema, file_id, &references, new_name) | 480 | source_edit_from_references(sema, file_id, &references, def, new_name) |
461 | })); | 481 | })); |
462 | 482 | ||
463 | let (file_id, edit) = source_edit_from_def(sema, def, new_name)?; | 483 | let (file_id, edit) = source_edit_from_def(sema, def, new_name)?; |
@@ -545,10 +565,8 @@ mod tests { | |||
545 | 565 | ||
546 | fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) { | 566 | fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) { |
547 | let (analysis, position) = fixture::position(ra_fixture); | 567 | let (analysis, position) = fixture::position(ra_fixture); |
548 | let source_change = analysis | 568 | let source_change = |
549 | .rename(position, new_name) | 569 | analysis.rename(position, new_name).unwrap().expect("Expect returned a RenameError"); |
550 | .unwrap() | ||
551 | .expect("Expect returned RangeInfo to be Some, but was None"); | ||
552 | expect.assert_debug_eq(&source_change) | 570 | expect.assert_debug_eq(&source_change) |
553 | } | 571 | } |
554 | 572 | ||
@@ -792,8 +810,8 @@ impl Foo { | |||
792 | } | 810 | } |
793 | 811 | ||
794 | #[test] | 812 | #[test] |
795 | fn test_rename_struct_field_for_shorthand() { | 813 | fn test_rename_field_in_field_shorthand() { |
796 | mark::check!(test_rename_struct_field_for_shorthand); | 814 | mark::check!(test_rename_field_in_field_shorthand); |
797 | check( | 815 | check( |
798 | "j", | 816 | "j", |
799 | r#" | 817 | r#" |
@@ -818,8 +836,8 @@ impl Foo { | |||
818 | } | 836 | } |
819 | 837 | ||
820 | #[test] | 838 | #[test] |
821 | fn test_rename_local_for_field_shorthand() { | 839 | fn test_rename_local_in_field_shorthand() { |
822 | mark::check!(test_rename_local_for_field_shorthand); | 840 | mark::check!(test_rename_local_in_field_shorthand); |
823 | check( | 841 | check( |
824 | "j", | 842 | "j", |
825 | r#" | 843 | r#" |
@@ -1417,8 +1435,8 @@ impl Foo { | |||
1417 | } | 1435 | } |
1418 | 1436 | ||
1419 | #[test] | 1437 | #[test] |
1420 | fn test_initializer_use_field_init_shorthand() { | 1438 | fn test_rename_field_put_init_shorthand() { |
1421 | mark::check!(test_rename_field_expr_pat); | 1439 | mark::check!(test_rename_field_put_init_shorthand); |
1422 | check( | 1440 | check( |
1423 | "bar", | 1441 | "bar", |
1424 | r#" | 1442 | r#" |
@@ -1439,7 +1457,30 @@ fn foo(bar: i32) -> Foo { | |||
1439 | } | 1457 | } |
1440 | 1458 | ||
1441 | #[test] | 1459 | #[test] |
1460 | fn test_rename_local_put_init_shorthand() { | ||
1461 | mark::check!(test_rename_local_put_init_shorthand); | ||
1462 | check( | ||
1463 | "i", | ||
1464 | r#" | ||
1465 | struct Foo { i: i32 } | ||
1466 | |||
1467 | fn foo(bar$0: i32) -> Foo { | ||
1468 | Foo { i: bar } | ||
1469 | } | ||
1470 | "#, | ||
1471 | r#" | ||
1472 | struct Foo { i: i32 } | ||
1473 | |||
1474 | fn foo(i: i32) -> Foo { | ||
1475 | Foo { i } | ||
1476 | } | ||
1477 | "#, | ||
1478 | ); | ||
1479 | } | ||
1480 | |||
1481 | #[test] | ||
1442 | fn test_struct_field_destructure_into_shorthand() { | 1482 | fn test_struct_field_destructure_into_shorthand() { |
1483 | mark::check!(test_rename_field_put_init_shorthand_pat); | ||
1443 | check( | 1484 | check( |
1444 | "baz", | 1485 | "baz", |
1445 | r#" | 1486 | r#" |
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index b9ba0aed5..d0aed26f7 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs | |||
@@ -10,7 +10,9 @@ use base_db::{FileId, FileRange, SourceDatabaseExt}; | |||
10 | use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; | 10 | use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; |
11 | use once_cell::unsync::Lazy; | 11 | use once_cell::unsync::Lazy; |
12 | use rustc_hash::FxHashMap; | 12 | use rustc_hash::FxHashMap; |
13 | use syntax::{ast, match_ast, AstNode, TextRange, TextSize}; | 13 | use syntax::{ |
14 | ast, match_ast, AstNode, NodeOrToken, SyntaxElement, SyntaxNode, TextRange, TextSize, | ||
15 | }; | ||
14 | 16 | ||
15 | use crate::defs::NameClass; | 17 | use crate::defs::NameClass; |
16 | use crate::{ | 18 | use crate::{ |
@@ -18,6 +20,13 @@ use crate::{ | |||
18 | RootDatabase, | 20 | RootDatabase, |
19 | }; | 21 | }; |
20 | 22 | ||
23 | #[derive(Debug, Clone)] | ||
24 | pub enum NameKind { | ||
25 | Name, | ||
26 | NameRef, | ||
27 | Lifetime, | ||
28 | } | ||
29 | |||
21 | #[derive(Debug, Default, Clone)] | 30 | #[derive(Debug, Default, Clone)] |
22 | pub struct UsageSearchResult { | 31 | pub struct UsageSearchResult { |
23 | pub references: FxHashMap<FileId, Vec<FileReference>>, | 32 | pub references: FxHashMap<FileId, Vec<FileReference>>, |
@@ -53,22 +62,52 @@ impl IntoIterator for UsageSearchResult { | |||
53 | } | 62 | } |
54 | 63 | ||
55 | #[derive(Debug, Clone)] | 64 | #[derive(Debug, Clone)] |
65 | pub enum NameLike { | ||
66 | NameRef(ast::NameRef), | ||
67 | Name(ast::Name), | ||
68 | Lifetime(ast::Lifetime), | ||
69 | } | ||
70 | |||
71 | mod __ { | ||
72 | use super::{ | ||
73 | ast::{Lifetime, Name, NameRef}, | ||
74 | NameLike, | ||
75 | }; | ||
76 | stdx::impl_from!(NameRef, Name, Lifetime for NameLike); | ||
77 | } | ||
78 | |||
79 | #[derive(Debug, Clone)] | ||
56 | pub struct FileReference { | 80 | pub struct FileReference { |
57 | pub range: TextRange, | 81 | pub range: TextRange, |
58 | pub kind: ReferenceKind, | 82 | pub name: NameKind, |
59 | pub access: Option<ReferenceAccess>, | 83 | pub access: Option<ReferenceAccess>, |
60 | } | 84 | } |
61 | 85 | ||
62 | #[derive(Debug, Clone, PartialEq)] | 86 | impl FileReference { |
63 | pub enum ReferenceKind { | 87 | pub fn name_from_syntax(&self, root: &SyntaxNode) -> Option<NameLike> { |
64 | FieldShorthandForField, | 88 | let node = node_or_parent(root.covering_element(self.range)); |
65 | FieldShorthandForLocal, | 89 | match self.name { |
66 | StructLiteral, | 90 | NameKind::Name => ast::Name::cast(node).map(Into::into), |
67 | RecordFieldExprOrPat, | 91 | NameKind::NameRef => ast::NameRef::cast(node).map(Into::into), |
68 | SelfParam, | 92 | NameKind::Lifetime => ast::Lifetime::cast(node).map(Into::into), |
69 | EnumLiteral, | 93 | } |
70 | Lifetime, | 94 | } |
71 | Other, | 95 | |
96 | pub fn as_name_ref(&self, root: &SyntaxNode) -> Option<ast::NameRef> { | ||
97 | match self.name { | ||
98 | NameKind::NameRef => { | ||
99 | ast::NameRef::cast(node_or_parent(root.covering_element(self.range))) | ||
100 | } | ||
101 | _ => None, | ||
102 | } | ||
103 | } | ||
104 | } | ||
105 | |||
106 | fn node_or_parent(ele: SyntaxElement) -> SyntaxNode { | ||
107 | match ele { | ||
108 | NodeOrToken::Node(node) => node, | ||
109 | NodeOrToken::Token(token) => token.parent(), | ||
110 | } | ||
72 | } | 111 | } |
73 | 112 | ||
74 | #[derive(Debug, Copy, Clone, PartialEq)] | 113 | #[derive(Debug, Copy, Clone, PartialEq)] |
@@ -369,8 +408,7 @@ impl<'a> FindUsages<'a> { | |||
369 | match NameRefClass::classify_lifetime(self.sema, lifetime) { | 408 | match NameRefClass::classify_lifetime(self.sema, lifetime) { |
370 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 409 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
371 | let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); | 410 | let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); |
372 | let reference = | 411 | let reference = FileReference { range, name: NameKind::Lifetime, access: None }; |
373 | FileReference { range, kind: ReferenceKind::Lifetime, access: None }; | ||
374 | sink(file_id, reference) | 412 | sink(file_id, reference) |
375 | } | 413 | } |
376 | _ => false, // not a usage | 414 | _ => false, // not a usage |
@@ -384,19 +422,12 @@ impl<'a> FindUsages<'a> { | |||
384 | ) -> bool { | 422 | ) -> bool { |
385 | match NameRefClass::classify(self.sema, &name_ref) { | 423 | match NameRefClass::classify(self.sema, &name_ref) { |
386 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 424 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
387 | let kind = if is_record_field_expr_or_pat(&name_ref) { | ||
388 | ReferenceKind::RecordFieldExprOrPat | ||
389 | } else if is_record_lit_name_ref(&name_ref) || is_call_expr_name_ref(&name_ref) { | ||
390 | ReferenceKind::StructLiteral | ||
391 | } else if is_enum_lit_name_ref(&name_ref) { | ||
392 | ReferenceKind::EnumLiteral | ||
393 | } else { | ||
394 | ReferenceKind::Other | ||
395 | }; | ||
396 | |||
397 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); | 425 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); |
398 | let reference = | 426 | let reference = FileReference { |
399 | FileReference { range, kind, access: reference_access(&def, &name_ref) }; | 427 | range, |
428 | name: NameKind::NameRef, | ||
429 | access: reference_access(&def, &name_ref), | ||
430 | }; | ||
400 | sink(file_id, reference) | 431 | sink(file_id, reference) |
401 | } | 432 | } |
402 | Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { | 433 | Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { |
@@ -404,12 +435,12 @@ impl<'a> FindUsages<'a> { | |||
404 | let reference = match self.def { | 435 | let reference = match self.def { |
405 | Definition::Field(_) if &field == self.def => FileReference { | 436 | Definition::Field(_) if &field == self.def => FileReference { |
406 | range, | 437 | range, |
407 | kind: ReferenceKind::FieldShorthandForField, | 438 | name: NameKind::NameRef, |
408 | access: reference_access(&field, &name_ref), | 439 | access: reference_access(&field, &name_ref), |
409 | }, | 440 | }, |
410 | Definition::Local(l) if &local == l => FileReference { | 441 | Definition::Local(l) if &local == l => FileReference { |
411 | range, | 442 | range, |
412 | kind: ReferenceKind::FieldShorthandForLocal, | 443 | name: NameKind::NameRef, |
413 | access: reference_access(&Definition::Local(local), &name_ref), | 444 | access: reference_access(&Definition::Local(local), &name_ref), |
414 | }, | 445 | }, |
415 | _ => return false, // not a usage | 446 | _ => return false, // not a usage |
@@ -433,7 +464,7 @@ impl<'a> FindUsages<'a> { | |||
433 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); | 464 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); |
434 | let reference = FileReference { | 465 | let reference = FileReference { |
435 | range, | 466 | range, |
436 | kind: ReferenceKind::FieldShorthandForField, | 467 | name: NameKind::Name, |
437 | // FIXME: mutable patterns should have `Write` access | 468 | // FIXME: mutable patterns should have `Write` access |
438 | access: Some(ReferenceAccess::Read), | 469 | access: Some(ReferenceAccess::Read), |
439 | }; | 470 | }; |
@@ -473,54 +504,3 @@ fn reference_access(def: &Definition, name_ref: &ast::NameRef) -> Option<Referen | |||
473 | // Default Locals and Fields to read | 504 | // Default Locals and Fields to read |
474 | mode.or(Some(ReferenceAccess::Read)) | 505 | mode.or(Some(ReferenceAccess::Read)) |
475 | } | 506 | } |
476 | |||
477 | fn is_call_expr_name_ref(name_ref: &ast::NameRef) -> bool { | ||
478 | name_ref | ||
479 | .syntax() | ||
480 | .ancestors() | ||
481 | .find_map(ast::CallExpr::cast) | ||
482 | .and_then(|c| match c.expr()? { | ||
483 | ast::Expr::PathExpr(p) => { | ||
484 | Some(p.path()?.segment()?.name_ref().as_ref() == Some(name_ref)) | ||
485 | } | ||
486 | _ => None, | ||
487 | }) | ||
488 | .unwrap_or(false) | ||
489 | } | ||
490 | |||
491 | fn is_record_lit_name_ref(name_ref: &ast::NameRef) -> bool { | ||
492 | name_ref | ||
493 | .syntax() | ||
494 | .ancestors() | ||
495 | .find_map(ast::RecordExpr::cast) | ||
496 | .and_then(|l| l.path()) | ||
497 | .and_then(|p| p.segment()) | ||
498 | .map(|p| p.name_ref().as_ref() == Some(name_ref)) | ||
499 | .unwrap_or(false) | ||
500 | } | ||
501 | |||
502 | fn is_record_field_expr_or_pat(name_ref: &ast::NameRef) -> bool { | ||
503 | if let Some(parent) = name_ref.syntax().parent() { | ||
504 | match_ast! { | ||
505 | match parent { | ||
506 | ast::RecordExprField(it) => true, | ||
507 | ast::RecordPatField(_it) => true, | ||
508 | _ => false, | ||
509 | } | ||
510 | } | ||
511 | } else { | ||
512 | false | ||
513 | } | ||
514 | } | ||
515 | |||
516 | fn is_enum_lit_name_ref(name_ref: &ast::NameRef) -> bool { | ||
517 | name_ref | ||
518 | .syntax() | ||
519 | .ancestors() | ||
520 | .find_map(ast::PathExpr::cast) | ||
521 | .and_then(|p| p.path()) | ||
522 | .and_then(|p| p.qualifier()) | ||
523 | .and_then(|p| p.segment()) | ||
524 | .map(|p| p.name_ref().as_ref() == Some(name_ref)) | ||
525 | .unwrap_or(false) | ||
526 | } | ||
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 5a6501216..8898c12e3 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -827,18 +827,23 @@ pub(crate) fn handle_references( | |||
827 | Some(refs) => refs, | 827 | Some(refs) => refs, |
828 | }; | 828 | }; |
829 | 829 | ||
830 | let locations = if params.context.include_declaration { | 830 | let decl = if params.context.include_declaration { |
831 | refs.references_with_declaration() | 831 | Some(FileRange { |
832 | .file_ranges() | 832 | file_id: refs.declaration.nav.file_id, |
833 | .filter_map(|frange| to_proto::location(&snap, frange).ok()) | 833 | range: refs.declaration.nav.focus_or_full_range(), |
834 | .collect() | 834 | }) |
835 | } else { | 835 | } else { |
836 | // Only iterate over the references if include_declaration was false | 836 | None |
837 | refs.references() | ||
838 | .file_ranges() | ||
839 | .filter_map(|frange| to_proto::location(&snap, frange).ok()) | ||
840 | .collect() | ||
841 | }; | 837 | }; |
838 | let locations = refs | ||
839 | .references | ||
840 | .into_iter() | ||
841 | .flat_map(|(file_id, refs)| { | ||
842 | refs.into_iter().map(move |(range, _)| FileRange { file_id, range }) | ||
843 | }) | ||
844 | .chain(decl) | ||
845 | .filter_map(|frange| to_proto::location(&snap, frange).ok()) | ||
846 | .collect(); | ||
842 | 847 | ||
843 | Ok(Some(locations)) | 848 | Ok(Some(locations)) |
844 | } | 849 | } |
@@ -1214,8 +1219,11 @@ pub(crate) fn handle_code_lens_resolve( | |||
1214 | .find_all_refs(position, None) | 1219 | .find_all_refs(position, None) |
1215 | .unwrap_or(None) | 1220 | .unwrap_or(None) |
1216 | .map(|r| { | 1221 | .map(|r| { |
1217 | r.references() | 1222 | r.references |
1218 | .file_ranges() | 1223 | .into_iter() |
1224 | .flat_map(|(file_id, ranges)| { | ||
1225 | ranges.into_iter().map(move |(range, _)| FileRange { file_id, range }) | ||
1226 | }) | ||
1219 | .filter_map(|frange| to_proto::location(&snap, frange).ok()) | 1227 | .filter_map(|frange| to_proto::location(&snap, frange).ok()) |
1220 | .collect_vec() | 1228 | .collect_vec() |
1221 | }) | 1229 | }) |
@@ -1259,17 +1267,26 @@ pub(crate) fn handle_document_highlight( | |||
1259 | Some(refs) => refs, | 1267 | Some(refs) => refs, |
1260 | }; | 1268 | }; |
1261 | 1269 | ||
1270 | let decl = if refs.declaration.nav.file_id == position.file_id { | ||
1271 | Some(DocumentHighlight { | ||
1272 | range: to_proto::range(&line_index, refs.declaration.nav.focus_or_full_range()), | ||
1273 | kind: refs.declaration.access.map(to_proto::document_highlight_kind), | ||
1274 | }) | ||
1275 | } else { | ||
1276 | None | ||
1277 | }; | ||
1278 | |||
1262 | let res = refs | 1279 | let res = refs |
1263 | .references_with_declaration() | ||
1264 | .references | 1280 | .references |
1265 | .get(&position.file_id) | 1281 | .get(&position.file_id) |
1266 | .map(|file_refs| { | 1282 | .map(|file_refs| { |
1267 | file_refs | 1283 | file_refs |
1268 | .into_iter() | 1284 | .into_iter() |
1269 | .map(|r| DocumentHighlight { | 1285 | .map(|&(range, access)| DocumentHighlight { |
1270 | range: to_proto::range(&line_index, r.range), | 1286 | range: to_proto::range(&line_index, range), |
1271 | kind: r.access.map(to_proto::document_highlight_kind), | 1287 | kind: access.map(to_proto::document_highlight_kind), |
1272 | }) | 1288 | }) |
1289 | .chain(decl) | ||
1273 | .collect() | 1290 | .collect() |
1274 | }) | 1291 | }) |
1275 | .unwrap_or_default(); | 1292 | .unwrap_or_default(); |
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 5c8cf900f..b105cb0e0 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -274,10 +274,7 @@ impl ast::Struct { | |||
274 | 274 | ||
275 | impl ast::RecordExprField { | 275 | impl ast::RecordExprField { |
276 | pub fn for_field_name(field_name: &ast::NameRef) -> Option<ast::RecordExprField> { | 276 | pub fn for_field_name(field_name: &ast::NameRef) -> Option<ast::RecordExprField> { |
277 | let candidate = | 277 | let candidate = Self::for_name_ref(field_name)?; |
278 | field_name.syntax().parent().and_then(ast::RecordExprField::cast).or_else(|| { | ||
279 | field_name.syntax().ancestors().nth(4).and_then(ast::RecordExprField::cast) | ||
280 | })?; | ||
281 | if candidate.field_name().as_ref() == Some(field_name) { | 278 | if candidate.field_name().as_ref() == Some(field_name) { |
282 | Some(candidate) | 279 | Some(candidate) |
283 | } else { | 280 | } else { |
@@ -285,6 +282,13 @@ impl ast::RecordExprField { | |||
285 | } | 282 | } |
286 | } | 283 | } |
287 | 284 | ||
285 | pub fn for_name_ref(name_ref: &ast::NameRef) -> Option<ast::RecordExprField> { | ||
286 | let syn = name_ref.syntax(); | ||
287 | syn.parent() | ||
288 | .and_then(ast::RecordExprField::cast) | ||
289 | .or_else(|| syn.ancestors().nth(4).and_then(ast::RecordExprField::cast)) | ||
290 | } | ||
291 | |||
288 | /// Deals with field init shorthand | 292 | /// Deals with field init shorthand |
289 | pub fn field_name(&self) -> Option<ast::NameRef> { | 293 | pub fn field_name(&self) -> Option<ast::NameRef> { |
290 | if let Some(name_ref) = self.name_ref() { | 294 | if let Some(name_ref) = self.name_ref() { |
@@ -294,6 +298,7 @@ impl ast::RecordExprField { | |||
294 | } | 298 | } |
295 | } | 299 | } |
296 | 300 | ||
301 | #[derive(Debug, Clone, PartialEq)] | ||
297 | pub enum NameOrNameRef { | 302 | pub enum NameOrNameRef { |
298 | Name(ast::Name), | 303 | Name(ast::Name), |
299 | NameRef(ast::NameRef), | 304 | NameRef(ast::NameRef), |
@@ -309,6 +314,23 @@ impl fmt::Display for NameOrNameRef { | |||
309 | } | 314 | } |
310 | 315 | ||
311 | impl ast::RecordPatField { | 316 | impl ast::RecordPatField { |
317 | pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> { | ||
318 | let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?; | ||
319 | match candidate.field_name()? { | ||
320 | NameOrNameRef::NameRef(name_ref) if name_ref == *field_name => Some(candidate), | ||
321 | _ => None, | ||
322 | } | ||
323 | } | ||
324 | |||
325 | pub fn for_field_name(field_name: &ast::Name) -> Option<ast::RecordPatField> { | ||
326 | let candidate = | ||
327 | field_name.syntax().ancestors().nth(3).and_then(ast::RecordPatField::cast)?; | ||
328 | match candidate.field_name()? { | ||
329 | NameOrNameRef::Name(name) if name == *field_name => Some(candidate), | ||
330 | _ => None, | ||
331 | } | ||
332 | } | ||
333 | |||
312 | /// Deals with field init shorthand | 334 | /// Deals with field init shorthand |
313 | pub fn field_name(&self) -> Option<NameOrNameRef> { | 335 | pub fn field_name(&self) -> Option<NameOrNameRef> { |
314 | if let Some(name_ref) = self.name_ref() { | 336 | if let Some(name_ref) = self.name_ref() { |