diff options
-rw-r--r-- | Cargo.lock | 76 | ||||
-rw-r--r-- | crates/hir_ty/Cargo.toml | 6 | ||||
-rw-r--r-- | crates/hir_ty/src/chalk_db.rs | 8 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/traits.rs | 31 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/attribute.rs | 37 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/fn_param.rs | 15 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/keyword.rs | 18 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/mod_.rs | 6 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/record.rs | 20 | ||||
-rw-r--r-- | crates/ide_completion/src/context.rs | 99 | ||||
-rw-r--r-- | crates/ide_completion/src/patterns.rs | 151 | ||||
-rw-r--r-- | crates/syntax/Cargo.toml | 2 |
12 files changed, 258 insertions, 211 deletions
diff --git a/Cargo.lock b/Cargo.lock index 192f0efc2..15ccf4146 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -146,9 +146,9 @@ dependencies = [ | |||
146 | 146 | ||
147 | [[package]] | 147 | [[package]] |
148 | name = "cc" | 148 | name = "cc" |
149 | version = "1.0.67" | 149 | version = "1.0.68" |
150 | source = "registry+https://github.com/rust-lang/crates.io-index" | 150 | source = "registry+https://github.com/rust-lang/crates.io-index" |
151 | checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" | 151 | checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" |
152 | 152 | ||
153 | [[package]] | 153 | [[package]] |
154 | name = "cfg" | 154 | name = "cfg" |
@@ -169,9 +169,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | |||
169 | 169 | ||
170 | [[package]] | 170 | [[package]] |
171 | name = "chalk-derive" | 171 | name = "chalk-derive" |
172 | version = "0.67.0" | 172 | version = "0.68.0" |
173 | source = "registry+https://github.com/rust-lang/crates.io-index" | 173 | source = "registry+https://github.com/rust-lang/crates.io-index" |
174 | checksum = "751a3cd9eeb24d7165e9f90daf1f10a23ffa16a7986f349027f8dfb60f51ee0c" | 174 | checksum = "ea1552e7666a857f5417e6051ce705ea6856ab2cda39be7605e5b626fa47416b" |
175 | dependencies = [ | 175 | dependencies = [ |
176 | "proc-macro2", | 176 | "proc-macro2", |
177 | "quote", | 177 | "quote", |
@@ -181,9 +181,9 @@ dependencies = [ | |||
181 | 181 | ||
182 | [[package]] | 182 | [[package]] |
183 | name = "chalk-ir" | 183 | name = "chalk-ir" |
184 | version = "0.67.0" | 184 | version = "0.68.0" |
185 | source = "registry+https://github.com/rust-lang/crates.io-index" | 185 | source = "registry+https://github.com/rust-lang/crates.io-index" |
186 | checksum = "4236da905504740d3f72cc8f0509aa01321cc236208e2c33b55eda2db74bc495" | 186 | checksum = "19d7d5f1448dbac493541e97221f7f4c32326c4c76c6ecf543daf72a1dd93e66" |
187 | dependencies = [ | 187 | dependencies = [ |
188 | "bitflags", | 188 | "bitflags", |
189 | "chalk-derive", | 189 | "chalk-derive", |
@@ -192,9 +192,9 @@ dependencies = [ | |||
192 | 192 | ||
193 | [[package]] | 193 | [[package]] |
194 | name = "chalk-recursive" | 194 | name = "chalk-recursive" |
195 | version = "0.67.0" | 195 | version = "0.68.0" |
196 | source = "registry+https://github.com/rust-lang/crates.io-index" | 196 | source = "registry+https://github.com/rust-lang/crates.io-index" |
197 | checksum = "7d0b123fe45a34c4cd5cb329650a0d163525d2acbe9d754a4538d3340884002e" | 197 | checksum = "e0df406d2927321021b48acd193459dd33c913732155c93442d03f5ae8275385" |
198 | dependencies = [ | 198 | dependencies = [ |
199 | "chalk-derive", | 199 | "chalk-derive", |
200 | "chalk-ir", | 200 | "chalk-ir", |
@@ -205,9 +205,9 @@ dependencies = [ | |||
205 | 205 | ||
206 | [[package]] | 206 | [[package]] |
207 | name = "chalk-solve" | 207 | name = "chalk-solve" |
208 | version = "0.67.0" | 208 | version = "0.68.0" |
209 | source = "registry+https://github.com/rust-lang/crates.io-index" | 209 | source = "registry+https://github.com/rust-lang/crates.io-index" |
210 | checksum = "9c54788f0ae3b38e2bb2266f395d462d988c64f92dbd55c68219908fd1ce7ddc" | 210 | checksum = "0cbfcd5daa5ab8b1c9e5e10e83b0ac26271480f6ae5b5f35e5b19e1f6a0e6e37" |
211 | dependencies = [ | 211 | dependencies = [ |
212 | "chalk-derive", | 212 | "chalk-derive", |
213 | "chalk-ir", | 213 | "chalk-ir", |
@@ -281,9 +281,9 @@ dependencies = [ | |||
281 | 281 | ||
282 | [[package]] | 282 | [[package]] |
283 | name = "crossbeam-epoch" | 283 | name = "crossbeam-epoch" |
284 | version = "0.9.4" | 284 | version = "0.9.5" |
285 | source = "registry+https://github.com/rust-lang/crates.io-index" | 285 | source = "registry+https://github.com/rust-lang/crates.io-index" |
286 | checksum = "52fb27eab85b17fbb9f6fd667089e07d6a2eb8743d02639ee7f6a7a7729c9c94" | 286 | checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd" |
287 | dependencies = [ | 287 | dependencies = [ |
288 | "cfg-if", | 288 | "cfg-if", |
289 | "crossbeam-utils", | 289 | "crossbeam-utils", |
@@ -294,11 +294,10 @@ dependencies = [ | |||
294 | 294 | ||
295 | [[package]] | 295 | [[package]] |
296 | name = "crossbeam-utils" | 296 | name = "crossbeam-utils" |
297 | version = "0.8.4" | 297 | version = "0.8.5" |
298 | source = "registry+https://github.com/rust-lang/crates.io-index" | 298 | source = "registry+https://github.com/rust-lang/crates.io-index" |
299 | checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278" | 299 | checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db" |
300 | dependencies = [ | 300 | dependencies = [ |
301 | "autocfg", | ||
302 | "cfg-if", | 301 | "cfg-if", |
303 | "lazy_static", | 302 | "lazy_static", |
304 | ] | 303 | ] |
@@ -426,20 +425,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
426 | checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" | 425 | checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" |
427 | 426 | ||
428 | [[package]] | 427 | [[package]] |
429 | name = "fsevent" | ||
430 | version = "2.0.2" | ||
431 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
432 | checksum = "97f347202c95c98805c216f9e1df210e8ebaec9fdb2365700a43c10797a35e63" | ||
433 | dependencies = [ | ||
434 | "bitflags", | ||
435 | "fsevent-sys", | ||
436 | ] | ||
437 | |||
438 | [[package]] | ||
439 | name = "fsevent-sys" | 428 | name = "fsevent-sys" |
440 | version = "3.1.0" | 429 | version = "4.0.0" |
441 | source = "registry+https://github.com/rust-lang/crates.io-index" | 430 | source = "registry+https://github.com/rust-lang/crates.io-index" |
442 | checksum = "ca6f5e6817058771c10f0eb0f05ddf1e35844266f972004fe8e4b21fda295bd5" | 431 | checksum = "5c0e564d24da983c053beff1bb7178e237501206840a3e6bf4e267b9e8ae734a" |
443 | dependencies = [ | 432 | dependencies = [ |
444 | "libc", | 433 | "libc", |
445 | ] | 434 | ] |
@@ -776,9 +765,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" | |||
776 | 765 | ||
777 | [[package]] | 766 | [[package]] |
778 | name = "libc" | 767 | name = "libc" |
779 | version = "0.2.94" | 768 | version = "0.2.95" |
780 | source = "registry+https://github.com/rust-lang/crates.io-index" | 769 | source = "registry+https://github.com/rust-lang/crates.io-index" |
781 | checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" | 770 | checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" |
782 | 771 | ||
783 | [[package]] | 772 | [[package]] |
784 | name = "libloading" | 773 | name = "libloading" |
@@ -881,18 +870,18 @@ checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" | |||
881 | 870 | ||
882 | [[package]] | 871 | [[package]] |
883 | name = "memmap2" | 872 | name = "memmap2" |
884 | version = "0.2.2" | 873 | version = "0.2.3" |
885 | source = "registry+https://github.com/rust-lang/crates.io-index" | 874 | source = "registry+https://github.com/rust-lang/crates.io-index" |
886 | checksum = "397d1a6d6d0563c0f5462bbdae662cf6c784edf5e828e40c7257f85d82bf56dd" | 875 | checksum = "723e3ebdcdc5c023db1df315364573789f8857c11b631a2fdfad7c00f5c046b4" |
887 | dependencies = [ | 876 | dependencies = [ |
888 | "libc", | 877 | "libc", |
889 | ] | 878 | ] |
890 | 879 | ||
891 | [[package]] | 880 | [[package]] |
892 | name = "memoffset" | 881 | name = "memoffset" |
893 | version = "0.6.3" | 882 | version = "0.6.4" |
894 | source = "registry+https://github.com/rust-lang/crates.io-index" | 883 | source = "registry+https://github.com/rust-lang/crates.io-index" |
895 | checksum = "f83fb6581e8ed1f85fd45c116db8405483899489e38406156c25eb743554361d" | 884 | checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" |
896 | dependencies = [ | 885 | dependencies = [ |
897 | "autocfg", | 886 | "autocfg", |
898 | ] | 887 | ] |
@@ -940,14 +929,13 @@ dependencies = [ | |||
940 | 929 | ||
941 | [[package]] | 930 | [[package]] |
942 | name = "notify" | 931 | name = "notify" |
943 | version = "5.0.0-pre.8" | 932 | version = "5.0.0-pre.9" |
944 | source = "registry+https://github.com/rust-lang/crates.io-index" | 933 | source = "registry+https://github.com/rust-lang/crates.io-index" |
945 | checksum = "46bbbcd078f1f00ddb7a9abe70b96e91229b44b0b3afdec610f8e5137f8f014b" | 934 | checksum = "b89869d77edd64db917d7903abeadc166f93686b342c56cc0ca51acb68441d09" |
946 | dependencies = [ | 935 | dependencies = [ |
947 | "bitflags", | 936 | "bitflags", |
948 | "crossbeam-channel", | 937 | "crossbeam-channel", |
949 | "filetime", | 938 | "filetime", |
950 | "fsevent", | ||
951 | "fsevent-sys", | 939 | "fsevent-sys", |
952 | "inotify", | 940 | "inotify", |
953 | "libc", | 941 | "libc", |
@@ -1360,9 +1348,9 @@ dependencies = [ | |||
1360 | 1348 | ||
1361 | [[package]] | 1349 | [[package]] |
1362 | name = "rustc-ap-rustc_lexer" | 1350 | name = "rustc-ap-rustc_lexer" |
1363 | version = "720.0.0" | 1351 | version = "721.0.0" |
1364 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1352 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1365 | checksum = "a025b453b0ae85335336f991f920ca9af5c0dc851171cb9035a16cea5619e9b2" | 1353 | checksum = "2ba1f60e2942dc7dc5ea64edeaae01cfba2303871b14936e1af0f54d5420b3d1" |
1366 | dependencies = [ | 1354 | dependencies = [ |
1367 | "unicode-xid", | 1355 | "unicode-xid", |
1368 | ] | 1356 | ] |
@@ -1828,9 +1816,9 @@ dependencies = [ | |||
1828 | 1816 | ||
1829 | [[package]] | 1817 | [[package]] |
1830 | name = "unicode-normalization" | 1818 | name = "unicode-normalization" |
1831 | version = "0.1.17" | 1819 | version = "0.1.18" |
1832 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1820 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1833 | checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" | 1821 | checksum = "33717dca7ac877f497014e10d73f3acf948c342bee31b5ca7892faf94ccc6b49" |
1834 | dependencies = [ | 1822 | dependencies = [ |
1835 | "tinyvec", | 1823 | "tinyvec", |
1836 | ] | 1824 | ] |
@@ -1958,18 +1946,18 @@ dependencies = [ | |||
1958 | 1946 | ||
1959 | [[package]] | 1947 | [[package]] |
1960 | name = "xshell" | 1948 | name = "xshell" |
1961 | version = "0.1.13" | 1949 | version = "0.1.14" |
1962 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1950 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1963 | checksum = "eb15bb1b41eb14efe628006294c294e10c366e03a0283b9c2063fc27d97934c6" | 1951 | checksum = "c640362f1b150e186b76e88606e4b01a7b2f1d56cc50fcc184ddb683fb567c23" |
1964 | dependencies = [ | 1952 | dependencies = [ |
1965 | "xshell-macros", | 1953 | "xshell-macros", |
1966 | ] | 1954 | ] |
1967 | 1955 | ||
1968 | [[package]] | 1956 | [[package]] |
1969 | name = "xshell-macros" | 1957 | name = "xshell-macros" |
1970 | version = "0.1.13" | 1958 | version = "0.1.14" |
1971 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1959 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1972 | checksum = "cf7ed94a2c75b9bcc57031229be2b57ee47ba71122f71aabef8610ec66a97e52" | 1960 | checksum = "0607c095c96c1d8420ce4a018a0954fb15d73d5eb59b521a05a0f04cffc05059" |
1973 | 1961 | ||
1974 | [[package]] | 1962 | [[package]] |
1975 | name = "xtask" | 1963 | name = "xtask" |
diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index a9994082a..c3d02424d 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml | |||
@@ -18,9 +18,9 @@ ena = "0.14.0" | |||
18 | log = "0.4.8" | 18 | log = "0.4.8" |
19 | rustc-hash = "1.1.0" | 19 | rustc-hash = "1.1.0" |
20 | scoped-tls = "1" | 20 | scoped-tls = "1" |
21 | chalk-solve = { version = "0.67", default-features = false } | 21 | chalk-solve = { version = "0.68", default-features = false } |
22 | chalk-ir = "0.67" | 22 | chalk-ir = "0.68" |
23 | chalk-recursive = "0.67" | 23 | chalk-recursive = "0.68" |
24 | la-arena = { version = "0.2.0", path = "../../lib/arena" } | 24 | la-arena = { version = "0.2.0", path = "../../lib/arena" } |
25 | 25 | ||
26 | stdx = { path = "../stdx", version = "0.0.0" } | 26 | stdx = { path = "../stdx", version = "0.0.0" } |
diff --git a/crates/hir_ty/src/chalk_db.rs b/crates/hir_ty/src/chalk_db.rs index b108fd559..4e042bf42 100644 --- a/crates/hir_ty/src/chalk_db.rs +++ b/crates/hir_ty/src/chalk_db.rs | |||
@@ -383,7 +383,7 @@ pub(crate) fn associated_ty_data_query( | |||
383 | // Lower bounds -- we could/should maybe move this to a separate query in `lower` | 383 | // Lower bounds -- we could/should maybe move this to a separate query in `lower` |
384 | let type_alias_data = db.type_alias_data(type_alias); | 384 | let type_alias_data = db.type_alias_data(type_alias); |
385 | let generic_params = generics(db.upcast(), type_alias.into()); | 385 | let generic_params = generics(db.upcast(), type_alias.into()); |
386 | let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); | 386 | // let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST); |
387 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); | 387 | let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); |
388 | let ctx = crate::TyLoweringContext::new(db, &resolver) | 388 | let ctx = crate::TyLoweringContext::new(db, &resolver) |
389 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); | 389 | .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); |
@@ -396,8 +396,10 @@ pub(crate) fn associated_ty_data_query( | |||
396 | .filter_map(|pred| generic_predicate_to_inline_bound(db, &pred, &self_ty)) | 396 | .filter_map(|pred| generic_predicate_to_inline_bound(db, &pred, &self_ty)) |
397 | .collect(); | 397 | .collect(); |
398 | 398 | ||
399 | let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); | 399 | // FIXME: Re-enable where clauses on associated types when an upstream chalk bug is fixed. |
400 | let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; | 400 | // (rust-analyzer#9052) |
401 | // let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); | ||
402 | let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses: vec![] }; | ||
401 | let datum = AssociatedTyDatum { | 403 | let datum = AssociatedTyDatum { |
402 | trait_id: to_chalk_trait_id(trait_), | 404 | trait_id: to_chalk_trait_id(trait_), |
403 | id, | 405 | id, |
diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 7c0ff2170..49add4ab9 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs | |||
@@ -161,7 +161,7 @@ mod result { | |||
161 | } | 161 | } |
162 | 162 | ||
163 | #[test] | 163 | #[test] |
164 | fn infer_tryv2() { | 164 | fn infer_try_trait_v2() { |
165 | check_types( | 165 | check_types( |
166 | r#" | 166 | r#" |
167 | //- /main.rs crate:main deps:core | 167 | //- /main.rs crate:main deps:core |
@@ -172,26 +172,41 @@ fn test() { | |||
172 | } //^ i32 | 172 | } //^ i32 |
173 | 173 | ||
174 | //- /core.rs crate:core | 174 | //- /core.rs crate:core |
175 | #[prelude_import] use ops::*; | ||
176 | mod ops { | 175 | mod ops { |
177 | trait Try { | 176 | mod try_trait { |
178 | type Output; | 177 | pub trait Try: FromResidual { |
179 | type Residual; | 178 | type Output; |
179 | type Residual; | ||
180 | } | ||
181 | pub trait FromResidual<R = <Self as Try>::Residual> {} | ||
180 | } | 182 | } |
183 | |||
184 | pub use self::try_trait::FromResidual; | ||
185 | pub use self::try_trait::Try; | ||
186 | } | ||
187 | |||
188 | mov convert { | ||
189 | pub trait From<T> {} | ||
190 | impl<T> From<T> for T {} | ||
181 | } | 191 | } |
182 | 192 | ||
183 | #[prelude_import] use result::*; | 193 | #[prelude_import] use result::*; |
184 | mod result { | 194 | mod result { |
185 | enum Infallible {} | 195 | use crate::convert::From; |
186 | enum Result<O, E> { | 196 | use crate::ops::{Try, FromResidual}; |
197 | |||
198 | pub enum Infallible {} | ||
199 | pub enum Result<O, E> { | ||
187 | Ok(O), | 200 | Ok(O), |
188 | Err(E) | 201 | Err(E) |
189 | } | 202 | } |
190 | 203 | ||
191 | impl<O, E> crate::ops::Try for Result<O, E> { | 204 | impl<O, E> Try for Result<O, E> { |
192 | type Output = O; | 205 | type Output = O; |
193 | type Error = Result<Infallible, E>; | 206 | type Error = Result<Infallible, E>; |
194 | } | 207 | } |
208 | |||
209 | impl<T, E, F: From<E>> FromResidual<Result<Infallible, E>> for Result<T, F> {} | ||
195 | } | 210 | } |
196 | "#, | 211 | "#, |
197 | ); | 212 | ); |
diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs index 13d5b90c9..76d926157 100644 --- a/crates/ide_completion/src/completions/attribute.rs +++ b/crates/ide_completion/src/completions/attribute.rs | |||
@@ -219,8 +219,7 @@ const ATTRIBUTES: &[AttrCompletion] = &[ | |||
219 | ), | 219 | ), |
220 | attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(), | 220 | attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(), |
221 | attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")), | 221 | attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")), |
222 | // FIXME: resolve through macro resolution? | 222 | attr("global_allocator", None, None), |
223 | attr("global_allocator", None, None).prefer_inner(), | ||
224 | attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)), | 223 | attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)), |
225 | attr("inline", Some("inline"), Some("inline")), | 224 | attr("inline", Some("inline"), Some("inline")), |
226 | attr("link", None, None), | 225 | attr("link", None, None), |
@@ -239,7 +238,7 @@ const ATTRIBUTES: &[AttrCompletion] = &[ | |||
239 | attr("no_mangle", None, None), | 238 | attr("no_mangle", None, None), |
240 | attr("no_std", None, None).prefer_inner(), | 239 | attr("no_std", None, None).prefer_inner(), |
241 | attr("non_exhaustive", None, None), | 240 | attr("non_exhaustive", None, None), |
242 | attr("panic_handler", None, None).prefer_inner(), | 241 | attr("panic_handler", None, None), |
243 | attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)), | 242 | attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)), |
244 | attr("proc_macro", None, None), | 243 | attr("proc_macro", None, None), |
245 | attr("proc_macro_attribute", None, None), | 244 | attr("proc_macro_attribute", None, None), |
@@ -609,6 +608,7 @@ mod tests { | |||
609 | at export_name = "…" | 608 | at export_name = "…" |
610 | at link_name = "…" | 609 | at link_name = "…" |
611 | at link_section = "…" | 610 | at link_section = "…" |
611 | at global_allocator | ||
612 | at used | 612 | at used |
613 | "#]], | 613 | "#]], |
614 | ); | 614 | ); |
@@ -732,9 +732,9 @@ mod tests { | |||
732 | } | 732 | } |
733 | 733 | ||
734 | #[test] | 734 | #[test] |
735 | fn complete_attribute_on_expr() { | 735 | fn complete_attribute_on_fn() { |
736 | check( | 736 | check( |
737 | r#"fn main() { #[$0] foo() }"#, | 737 | r#"#[$0] fn main() {}"#, |
738 | expect![[r#" | 738 | expect![[r#" |
739 | at allow(…) | 739 | at allow(…) |
740 | at cfg(…) | 740 | at cfg(…) |
@@ -742,10 +742,35 @@ mod tests { | |||
742 | at deny(…) | 742 | at deny(…) |
743 | at forbid(…) | 743 | at forbid(…) |
744 | at warn(…) | 744 | at warn(…) |
745 | at deprecated | ||
746 | at doc = "…" | ||
747 | at doc(hidden) | ||
748 | at doc(alias = "…") | ||
749 | at must_use | ||
750 | at no_mangle | ||
751 | at export_name = "…" | ||
752 | at link_name = "…" | ||
753 | at link_section = "…" | ||
754 | at cold | ||
755 | at ignore = "…" | ||
756 | at inline | ||
757 | at must_use | ||
758 | at panic_handler | ||
759 | at proc_macro | ||
760 | at proc_macro_derive(…) | ||
761 | at proc_macro_attribute | ||
762 | at should_panic | ||
763 | at target_feature = "…" | ||
764 | at test | ||
765 | at track_caller | ||
745 | "#]], | 766 | "#]], |
746 | ); | 767 | ); |
768 | } | ||
769 | |||
770 | #[test] | ||
771 | fn complete_attribute_on_expr() { | ||
747 | check( | 772 | check( |
748 | r#"fn main() { #[$0] foo(); }"#, | 773 | r#"fn main() { #[$0] foo() }"#, |
749 | expect![[r#" | 774 | expect![[r#" |
750 | at allow(…) | 775 | at allow(…) |
751 | at cfg(…) | 776 | at cfg(…) |
diff --git a/crates/ide_completion/src/completions/fn_param.rs b/crates/ide_completion/src/completions/fn_param.rs index 0ea558489..cb90e8a3e 100644 --- a/crates/ide_completion/src/completions/fn_param.rs +++ b/crates/ide_completion/src/completions/fn_param.rs | |||
@@ -128,4 +128,19 @@ fn outer(text: String) { | |||
128 | "#]], | 128 | "#]], |
129 | ) | 129 | ) |
130 | } | 130 | } |
131 | |||
132 | #[test] | ||
133 | fn completes_non_ident_pat_param() { | ||
134 | check( | ||
135 | r#" | ||
136 | struct Bar { bar: u32 } | ||
137 | |||
138 | fn foo(Bar { bar }: Bar) {} | ||
139 | fn foo2($0) {} | ||
140 | "#, | ||
141 | expect![[r#" | ||
142 | bn Bar { bar }: Bar | ||
143 | "#]], | ||
144 | ) | ||
145 | } | ||
131 | } | 146 | } |
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index e71a04b6e..0d035c611 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs | |||
@@ -4,7 +4,10 @@ use std::iter; | |||
4 | 4 | ||
5 | use syntax::{SyntaxKind, T}; | 5 | use syntax::{SyntaxKind, T}; |
6 | 6 | ||
7 | use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions}; | 7 | use crate::{ |
8 | patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind, | ||
9 | CompletionKind, Completions, | ||
10 | }; | ||
8 | 11 | ||
9 | pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) { | 12 | pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) { |
10 | // complete keyword "crate" in use stmt | 13 | // complete keyword "crate" in use stmt |
@@ -44,7 +47,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
44 | cov_mark::hit!(no_keyword_completion_in_comments); | 47 | cov_mark::hit!(no_keyword_completion_in_comments); |
45 | return; | 48 | return; |
46 | } | 49 | } |
47 | if ctx.record_lit_syntax.is_some() { | 50 | if matches!(ctx.completion_location, Some(ImmediateLocation::RecordExpr(_))) { |
48 | cov_mark::hit!(no_keyword_completion_in_record_lit); | 51 | cov_mark::hit!(no_keyword_completion_in_record_lit); |
49 | return; | 52 | return; |
50 | } | 53 | } |
@@ -55,7 +58,6 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
55 | let expects_item = ctx.expects_item(); | 58 | let expects_item = ctx.expects_item(); |
56 | 59 | ||
57 | if ctx.has_impl_or_trait_prev_sibling() { | 60 | if ctx.has_impl_or_trait_prev_sibling() { |
58 | // FIXME this also incorrectly shows up after a complete trait/impl | ||
59 | add_keyword("where", "where "); | 61 | add_keyword("where", "where "); |
60 | return; | 62 | return; |
61 | } | 63 | } |
@@ -77,11 +79,8 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
77 | add_keyword("pub", "pub "); | 79 | add_keyword("pub", "pub "); |
78 | } | 80 | } |
79 | 81 | ||
80 | if expects_item || expects_assoc_item || has_block_expr_parent || ctx.is_match_arm { | ||
81 | add_keyword("unsafe", "unsafe "); | ||
82 | } | ||
83 | |||
84 | if expects_item || expects_assoc_item || has_block_expr_parent { | 82 | if expects_item || expects_assoc_item || has_block_expr_parent { |
83 | add_keyword("unsafe", "unsafe "); | ||
85 | add_keyword("fn", "fn $1($2) {\n $0\n}"); | 84 | add_keyword("fn", "fn $1($2) {\n $0\n}"); |
86 | add_keyword("const", "const $0"); | 85 | add_keyword("const", "const $0"); |
87 | add_keyword("type", "type $0"); | 86 | add_keyword("type", "type $0"); |
@@ -103,6 +102,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
103 | } | 102 | } |
104 | 103 | ||
105 | if ctx.expects_expression() { | 104 | if ctx.expects_expression() { |
105 | if !has_block_expr_parent { | ||
106 | add_keyword("unsafe", "unsafe {\n $0\n}"); | ||
107 | } | ||
106 | add_keyword("match", "match $1 {\n $0\n}"); | 108 | add_keyword("match", "match $1 {\n $0\n}"); |
107 | add_keyword("while", "while $1 {\n $0\n}"); | 109 | add_keyword("while", "while $1 {\n $0\n}"); |
108 | add_keyword("while let", "while let $1 = $2 {\n $0\n}"); | 110 | add_keyword("while let", "while let $1 = $2 {\n $0\n}"); |
@@ -574,6 +576,7 @@ pub mod future { | |||
574 | check( | 576 | check( |
575 | r#"fn main() { let _ = $0 }"#, | 577 | r#"fn main() { let _ = $0 }"#, |
576 | expect![[r#" | 578 | expect![[r#" |
579 | kw unsafe | ||
577 | kw match | 580 | kw match |
578 | kw while | 581 | kw while |
579 | kw while let | 582 | kw while let |
@@ -634,6 +637,7 @@ fn foo() { | |||
634 | } | 637 | } |
635 | "#, | 638 | "#, |
636 | expect![[r#" | 639 | expect![[r#" |
640 | kw unsafe | ||
637 | kw match | 641 | kw match |
638 | kw while | 642 | kw while |
639 | kw while let | 643 | kw while let |
diff --git a/crates/ide_completion/src/completions/mod_.rs b/crates/ide_completion/src/completions/mod_.rs index 4f9415736..6a5746fb9 100644 --- a/crates/ide_completion/src/completions/mod_.rs +++ b/crates/ide_completion/src/completions/mod_.rs | |||
@@ -9,14 +9,14 @@ use ide_db::{ | |||
9 | }; | 9 | }; |
10 | use rustc_hash::FxHashSet; | 10 | use rustc_hash::FxHashSet; |
11 | 11 | ||
12 | use crate::CompletionItem; | 12 | use crate::{patterns::ImmediateLocation, CompletionItem}; |
13 | 13 | ||
14 | use crate::{context::CompletionContext, item::CompletionKind, Completions}; | 14 | use crate::{context::CompletionContext, item::CompletionKind, Completions}; |
15 | 15 | ||
16 | /// Complete mod declaration, i.e. `mod $0 ;` | 16 | /// Complete mod declaration, i.e. `mod $0 ;` |
17 | pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | 17 | pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { |
18 | let mod_under_caret = match &ctx.mod_declaration_under_caret { | 18 | let mod_under_caret = match &ctx.completion_location { |
19 | Some(mod_under_caret) if mod_under_caret.item_list().is_none() => mod_under_caret, | 19 | Some(ImmediateLocation::ModDeclaration(mod_under_caret)) => mod_under_caret, |
20 | _ => return None, | 20 | _ => return None, |
21 | }; | 21 | }; |
22 | 22 | ||
diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs index e1526b70b..227c08d01 100644 --- a/crates/ide_completion/src/completions/record.rs +++ b/crates/ide_completion/src/completions/record.rs | |||
@@ -2,21 +2,21 @@ | |||
2 | use ide_db::{helpers::FamousDefs, SymbolKind}; | 2 | use ide_db::{helpers::FamousDefs, SymbolKind}; |
3 | use syntax::ast::Expr; | 3 | use syntax::ast::Expr; |
4 | 4 | ||
5 | use crate::{item::CompletionKind, CompletionContext, CompletionItem, Completions}; | 5 | use crate::{ |
6 | item::CompletionKind, patterns::ImmediateLocation, CompletionContext, CompletionItem, | ||
7 | Completions, | ||
8 | }; | ||
6 | 9 | ||
7 | pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | 10 | pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { |
8 | let missing_fields = match (ctx.record_pat_syntax.as_ref(), ctx.record_lit_syntax.as_ref()) { | 11 | let missing_fields = match &ctx.completion_location { |
9 | (None, None) => return None, | 12 | Some(ImmediateLocation::RecordExpr(record_expr)) => { |
10 | (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"), | 13 | let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone())); |
11 | (Some(record_pat), _) => ctx.sema.record_pattern_missing_fields(record_pat), | ||
12 | (_, Some(record_lit)) => { | ||
13 | let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_lit.clone())); | ||
14 | let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default(); | 14 | let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default(); |
15 | let impl_default_trait = default_trait | 15 | let impl_default_trait = default_trait |
16 | .zip(ty) | 16 | .zip(ty) |
17 | .map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[])); | 17 | .map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[])); |
18 | 18 | ||
19 | let missing_fields = ctx.sema.record_literal_missing_fields(record_lit); | 19 | let missing_fields = ctx.sema.record_literal_missing_fields(record_expr); |
20 | if impl_default_trait && !missing_fields.is_empty() { | 20 | if impl_default_trait && !missing_fields.is_empty() { |
21 | let completion_text = "..Default::default()"; | 21 | let completion_text = "..Default::default()"; |
22 | let mut item = CompletionItem::new( | 22 | let mut item = CompletionItem::new( |
@@ -32,6 +32,10 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> | |||
32 | 32 | ||
33 | missing_fields | 33 | missing_fields |
34 | } | 34 | } |
35 | Some(ImmediateLocation::RecordPat(record_pat)) => { | ||
36 | ctx.sema.record_pattern_missing_fields(record_pat) | ||
37 | } | ||
38 | _ => return None, | ||
35 | }; | 39 | }; |
36 | 40 | ||
37 | for (field, ty) in missing_fields { | 41 | for (field, ty) in missing_fields { |
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 8d6440cb2..7c46c815d 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -18,7 +18,7 @@ use text_edit::Indel; | |||
18 | use crate::{ | 18 | use crate::{ |
19 | patterns::{ | 19 | patterns::{ |
20 | determine_location, determine_prev_sibling, for_is_prev2, inside_impl_trait_block, | 20 | determine_location, determine_prev_sibling, for_is_prev2, inside_impl_trait_block, |
21 | is_in_loop_body, is_match_arm, previous_token, ImmediateLocation, ImmediatePrevSibling, | 21 | is_in_loop_body, previous_token, ImmediateLocation, ImmediatePrevSibling, |
22 | }, | 22 | }, |
23 | CompletionConfig, | 23 | CompletionConfig, |
24 | }; | 24 | }; |
@@ -54,11 +54,6 @@ pub(crate) struct CompletionContext<'a> { | |||
54 | /// The parent impl of the cursor position if it exists. | 54 | /// The parent impl of the cursor position if it exists. |
55 | pub(super) impl_def: Option<ast::Impl>, | 55 | pub(super) impl_def: Option<ast::Impl>, |
56 | 56 | ||
57 | /// RecordExpr the token is a field of | ||
58 | pub(super) record_lit_syntax: Option<ast::RecordExpr>, | ||
59 | /// RecordPat the token is a field of | ||
60 | pub(super) record_pat_syntax: Option<ast::RecordPat>, | ||
61 | |||
62 | // potentially set if we are completing a lifetime | 57 | // potentially set if we are completing a lifetime |
63 | pub(super) lifetime_syntax: Option<ast::Lifetime>, | 58 | pub(super) lifetime_syntax: Option<ast::Lifetime>, |
64 | pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>, | 59 | pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>, |
@@ -71,6 +66,7 @@ pub(crate) struct CompletionContext<'a> { | |||
71 | 66 | ||
72 | pub(super) completion_location: Option<ImmediateLocation>, | 67 | pub(super) completion_location: Option<ImmediateLocation>, |
73 | pub(super) prev_sibling: Option<ImmediatePrevSibling>, | 68 | pub(super) prev_sibling: Option<ImmediatePrevSibling>, |
69 | pub(super) attribute_under_caret: Option<ast::Attr>, | ||
74 | 70 | ||
75 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong | 71 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong |
76 | pub(super) active_parameter: Option<ActiveParameter>, | 72 | pub(super) active_parameter: Option<ActiveParameter>, |
@@ -95,14 +91,10 @@ pub(crate) struct CompletionContext<'a> { | |||
95 | pub(super) is_macro_call: bool, | 91 | pub(super) is_macro_call: bool, |
96 | pub(super) is_path_type: bool, | 92 | pub(super) is_path_type: bool, |
97 | pub(super) has_type_args: bool, | 93 | pub(super) has_type_args: bool, |
98 | pub(super) attribute_under_caret: Option<ast::Attr>, | ||
99 | pub(super) mod_declaration_under_caret: Option<ast::Module>, | ||
100 | pub(super) locals: Vec<(String, Local)>, | 94 | pub(super) locals: Vec<(String, Local)>, |
101 | 95 | ||
102 | // keyword patterns | ||
103 | pub(super) previous_token: Option<SyntaxToken>, | 96 | pub(super) previous_token: Option<SyntaxToken>, |
104 | pub(super) in_loop_body: bool, | 97 | pub(super) in_loop_body: bool, |
105 | pub(super) is_match_arm: bool, | ||
106 | pub(super) incomplete_let: bool, | 98 | pub(super) incomplete_let: bool, |
107 | 99 | ||
108 | no_completion_required: bool, | 100 | no_completion_required: bool, |
@@ -157,8 +149,6 @@ impl<'a> CompletionContext<'a> { | |||
157 | lifetime_param_syntax: None, | 149 | lifetime_param_syntax: None, |
158 | function_def: None, | 150 | function_def: None, |
159 | use_item_syntax: None, | 151 | use_item_syntax: None, |
160 | record_lit_syntax: None, | ||
161 | record_pat_syntax: None, | ||
162 | impl_def: None, | 152 | impl_def: None, |
163 | active_parameter: ActiveParameter::at(db, position), | 153 | active_parameter: ActiveParameter::at(db, position), |
164 | is_label_ref: false, | 154 | is_label_ref: false, |
@@ -176,15 +166,13 @@ impl<'a> CompletionContext<'a> { | |||
176 | is_macro_call: false, | 166 | is_macro_call: false, |
177 | is_path_type: false, | 167 | is_path_type: false, |
178 | has_type_args: false, | 168 | has_type_args: false, |
179 | attribute_under_caret: None, | ||
180 | mod_declaration_under_caret: None, | ||
181 | previous_token: None, | 169 | previous_token: None, |
182 | in_loop_body: false, | 170 | in_loop_body: false, |
183 | completion_location: None, | 171 | completion_location: None, |
184 | prev_sibling: None, | 172 | prev_sibling: None, |
185 | is_match_arm: false, | ||
186 | no_completion_required: false, | 173 | no_completion_required: false, |
187 | incomplete_let: false, | 174 | incomplete_let: false, |
175 | attribute_under_caret: None, | ||
188 | locals, | 176 | locals, |
189 | }; | 177 | }; |
190 | 178 | ||
@@ -227,7 +215,6 @@ impl<'a> CompletionContext<'a> { | |||
227 | break; | 215 | break; |
228 | } | 216 | } |
229 | } | 217 | } |
230 | ctx.fill_keyword_patterns(&speculative_file, offset); | ||
231 | ctx.fill(&original_file, speculative_file, offset); | 218 | ctx.fill(&original_file, speculative_file, offset); |
232 | Some(ctx) | 219 | Some(ctx) |
233 | } | 220 | } |
@@ -311,31 +298,13 @@ impl<'a> CompletionContext<'a> { | |||
311 | } | 298 | } |
312 | 299 | ||
313 | pub(crate) fn is_path_disallowed(&self) -> bool { | 300 | pub(crate) fn is_path_disallowed(&self) -> bool { |
314 | self.record_lit_syntax.is_some() | 301 | matches!( |
315 | || self.record_pat_syntax.is_some() | 302 | self.completion_location, |
316 | || self.attribute_under_caret.is_some() | 303 | Some(ImmediateLocation::Attribute(_)) |
317 | || self.mod_declaration_under_caret.is_some() | 304 | | Some(ImmediateLocation::ModDeclaration(_)) |
318 | } | 305 | | Some(ImmediateLocation::RecordPat(_)) |
319 | 306 | | Some(ImmediateLocation::RecordExpr(_)) | |
320 | fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { | 307 | ) || self.attribute_under_caret.is_some() |
321 | let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); | ||
322 | let syntax_element = NodeOrToken::Token(fake_ident_token); | ||
323 | self.previous_token = previous_token(syntax_element.clone()); | ||
324 | self.in_loop_body = is_in_loop_body(syntax_element.clone()); | ||
325 | self.is_match_arm = is_match_arm(syntax_element.clone()); | ||
326 | |||
327 | self.mod_declaration_under_caret = | ||
328 | find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset) | ||
329 | .filter(|module| module.item_list().is_none()); | ||
330 | self.incomplete_let = | ||
331 | syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| { | ||
332 | it.syntax().text_range().end() == syntax_element.text_range().end() | ||
333 | }); | ||
334 | |||
335 | let inside_impl_trait_block = inside_impl_trait_block(syntax_element.clone()); | ||
336 | let fn_is_prev = self.previous_token_is(T![fn]); | ||
337 | let for_is_prev2 = for_is_prev2(syntax_element.clone()); | ||
338 | self.no_completion_required = (fn_is_prev && !inside_impl_trait_block) || for_is_prev2; | ||
339 | } | 308 | } |
340 | 309 | ||
341 | fn fill_impl_def(&mut self) { | 310 | fn fill_impl_def(&mut self) { |
@@ -453,25 +422,43 @@ impl<'a> CompletionContext<'a> { | |||
453 | file_with_fake_ident: SyntaxNode, | 422 | file_with_fake_ident: SyntaxNode, |
454 | offset: TextSize, | 423 | offset: TextSize, |
455 | ) { | 424 | ) { |
425 | let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); | ||
426 | let syntax_element = NodeOrToken::Token(fake_ident_token); | ||
427 | self.previous_token = previous_token(syntax_element.clone()); | ||
428 | self.attribute_under_caret = syntax_element.ancestors().find_map(ast::Attr::cast); | ||
429 | self.no_completion_required = { | ||
430 | let inside_impl_trait_block = inside_impl_trait_block(syntax_element.clone()); | ||
431 | let fn_is_prev = self.previous_token_is(T![fn]); | ||
432 | let for_is_prev2 = for_is_prev2(syntax_element.clone()); | ||
433 | (fn_is_prev && !inside_impl_trait_block) || for_is_prev2 | ||
434 | }; | ||
435 | self.in_loop_body = is_in_loop_body(syntax_element.clone()); | ||
436 | |||
437 | self.incomplete_let = | ||
438 | syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| { | ||
439 | it.syntax().text_range().end() == syntax_element.text_range().end() | ||
440 | }); | ||
441 | |||
456 | let (expected_type, expected_name) = self.expected_type_and_name(); | 442 | let (expected_type, expected_name) = self.expected_type_and_name(); |
457 | self.expected_type = expected_type; | 443 | self.expected_type = expected_type; |
458 | self.expected_name = expected_name; | 444 | self.expected_name = expected_name; |
459 | self.attribute_under_caret = find_node_at_offset(&file_with_fake_ident, offset); | 445 | |
460 | let name_like = match find_node_at_offset(&&file_with_fake_ident, offset) { | 446 | let name_like = match find_node_at_offset(&&file_with_fake_ident, offset) { |
461 | Some(it) => it, | 447 | Some(it) => it, |
462 | None => return, | 448 | None => return, |
463 | }; | 449 | }; |
464 | self.completion_location = determine_location(&name_like); | 450 | self.completion_location = |
451 | determine_location(&self.sema, original_file, offset, &name_like); | ||
465 | self.prev_sibling = determine_prev_sibling(&name_like); | 452 | self.prev_sibling = determine_prev_sibling(&name_like); |
466 | match name_like { | 453 | match name_like { |
467 | ast::NameLike::Lifetime(lifetime) => { | 454 | ast::NameLike::Lifetime(lifetime) => { |
468 | self.classify_lifetime(original_file, lifetime, offset); | 455 | self.classify_lifetime(original_file, lifetime, offset); |
469 | } | 456 | } |
470 | ast::NameLike::NameRef(name_ref) => { | 457 | ast::NameLike::NameRef(name_ref) => { |
471 | self.classify_name_ref(original_file, name_ref, offset); | 458 | self.classify_name_ref(original_file, name_ref); |
472 | } | 459 | } |
473 | ast::NameLike::Name(name) => { | 460 | ast::NameLike::Name(name) => { |
474 | self.classify_name(original_file, name, offset); | 461 | self.classify_name(name); |
475 | } | 462 | } |
476 | } | 463 | } |
477 | } | 464 | } |
@@ -505,7 +492,7 @@ impl<'a> CompletionContext<'a> { | |||
505 | } | 492 | } |
506 | } | 493 | } |
507 | 494 | ||
508 | fn classify_name(&mut self, original_file: &SyntaxNode, name: ast::Name, offset: TextSize) { | 495 | fn classify_name(&mut self, name: ast::Name) { |
509 | if let Some(bind_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) { | 496 | if let Some(bind_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) { |
510 | self.is_pat_or_const = Some(PatternRefutability::Refutable); | 497 | self.is_pat_or_const = Some(PatternRefutability::Refutable); |
511 | // if any of these is here our bind pat can't be a const pat anymore | 498 | // if any of these is here our bind pat can't be a const pat anymore |
@@ -543,28 +530,12 @@ impl<'a> CompletionContext<'a> { | |||
543 | 530 | ||
544 | self.fill_impl_def(); | 531 | self.fill_impl_def(); |
545 | } | 532 | } |
533 | |||
546 | self.is_param |= is_node::<ast::Param>(name.syntax()); | 534 | self.is_param |= is_node::<ast::Param>(name.syntax()); |
547 | if ast::RecordPatField::for_field_name(&name).is_some() { | ||
548 | self.record_pat_syntax = | ||
549 | self.sema.find_node_at_offset_with_macros(&original_file, offset); | ||
550 | } | ||
551 | } | 535 | } |
552 | 536 | ||
553 | fn classify_name_ref( | 537 | fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameRef) { |
554 | &mut self, | ||
555 | original_file: &SyntaxNode, | ||
556 | name_ref: ast::NameRef, | ||
557 | offset: TextSize, | ||
558 | ) { | ||
559 | self.fill_impl_def(); | 538 | self.fill_impl_def(); |
560 | if ast::RecordExprField::for_field_name(&name_ref).is_some() { | ||
561 | self.record_lit_syntax = | ||
562 | self.sema.find_node_at_offset_with_macros(original_file, offset); | ||
563 | } | ||
564 | if ast::RecordPatField::for_field_name_ref(&name_ref).is_some() { | ||
565 | self.record_pat_syntax = | ||
566 | self.sema.find_node_at_offset_with_macros(&original_file, offset); | ||
567 | } | ||
568 | 539 | ||
569 | self.name_ref_syntax = | 540 | self.name_ref_syntax = |
570 | find_node_at_offset(original_file, name_ref.syntax().text_range().start()); | 541 | find_node_at_offset(original_file, name_ref.syntax().text_range().start()); |
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs index caf0ef39f..26516046b 100644 --- a/crates/ide_completion/src/patterns.rs +++ b/crates/ide_completion/src/patterns.rs | |||
@@ -1,15 +1,18 @@ | |||
1 | //! Patterns telling us certain facts about current syntax element, they are used in completion context | 1 | //! Patterns telling us certain facts about current syntax element, they are used in completion context |
2 | 2 | ||
3 | use hir::Semantics; | ||
4 | use ide_db::RootDatabase; | ||
3 | use syntax::{ | 5 | use syntax::{ |
4 | algo::non_trivia_sibling, | 6 | algo::non_trivia_sibling, |
5 | ast::{self, LoopBodyOwner}, | 7 | ast::{self, LoopBodyOwner}, |
6 | match_ast, AstNode, Direction, NodeOrToken, SyntaxElement, | 8 | match_ast, AstNode, Direction, SyntaxElement, |
7 | SyntaxKind::*, | 9 | SyntaxKind::*, |
8 | SyntaxNode, SyntaxToken, T, | 10 | SyntaxNode, SyntaxToken, TextSize, T, |
9 | }; | 11 | }; |
10 | 12 | ||
11 | #[cfg(test)] | 13 | #[cfg(test)] |
12 | use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable}; | 14 | use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable}; |
15 | |||
13 | /// Direct parent container of the cursor position | 16 | /// Direct parent container of the cursor position |
14 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | 17 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
15 | pub(crate) enum ImmediatePrevSibling { | 18 | pub(crate) enum ImmediatePrevSibling { |
@@ -19,7 +22,7 @@ pub(crate) enum ImmediatePrevSibling { | |||
19 | } | 22 | } |
20 | 23 | ||
21 | /// Direct parent container of the cursor position | 24 | /// Direct parent container of the cursor position |
22 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | 25 | #[derive(Clone, Debug, PartialEq, Eq)] |
23 | pub(crate) enum ImmediateLocation { | 26 | pub(crate) enum ImmediateLocation { |
24 | Use, | 27 | Use, |
25 | Impl, | 28 | Impl, |
@@ -29,10 +32,24 @@ pub(crate) enum ImmediateLocation { | |||
29 | IdentPat, | 32 | IdentPat, |
30 | BlockExpr, | 33 | BlockExpr, |
31 | ItemList, | 34 | ItemList, |
35 | // Fake file ast node | ||
36 | Attribute(ast::Attr), | ||
37 | // Fake file ast node | ||
38 | ModDeclaration(ast::Module), | ||
39 | // Original file ast node | ||
40 | /// The record expr of the field name we are completing | ||
41 | RecordExpr(ast::RecordExpr), | ||
42 | // Original file ast node | ||
43 | /// The record pat of the field name we are completing | ||
44 | RecordPat(ast::RecordPat), | ||
32 | } | 45 | } |
33 | 46 | ||
34 | pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option<ImmediatePrevSibling> { | 47 | pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option<ImmediatePrevSibling> { |
35 | let node = maximize_name_ref(name_like)?; | 48 | let node = match name_like { |
49 | ast::NameLike::NameRef(name_ref) => maximize_name_ref(name_ref), | ||
50 | ast::NameLike::Name(n) => n.syntax().clone(), | ||
51 | ast::NameLike::Lifetime(lt) => lt.syntax().clone(), | ||
52 | }; | ||
36 | let node = match node.parent().and_then(ast::MacroCall::cast) { | 53 | let node = match node.parent().and_then(ast::MacroCall::cast) { |
37 | // When a path is being typed after the name of a trait/type of an impl it is being | 54 | // When a path is being typed after the name of a trait/type of an impl it is being |
38 | // parsed as a macro, so when the trait/impl has a block following it an we are between the | 55 | // parsed as a macro, so when the trait/impl has a block following it an we are between the |
@@ -77,8 +94,37 @@ pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option<Immedi | |||
77 | Some(res) | 94 | Some(res) |
78 | } | 95 | } |
79 | 96 | ||
80 | pub(crate) fn determine_location(name_like: &ast::NameLike) -> Option<ImmediateLocation> { | 97 | pub(crate) fn determine_location( |
81 | let node = maximize_name_ref(name_like)?; | 98 | sema: &Semantics<RootDatabase>, |
99 | original_file: &SyntaxNode, | ||
100 | offset: TextSize, | ||
101 | name_like: &ast::NameLike, | ||
102 | ) -> Option<ImmediateLocation> { | ||
103 | let node = match name_like { | ||
104 | ast::NameLike::NameRef(name_ref) => { | ||
105 | if ast::RecordExprField::for_field_name(&name_ref).is_some() { | ||
106 | return sema | ||
107 | .find_node_at_offset_with_macros(original_file, offset) | ||
108 | .map(ImmediateLocation::RecordExpr); | ||
109 | } | ||
110 | if ast::RecordPatField::for_field_name_ref(&name_ref).is_some() { | ||
111 | return sema | ||
112 | .find_node_at_offset_with_macros(original_file, offset) | ||
113 | .map(ImmediateLocation::RecordPat); | ||
114 | } | ||
115 | maximize_name_ref(name_ref) | ||
116 | } | ||
117 | ast::NameLike::Name(name) => { | ||
118 | if ast::RecordPatField::for_field_name(&name).is_some() { | ||
119 | return sema | ||
120 | .find_node_at_offset_with_macros(original_file, offset) | ||
121 | .map(ImmediateLocation::RecordPat); | ||
122 | } | ||
123 | name.syntax().clone() | ||
124 | } | ||
125 | ast::NameLike::Lifetime(lt) => lt.syntax().clone(), | ||
126 | }; | ||
127 | |||
82 | let parent = match node.parent() { | 128 | let parent = match node.parent() { |
83 | Some(parent) => match ast::MacroCall::cast(parent.clone()) { | 129 | Some(parent) => match ast::MacroCall::cast(parent.clone()) { |
84 | // When a path is being typed in an (Assoc)ItemList the parser will always emit a macro_call. | 130 | // When a path is being typed in an (Assoc)ItemList the parser will always emit a macro_call. |
@@ -103,6 +149,7 @@ pub(crate) fn determine_location(name_like: &ast::NameLike) -> Option<ImmediateL | |||
103 | } | 149 | } |
104 | } | 150 | } |
105 | }; | 151 | }; |
152 | |||
106 | let res = match_ast! { | 153 | let res = match_ast! { |
107 | match parent { | 154 | match parent { |
108 | ast::IdentPat(_it) => ImmediateLocation::IdentPat, | 155 | ast::IdentPat(_it) => ImmediateLocation::IdentPat, |
@@ -117,36 +164,34 @@ pub(crate) fn determine_location(name_like: &ast::NameLike) -> Option<ImmediateL | |||
117 | Some(TRAIT) => ImmediateLocation::Trait, | 164 | Some(TRAIT) => ImmediateLocation::Trait, |
118 | _ => return None, | 165 | _ => return None, |
119 | }, | 166 | }, |
167 | ast::Module(it) => if it.item_list().is_none() { | ||
168 | ImmediateLocation::ModDeclaration(it) | ||
169 | } else { | ||
170 | return None | ||
171 | }, | ||
172 | ast::Attr(it) => ImmediateLocation::Attribute(it), | ||
120 | _ => return None, | 173 | _ => return None, |
121 | } | 174 | } |
122 | }; | 175 | }; |
123 | Some(res) | 176 | Some(res) |
124 | } | 177 | } |
125 | 178 | ||
126 | fn maximize_name_ref(name_like: &ast::NameLike) -> Option<SyntaxNode> { | 179 | fn maximize_name_ref(name_ref: &ast::NameRef) -> SyntaxNode { |
127 | // First walk the element we are completing up to its highest node that has the same text range | 180 | // Maximize a nameref to its enclosing path if its the last segment of said path |
128 | // as the element so that we can check in what context it immediately lies. We only do this for | 181 | if let Some(segment) = name_ref.syntax().parent().and_then(ast::PathSegment::cast) { |
129 | // NameRef -> Path as that's the only thing that makes sense to being "expanded" semantically. | 182 | let p = segment.parent_path(); |
130 | // We only wanna do this if the NameRef is the last segment of the path. | 183 | if p.parent_path().is_none() { |
131 | let node = match name_like { | 184 | if let Some(it) = p |
132 | ast::NameLike::NameRef(name_ref) => { | 185 | .syntax() |
133 | if let Some(segment) = name_ref.syntax().parent().and_then(ast::PathSegment::cast) { | 186 | .ancestors() |
134 | let p = segment.parent_path(); | 187 | .take_while(|it| it.text_range() == p.syntax().text_range()) |
135 | if p.parent_path().is_none() { | 188 | .last() |
136 | p.syntax() | 189 | { |
137 | .ancestors() | 190 | return it; |
138 | .take_while(|it| it.text_range() == p.syntax().text_range()) | ||
139 | .last()? | ||
140 | } else { | ||
141 | return None; | ||
142 | } | ||
143 | } else { | ||
144 | return None; | ||
145 | } | 191 | } |
146 | } | 192 | } |
147 | it @ ast::NameLike::Name(_) | it @ ast::NameLike::Lifetime(_) => it.syntax().clone(), | 193 | } |
148 | }; | 194 | name_ref.syntax().clone() |
149 | Some(node) | ||
150 | } | 195 | } |
151 | 196 | ||
152 | pub(crate) fn inside_impl_trait_block(element: SyntaxElement) -> bool { | 197 | pub(crate) fn inside_impl_trait_block(element: SyntaxElement) -> bool { |
@@ -167,18 +212,6 @@ fn test_inside_impl_trait_block() { | |||
167 | check_pattern_is_not_applicable(r"impl A { fn f$0 }", inside_impl_trait_block); | 212 | check_pattern_is_not_applicable(r"impl A { fn f$0 }", inside_impl_trait_block); |
168 | } | 213 | } |
169 | 214 | ||
170 | pub(crate) fn is_match_arm(element: SyntaxElement) -> bool { | ||
171 | not_same_range_ancestor(element.clone()).filter(|it| it.kind() == MATCH_ARM).is_some() | ||
172 | && previous_sibling_or_ancestor_sibling(element) | ||
173 | .and_then(|it| it.into_token()) | ||
174 | .filter(|it| it.kind() == FAT_ARROW) | ||
175 | .is_some() | ||
176 | } | ||
177 | #[test] | ||
178 | fn test_is_match_arm() { | ||
179 | check_pattern_is_applicable(r"fn my_fn() { match () { () => m$0 } }", is_match_arm); | ||
180 | } | ||
181 | |||
182 | pub(crate) fn previous_token(element: SyntaxElement) -> Option<SyntaxToken> { | 215 | pub(crate) fn previous_token(element: SyntaxElement) -> Option<SyntaxToken> { |
183 | element.into_token().and_then(|it| previous_non_trivia_token(it)) | 216 | element.into_token().and_then(|it| previous_non_trivia_token(it)) |
184 | } | 217 | } |
@@ -216,10 +249,6 @@ pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool { | |||
216 | .is_some() | 249 | .is_some() |
217 | } | 250 | } |
218 | 251 | ||
219 | pub(crate) fn not_same_range_ancestor(element: SyntaxElement) -> Option<SyntaxNode> { | ||
220 | element.ancestors().skip_while(|it| it.text_range() == element.text_range()).next() | ||
221 | } | ||
222 | |||
223 | fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> { | 252 | fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> { |
224 | let mut token = token.prev_token(); | 253 | let mut token = token.prev_token(); |
225 | while let Some(inner) = token.clone() { | 254 | while let Some(inner) = token.clone() { |
@@ -232,31 +261,25 @@ fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> { | |||
232 | None | 261 | None |
233 | } | 262 | } |
234 | 263 | ||
235 | fn previous_sibling_or_ancestor_sibling(element: SyntaxElement) -> Option<SyntaxElement> { | ||
236 | let token_sibling = non_trivia_sibling(element.clone(), Direction::Prev); | ||
237 | if let Some(sibling) = token_sibling { | ||
238 | Some(sibling) | ||
239 | } else { | ||
240 | // if not trying to find first ancestor which has such a sibling | ||
241 | let range = element.text_range(); | ||
242 | let top_node = element.ancestors().take_while(|it| it.text_range() == range).last()?; | ||
243 | let prev_sibling_node = top_node.ancestors().find(|it| { | ||
244 | non_trivia_sibling(NodeOrToken::Node(it.to_owned()), Direction::Prev).is_some() | ||
245 | })?; | ||
246 | non_trivia_sibling(NodeOrToken::Node(prev_sibling_node), Direction::Prev) | ||
247 | } | ||
248 | } | ||
249 | |||
250 | #[cfg(test)] | 264 | #[cfg(test)] |
251 | mod tests { | 265 | mod tests { |
266 | use syntax::algo::find_node_at_offset; | ||
267 | |||
268 | use crate::test_utils::position; | ||
269 | |||
252 | use super::*; | 270 | use super::*; |
253 | 271 | ||
254 | fn check_location(code: &str, loc: impl Into<Option<ImmediateLocation>>) { | 272 | fn check_location(code: &str, loc: impl Into<Option<ImmediateLocation>>) { |
255 | check_pattern_is_applicable(code, |e| { | 273 | let (db, pos) = position(code); |
256 | let name = &e.parent().and_then(ast::NameLike::cast).expect("Expected a namelike"); | 274 | |
257 | assert_eq!(determine_location(name), loc.into()); | 275 | let sema = Semantics::new(&db); |
258 | true | 276 | let original_file = sema.parse(pos.file_id); |
259 | }); | 277 | |
278 | let name_like = find_node_at_offset(original_file.syntax(), pos.offset).unwrap(); | ||
279 | assert_eq!( | ||
280 | determine_location(&sema, original_file.syntax(), pos.offset, &name_like), | ||
281 | loc.into() | ||
282 | ); | ||
260 | } | 283 | } |
261 | 284 | ||
262 | fn check_prev_sibling(code: &str, sibling: impl Into<Option<ImmediatePrevSibling>>) { | 285 | fn check_prev_sibling(code: &str, sibling: impl Into<Option<ImmediatePrevSibling>>) { |
diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index a6c294245..2106732cd 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml | |||
@@ -14,7 +14,7 @@ doctest = false | |||
14 | cov-mark = { version = "1.1", features = ["thread-local"] } | 14 | cov-mark = { version = "1.1", features = ["thread-local"] } |
15 | itertools = "0.10.0" | 15 | itertools = "0.10.0" |
16 | rowan = "=0.13.0-pre.6" | 16 | rowan = "=0.13.0-pre.6" |
17 | rustc_lexer = { version = "720.0.0", package = "rustc-ap-rustc_lexer" } | 17 | rustc_lexer = { version = "721.0.0", package = "rustc-ap-rustc_lexer" } |
18 | rustc-hash = "1.1.0" | 18 | rustc-hash = "1.1.0" |
19 | arrayvec = "0.7" | 19 | arrayvec = "0.7" |
20 | once_cell = "1.3.1" | 20 | once_cell = "1.3.1" |