diff options
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/annotations.rs | 83 | ||||
-rw-r--r-- | crates/ide/src/file_structure.rs | 228 | ||||
-rw-r--r-- | crates/ide/src/goto_definition.rs | 11 | ||||
-rw-r--r-- | crates/ide/src/join_lines.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/references.rs | 26 | ||||
-rw-r--r-- | crates/ide/src/references/rename.rs | 3 | ||||
-rw-r--r-- | crates/ide/src/runnables.rs | 60 | ||||
-rw-r--r-- | crates/ide/src/typing.rs | 3 |
9 files changed, 304 insertions, 118 deletions
diff --git a/crates/ide/src/annotations.rs b/crates/ide/src/annotations.rs index 2e8e82b70..72492f826 100644 --- a/crates/ide/src/annotations.rs +++ b/crates/ide/src/annotations.rs | |||
@@ -1,12 +1,13 @@ | |||
1 | use hir::Semantics; | 1 | use either::Either; |
2 | use hir::{HasSource, Semantics}; | ||
2 | use ide_db::{ | 3 | use ide_db::{ |
3 | base_db::{FileId, FilePosition, FileRange, SourceDatabase}, | 4 | base_db::{FileId, FilePosition, FileRange}, |
4 | RootDatabase, SymbolKind, | 5 | helpers::visit_file_defs, |
6 | RootDatabase, | ||
5 | }; | 7 | }; |
6 | use syntax::TextRange; | 8 | use syntax::{ast::NameOwner, AstNode, TextRange, TextSize}; |
7 | 9 | ||
8 | use crate::{ | 10 | use crate::{ |
9 | file_structure::file_structure, | ||
10 | fn_references::find_all_methods, | 11 | fn_references::find_all_methods, |
11 | goto_implementation::goto_implementation, | 12 | goto_implementation::goto_implementation, |
12 | references::find_all_refs, | 13 | references::find_all_refs, |
@@ -75,39 +76,56 @@ pub(crate) fn annotations( | |||
75 | } | 76 | } |
76 | } | 77 | } |
77 | 78 | ||
78 | file_structure(&db.parse(file_id).tree()) | 79 | visit_file_defs(&Semantics::new(db), file_id, &mut |def| match def { |
79 | .into_iter() | 80 | Either::Left(def) => { |
80 | .filter(|node| { | 81 | let node = match def { |
81 | matches!( | 82 | hir::ModuleDef::Const(konst) => { |
82 | node.kind, | 83 | konst.source(db).and_then(|node| range_and_position_of(&node.value)) |
83 | SymbolKind::Trait | 84 | } |
84 | | SymbolKind::Struct | 85 | hir::ModuleDef::Trait(trait_) => { |
85 | | SymbolKind::Enum | 86 | trait_.source(db).and_then(|node| range_and_position_of(&node.value)) |
86 | | SymbolKind::Union | 87 | } |
87 | | SymbolKind::Const | 88 | hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => { |
88 | ) | 89 | strukt.source(db).and_then(|node| range_and_position_of(&node.value)) |
89 | }) | 90 | } |
90 | .for_each(|node| { | 91 | hir::ModuleDef::Adt(hir::Adt::Enum(enum_)) => { |
91 | if config.annotate_impls && node.kind != SymbolKind::Const { | 92 | enum_.source(db).and_then(|node| range_and_position_of(&node.value)) |
93 | } | ||
94 | hir::ModuleDef::Adt(hir::Adt::Union(union)) => { | ||
95 | union.source(db).and_then(|node| range_and_position_of(&node.value)) | ||
96 | } | ||
97 | _ => None, | ||
98 | }; | ||
99 | let (offset, range) = match node { | ||
100 | Some(node) => node, | ||
101 | None => return, | ||
102 | }; | ||
103 | |||
104 | if config.annotate_impls && !matches!(def, hir::ModuleDef::Const(_)) { | ||
92 | annotations.push(Annotation { | 105 | annotations.push(Annotation { |
93 | range: node.node_range, | 106 | range, |
94 | kind: AnnotationKind::HasImpls { | 107 | kind: AnnotationKind::HasImpls { |
95 | position: FilePosition { file_id, offset: node.navigation_range.start() }, | 108 | position: FilePosition { file_id, offset }, |
96 | data: None, | 109 | data: None, |
97 | }, | 110 | }, |
98 | }); | 111 | }); |
99 | } | 112 | } |
100 | |||
101 | if config.annotate_references { | 113 | if config.annotate_references { |
102 | annotations.push(Annotation { | 114 | annotations.push(Annotation { |
103 | range: node.node_range, | 115 | range, |
104 | kind: AnnotationKind::HasReferences { | 116 | kind: AnnotationKind::HasReferences { |
105 | position: FilePosition { file_id, offset: node.navigation_range.start() }, | 117 | position: FilePosition { file_id, offset }, |
106 | data: None, | 118 | data: None, |
107 | }, | 119 | }, |
108 | }); | 120 | }); |
109 | } | 121 | } |
110 | }); | 122 | |
123 | fn range_and_position_of(node: &dyn NameOwner) -> Option<(TextSize, TextRange)> { | ||
124 | Some((node.name()?.syntax().text_range().start(), node.syntax().text_range())) | ||
125 | } | ||
126 | } | ||
127 | Either::Right(_) => (), | ||
128 | }); | ||
111 | 129 | ||
112 | if config.annotate_method_references { | 130 | if config.annotate_method_references { |
113 | annotations.extend(find_all_methods(db, file_id).into_iter().map(|method| Annotation { | 131 | annotations.extend(find_all_methods(db, file_id).into_iter().map(|method| Annotation { |
@@ -934,4 +952,19 @@ mod tests { | |||
934 | "#]], | 952 | "#]], |
935 | ); | 953 | ); |
936 | } | 954 | } |
955 | |||
956 | #[test] | ||
957 | fn test_no_annotations_outside_module_tree() { | ||
958 | check( | ||
959 | r#" | ||
960 | //- /foo.rs | ||
961 | struct Foo; | ||
962 | //- /lib.rs | ||
963 | // this file comes last since `check` checks the first file only | ||
964 | "#, | ||
965 | expect![[r#" | ||
966 | [] | ||
967 | "#]], | ||
968 | ); | ||
969 | } | ||
937 | } | 970 | } |
diff --git a/crates/ide/src/file_structure.rs b/crates/ide/src/file_structure.rs index 26793bdb4..9f879a66e 100644 --- a/crates/ide/src/file_structure.rs +++ b/crates/ide/src/file_structure.rs | |||
@@ -1,7 +1,8 @@ | |||
1 | use ide_db::SymbolKind; | 1 | use ide_db::SymbolKind; |
2 | use syntax::{ | 2 | use syntax::{ |
3 | ast::{self, AttrsOwner, GenericParamsOwner, NameOwner}, | 3 | ast::{self, AttrsOwner, GenericParamsOwner, NameOwner}, |
4 | match_ast, AstNode, SourceFile, SyntaxNode, TextRange, WalkEvent, | 4 | match_ast, AstNode, AstToken, NodeOrToken, SourceFile, SyntaxNode, SyntaxToken, TextRange, |
5 | WalkEvent, | ||
5 | }; | 6 | }; |
6 | 7 | ||
7 | #[derive(Debug, Clone)] | 8 | #[derive(Debug, Clone)] |
@@ -10,11 +11,17 @@ pub struct StructureNode { | |||
10 | pub label: String, | 11 | pub label: String, |
11 | pub navigation_range: TextRange, | 12 | pub navigation_range: TextRange, |
12 | pub node_range: TextRange, | 13 | pub node_range: TextRange, |
13 | pub kind: SymbolKind, | 14 | pub kind: StructureNodeKind, |
14 | pub detail: Option<String>, | 15 | pub detail: Option<String>, |
15 | pub deprecated: bool, | 16 | pub deprecated: bool, |
16 | } | 17 | } |
17 | 18 | ||
19 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
20 | pub enum StructureNodeKind { | ||
21 | SymbolKind(SymbolKind), | ||
22 | Region, | ||
23 | } | ||
24 | |||
18 | // Feature: File Structure | 25 | // Feature: File Structure |
19 | // | 26 | // |
20 | // Provides a tree of the symbols defined in the file. Can be used to | 27 | // Provides a tree of the symbols defined in the file. Can be used to |
@@ -32,34 +39,46 @@ pub(crate) fn file_structure(file: &SourceFile) -> Vec<StructureNode> { | |||
32 | let mut res = Vec::new(); | 39 | let mut res = Vec::new(); |
33 | let mut stack = Vec::new(); | 40 | let mut stack = Vec::new(); |
34 | 41 | ||
35 | for event in file.syntax().preorder() { | 42 | for event in file.syntax().preorder_with_tokens() { |
36 | match event { | 43 | match event { |
37 | WalkEvent::Enter(node) => { | 44 | WalkEvent::Enter(NodeOrToken::Node(node)) => { |
38 | if let Some(mut symbol) = structure_node(&node) { | 45 | if let Some(mut symbol) = structure_node(&node) { |
39 | symbol.parent = stack.last().copied(); | 46 | symbol.parent = stack.last().copied(); |
40 | stack.push(res.len()); | 47 | stack.push(res.len()); |
41 | res.push(symbol); | 48 | res.push(symbol); |
42 | } | 49 | } |
43 | } | 50 | } |
44 | WalkEvent::Leave(node) => { | 51 | WalkEvent::Leave(NodeOrToken::Node(node)) => { |
45 | if structure_node(&node).is_some() { | 52 | if structure_node(&node).is_some() { |
46 | stack.pop().unwrap(); | 53 | stack.pop().unwrap(); |
47 | } | 54 | } |
48 | } | 55 | } |
56 | WalkEvent::Enter(NodeOrToken::Token(token)) => { | ||
57 | if let Some(mut symbol) = structure_token(token) { | ||
58 | symbol.parent = stack.last().copied(); | ||
59 | stack.push(res.len()); | ||
60 | res.push(symbol); | ||
61 | } | ||
62 | } | ||
63 | WalkEvent::Leave(NodeOrToken::Token(token)) => { | ||
64 | if structure_token(token).is_some() { | ||
65 | stack.pop().unwrap(); | ||
66 | } | ||
67 | } | ||
49 | } | 68 | } |
50 | } | 69 | } |
51 | res | 70 | res |
52 | } | 71 | } |
53 | 72 | ||
54 | fn structure_node(node: &SyntaxNode) -> Option<StructureNode> { | 73 | fn structure_node(node: &SyntaxNode) -> Option<StructureNode> { |
55 | fn decl<N: NameOwner + AttrsOwner>(node: N, kind: SymbolKind) -> Option<StructureNode> { | 74 | fn decl<N: NameOwner + AttrsOwner>(node: N, kind: StructureNodeKind) -> Option<StructureNode> { |
56 | decl_with_detail(&node, None, kind) | 75 | decl_with_detail(&node, None, kind) |
57 | } | 76 | } |
58 | 77 | ||
59 | fn decl_with_type_ref<N: NameOwner + AttrsOwner>( | 78 | fn decl_with_type_ref<N: NameOwner + AttrsOwner>( |
60 | node: &N, | 79 | node: &N, |
61 | type_ref: Option<ast::Type>, | 80 | type_ref: Option<ast::Type>, |
62 | kind: SymbolKind, | 81 | kind: StructureNodeKind, |
63 | ) -> Option<StructureNode> { | 82 | ) -> Option<StructureNode> { |
64 | let detail = type_ref.map(|type_ref| { | 83 | let detail = type_ref.map(|type_ref| { |
65 | let mut detail = String::new(); | 84 | let mut detail = String::new(); |
@@ -72,7 +91,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> { | |||
72 | fn decl_with_detail<N: NameOwner + AttrsOwner>( | 91 | fn decl_with_detail<N: NameOwner + AttrsOwner>( |
73 | node: &N, | 92 | node: &N, |
74 | detail: Option<String>, | 93 | detail: Option<String>, |
75 | kind: SymbolKind, | 94 | kind: StructureNodeKind, |
76 | ) -> Option<StructureNode> { | 95 | ) -> Option<StructureNode> { |
77 | let name = node.name()?; | 96 | let name = node.name()?; |
78 | 97 | ||
@@ -120,18 +139,18 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> { | |||
120 | collapse_ws(ret_type.syntax(), &mut detail); | 139 | collapse_ws(ret_type.syntax(), &mut detail); |
121 | } | 140 | } |
122 | 141 | ||
123 | decl_with_detail(&it, Some(detail), SymbolKind::Function) | 142 | decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(SymbolKind::Function)) |
124 | }, | 143 | }, |
125 | ast::Struct(it) => decl(it, SymbolKind::Struct), | 144 | ast::Struct(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Struct)), |
126 | ast::Union(it) => decl(it, SymbolKind::Union), | 145 | ast::Union(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Union)), |
127 | ast::Enum(it) => decl(it, SymbolKind::Enum), | 146 | ast::Enum(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Enum)), |
128 | ast::Variant(it) => decl(it, SymbolKind::Variant), | 147 | ast::Variant(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Variant)), |
129 | ast::Trait(it) => decl(it, SymbolKind::Trait), | 148 | ast::Trait(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Trait)), |
130 | ast::Module(it) => decl(it, SymbolKind::Module), | 149 | ast::Module(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Module)), |
131 | ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::TypeAlias), | 150 | ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::TypeAlias)), |
132 | ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::Field), | 151 | ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Field)), |
133 | ast::Const(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::Const), | 152 | ast::Const(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Const)), |
134 | ast::Static(it) => decl_with_type_ref(&it, it.ty(), SymbolKind::Static), | 153 | ast::Static(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Static)), |
135 | ast::Impl(it) => { | 154 | ast::Impl(it) => { |
136 | let target_type = it.self_ty()?; | 155 | let target_type = it.self_ty()?; |
137 | let target_trait = it.trait_(); | 156 | let target_trait = it.trait_(); |
@@ -147,18 +166,38 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> { | |||
147 | label, | 166 | label, |
148 | navigation_range: target_type.syntax().text_range(), | 167 | navigation_range: target_type.syntax().text_range(), |
149 | node_range: it.syntax().text_range(), | 168 | node_range: it.syntax().text_range(), |
150 | kind: SymbolKind::Impl, | 169 | kind: StructureNodeKind::SymbolKind(SymbolKind::Impl), |
151 | detail: None, | 170 | detail: None, |
152 | deprecated: false, | 171 | deprecated: false, |
153 | }; | 172 | }; |
154 | Some(node) | 173 | Some(node) |
155 | }, | 174 | }, |
156 | ast::MacroRules(it) => decl(it, SymbolKind::Macro), | 175 | ast::MacroRules(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)), |
157 | _ => None, | 176 | _ => None, |
158 | } | 177 | } |
159 | } | 178 | } |
160 | } | 179 | } |
161 | 180 | ||
181 | fn structure_token(token: SyntaxToken) -> Option<StructureNode> { | ||
182 | if let Some(comment) = ast::Comment::cast(token) { | ||
183 | let text = comment.text().trim(); | ||
184 | |||
185 | if let Some(region_name) = text.strip_prefix("// region:").map(str::trim) { | ||
186 | return Some(StructureNode { | ||
187 | parent: None, | ||
188 | label: region_name.to_string(), | ||
189 | navigation_range: comment.syntax().text_range(), | ||
190 | node_range: comment.syntax().text_range(), | ||
191 | kind: StructureNodeKind::Region, | ||
192 | detail: None, | ||
193 | deprecated: false, | ||
194 | }); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | None | ||
199 | } | ||
200 | |||
162 | #[cfg(test)] | 201 | #[cfg(test)] |
163 | mod tests { | 202 | mod tests { |
164 | use expect_test::{expect, Expect}; | 203 | use expect_test::{expect, Expect}; |
@@ -217,6 +256,16 @@ fn obsolete() {} | |||
217 | 256 | ||
218 | #[deprecated(note = "for awhile")] | 257 | #[deprecated(note = "for awhile")] |
219 | fn very_obsolete() {} | 258 | fn very_obsolete() {} |
259 | |||
260 | // region: Some region name | ||
261 | // endregion | ||
262 | |||
263 | // region: dontpanic | ||
264 | mod m { | ||
265 | fn f() {} | ||
266 | // endregion | ||
267 | fn g() {} | ||
268 | } | ||
220 | "#, | 269 | "#, |
221 | expect![[r#" | 270 | expect![[r#" |
222 | [ | 271 | [ |
@@ -225,7 +274,9 @@ fn very_obsolete() {} | |||
225 | label: "Foo", | 274 | label: "Foo", |
226 | navigation_range: 8..11, | 275 | navigation_range: 8..11, |
227 | node_range: 1..26, | 276 | node_range: 1..26, |
228 | kind: Struct, | 277 | kind: SymbolKind( |
278 | Struct, | ||
279 | ), | ||
229 | detail: None, | 280 | detail: None, |
230 | deprecated: false, | 281 | deprecated: false, |
231 | }, | 282 | }, |
@@ -236,7 +287,9 @@ fn very_obsolete() {} | |||
236 | label: "x", | 287 | label: "x", |
237 | navigation_range: 18..19, | 288 | navigation_range: 18..19, |
238 | node_range: 18..24, | 289 | node_range: 18..24, |
239 | kind: Field, | 290 | kind: SymbolKind( |
291 | Field, | ||
292 | ), | ||
240 | detail: Some( | 293 | detail: Some( |
241 | "i32", | 294 | "i32", |
242 | ), | 295 | ), |
@@ -247,7 +300,9 @@ fn very_obsolete() {} | |||
247 | label: "m", | 300 | label: "m", |
248 | navigation_range: 32..33, | 301 | navigation_range: 32..33, |
249 | node_range: 28..158, | 302 | node_range: 28..158, |
250 | kind: Module, | 303 | kind: SymbolKind( |
304 | Module, | ||
305 | ), | ||
251 | detail: None, | 306 | detail: None, |
252 | deprecated: false, | 307 | deprecated: false, |
253 | }, | 308 | }, |
@@ -258,7 +313,9 @@ fn very_obsolete() {} | |||
258 | label: "bar1", | 313 | label: "bar1", |
259 | navigation_range: 43..47, | 314 | navigation_range: 43..47, |
260 | node_range: 40..52, | 315 | node_range: 40..52, |
261 | kind: Function, | 316 | kind: SymbolKind( |
317 | Function, | ||
318 | ), | ||
262 | detail: Some( | 319 | detail: Some( |
263 | "fn()", | 320 | "fn()", |
264 | ), | 321 | ), |
@@ -271,7 +328,9 @@ fn very_obsolete() {} | |||
271 | label: "bar2", | 328 | label: "bar2", |
272 | navigation_range: 60..64, | 329 | navigation_range: 60..64, |
273 | node_range: 57..81, | 330 | node_range: 57..81, |
274 | kind: Function, | 331 | kind: SymbolKind( |
332 | Function, | ||
333 | ), | ||
275 | detail: Some( | 334 | detail: Some( |
276 | "fn<T>(t: T) -> T", | 335 | "fn<T>(t: T) -> T", |
277 | ), | 336 | ), |
@@ -284,7 +343,9 @@ fn very_obsolete() {} | |||
284 | label: "bar3", | 343 | label: "bar3", |
285 | navigation_range: 89..93, | 344 | navigation_range: 89..93, |
286 | node_range: 86..156, | 345 | node_range: 86..156, |
287 | kind: Function, | 346 | kind: SymbolKind( |
347 | Function, | ||
348 | ), | ||
288 | detail: Some( | 349 | detail: Some( |
289 | "fn<A, B>(a: A, b: B) -> Vec< u32 >", | 350 | "fn<A, B>(a: A, b: B) -> Vec< u32 >", |
290 | ), | 351 | ), |
@@ -295,7 +356,9 @@ fn very_obsolete() {} | |||
295 | label: "E", | 356 | label: "E", |
296 | navigation_range: 165..166, | 357 | navigation_range: 165..166, |
297 | node_range: 160..180, | 358 | node_range: 160..180, |
298 | kind: Enum, | 359 | kind: SymbolKind( |
360 | Enum, | ||
361 | ), | ||
299 | detail: None, | 362 | detail: None, |
300 | deprecated: false, | 363 | deprecated: false, |
301 | }, | 364 | }, |
@@ -306,7 +369,9 @@ fn very_obsolete() {} | |||
306 | label: "X", | 369 | label: "X", |
307 | navigation_range: 169..170, | 370 | navigation_range: 169..170, |
308 | node_range: 169..170, | 371 | node_range: 169..170, |
309 | kind: Variant, | 372 | kind: SymbolKind( |
373 | Variant, | ||
374 | ), | ||
310 | detail: None, | 375 | detail: None, |
311 | deprecated: false, | 376 | deprecated: false, |
312 | }, | 377 | }, |
@@ -317,7 +382,9 @@ fn very_obsolete() {} | |||
317 | label: "Y", | 382 | label: "Y", |
318 | navigation_range: 172..173, | 383 | navigation_range: 172..173, |
319 | node_range: 172..178, | 384 | node_range: 172..178, |
320 | kind: Variant, | 385 | kind: SymbolKind( |
386 | Variant, | ||
387 | ), | ||
321 | detail: None, | 388 | detail: None, |
322 | deprecated: false, | 389 | deprecated: false, |
323 | }, | 390 | }, |
@@ -326,7 +393,9 @@ fn very_obsolete() {} | |||
326 | label: "T", | 393 | label: "T", |
327 | navigation_range: 186..187, | 394 | navigation_range: 186..187, |
328 | node_range: 181..193, | 395 | node_range: 181..193, |
329 | kind: TypeAlias, | 396 | kind: SymbolKind( |
397 | TypeAlias, | ||
398 | ), | ||
330 | detail: Some( | 399 | detail: Some( |
331 | "()", | 400 | "()", |
332 | ), | 401 | ), |
@@ -337,7 +406,9 @@ fn very_obsolete() {} | |||
337 | label: "S", | 406 | label: "S", |
338 | navigation_range: 201..202, | 407 | navigation_range: 201..202, |
339 | node_range: 194..213, | 408 | node_range: 194..213, |
340 | kind: Static, | 409 | kind: SymbolKind( |
410 | Static, | ||
411 | ), | ||
341 | detail: Some( | 412 | detail: Some( |
342 | "i32", | 413 | "i32", |
343 | ), | 414 | ), |
@@ -348,7 +419,9 @@ fn very_obsolete() {} | |||
348 | label: "C", | 419 | label: "C", |
349 | navigation_range: 220..221, | 420 | navigation_range: 220..221, |
350 | node_range: 214..232, | 421 | node_range: 214..232, |
351 | kind: Const, | 422 | kind: SymbolKind( |
423 | Const, | ||
424 | ), | ||
352 | detail: Some( | 425 | detail: Some( |
353 | "i32", | 426 | "i32", |
354 | ), | 427 | ), |
@@ -359,7 +432,9 @@ fn very_obsolete() {} | |||
359 | label: "impl E", | 432 | label: "impl E", |
360 | navigation_range: 239..240, | 433 | navigation_range: 239..240, |
361 | node_range: 234..243, | 434 | node_range: 234..243, |
362 | kind: Impl, | 435 | kind: SymbolKind( |
436 | Impl, | ||
437 | ), | ||
363 | detail: None, | 438 | detail: None, |
364 | deprecated: false, | 439 | deprecated: false, |
365 | }, | 440 | }, |
@@ -368,7 +443,9 @@ fn very_obsolete() {} | |||
368 | label: "impl fmt::Debug for E", | 443 | label: "impl fmt::Debug for E", |
369 | navigation_range: 265..266, | 444 | navigation_range: 265..266, |
370 | node_range: 245..269, | 445 | node_range: 245..269, |
371 | kind: Impl, | 446 | kind: SymbolKind( |
447 | Impl, | ||
448 | ), | ||
372 | detail: None, | 449 | detail: None, |
373 | deprecated: false, | 450 | deprecated: false, |
374 | }, | 451 | }, |
@@ -377,7 +454,9 @@ fn very_obsolete() {} | |||
377 | label: "mc", | 454 | label: "mc", |
378 | navigation_range: 284..286, | 455 | navigation_range: 284..286, |
379 | node_range: 271..303, | 456 | node_range: 271..303, |
380 | kind: Macro, | 457 | kind: SymbolKind( |
458 | Macro, | ||
459 | ), | ||
381 | detail: None, | 460 | detail: None, |
382 | deprecated: false, | 461 | deprecated: false, |
383 | }, | 462 | }, |
@@ -386,7 +465,9 @@ fn very_obsolete() {} | |||
386 | label: "mcexp", | 465 | label: "mcexp", |
387 | navigation_range: 334..339, | 466 | navigation_range: 334..339, |
388 | node_range: 305..356, | 467 | node_range: 305..356, |
389 | kind: Macro, | 468 | kind: SymbolKind( |
469 | Macro, | ||
470 | ), | ||
390 | detail: None, | 471 | detail: None, |
391 | deprecated: false, | 472 | deprecated: false, |
392 | }, | 473 | }, |
@@ -395,7 +476,9 @@ fn very_obsolete() {} | |||
395 | label: "mcexp", | 476 | label: "mcexp", |
396 | navigation_range: 387..392, | 477 | navigation_range: 387..392, |
397 | node_range: 358..409, | 478 | node_range: 358..409, |
398 | kind: Macro, | 479 | kind: SymbolKind( |
480 | Macro, | ||
481 | ), | ||
399 | detail: None, | 482 | detail: None, |
400 | deprecated: false, | 483 | deprecated: false, |
401 | }, | 484 | }, |
@@ -404,7 +487,9 @@ fn very_obsolete() {} | |||
404 | label: "obsolete", | 487 | label: "obsolete", |
405 | navigation_range: 428..436, | 488 | navigation_range: 428..436, |
406 | node_range: 411..441, | 489 | node_range: 411..441, |
407 | kind: Function, | 490 | kind: SymbolKind( |
491 | Function, | ||
492 | ), | ||
408 | detail: Some( | 493 | detail: Some( |
409 | "fn()", | 494 | "fn()", |
410 | ), | 495 | ), |
@@ -415,12 +500,75 @@ fn very_obsolete() {} | |||
415 | label: "very_obsolete", | 500 | label: "very_obsolete", |
416 | navigation_range: 481..494, | 501 | navigation_range: 481..494, |
417 | node_range: 443..499, | 502 | node_range: 443..499, |
418 | kind: Function, | 503 | kind: SymbolKind( |
504 | Function, | ||
505 | ), | ||
419 | detail: Some( | 506 | detail: Some( |
420 | "fn()", | 507 | "fn()", |
421 | ), | 508 | ), |
422 | deprecated: true, | 509 | deprecated: true, |
423 | }, | 510 | }, |
511 | StructureNode { | ||
512 | parent: None, | ||
513 | label: "Some region name", | ||
514 | navigation_range: 501..528, | ||
515 | node_range: 501..528, | ||
516 | kind: Region, | ||
517 | detail: None, | ||
518 | deprecated: false, | ||
519 | }, | ||
520 | StructureNode { | ||
521 | parent: None, | ||
522 | label: "m", | ||
523 | navigation_range: 568..569, | ||
524 | node_range: 543..606, | ||
525 | kind: SymbolKind( | ||
526 | Module, | ||
527 | ), | ||
528 | detail: None, | ||
529 | deprecated: false, | ||
530 | }, | ||
531 | StructureNode { | ||
532 | parent: Some( | ||
533 | 20, | ||
534 | ), | ||
535 | label: "dontpanic", | ||
536 | navigation_range: 543..563, | ||
537 | node_range: 543..563, | ||
538 | kind: Region, | ||
539 | detail: None, | ||
540 | deprecated: false, | ||
541 | }, | ||
542 | StructureNode { | ||
543 | parent: Some( | ||
544 | 20, | ||
545 | ), | ||
546 | label: "f", | ||
547 | navigation_range: 575..576, | ||
548 | node_range: 572..581, | ||
549 | kind: SymbolKind( | ||
550 | Function, | ||
551 | ), | ||
552 | detail: Some( | ||
553 | "fn()", | ||
554 | ), | ||
555 | deprecated: false, | ||
556 | }, | ||
557 | StructureNode { | ||
558 | parent: Some( | ||
559 | 20, | ||
560 | ), | ||
561 | label: "g", | ||
562 | navigation_range: 598..599, | ||
563 | node_range: 582..604, | ||
564 | kind: SymbolKind( | ||
565 | Function, | ||
566 | ), | ||
567 | detail: Some( | ||
568 | "fn()", | ||
569 | ), | ||
570 | deprecated: false, | ||
571 | }, | ||
424 | ] | 572 | ] |
425 | "#]], | 573 | "#]], |
426 | ); | 574 | ); |
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index abed1969e..e8f31e4b1 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs | |||
@@ -1161,4 +1161,15 @@ struct S; | |||
1161 | "#, | 1161 | "#, |
1162 | ) | 1162 | ) |
1163 | } | 1163 | } |
1164 | |||
1165 | #[test] | ||
1166 | fn goto_incomplete_field() { | ||
1167 | check( | ||
1168 | r#" | ||
1169 | struct A { a: u32 } | ||
1170 | //^ | ||
1171 | fn foo() { A { a$0: }; } | ||
1172 | "#, | ||
1173 | ) | ||
1174 | } | ||
1164 | } | 1175 | } |
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index 20a920ddb..d571ed559 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs | |||
@@ -218,7 +218,7 @@ mod tests { | |||
218 | let result = join_lines(&file, range); | 218 | let result = join_lines(&file, range); |
219 | 219 | ||
220 | let actual = { | 220 | let actual = { |
221 | let mut actual = before.to_string(); | 221 | let mut actual = before; |
222 | result.apply(&mut actual); | 222 | result.apply(&mut actual); |
223 | actual | 223 | actual |
224 | }; | 224 | }; |
@@ -622,7 +622,7 @@ fn foo() { | |||
622 | let parse = SourceFile::parse(&before); | 622 | let parse = SourceFile::parse(&before); |
623 | let result = join_lines(&parse.tree(), sel); | 623 | let result = join_lines(&parse.tree(), sel); |
624 | let actual = { | 624 | let actual = { |
625 | let mut actual = before.to_string(); | 625 | let mut actual = before; |
626 | result.apply(&mut actual); | 626 | result.apply(&mut actual); |
627 | actual | 627 | actual |
628 | }; | 628 | }; |
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index a8b169e87..662da5a96 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs | |||
@@ -71,7 +71,7 @@ pub use crate::{ | |||
71 | diagnostics::{Diagnostic, DiagnosticsConfig, Fix, Severity}, | 71 | diagnostics::{Diagnostic, DiagnosticsConfig, Fix, Severity}, |
72 | display::navigation_target::NavigationTarget, | 72 | display::navigation_target::NavigationTarget, |
73 | expand_macro::ExpandedMacro, | 73 | expand_macro::ExpandedMacro, |
74 | file_structure::StructureNode, | 74 | file_structure::{StructureNode, StructureNodeKind}, |
75 | folding_ranges::{Fold, FoldKind}, | 75 | folding_ranges::{Fold, FoldKind}, |
76 | hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult}, | 76 | hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult}, |
77 | inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, | 77 | inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, |
@@ -101,7 +101,7 @@ pub use ide_db::{ | |||
101 | search::{ReferenceAccess, SearchScope}, | 101 | search::{ReferenceAccess, SearchScope}, |
102 | source_change::{FileSystemEdit, SourceChange}, | 102 | source_change::{FileSystemEdit, SourceChange}, |
103 | symbol_index::Query, | 103 | symbol_index::Query, |
104 | RootDatabase, | 104 | RootDatabase, SymbolKind, |
105 | }; | 105 | }; |
106 | pub use ide_ssr::SsrError; | 106 | pub use ide_ssr::SsrError; |
107 | pub use syntax::{TextRange, TextSize}; | 107 | pub use syntax::{TextRange, TextSize}; |
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index fef70533d..ec7c7686d 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs | |||
@@ -29,7 +29,7 @@ use crate::{display::TryToNav, FilePosition, NavigationTarget}; | |||
29 | 29 | ||
30 | #[derive(Debug, Clone)] | 30 | #[derive(Debug, Clone)] |
31 | pub struct ReferenceSearchResult { | 31 | pub struct ReferenceSearchResult { |
32 | pub declaration: Declaration, | 32 | pub declaration: Option<Declaration>, |
33 | pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>, | 33 | pub references: FxHashMap<FileId, Vec<(TextRange, Option<ReferenceAccess>)>>, |
34 | } | 34 | } |
35 | 35 | ||
@@ -91,10 +91,10 @@ pub(crate) fn find_all_refs( | |||
91 | _ => {} | 91 | _ => {} |
92 | } | 92 | } |
93 | } | 93 | } |
94 | let nav = def.try_to_nav(sema.db)?; | 94 | let declaration = def.try_to_nav(sema.db).map(|nav| { |
95 | let decl_range = nav.focus_or_full_range(); | 95 | let decl_range = nav.focus_or_full_range(); |
96 | 96 | Declaration { nav, access: decl_access(&def, &syntax, decl_range) } | |
97 | let declaration = Declaration { nav, access: decl_access(&def, &syntax, decl_range) }; | 97 | }); |
98 | let references = usages | 98 | let references = usages |
99 | .into_iter() | 99 | .into_iter() |
100 | .map(|(file_id, refs)| { | 100 | .map(|(file_id, refs)| { |
@@ -1004,8 +1004,7 @@ impl Foo { | |||
1004 | let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap(); | 1004 | let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap(); |
1005 | 1005 | ||
1006 | let mut actual = String::new(); | 1006 | let mut actual = String::new(); |
1007 | { | 1007 | if let Some(decl) = refs.declaration { |
1008 | let decl = refs.declaration; | ||
1009 | format_to!(actual, "{}", decl.nav.debug_render()); | 1008 | format_to!(actual, "{}", decl.nav.debug_render()); |
1010 | if let Some(access) = decl.access { | 1009 | if let Some(access) = decl.access { |
1011 | format_to!(actual, " {:?}", access) | 1010 | format_to!(actual, " {:?}", access) |
@@ -1258,4 +1257,17 @@ fn main() { | |||
1258 | "#]], | 1257 | "#]], |
1259 | ); | 1258 | ); |
1260 | } | 1259 | } |
1260 | |||
1261 | #[test] | ||
1262 | fn test_primitives() { | ||
1263 | check( | ||
1264 | r#" | ||
1265 | fn foo(_: bool) -> bo$0ol { true } | ||
1266 | "#, | ||
1267 | expect![[r#" | ||
1268 | FileId(0) 10..14 | ||
1269 | FileId(0) 19..23 | ||
1270 | "#]], | ||
1271 | ); | ||
1272 | } | ||
1261 | } | 1273 | } |
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 1e378279d..5340b638a 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -510,7 +510,8 @@ fn source_edit_from_def( | |||
510 | def: Definition, | 510 | def: Definition, |
511 | new_name: &str, | 511 | new_name: &str, |
512 | ) -> RenameResult<(FileId, TextEdit)> { | 512 | ) -> RenameResult<(FileId, TextEdit)> { |
513 | let nav = def.try_to_nav(sema.db).unwrap(); | 513 | let nav = |
514 | def.try_to_nav(sema.db).ok_or_else(|| format_err!("No references found at position"))?; | ||
514 | 515 | ||
515 | let mut replacement_text = String::new(); | 516 | let mut replacement_text = String::new(); |
516 | let mut repl_range = nav.focus_or_full_range(); | 517 | let mut repl_range = nav.focus_or_full_range(); |
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 27d35de5b..0c7a8fbf8 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs | |||
@@ -2,11 +2,13 @@ use std::fmt; | |||
2 | 2 | ||
3 | use ast::NameOwner; | 3 | use ast::NameOwner; |
4 | use cfg::CfgExpr; | 4 | use cfg::CfgExpr; |
5 | use either::Either; | ||
5 | use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics}; | 6 | use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics}; |
6 | use ide_assists::utils::test_related_attribute; | 7 | use ide_assists::utils::test_related_attribute; |
7 | use ide_db::{ | 8 | use ide_db::{ |
8 | base_db::{FilePosition, FileRange}, | 9 | base_db::{FilePosition, FileRange}, |
9 | defs::Definition, | 10 | defs::Definition, |
11 | helpers::visit_file_defs, | ||
10 | search::SearchScope, | 12 | search::SearchScope, |
11 | RootDatabase, SymbolKind, | 13 | RootDatabase, SymbolKind, |
12 | }; | 14 | }; |
@@ -102,13 +104,27 @@ impl Runnable { | |||
102 | // |=== | 104 | // |=== |
103 | pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { | 105 | pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { |
104 | let sema = Semantics::new(db); | 106 | let sema = Semantics::new(db); |
105 | let module = match sema.to_module_def(file_id) { | ||
106 | None => return Vec::new(), | ||
107 | Some(it) => it, | ||
108 | }; | ||
109 | 107 | ||
110 | let mut res = Vec::new(); | 108 | let mut res = Vec::new(); |
111 | runnables_mod(&sema, &mut res, module); | 109 | visit_file_defs(&sema, file_id, &mut |def| match def { |
110 | Either::Left(def) => { | ||
111 | let runnable = match def { | ||
112 | hir::ModuleDef::Module(it) => runnable_mod(&sema, it), | ||
113 | hir::ModuleDef::Function(it) => runnable_fn(&sema, it), | ||
114 | _ => None, | ||
115 | }; | ||
116 | res.extend(runnable.or_else(|| module_def_doctest(&sema, def))) | ||
117 | } | ||
118 | Either::Right(impl_) => { | ||
119 | res.extend(impl_.items(db).into_iter().filter_map(|assoc| match assoc { | ||
120 | hir::AssocItem::Function(it) => { | ||
121 | runnable_fn(&sema, it).or_else(|| module_def_doctest(&sema, it.into())) | ||
122 | } | ||
123 | hir::AssocItem::Const(it) => module_def_doctest(&sema, it.into()), | ||
124 | hir::AssocItem::TypeAlias(it) => module_def_doctest(&sema, it.into()), | ||
125 | })) | ||
126 | } | ||
127 | }); | ||
112 | res | 128 | res |
113 | } | 129 | } |
114 | 130 | ||
@@ -211,39 +227,6 @@ fn parent_test_module(sema: &Semantics<RootDatabase>, fn_def: &ast::Fn) -> Optio | |||
211 | }) | 227 | }) |
212 | } | 228 | } |
213 | 229 | ||
214 | fn runnables_mod(sema: &Semantics<RootDatabase>, acc: &mut Vec<Runnable>, module: hir::Module) { | ||
215 | acc.extend(module.declarations(sema.db).into_iter().filter_map(|def| { | ||
216 | let runnable = match def { | ||
217 | hir::ModuleDef::Module(it) => runnable_mod(&sema, it), | ||
218 | hir::ModuleDef::Function(it) => runnable_fn(&sema, it), | ||
219 | _ => None, | ||
220 | }; | ||
221 | runnable.or_else(|| module_def_doctest(&sema, def)) | ||
222 | })); | ||
223 | |||
224 | acc.extend(module.impl_defs(sema.db).into_iter().flat_map(|it| it.items(sema.db)).filter_map( | ||
225 | |def| match def { | ||
226 | hir::AssocItem::Function(it) => { | ||
227 | runnable_fn(&sema, it).or_else(|| module_def_doctest(&sema, it.into())) | ||
228 | } | ||
229 | hir::AssocItem::Const(it) => module_def_doctest(&sema, it.into()), | ||
230 | hir::AssocItem::TypeAlias(it) => module_def_doctest(&sema, it.into()), | ||
231 | }, | ||
232 | )); | ||
233 | |||
234 | for def in module.declarations(sema.db) { | ||
235 | if let hir::ModuleDef::Module(submodule) = def { | ||
236 | match submodule.definition_source(sema.db).value { | ||
237 | hir::ModuleSource::Module(_) => runnables_mod(sema, acc, submodule), | ||
238 | hir::ModuleSource::SourceFile(_) => { | ||
239 | cov_mark::hit!(dont_recurse_in_outline_submodules) | ||
240 | } | ||
241 | hir::ModuleSource::BlockExpr(_) => {} // inner items aren't runnable | ||
242 | } | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | |||
247 | pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) -> Option<Runnable> { | 230 | pub(crate) fn runnable_fn(sema: &Semantics<RootDatabase>, def: hir::Function) -> Option<Runnable> { |
248 | let func = def.source(sema.db)?; | 231 | let func = def.source(sema.db)?; |
249 | let name_string = def.name(sema.db).to_string(); | 232 | let name_string = def.name(sema.db).to_string(); |
@@ -1178,7 +1161,6 @@ mod tests { | |||
1178 | 1161 | ||
1179 | #[test] | 1162 | #[test] |
1180 | fn dont_recurse_in_outline_submodules() { | 1163 | fn dont_recurse_in_outline_submodules() { |
1181 | cov_mark::check!(dont_recurse_in_outline_submodules); | ||
1182 | check( | 1164 | check( |
1183 | r#" | 1165 | r#" |
1184 | //- /lib.rs | 1166 | //- /lib.rs |
diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs index e3c3aebac..a718faf63 100644 --- a/crates/ide/src/typing.rs +++ b/crates/ide/src/typing.rs | |||
@@ -145,9 +145,8 @@ mod tests { | |||
145 | use super::*; | 145 | use super::*; |
146 | 146 | ||
147 | fn do_type_char(char_typed: char, before: &str) -> Option<String> { | 147 | fn do_type_char(char_typed: char, before: &str) -> Option<String> { |
148 | let (offset, before) = extract_offset(before); | 148 | let (offset, mut before) = extract_offset(before); |
149 | let edit = TextEdit::insert(offset, char_typed.to_string()); | 149 | let edit = TextEdit::insert(offset, char_typed.to_string()); |
150 | let mut before = before.to_string(); | ||
151 | edit.apply(&mut before); | 150 | edit.apply(&mut before); |
152 | let parse = SourceFile::parse(&before); | 151 | let parse = SourceFile::parse(&before); |
153 | on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| { | 152 | on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| { |