aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/completions/attribute.rs2
-rw-r--r--crates/ide_completion/src/completions/attribute/repr.rs199
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs18
-rw-r--r--crates/ide_completion/src/completions/record.rs28
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs8
-rw-r--r--crates/ide_completion/src/context.rs31
-rw-r--r--crates/ide_completion/src/render/builder_ext.rs2
7 files changed, 242 insertions, 46 deletions
diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs
index ab24a6521..78fc30e16 100644
--- a/crates/ide_completion/src/completions/attribute.rs
+++ b/crates/ide_completion/src/completions/attribute.rs
@@ -17,12 +17,14 @@ use crate::{
17 17
18mod derive; 18mod derive;
19mod lint; 19mod lint;
20mod repr;
20 21
21pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { 22pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
22 let attribute = ctx.attribute_under_caret.as_ref()?; 23 let attribute = ctx.attribute_under_caret.as_ref()?;
23 match (attribute.path().and_then(|p| p.as_single_name_ref()), attribute.token_tree()) { 24 match (attribute.path().and_then(|p| p.as_single_name_ref()), attribute.token_tree()) {
24 (Some(path), Some(token_tree)) => match path.text().as_str() { 25 (Some(path), Some(token_tree)) => match path.text().as_str() {
25 "derive" => derive::complete_derive(acc, ctx, token_tree), 26 "derive" => derive::complete_derive(acc, ctx, token_tree),
27 "repr" => repr::complete_repr(acc, ctx, token_tree),
26 "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES), 28 "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
27 "allow" | "warn" | "deny" | "forbid" => { 29 "allow" | "warn" | "deny" | "forbid" => {
28 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS); 30 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
diff --git a/crates/ide_completion/src/completions/attribute/repr.rs b/crates/ide_completion/src/completions/attribute/repr.rs
new file mode 100644
index 000000000..92a262a43
--- /dev/null
+++ b/crates/ide_completion/src/completions/attribute/repr.rs
@@ -0,0 +1,199 @@
1//! Completion for representations.
2
3use syntax::ast;
4
5use crate::{
6 context::CompletionContext,
7 item::{CompletionItem, CompletionItemKind, CompletionKind},
8 Completions,
9};
10
11pub(super) fn complete_repr(
12 acc: &mut Completions,
13 ctx: &CompletionContext,
14 derive_input: ast::TokenTree,
15) {
16 if let Some(existing_reprs) = super::parse_comma_sep_input(derive_input) {
17 for repr_completion in REPR_COMPLETIONS {
18 if existing_reprs
19 .iter()
20 .any(|it| repr_completion.label == it || repr_completion.collides.contains(&&**it))
21 {
22 continue;
23 }
24 let mut item = CompletionItem::new(
25 CompletionKind::Attribute,
26 ctx.source_range(),
27 repr_completion.label,
28 );
29 item.kind(CompletionItemKind::Attribute);
30 if let Some(lookup) = repr_completion.lookup {
31 item.lookup_by(lookup);
32 }
33 if let Some((snippet, cap)) = repr_completion.snippet.zip(ctx.config.snippet_cap) {
34 item.insert_snippet(cap, snippet);
35 }
36 item.add_to(acc);
37 }
38 }
39}
40
41struct ReprCompletion {
42 label: &'static str,
43 snippet: Option<&'static str>,
44 lookup: Option<&'static str>,
45 collides: &'static [&'static str],
46}
47
48const fn attr(label: &'static str, collides: &'static [&'static str]) -> ReprCompletion {
49 ReprCompletion { label, snippet: None, lookup: None, collides }
50}
51
52#[rustfmt::skip]
53const REPR_COMPLETIONS: &[ReprCompletion] = &[
54 ReprCompletion { label: "align($0)", snippet: Some("align($0)"), lookup: Some("align"), collides: &["transparent", "packed"] },
55 attr("packed", &["transparent", "align"]),
56 attr("transparent", &["C", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
57 attr("C", &["transparent"]),
58 attr("u8", &["transparent", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
59 attr("u16", &["transparent", "u8", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
60 attr("u32", &["transparent", "u8", "u16", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
61 attr("u64", &["transparent", "u8", "u16", "u32", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
62 attr("u128", &["transparent", "u8", "u16", "u32", "u64", "usize", "i8", "i16", "i32", "i64", "i128", "isize"]),
63 attr("usize", &["transparent", "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32", "i64", "i128", "isize"]),
64 attr("i8", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i16", "i32", "i64", "i128", "isize"]),
65 attr("i16", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i32", "i64", "i128", "isize"]),
66 attr("i32", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i64", "i128", "isize"]),
67 attr("i64", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i128", "isize"]),
68 attr("i28", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "isize"]),
69 attr("isize", &["transparent", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128"]),
70];
71
72#[cfg(test)]
73mod tests {
74 use expect_test::{expect, Expect};
75
76 use crate::tests::completion_list;
77
78 fn check(ra_fixture: &str, expect: Expect) {
79 let actual = completion_list(ra_fixture);
80 expect.assert_eq(&actual);
81 }
82
83 #[test]
84 fn no_completion_for_incorrect_repr() {
85 check(r#"#[repr{$0)] struct Test;"#, expect![[]])
86 }
87
88 #[test]
89 fn empty() {
90 check(
91 r#"#[repr($0)] struct Test;"#,
92 expect![[r#"
93 at align($0)
94 at packed
95 at transparent
96 at C
97 at u8
98 at u16
99 at u32
100 at u64
101 at u128
102 at usize
103 at i8
104 at i16
105 at i32
106 at i64
107 at i28
108 at isize
109 "#]],
110 );
111 }
112
113 #[test]
114 fn transparent() {
115 check(r#"#[repr(transparent, $0)] struct Test;"#, expect![[r#""#]]);
116 }
117
118 #[test]
119 fn align() {
120 check(
121 r#"#[repr(align(1), $0)] struct Test;"#,
122 expect![[r#"
123 at align($0)
124 at transparent
125 at C
126 at u8
127 at u16
128 at u32
129 at u64
130 at u128
131 at usize
132 at i8
133 at i16
134 at i32
135 at i64
136 at i28
137 at isize
138 "#]],
139 );
140 }
141
142 #[test]
143 fn packed() {
144 check(
145 r#"#[repr(packed, $0)] struct Test;"#,
146 expect![[r#"
147 at transparent
148 at C
149 at u8
150 at u16
151 at u32
152 at u64
153 at u128
154 at usize
155 at i8
156 at i16
157 at i32
158 at i64
159 at i28
160 at isize
161 "#]],
162 );
163 }
164
165 #[test]
166 fn c() {
167 check(
168 r#"#[repr(C, $0)] struct Test;"#,
169 expect![[r#"
170 at align($0)
171 at packed
172 at u8
173 at u16
174 at u32
175 at u64
176 at u128
177 at usize
178 at i8
179 at i16
180 at i32
181 at i64
182 at i28
183 at isize
184 "#]],
185 );
186 }
187
188 #[test]
189 fn prim() {
190 check(
191 r#"#[repr(usize, $0)] struct Test;"#,
192 expect![[r#"
193 at align($0)
194 at packed
195 at C
196 "#]],
197 );
198 }
199}
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index 88f4d940d..f5dbd203b 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -65,9 +65,11 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
65 // Don't suggest attribute macros and derives. 65 // Don't suggest attribute macros and derives.
66 hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(), 66 hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
67 // no values in type places 67 // no values in type places
68 hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(_)) 68 hir::ScopeDef::ModuleDef(
69 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Variant(_)) 69 hir::ModuleDef::Function(_)
70 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Static(_)) 70 | hir::ModuleDef::Variant(_)
71 | hir::ModuleDef::Static(_),
72 )
71 | hir::ScopeDef::Local(_) => !ctx.expects_type(), 73 | hir::ScopeDef::Local(_) => !ctx.expects_type(),
72 // unless its a constant in a generic arg list position 74 // unless its a constant in a generic arg list position
73 hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(_)) => { 75 hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(_)) => {
@@ -81,9 +83,13 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
81 } 83 }
82 } 84 }
83 } 85 }
84 hir::PathResolution::Def(def @ hir::ModuleDef::Adt(_)) 86 hir::PathResolution::Def(
85 | hir::PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) 87 def
86 | hir::PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => { 88 @
89 (hir::ModuleDef::Adt(_)
90 | hir::ModuleDef::TypeAlias(_)
91 | hir::ModuleDef::BuiltinType(_)),
92 ) => {
87 if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def { 93 if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def {
88 add_enum_variants(acc, ctx, e); 94 add_enum_variants(acc, ctx, e);
89 } 95 }
diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs
index 47523f72f..e876337f1 100644
--- a/crates/ide_completion/src/completions/record.rs
+++ b/crates/ide_completion/src/completions/record.rs
@@ -48,10 +48,9 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
48#[cfg(test)] 48#[cfg(test)]
49mod tests { 49mod tests {
50 use expect_test::{expect, Expect}; 50 use expect_test::{expect, Expect};
51 use ide_db::helpers::FamousDefs;
52 51
53 use crate::{ 52 use crate::{
54 tests::{self, filtered_completion_list}, 53 tests::{check_edit, filtered_completion_list},
55 CompletionKind, 54 CompletionKind,
56 }; 55 };
57 56
@@ -61,31 +60,17 @@ mod tests {
61 } 60 }
62 61
63 fn check_snippet(ra_fixture: &str, expect: Expect) { 62 fn check_snippet(ra_fixture: &str, expect: Expect) {
64 let actual = filtered_completion_list( 63 let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet);
65 &format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE),
66 CompletionKind::Snippet,
67 );
68 expect.assert_eq(&actual); 64 expect.assert_eq(&actual);
69 } 65 }
70 66
71 fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
72 tests::check_edit(
73 what,
74 &format!(
75 "//- /main.rs crate:main deps:core{}\n{}",
76 ra_fixture_before,
77 FamousDefs::FIXTURE,
78 ),
79 &(ra_fixture_after.to_owned() + "\n"),
80 );
81 }
82
83 #[test] 67 #[test]
84 fn test_record_literal_field_default() { 68 fn test_record_literal_field_default() {
85 let test_code = r#" 69 let test_code = r#"
70//- minicore: default
86struct S { foo: u32, bar: usize } 71struct S { foo: u32, bar: usize }
87 72
88impl core::default::Default for S { 73impl Default for S {
89 fn default() -> Self { 74 fn default() -> Self {
90 S { 75 S {
91 foo: 0, 76 foo: 0,
@@ -121,9 +106,10 @@ fn process(f: S) {
121 check_edit( 106 check_edit(
122 "..Default::default()", 107 "..Default::default()",
123 r#" 108 r#"
109//- minicore: default
124struct S { foo: u32, bar: usize } 110struct S { foo: u32, bar: usize }
125 111
126impl core::default::Default for S { 112impl Default for S {
127 fn default() -> Self { 113 fn default() -> Self {
128 S { 114 S {
129 foo: 0, 115 foo: 0,
@@ -142,7 +128,7 @@ fn process(f: S) {
142 r#" 128 r#"
143struct S { foo: u32, bar: usize } 129struct S { foo: u32, bar: usize }
144 130
145impl core::default::Default for S { 131impl Default for S {
146 fn default() -> Self { 132 fn default() -> Self {
147 S { 133 S {
148 foo: 0, 134 foo: 0,
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index 8ea5a2d5b..81c4fb305 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -74,9 +74,11 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
74 // Don't suggest attribute macros and derives. 74 // Don't suggest attribute macros and derives.
75 ScopeDef::MacroDef(mac) => mac.is_fn_like(), 75 ScopeDef::MacroDef(mac) => mac.is_fn_like(),
76 // no values in type places 76 // no values in type places
77 ScopeDef::ModuleDef(hir::ModuleDef::Function(_)) 77 ScopeDef::ModuleDef(
78 | ScopeDef::ModuleDef(hir::ModuleDef::Variant(_)) 78 hir::ModuleDef::Function(_)
79 | ScopeDef::ModuleDef(hir::ModuleDef::Static(_)) 79 | hir::ModuleDef::Variant(_)
80 | hir::ModuleDef::Static(_),
81 )
80 | ScopeDef::Local(_) => !ctx.expects_type(), 82 | ScopeDef::Local(_) => !ctx.expects_type(),
81 // unless its a constant in a generic arg list position 83 // unless its a constant in a generic arg list position
82 ScopeDef::ModuleDef(hir::ModuleDef::Const(_)) 84 ScopeDef::ModuleDef(hir::ModuleDef::Const(_))
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index d7a7e9cca..98fb36042 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -242,24 +242,23 @@ impl<'a> CompletionContext<'a> {
242 } 242 }
243 243
244 pub(crate) fn expects_assoc_item(&self) -> bool { 244 pub(crate) fn expects_assoc_item(&self) -> bool {
245 matches!( 245 matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl))
246 self.completion_location,
247 Some(ImmediateLocation::Trait) | Some(ImmediateLocation::Impl)
248 )
249 } 246 }
250 247
251 pub(crate) fn has_dot_receiver(&self) -> bool { 248 pub(crate) fn has_dot_receiver(&self) -> bool {
252 matches!( 249 matches!(
253 &self.completion_location, 250 &self.completion_location,
254 Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver,.. }) 251 Some(ImmediateLocation::FieldAccess { receiver, .. } | ImmediateLocation::MethodCall { receiver,.. })
255 if receiver.is_some() 252 if receiver.is_some()
256 ) 253 )
257 } 254 }
258 255
259 pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> { 256 pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
260 match &self.completion_location { 257 match &self.completion_location {
261 Some(ImmediateLocation::MethodCall { receiver, .. }) 258 Some(
262 | Some(ImmediateLocation::FieldAccess { receiver, .. }) => receiver.as_ref(), 259 ImmediateLocation::MethodCall { receiver, .. }
260 | ImmediateLocation::FieldAccess { receiver, .. },
261 ) => receiver.as_ref(),
263 _ => None, 262 _ => None,
264 } 263 }
265 } 264 }
@@ -283,7 +282,7 @@ impl<'a> CompletionContext<'a> {
283 pub(crate) fn expects_ident_pat_or_ref_expr(&self) -> bool { 282 pub(crate) fn expects_ident_pat_or_ref_expr(&self) -> bool {
284 matches!( 283 matches!(
285 self.completion_location, 284 self.completion_location,
286 Some(ImmediateLocation::IdentPat) | Some(ImmediateLocation::RefExpr) 285 Some(ImmediateLocation::IdentPat | ImmediateLocation::RefExpr)
287 ) 286 )
288 } 287 }
289 288
@@ -294,14 +293,14 @@ impl<'a> CompletionContext<'a> {
294 pub(crate) fn in_use_tree(&self) -> bool { 293 pub(crate) fn in_use_tree(&self) -> bool {
295 matches!( 294 matches!(
296 self.completion_location, 295 self.completion_location,
297 Some(ImmediateLocation::Use) | Some(ImmediateLocation::UseTree) 296 Some(ImmediateLocation::Use | ImmediateLocation::UseTree)
298 ) 297 )
299 } 298 }
300 299
301 pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool { 300 pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool {
302 matches!( 301 matches!(
303 self.prev_sibling, 302 self.prev_sibling,
304 Some(ImmediatePrevSibling::ImplDefType) | Some(ImmediatePrevSibling::TraitDefName) 303 Some(ImmediatePrevSibling::ImplDefType | ImmediatePrevSibling::TraitDefName)
305 ) 304 )
306 } 305 }
307 306
@@ -322,14 +321,16 @@ impl<'a> CompletionContext<'a> {
322 || self.previous_token_is(T![unsafe]) 321 || self.previous_token_is(T![unsafe])
323 || matches!( 322 || matches!(
324 self.prev_sibling, 323 self.prev_sibling,
325 Some(ImmediatePrevSibling::Attribute) | Some(ImmediatePrevSibling::Visibility) 324 Some(ImmediatePrevSibling::Attribute | ImmediatePrevSibling::Visibility)
326 ) 325 )
327 || matches!( 326 || matches!(
328 self.completion_location, 327 self.completion_location,
329 Some(ImmediateLocation::Attribute(_)) 328 Some(
330 | Some(ImmediateLocation::ModDeclaration(_)) 329 ImmediateLocation::Attribute(_)
331 | Some(ImmediateLocation::RecordPat(_)) 330 | ImmediateLocation::ModDeclaration(_)
332 | Some(ImmediateLocation::RecordExpr(_)) 331 | ImmediateLocation::RecordPat(_)
332 | ImmediateLocation::RecordExpr(_)
333 )
333 ) 334 )
334 } 335 }
335 336
diff --git a/crates/ide_completion/src/render/builder_ext.rs b/crates/ide_completion/src/render/builder_ext.rs
index 749dfc665..33d3a5ee1 100644
--- a/crates/ide_completion/src/render/builder_ext.rs
+++ b/crates/ide_completion/src/render/builder_ext.rs
@@ -32,7 +32,7 @@ impl Builder {
32 cov_mark::hit!(no_parens_in_use_item); 32 cov_mark::hit!(no_parens_in_use_item);
33 return false; 33 return false;
34 } 34 }
35 if matches!(ctx.path_call_kind(), Some(CallKind::Expr) | Some(CallKind::Pat)) 35 if matches!(ctx.path_call_kind(), Some(CallKind::Expr | CallKind::Pat))
36 | matches!( 36 | matches!(
37 ctx.completion_location, 37 ctx.completion_location,
38 Some(ImmediateLocation::MethodCall { has_parens: true, .. }) 38 Some(ImmediateLocation::MethodCall { has_parens: true, .. })