diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 24 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/add_explicit_type.rs | 4 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | 177 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_db/src/fixture.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests/incremental.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/ast_id_map.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/db.rs | 1 | ||||
-rw-r--r-- | crates/ra_ide_db/src/change.rs | 12 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 10 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/traits.rs | 31 |
16 files changed, 253 insertions, 62 deletions
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 62dcb3808..543224232 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -623,6 +623,30 @@ fn process(map: HashMap<String, String>) {} | |||
623 | } | 623 | } |
624 | 624 | ||
625 | #[test] | 625 | #[test] |
626 | fn doctest_replace_unwrap_with_match() { | ||
627 | check( | ||
628 | "replace_unwrap_with_match", | ||
629 | r#####" | ||
630 | enum Result<T, E> { Ok(T), Err(E) } | ||
631 | fn main() { | ||
632 | let x: Result<i32, i32> = Result::Ok(92); | ||
633 | let y = x.<|>unwrap(); | ||
634 | } | ||
635 | "#####, | ||
636 | r#####" | ||
637 | enum Result<T, E> { Ok(T), Err(E) } | ||
638 | fn main() { | ||
639 | let x: Result<i32, i32> = Result::Ok(92); | ||
640 | let y = match x { | ||
641 | Ok(a) => a, | ||
642 | _ => unreachable!(), | ||
643 | }; | ||
644 | } | ||
645 | "#####, | ||
646 | ) | ||
647 | } | ||
648 | |||
649 | #[test] | ||
626 | fn doctest_split_import() { | 650 | fn doctest_split_import() { |
627 | check( | 651 | check( |
628 | "split_import", | 652 | "split_import", |
diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index a63ef48b1..d86d804b2 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs | |||
@@ -130,8 +130,8 @@ mod tests { | |||
130 | fn add_explicit_type_works_for_macro_call() { | 130 | fn add_explicit_type_works_for_macro_call() { |
131 | check_assist( | 131 | check_assist( |
132 | add_explicit_type, | 132 | add_explicit_type, |
133 | "macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }", | 133 | r"macro_rules! v { () => {0u64} } fn f() { let a<|> = v!(); }", |
134 | "macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }", | 134 | r"macro_rules! v { () => {0u64} } fn f() { let a<|>: u64 = v!(); }", |
135 | ); | 135 | ); |
136 | } | 136 | } |
137 | 137 | ||
diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs new file mode 100644 index 000000000..62cb7a763 --- /dev/null +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | |||
@@ -0,0 +1,177 @@ | |||
1 | use std::iter; | ||
2 | |||
3 | use ra_syntax::{ | ||
4 | ast::{self, make}, | ||
5 | AstNode, | ||
6 | }; | ||
7 | |||
8 | use crate::{Assist, AssistCtx, AssistId}; | ||
9 | use ast::edit::IndentLevel; | ||
10 | |||
11 | // Assist: replace_unwrap_with_match | ||
12 | // | ||
13 | // Replaces `unwrap` a `match` expression. Works for Result and Option. | ||
14 | // | ||
15 | // ``` | ||
16 | // enum Result<T, E> { Ok(T), Err(E) } | ||
17 | // fn main() { | ||
18 | // let x: Result<i32, i32> = Result::Ok(92); | ||
19 | // let y = x.<|>unwrap(); | ||
20 | // } | ||
21 | // ``` | ||
22 | // -> | ||
23 | // ``` | ||
24 | // enum Result<T, E> { Ok(T), Err(E) } | ||
25 | // fn main() { | ||
26 | // let x: Result<i32, i32> = Result::Ok(92); | ||
27 | // let y = match x { | ||
28 | // Ok(a) => a, | ||
29 | // _ => unreachable!(), | ||
30 | // }; | ||
31 | // } | ||
32 | // ``` | ||
33 | pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option<Assist> { | ||
34 | let method_call: ast::MethodCallExpr = ctx.find_node_at_offset()?; | ||
35 | let name = method_call.name_ref()?; | ||
36 | if name.text() != "unwrap" { | ||
37 | return None; | ||
38 | } | ||
39 | let caller = method_call.expr()?; | ||
40 | let ty = ctx.sema.type_of_expr(&caller)?; | ||
41 | |||
42 | let type_name = ty.as_adt()?.name(ctx.sema.db).to_string(); | ||
43 | |||
44 | for (unwrap_type, variant_name) in [("Result", "Ok"), ("Option", "Some")].iter() { | ||
45 | if &type_name == unwrap_type { | ||
46 | return ctx.add_assist( | ||
47 | AssistId("replace_unwrap_with_match"), | ||
48 | "Replace unwrap with match", | ||
49 | |edit| { | ||
50 | let ok_path = | ||
51 | make::path_unqualified(make::path_segment(make::name_ref(variant_name))); | ||
52 | let it = make::bind_pat(make::name("a")).into(); | ||
53 | let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); | ||
54 | |||
55 | let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); | ||
56 | let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); | ||
57 | |||
58 | let unreachable_call = make::unreachable_macro_call().into(); | ||
59 | let err_arm = make::match_arm( | ||
60 | iter::once(make::placeholder_pat().into()), | ||
61 | unreachable_call, | ||
62 | ); | ||
63 | |||
64 | let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); | ||
65 | let match_expr = make::expr_match(caller.clone(), match_arm_list); | ||
66 | let match_expr = | ||
67 | IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); | ||
68 | |||
69 | edit.target(method_call.syntax().text_range()); | ||
70 | edit.set_cursor(caller.syntax().text_range().start()); | ||
71 | edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); | ||
72 | }, | ||
73 | ); | ||
74 | } | ||
75 | } | ||
76 | None | ||
77 | } | ||
78 | |||
79 | #[cfg(test)] | ||
80 | mod tests { | ||
81 | use super::*; | ||
82 | use crate::helpers::{check_assist, check_assist_target}; | ||
83 | |||
84 | #[test] | ||
85 | fn test_replace_result_unwrap_with_match() { | ||
86 | check_assist( | ||
87 | replace_unwrap_with_match, | ||
88 | r" | ||
89 | enum Result<T, E> { Ok(T), Err(E) } | ||
90 | fn i<T>(a: T) -> T { a } | ||
91 | fn main() { | ||
92 | let x: Result<i32, i32> = Result::Ok(92); | ||
93 | let y = i(x).<|>unwrap(); | ||
94 | } | ||
95 | ", | ||
96 | r" | ||
97 | enum Result<T, E> { Ok(T), Err(E) } | ||
98 | fn i<T>(a: T) -> T { a } | ||
99 | fn main() { | ||
100 | let x: Result<i32, i32> = Result::Ok(92); | ||
101 | let y = <|>match i(x) { | ||
102 | Ok(a) => a, | ||
103 | _ => unreachable!(), | ||
104 | }; | ||
105 | } | ||
106 | ", | ||
107 | ) | ||
108 | } | ||
109 | |||
110 | #[test] | ||
111 | fn test_replace_option_unwrap_with_match() { | ||
112 | check_assist( | ||
113 | replace_unwrap_with_match, | ||
114 | r" | ||
115 | enum Option<T> { Some(T), None } | ||
116 | fn i<T>(a: T) -> T { a } | ||
117 | fn main() { | ||
118 | let x = Option::Some(92); | ||
119 | let y = i(x).<|>unwrap(); | ||
120 | } | ||
121 | ", | ||
122 | r" | ||
123 | enum Option<T> { Some(T), None } | ||
124 | fn i<T>(a: T) -> T { a } | ||
125 | fn main() { | ||
126 | let x = Option::Some(92); | ||
127 | let y = <|>match i(x) { | ||
128 | Some(a) => a, | ||
129 | _ => unreachable!(), | ||
130 | }; | ||
131 | } | ||
132 | ", | ||
133 | ); | ||
134 | } | ||
135 | |||
136 | #[test] | ||
137 | fn test_replace_result_unwrap_with_match_chaining() { | ||
138 | check_assist( | ||
139 | replace_unwrap_with_match, | ||
140 | r" | ||
141 | enum Result<T, E> { Ok(T), Err(E) } | ||
142 | fn i<T>(a: T) -> T { a } | ||
143 | fn main() { | ||
144 | let x: Result<i32, i32> = Result::Ok(92); | ||
145 | let y = i(x).<|>unwrap().count_zeroes(); | ||
146 | } | ||
147 | ", | ||
148 | r" | ||
149 | enum Result<T, E> { Ok(T), Err(E) } | ||
150 | fn i<T>(a: T) -> T { a } | ||
151 | fn main() { | ||
152 | let x: Result<i32, i32> = Result::Ok(92); | ||
153 | let y = <|>match i(x) { | ||
154 | Ok(a) => a, | ||
155 | _ => unreachable!(), | ||
156 | }.count_zeroes(); | ||
157 | } | ||
158 | ", | ||
159 | ) | ||
160 | } | ||
161 | |||
162 | #[test] | ||
163 | fn replace_unwrap_with_match_target() { | ||
164 | check_assist_target( | ||
165 | replace_unwrap_with_match, | ||
166 | r" | ||
167 | enum Option<T> { Some(T), None } | ||
168 | fn i<T>(a: T) -> T { a } | ||
169 | fn main() { | ||
170 | let x = Option::Some(92); | ||
171 | let y = i(x).<|>unwrap(); | ||
172 | } | ||
173 | ", | ||
174 | r"i(x).unwrap()", | ||
175 | ); | ||
176 | } | ||
177 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index bcc9b3f10..becd5e99d 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -119,6 +119,7 @@ mod handlers { | |||
119 | mod remove_mut; | 119 | mod remove_mut; |
120 | mod replace_if_let_with_match; | 120 | mod replace_if_let_with_match; |
121 | mod replace_qualified_name_with_use; | 121 | mod replace_qualified_name_with_use; |
122 | mod replace_unwrap_with_match; | ||
122 | mod split_import; | 123 | mod split_import; |
123 | 124 | ||
124 | pub(crate) fn all() -> &'static [AssistHandler] { | 125 | pub(crate) fn all() -> &'static [AssistHandler] { |
@@ -154,6 +155,7 @@ mod handlers { | |||
154 | remove_mut::remove_mut, | 155 | remove_mut::remove_mut, |
155 | replace_if_let_with_match::replace_if_let_with_match, | 156 | replace_if_let_with_match::replace_if_let_with_match, |
156 | replace_qualified_name_with_use::replace_qualified_name_with_use, | 157 | replace_qualified_name_with_use::replace_qualified_name_with_use, |
158 | replace_unwrap_with_match::replace_unwrap_with_match, | ||
157 | split_import::split_import, | 159 | split_import::split_import, |
158 | ] | 160 | ] |
159 | } | 161 | } |
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 9d992886e..7777ce81e 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -28,9 +28,9 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { | |||
28 | db | 28 | db |
29 | } | 29 | } |
30 | 30 | ||
31 | fn with_position(fixture: &str) -> (Self, FilePosition) { | 31 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { |
32 | let mut db = Self::default(); | 32 | let mut db = Self::default(); |
33 | let pos = with_files(&mut db, fixture); | 33 | let pos = with_files(&mut db, ra_fixture); |
34 | (db, pos.unwrap()) | 34 | (db, pos.unwrap()) |
35 | } | 35 | } |
36 | 36 | ||
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index fcba95091..ec931b34f 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -10,15 +10,16 @@ pub use hir_def::db::{ | |||
10 | TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, | 10 | TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, |
11 | }; | 11 | }; |
12 | pub use hir_expand::db::{ | 12 | pub use hir_expand::db::{ |
13 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternMacroQuery, MacroArgQuery, MacroDefQuery, | 13 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery, |
14 | MacroExpandQuery, ParseMacroQuery, | 14 | MacroArgQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, |
15 | }; | 15 | }; |
16 | pub use hir_ty::db::{ | 16 | pub use hir_ty::db::{ |
17 | AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, | 17 | AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, |
18 | GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, | 18 | GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, |
19 | HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, ImplsForTraitQuery, | 19 | HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, ImplsForTraitQuery, |
20 | ImplsInCrateQuery, InferQueryQuery, InternAssocTyValueQuery, InternChalkImplQuery, | 20 | ImplsInCrateQuery, InferQueryQuery, InternAssocTyValueQuery, InternChalkImplQuery, |
21 | InternTypeCtorQuery, StructDatumQuery, TraitDatumQuery, TraitSolveQuery, TyQuery, ValueTyQuery, | 21 | InternTypeCtorQuery, InternTypeParamIdQuery, StructDatumQuery, TraitDatumQuery, |
22 | TraitSolveQuery, TyQuery, ValueTyQuery, | ||
22 | }; | 23 | }; |
23 | 24 | ||
24 | #[test] | 25 | #[test] |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 3cf0c66ea..e8443dde8 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -563,7 +563,8 @@ impl ExprCollector<'_> { | |||
563 | ast::ModuleItem::ImplDef(_) | 563 | ast::ModuleItem::ImplDef(_) |
564 | | ast::ModuleItem::UseItem(_) | 564 | | ast::ModuleItem::UseItem(_) |
565 | | ast::ModuleItem::ExternCrateItem(_) | 565 | | ast::ModuleItem::ExternCrateItem(_) |
566 | | ast::ModuleItem::Module(_) => continue, | 566 | | ast::ModuleItem::Module(_) |
567 | | ast::ModuleItem::MacroCall(_) => continue, | ||
567 | }; | 568 | }; |
568 | self.body.item_scope.define_def(def); | 569 | self.body.item_scope.define_def(def); |
569 | if let Some(name) = name { | 570 | if let Some(name) = name { |
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 7f8c1ea21..5dc7395f5 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -48,6 +48,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> { | |||
48 | fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>; | 48 | fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>; |
49 | 49 | ||
50 | #[salsa::invoke(crate_def_map_wait)] | 50 | #[salsa::invoke(crate_def_map_wait)] |
51 | #[salsa::transparent] | ||
51 | fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>; | 52 | fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>; |
52 | 53 | ||
53 | #[salsa::invoke(CrateDefMap::crate_def_map_query)] | 54 | #[salsa::invoke(CrateDefMap::crate_def_map_query)] |
@@ -109,12 +110,6 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> { | |||
109 | fn documentation(&self, def: AttrDefId) -> Option<Documentation>; | 110 | fn documentation(&self, def: AttrDefId) -> Option<Documentation>; |
110 | } | 111 | } |
111 | 112 | ||
112 | // impl<T: DefDatabase> Upcast<dyn AstDatabase> for T { | ||
113 | // fn upcast(&self) -> &dyn AstDatabase { | ||
114 | // &*self | ||
115 | // } | ||
116 | // } | ||
117 | |||
118 | fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { | 113 | fn crate_def_map_wait(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { |
119 | let _p = profile("crate_def_map:wait"); | 114 | let _p = profile("crate_def_map:wait"); |
120 | db.crate_def_map_query(krate) | 115 | db.crate_def_map_query(krate) |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 1631e87b8..8f190e7f9 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -209,11 +209,8 @@ impl RawItemsCollector { | |||
209 | current_module: Option<Idx<ModuleData>>, | 209 | current_module: Option<Idx<ModuleData>>, |
210 | body: impl ast::ModuleItemOwner, | 210 | body: impl ast::ModuleItemOwner, |
211 | ) { | 211 | ) { |
212 | for item_or_macro in body.items_with_macros() { | 212 | for item in body.items() { |
213 | match item_or_macro { | 213 | self.add_item(current_module, item) |
214 | ast::ItemOrMacro::Macro(m) => self.add_macro(current_module, m), | ||
215 | ast::ItemOrMacro::Item(item) => self.add_item(current_module, item), | ||
216 | } | ||
217 | } | 214 | } |
218 | } | 215 | } |
219 | 216 | ||
@@ -265,6 +262,10 @@ impl RawItemsCollector { | |||
265 | ast::ModuleItem::StaticDef(it) => { | 262 | ast::ModuleItem::StaticDef(it) => { |
266 | (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name()) | 263 | (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name()) |
267 | } | 264 | } |
265 | ast::ModuleItem::MacroCall(it) => { | ||
266 | self.add_macro(current_module, it); | ||
267 | return; | ||
268 | } | ||
268 | }; | 269 | }; |
269 | if let Some(name) = name { | 270 | if let Some(name) = name { |
270 | let name = name.as_name(); | 271 | let name = name.as_name(); |
diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs index 83f429c29..496fc6b08 100644 --- a/crates/ra_hir_def/src/nameres/tests/incremental.rs +++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs | |||
@@ -4,8 +4,8 @@ use ra_db::SourceDatabaseExt; | |||
4 | 4 | ||
5 | use super::*; | 5 | use super::*; |
6 | 6 | ||
7 | fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) { | 7 | fn check_def_map_is_not_recomputed(ra_fixture_initial: &str, ra_fixture_change: &str) { |
8 | let (mut db, pos) = TestDB::with_position(initial); | 8 | let (mut db, pos) = TestDB::with_position(ra_fixture_initial); |
9 | let krate = db.test_crate(); | 9 | let krate = db.test_crate(); |
10 | { | 10 | { |
11 | let events = db.log_executed(|| { | 11 | let events = db.log_executed(|| { |
@@ -13,7 +13,7 @@ fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) { | |||
13 | }); | 13 | }); |
14 | assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) | 14 | assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events) |
15 | } | 15 | } |
16 | db.set_file_text(pos.file_id, Arc::new(file_change.to_string())); | 16 | db.set_file_text(pos.file_id, Arc::new(ra_fixture_change.to_string())); |
17 | 17 | ||
18 | { | 18 | { |
19 | let events = db.log_executed(|| { | 19 | let events = db.log_executed(|| { |
@@ -26,7 +26,7 @@ fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) { | |||
26 | #[test] | 26 | #[test] |
27 | fn typing_inside_a_function_should_not_invalidate_def_map() { | 27 | fn typing_inside_a_function_should_not_invalidate_def_map() { |
28 | check_def_map_is_not_recomputed( | 28 | check_def_map_is_not_recomputed( |
29 | " | 29 | r" |
30 | //- /lib.rs | 30 | //- /lib.rs |
31 | mod foo;<|> | 31 | mod foo;<|> |
32 | 32 | ||
@@ -41,7 +41,7 @@ fn typing_inside_a_function_should_not_invalidate_def_map() { | |||
41 | //- /foo/bar.rs | 41 | //- /foo/bar.rs |
42 | pub struct Baz; | 42 | pub struct Baz; |
43 | ", | 43 | ", |
44 | " | 44 | r" |
45 | mod foo; | 45 | mod foo; |
46 | 46 | ||
47 | use crate::foo::bar::Baz; | 47 | use crate::foo::bar::Baz; |
@@ -54,7 +54,7 @@ fn typing_inside_a_function_should_not_invalidate_def_map() { | |||
54 | #[test] | 54 | #[test] |
55 | fn adding_inner_items_should_not_invalidate_def_map() { | 55 | fn adding_inner_items_should_not_invalidate_def_map() { |
56 | check_def_map_is_not_recomputed( | 56 | check_def_map_is_not_recomputed( |
57 | " | 57 | r" |
58 | //- /lib.rs | 58 | //- /lib.rs |
59 | struct S { a: i32} | 59 | struct S { a: i32} |
60 | enum E { A } | 60 | enum E { A } |
@@ -72,7 +72,7 @@ fn adding_inner_items_should_not_invalidate_def_map() { | |||
72 | //- /foo/bar.rs | 72 | //- /foo/bar.rs |
73 | pub struct Baz; | 73 | pub struct Baz; |
74 | ", | 74 | ", |
75 | " | 75 | r" |
76 | struct S { a: i32, b: () } | 76 | struct S { a: i32, b: () } |
77 | enum E { A, B } | 77 | enum E { A, B } |
78 | trait T { | 78 | trait T { |
@@ -92,7 +92,7 @@ fn adding_inner_items_should_not_invalidate_def_map() { | |||
92 | #[test] | 92 | #[test] |
93 | fn typing_inside_a_macro_should_not_invalidate_def_map() { | 93 | fn typing_inside_a_macro_should_not_invalidate_def_map() { |
94 | let (mut db, pos) = TestDB::with_position( | 94 | let (mut db, pos) = TestDB::with_position( |
95 | " | 95 | r" |
96 | //- /lib.rs | 96 | //- /lib.rs |
97 | macro_rules! m { | 97 | macro_rules! m { |
98 | ($ident:ident) => { | 98 | ($ident:ident) => { |
diff --git a/crates/ra_hir_expand/src/ast_id_map.rs b/crates/ra_hir_expand/src/ast_id_map.rs index a6644d55f..5643ecdce 100644 --- a/crates/ra_hir_expand/src/ast_id_map.rs +++ b/crates/ra_hir_expand/src/ast_id_map.rs | |||
@@ -68,8 +68,6 @@ impl AstIdMap { | |||
68 | bfs(node, |it| { | 68 | bfs(node, |it| { |
69 | if let Some(module_item) = ast::ModuleItem::cast(it.clone()) { | 69 | if let Some(module_item) = ast::ModuleItem::cast(it.clone()) { |
70 | res.alloc(module_item.syntax()); | 70 | res.alloc(module_item.syntax()); |
71 | } else if let Some(macro_call) = ast::MacroCall::cast(it) { | ||
72 | res.alloc(macro_call.syntax()); | ||
73 | } | 71 | } |
74 | }); | 72 | }); |
75 | res | 73 | res |
diff --git a/crates/ra_hir_ty/src/db.rs b/crates/ra_hir_ty/src/db.rs index 11fc2ac3d..1462b053f 100644 --- a/crates/ra_hir_ty/src/db.rs +++ b/crates/ra_hir_ty/src/db.rs | |||
@@ -22,6 +22,7 @@ use hir_expand::name::Name; | |||
22 | #[salsa::requires(salsa::Database)] | 22 | #[salsa::requires(salsa::Database)] |
23 | pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> { | 23 | pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> { |
24 | #[salsa::invoke(infer_wait)] | 24 | #[salsa::invoke(infer_wait)] |
25 | #[salsa::transparent] | ||
25 | fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>; | 26 | fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>; |
26 | 27 | ||
27 | #[salsa::invoke(crate::infer::infer_query)] | 28 | #[salsa::invoke(crate::infer::infer_query)] |
diff --git a/crates/ra_ide_db/src/change.rs b/crates/ra_ide_db/src/change.rs index 628cf6416..8446ef88e 100644 --- a/crates/ra_ide_db/src/change.rs +++ b/crates/ra_ide_db/src/change.rs | |||
@@ -311,6 +311,7 @@ impl RootDatabase { | |||
311 | hir::db::MacroDefQuery | 311 | hir::db::MacroDefQuery |
312 | hir::db::ParseMacroQuery | 312 | hir::db::ParseMacroQuery |
313 | hir::db::MacroExpandQuery | 313 | hir::db::MacroExpandQuery |
314 | hir::db::InternEagerExpansionQuery | ||
314 | 315 | ||
315 | // DefDatabase | 316 | // DefDatabase |
316 | hir::db::RawItemsQuery | 317 | hir::db::RawItemsQuery |
@@ -359,14 +360,21 @@ impl RootDatabase { | |||
359 | hir::db::ImplsInCrateQuery | 360 | hir::db::ImplsInCrateQuery |
360 | hir::db::ImplsForTraitQuery | 361 | hir::db::ImplsForTraitQuery |
361 | hir::db::InternTypeCtorQuery | 362 | hir::db::InternTypeCtorQuery |
363 | hir::db::InternTypeParamIdQuery | ||
362 | hir::db::InternChalkImplQuery | 364 | hir::db::InternChalkImplQuery |
363 | hir::db::InternAssocTyValueQuery | 365 | hir::db::InternAssocTyValueQuery |
364 | hir::db::AssociatedTyDataQuery | 366 | hir::db::AssociatedTyDataQuery |
365 | hir::db::AssociatedTyValueQuery | ||
366 | hir::db::TraitSolveQuery | ||
367 | hir::db::TraitDatumQuery | 367 | hir::db::TraitDatumQuery |
368 | hir::db::StructDatumQuery | 368 | hir::db::StructDatumQuery |
369 | hir::db::ImplDatumQuery | 369 | hir::db::ImplDatumQuery |
370 | hir::db::AssociatedTyValueQuery | ||
371 | hir::db::TraitSolveQuery | ||
372 | |||
373 | // SymbolsDatabase | ||
374 | crate::symbol_index::FileSymbolsQuery | ||
375 | |||
376 | // LineIndexDatabase | ||
377 | crate::LineIndexQuery | ||
370 | ]; | 378 | ]; |
371 | acc.sort_by_key(|it| std::cmp::Reverse(it.1)); | 379 | acc.sort_by_key(|it| std::cmp::Reverse(it.1)); |
372 | acc | 380 | acc |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 002f453cd..7204ca5b1 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -4135,6 +4135,7 @@ pub enum ModuleItem { | |||
4135 | ConstDef(ConstDef), | 4135 | ConstDef(ConstDef), |
4136 | StaticDef(StaticDef), | 4136 | StaticDef(StaticDef), |
4137 | Module(Module), | 4137 | Module(Module), |
4138 | MacroCall(MacroCall), | ||
4138 | } | 4139 | } |
4139 | impl From<StructDef> for ModuleItem { | 4140 | impl From<StructDef> for ModuleItem { |
4140 | fn from(node: StructDef) -> ModuleItem { | 4141 | fn from(node: StructDef) -> ModuleItem { |
@@ -4196,6 +4197,11 @@ impl From<Module> for ModuleItem { | |||
4196 | ModuleItem::Module(node) | 4197 | ModuleItem::Module(node) |
4197 | } | 4198 | } |
4198 | } | 4199 | } |
4200 | impl From<MacroCall> for ModuleItem { | ||
4201 | fn from(node: MacroCall) -> ModuleItem { | ||
4202 | ModuleItem::MacroCall(node) | ||
4203 | } | ||
4204 | } | ||
4199 | impl std::fmt::Display for ModuleItem { | 4205 | impl std::fmt::Display for ModuleItem { |
4200 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | 4206 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
4201 | std::fmt::Display::fmt(self.syntax(), f) | 4207 | std::fmt::Display::fmt(self.syntax(), f) |
@@ -4205,7 +4211,7 @@ impl AstNode for ModuleItem { | |||
4205 | fn can_cast(kind: SyntaxKind) -> bool { | 4211 | fn can_cast(kind: SyntaxKind) -> bool { |
4206 | match kind { | 4212 | match kind { |
4207 | STRUCT_DEF | UNION_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_DEF | 4213 | STRUCT_DEF | UNION_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | IMPL_DEF |
4208 | | USE_ITEM | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE => true, | 4214 | | USE_ITEM | EXTERN_CRATE_ITEM | CONST_DEF | STATIC_DEF | MODULE | MACRO_CALL => true, |
4209 | _ => false, | 4215 | _ => false, |
4210 | } | 4216 | } |
4211 | } | 4217 | } |
@@ -4223,6 +4229,7 @@ impl AstNode for ModuleItem { | |||
4223 | CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }), | 4229 | CONST_DEF => ModuleItem::ConstDef(ConstDef { syntax }), |
4224 | STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }), | 4230 | STATIC_DEF => ModuleItem::StaticDef(StaticDef { syntax }), |
4225 | MODULE => ModuleItem::Module(Module { syntax }), | 4231 | MODULE => ModuleItem::Module(Module { syntax }), |
4232 | MACRO_CALL => ModuleItem::MacroCall(MacroCall { syntax }), | ||
4226 | _ => return None, | 4233 | _ => return None, |
4227 | }; | 4234 | }; |
4228 | Some(res) | 4235 | Some(res) |
@@ -4241,6 +4248,7 @@ impl AstNode for ModuleItem { | |||
4241 | ModuleItem::ConstDef(it) => &it.syntax, | 4248 | ModuleItem::ConstDef(it) => &it.syntax, |
4242 | ModuleItem::StaticDef(it) => &it.syntax, | 4249 | ModuleItem::StaticDef(it) => &it.syntax, |
4243 | ModuleItem::Module(it) => &it.syntax, | 4250 | ModuleItem::Module(it) => &it.syntax, |
4251 | ModuleItem::MacroCall(it) => &it.syntax, | ||
4244 | } | 4252 | } |
4245 | } | 4253 | } |
4246 | } | 4254 | } |
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 1145b69e8..e29600439 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -250,6 +250,10 @@ pub fn token(kind: SyntaxKind) -> SyntaxToken { | |||
250 | .unwrap_or_else(|| panic!("unhandled token: {:?}", kind)) | 250 | .unwrap_or_else(|| panic!("unhandled token: {:?}", kind)) |
251 | } | 251 | } |
252 | 252 | ||
253 | pub fn unreachable_macro_call() -> ast::MacroCall { | ||
254 | ast_from_text(&format!("unreachable!()")) | ||
255 | } | ||
256 | |||
253 | fn ast_from_text<N: AstNode>(text: &str) -> N { | 257 | fn ast_from_text<N: AstNode>(text: &str) -> N { |
254 | let parse = SourceFile::parse(text); | 258 | let parse = SourceFile::parse(text); |
255 | let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); | 259 | let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); |
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index f8cf1e3eb..576378306 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs | |||
@@ -6,8 +6,7 @@ use itertools::Itertools; | |||
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | ast::{self, child_opt, children, AstChildren, AstNode, AstToken}, | 8 | ast::{self, child_opt, children, AstChildren, AstNode, AstToken}, |
9 | match_ast, | 9 | syntax_node::SyntaxElementChildren, |
10 | syntax_node::{SyntaxElementChildren, SyntaxNodeChildren}, | ||
11 | }; | 10 | }; |
12 | 11 | ||
13 | pub trait TypeAscriptionOwner: AstNode { | 12 | pub trait TypeAscriptionOwner: AstNode { |
@@ -46,38 +45,10 @@ pub trait FnDefOwner: AstNode { | |||
46 | } | 45 | } |
47 | } | 46 | } |
48 | 47 | ||
49 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
50 | pub enum ItemOrMacro { | ||
51 | Item(ast::ModuleItem), | ||
52 | Macro(ast::MacroCall), | ||
53 | } | ||
54 | |||
55 | pub trait ModuleItemOwner: AstNode { | 48 | pub trait ModuleItemOwner: AstNode { |
56 | fn items(&self) -> AstChildren<ast::ModuleItem> { | 49 | fn items(&self) -> AstChildren<ast::ModuleItem> { |
57 | children(self) | 50 | children(self) |
58 | } | 51 | } |
59 | fn items_with_macros(&self) -> ItemOrMacroIter { | ||
60 | ItemOrMacroIter(self.syntax().children()) | ||
61 | } | ||
62 | } | ||
63 | |||
64 | #[derive(Debug)] | ||
65 | pub struct ItemOrMacroIter(SyntaxNodeChildren); | ||
66 | |||
67 | impl Iterator for ItemOrMacroIter { | ||
68 | type Item = ItemOrMacro; | ||
69 | fn next(&mut self) -> Option<ItemOrMacro> { | ||
70 | loop { | ||
71 | let n = self.0.next()?; | ||
72 | match_ast! { | ||
73 | match n { | ||
74 | ast::ModuleItem(it) => { return Some(ItemOrMacro::Item(it)) }, | ||
75 | ast::MacroCall(it) => { return Some(ItemOrMacro::Macro(it)) }, | ||
76 | _ => {}, | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | } | 52 | } |
82 | 53 | ||
83 | pub trait TypeParamsOwner: AstNode { | 54 | pub trait TypeParamsOwner: AstNode { |