aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/completion/complete_unqualified_path.rs211
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs13
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs2
-rw-r--r--crates/ra_ide/src/runnables.rs35
4 files changed, 251 insertions, 10 deletions
diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs
index ad5fdcc4e..ad00154a3 100644
--- a/crates/ra_ide/src/completion/complete_unqualified_path.rs
+++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs
@@ -4,20 +4,23 @@ use hir::ScopeDef;
4use test_utils::tested_by; 4use test_utils::tested_by;
5 5
6use crate::completion::{CompletionContext, Completions}; 6use crate::completion::{CompletionContext, Completions};
7use hir::{Adt, ModuleDef};
7use ra_syntax::AstNode; 8use ra_syntax::AstNode;
8 9
9pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { 10pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
10 if !ctx.is_trivial_path { 11 if (!ctx.is_trivial_path && !ctx.is_pat_binding_or_const)
11 return;
12 }
13
14 if ctx.is_pat_binding_or_const
15 || ctx.record_lit_syntax.is_some() 12 || ctx.record_lit_syntax.is_some()
16 || ctx.record_pat_syntax.is_some() 13 || ctx.record_pat_syntax.is_some()
17 { 14 {
18 return; 15 return;
19 } 16 }
20 17
18 complete_enum_variants(acc, ctx);
19
20 if ctx.is_pat_binding_or_const {
21 return;
22 }
23
21 ctx.scope().process_all_names(&mut |name, res| { 24 ctx.scope().process_all_names(&mut |name, res| {
22 if ctx.use_item_syntax.is_some() { 25 if ctx.use_item_syntax.is_some() {
23 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) { 26 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
@@ -31,6 +34,24 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
31 }); 34 });
32} 35}
33 36
37fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext) {
38 if let Some(ty) = ctx.expected_type_of(&ctx.token.parent()) {
39 if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
40 let variants = enum_data.variants(ctx.db);
41 let module = enum_data.module(ctx.db);
42 for variant in variants {
43 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
44 // Variants with trivial paths are already added by the existing completion logic,
45 // so we should avoid adding these twice
46 if path.segments.len() > 1 {
47 acc.add_enum_variant(ctx, variant, Some(path.to_string()));
48 }
49 }
50 }
51 }
52 }
53}
54
34#[cfg(test)] 55#[cfg(test)]
35mod tests { 56mod tests {
36 use insta::assert_debug_snapshot; 57 use insta::assert_debug_snapshot;
@@ -82,7 +103,7 @@ mod tests {
82 } 103 }
83 " 104 "
84 ), 105 ),
85 @r###"[]"### 106 @"[]"
86 ); 107 );
87 } 108 }
88 109
@@ -1109,4 +1130,182 @@ mod tests {
1109 "### 1130 "###
1110 ); 1131 );
1111 } 1132 }
1133 #[test]
1134 fn completes_enum_variant_matcharm() {
1135 assert_debug_snapshot!(
1136 do_reference_completion(
1137 r"
1138 enum Foo {
1139 Bar,
1140 Baz,
1141 Quux
1142 }
1143
1144 fn main() {
1145 let foo = Foo::Quux;
1146
1147 match foo {
1148 Qu<|>
1149 }
1150 }
1151 "
1152 ),
1153 @r###"
1154 [
1155 CompletionItem {
1156 label: "Foo",
1157 source_range: [248; 250),
1158 delete: [248; 250),
1159 insert: "Foo",
1160 kind: Enum,
1161 },
1162 CompletionItem {
1163 label: "Foo::Bar",
1164 source_range: [248; 250),
1165 delete: [248; 250),
1166 insert: "Foo::Bar",
1167 kind: EnumVariant,
1168 detail: "()",
1169 },
1170 CompletionItem {
1171 label: "Foo::Baz",
1172 source_range: [248; 250),
1173 delete: [248; 250),
1174 insert: "Foo::Baz",
1175 kind: EnumVariant,
1176 detail: "()",
1177 },
1178 CompletionItem {
1179 label: "Foo::Quux",
1180 source_range: [248; 250),
1181 delete: [248; 250),
1182 insert: "Foo::Quux",
1183 kind: EnumVariant,
1184 detail: "()",
1185 },
1186 ]
1187 "###
1188 )
1189 }
1190
1191 #[test]
1192 fn completes_enum_variant_iflet() {
1193 assert_debug_snapshot!(
1194 do_reference_completion(
1195 r"
1196 enum Foo {
1197 Bar,
1198 Baz,
1199 Quux
1200 }
1201
1202 fn main() {
1203 let foo = Foo::Quux;
1204
1205 if let Qu<|> = foo {
1206
1207 }
1208 }
1209 "
1210 ),
1211 @r###"
1212 [
1213 CompletionItem {
1214 label: "Foo",
1215 source_range: [219; 221),
1216 delete: [219; 221),
1217 insert: "Foo",
1218 kind: Enum,
1219 },
1220 CompletionItem {
1221 label: "Foo::Bar",
1222 source_range: [219; 221),
1223 delete: [219; 221),
1224 insert: "Foo::Bar",
1225 kind: EnumVariant,
1226 detail: "()",
1227 },
1228 CompletionItem {
1229 label: "Foo::Baz",
1230 source_range: [219; 221),
1231 delete: [219; 221),
1232 insert: "Foo::Baz",
1233 kind: EnumVariant,
1234 detail: "()",
1235 },
1236 CompletionItem {
1237 label: "Foo::Quux",
1238 source_range: [219; 221),
1239 delete: [219; 221),
1240 insert: "Foo::Quux",
1241 kind: EnumVariant,
1242 detail: "()",
1243 },
1244 ]
1245 "###
1246 )
1247 }
1248
1249 #[test]
1250 fn completes_enum_variant_basic_expr() {
1251 assert_debug_snapshot!(
1252 do_reference_completion(
1253 r"
1254 enum Foo {
1255 Bar,
1256 Baz,
1257 Quux
1258 }
1259
1260 fn main() {
1261 let foo: Foo = Q<|>
1262 }
1263 "
1264 ),
1265 @r###"
1266 [
1267 CompletionItem {
1268 label: "Foo",
1269 source_range: [185; 186),
1270 delete: [185; 186),
1271 insert: "Foo",
1272 kind: Enum,
1273 },
1274 CompletionItem {
1275 label: "Foo::Bar",
1276 source_range: [185; 186),
1277 delete: [185; 186),
1278 insert: "Foo::Bar",
1279 kind: EnumVariant,
1280 detail: "()",
1281 },
1282 CompletionItem {
1283 label: "Foo::Baz",
1284 source_range: [185; 186),
1285 delete: [185; 186),
1286 insert: "Foo::Baz",
1287 kind: EnumVariant,
1288 detail: "()",
1289 },
1290 CompletionItem {
1291 label: "Foo::Quux",
1292 source_range: [185; 186),
1293 delete: [185; 186),
1294 insert: "Foo::Quux",
1295 kind: EnumVariant,
1296 detail: "()",
1297 },
1298 CompletionItem {
1299 label: "main()",
1300 source_range: [185; 186),
1301 delete: [185; 186),
1302 insert: "main()$0",
1303 kind: Function,
1304 lookup: "main",
1305 detail: "fn main()",
1306 },
1307 ]
1308 "###
1309 )
1310 }
1112} 1311}
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs
index 46e243192..dd7c8a873 100644
--- a/crates/ra_ide/src/completion/completion_context.rs
+++ b/crates/ra_ide/src/completion/completion_context.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{Semantics, SemanticsScope}; 3use hir::{Semantics, SemanticsScope, Type};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::{ 6use ra_syntax::{
@@ -172,6 +172,17 @@ impl<'a> CompletionContext<'a> {
172 self.sema.scope_at_offset(&self.token.parent(), self.offset) 172 self.sema.scope_at_offset(&self.token.parent(), self.offset)
173 } 173 }
174 174
175 pub(crate) fn expected_type_of(&self, node: &SyntaxNode) -> Option<Type> {
176 for ancestor in node.ancestors() {
177 if let Some(pat) = ast::Pat::cast(ancestor.clone()) {
178 return self.sema.type_of_pat(&pat);
179 } else if let Some(expr) = ast::Expr::cast(ancestor) {
180 return self.sema.type_of_expr(&expr);
181 }
182 }
183 None
184 }
185
175 fn fill( 186 fn fill(
176 &mut self, 187 &mut self,
177 original_file: &SyntaxNode, 188 original_file: &SyntaxNode,
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs
index 6289f53f3..67bc9c31b 100644
--- a/crates/ra_ide/src/display/navigation_target.rs
+++ b/crates/ra_ide/src/display/navigation_target.rs
@@ -176,7 +176,7 @@ impl ToNav for FileSymbol {
176 file_id: self.file_id, 176 file_id: self.file_id,
177 name: self.name.clone(), 177 name: self.name.clone(),
178 kind: self.kind, 178 kind: self.kind,
179 full_range: self.ptr.range(), 179 full_range: self.range,
180 focus_range: self.name_range, 180 focus_range: self.name_range,
181 container_name: self.container_name.clone(), 181 container_name: self.container_name.clone(),
182 description: description_from_symbol(db, self), 182 description: description_from_symbol(db, self),
diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs
index 9433f3a24..05a66e03c 100644
--- a/crates/ra_ide/src/runnables.rs
+++ b/crates/ra_ide/src/runnables.rs
@@ -34,7 +34,7 @@ impl Display for TestId {
34 34
35#[derive(Debug)] 35#[derive(Debug)]
36pub enum RunnableKind { 36pub enum RunnableKind {
37 Test { test_id: TestId }, 37 Test { test_id: TestId, attr: TestAttr },
38 TestMod { path: String }, 38 TestMod { path: String },
39 Bench { test_id: TestId }, 39 Bench { test_id: TestId },
40 Bin, 40 Bin,
@@ -77,7 +77,8 @@ fn runnable_fn(sema: &Semantics<RootDatabase>, fn_def: ast::FnDef) -> Option<Run
77 }; 77 };
78 78
79 if has_test_related_attribute(&fn_def) { 79 if has_test_related_attribute(&fn_def) {
80 RunnableKind::Test { test_id } 80 let attr = TestAttr::from_fn(&fn_def);
81 RunnableKind::Test { test_id, attr }
81 } else if fn_def.has_atom_attr("bench") { 82 } else if fn_def.has_atom_attr("bench") {
82 RunnableKind::Bench { test_id } 83 RunnableKind::Bench { test_id }
83 } else { 84 } else {
@@ -87,6 +88,21 @@ fn runnable_fn(sema: &Semantics<RootDatabase>, fn_def: ast::FnDef) -> Option<Run
87 Some(Runnable { range: fn_def.syntax().text_range(), kind }) 88 Some(Runnable { range: fn_def.syntax().text_range(), kind })
88} 89}
89 90
91#[derive(Debug)]
92pub struct TestAttr {
93 pub ignore: bool,
94}
95
96impl TestAttr {
97 fn from_fn(fn_def: &ast::FnDef) -> TestAttr {
98 let ignore = fn_def
99 .attrs()
100 .filter_map(|attr| attr.simple_name())
101 .any(|attribute_text| attribute_text == "ignore");
102 TestAttr { ignore }
103 }
104}
105
90/// This is a method with a heuristics to support test methods annotated with custom test annotations, such as 106/// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
91/// `#[test_case(...)]`, `#[tokio::test]` and similar. 107/// `#[test_case(...)]`, `#[tokio::test]` and similar.
92/// Also a regular `#[test]` annotation is supported. 108/// Also a regular `#[test]` annotation is supported.
@@ -157,6 +173,9 @@ mod tests {
157 test_id: Path( 173 test_id: Path(
158 "test_foo", 174 "test_foo",
159 ), 175 ),
176 attr: TestAttr {
177 ignore: false,
178 },
160 }, 179 },
161 }, 180 },
162 Runnable { 181 Runnable {
@@ -165,6 +184,9 @@ mod tests {
165 test_id: Path( 184 test_id: Path(
166 "test_foo", 185 "test_foo",
167 ), 186 ),
187 attr: TestAttr {
188 ignore: true,
189 },
168 }, 190 },
169 }, 191 },
170 ] 192 ]
@@ -200,6 +222,9 @@ mod tests {
200 test_id: Path( 222 test_id: Path(
201 "test_mod::test_foo1", 223 "test_mod::test_foo1",
202 ), 224 ),
225 attr: TestAttr {
226 ignore: false,
227 },
203 }, 228 },
204 }, 229 },
205 ] 230 ]
@@ -237,6 +262,9 @@ mod tests {
237 test_id: Path( 262 test_id: Path(
238 "foo::test_mod::test_foo1", 263 "foo::test_mod::test_foo1",
239 ), 264 ),
265 attr: TestAttr {
266 ignore: false,
267 },
240 }, 268 },
241 }, 269 },
242 ] 270 ]
@@ -276,6 +304,9 @@ mod tests {
276 test_id: Path( 304 test_id: Path(
277 "foo::bar::test_mod::test_foo1", 305 "foo::bar::test_mod::test_foo1",
278 ), 306 ),
307 attr: TestAttr {
308 ignore: false,
309 },
279 }, 310 },
280 }, 311 },
281 ] 312 ]