aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/diagnostics.rs3
-rw-r--r--crates/ra_ide/src/goto_definition.rs80
-rw-r--r--crates/ra_ide/src/mock_analysis.rs10
3 files changed, 88 insertions, 5 deletions
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs
index a88a978d7..9bde1db8e 100644
--- a/crates/ra_ide/src/diagnostics.rs
+++ b/crates/ra_ide/src/diagnostics.rs
@@ -167,6 +167,9 @@ fn missing_struct_field_fix(
167 }; 167 };
168 168
169 let new_field_type = sema.type_of_expr(&record_expr.expr()?)?; 169 let new_field_type = sema.type_of_expr(&record_expr.expr()?)?;
170 if new_field_type.is_unknown() {
171 return None;
172 }
170 let new_field = make::record_field_def( 173 let new_field = make::record_field_def(
171 record_expr.field_name()?, 174 record_expr.field_name()?,
172 make::type_ref(&new_field_type.display_source_code(sema.db, module.into()).ok()?), 175 make::type_ref(&new_field_type.display_source_code(sema.db, module.into()).ok()?),
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs
index 0798d2c36..450ce0ba7 100644
--- a/crates/ra_ide/src/goto_definition.rs
+++ b/crates/ra_ide/src/goto_definition.rs
@@ -908,4 +908,84 @@ mod tests {
908 "x: i32|x", 908 "x: i32|x",
909 ); 909 );
910 } 910 }
911
912 #[test]
913 fn goto_def_for_enum_variant_self_pattern_const() {
914 check_goto(
915 "
916 //- /lib.rs
917 enum Foo {
918 Bar,
919 }
920 impl Foo {
921 fn baz(self) {
922 match self {
923 Self::Bar<|> => {}
924 }
925 }
926 }
927 ",
928 "Bar ENUM_VARIANT FileId(1) 15..18 15..18",
929 "Bar|Bar",
930 );
931 }
932
933 #[test]
934 fn goto_def_for_enum_variant_self_pattern_record() {
935 check_goto(
936 "
937 //- /lib.rs
938 enum Foo {
939 Bar { val: i32 },
940 }
941 impl Foo {
942 fn baz(self) -> i32 {
943 match self {
944 Self::Bar<|> { val } => {}
945 }
946 }
947 }
948 ",
949 "Bar ENUM_VARIANT FileId(1) 15..31 15..18",
950 "Bar { val: i32 }|Bar",
951 );
952 }
953
954 #[test]
955 fn goto_def_for_enum_variant_self_expr_const() {
956 check_goto(
957 "
958 //- /lib.rs
959 enum Foo {
960 Bar,
961 }
962 impl Foo {
963 fn baz(self) {
964 Self::Bar<|>;
965 }
966 }
967 ",
968 "Bar ENUM_VARIANT FileId(1) 15..18 15..18",
969 "Bar|Bar",
970 );
971 }
972
973 #[test]
974 fn goto_def_for_enum_variant_self_expr_record() {
975 check_goto(
976 "
977 //- /lib.rs
978 enum Foo {
979 Bar { val: i32 },
980 }
981 impl Foo {
982 fn baz(self) {
983 Self::Bar<|> {val: 4};
984 }
985 }
986 ",
987 "Bar ENUM_VARIANT FileId(1) 15..31 15..18",
988 "Bar { val: i32 }|Bar",
989 );
990 }
911} 991}
diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs
index ad78d2d93..76910d09b 100644
--- a/crates/ra_ide/src/mock_analysis.rs
+++ b/crates/ra_ide/src/mock_analysis.rs
@@ -4,7 +4,7 @@ use std::str::FromStr;
4use std::sync::Arc; 4use std::sync::Arc;
5 5
6use ra_cfg::CfgOptions; 6use ra_cfg::CfgOptions;
7use ra_db::{CrateName, Env, RelativePathBuf}; 7use ra_db::{CrateName, Env};
8use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER}; 8use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER};
9 9
10use crate::{ 10use crate::{
@@ -28,7 +28,7 @@ impl MockFileData {
28 fn path(&self) -> &str { 28 fn path(&self) -> &str {
29 match self { 29 match self {
30 MockFileData::Plain { path, .. } => path.as_str(), 30 MockFileData::Plain { path, .. } => path.as_str(),
31 MockFileData::Fixture(f) => f.meta.path().as_str(), 31 MockFileData::Fixture(f) => f.meta.path(),
32 } 32 }
33 } 33 }
34 34
@@ -167,7 +167,6 @@ impl MockAnalysis {
167 for (i, data) in self.files.into_iter().enumerate() { 167 for (i, data) in self.files.into_iter().enumerate() {
168 let path = data.path(); 168 let path = data.path();
169 assert!(path.starts_with('/')); 169 assert!(path.starts_with('/'));
170 let path = RelativePathBuf::from_path(&path[1..]).unwrap();
171 let cfg_options = data.cfg_options(); 170 let cfg_options = data.cfg_options();
172 let file_id = FileId(i as u32 + 1); 171 let file_id = FileId(i as u32 + 1);
173 let edition = data.edition(); 172 let edition = data.edition();
@@ -183,7 +182,8 @@ impl MockAnalysis {
183 Default::default(), 182 Default::default(),
184 )); 183 ));
185 } else if path.ends_with("/lib.rs") { 184 } else if path.ends_with("/lib.rs") {
186 let crate_name = path.parent().unwrap().file_name().unwrap(); 185 let base = &path[..path.len() - "/lib.rs".len()];
186 let crate_name = &base[base.rfind('/').unwrap() + '/'.len_utf8()..];
187 let other_crate = crate_graph.add_crate_root( 187 let other_crate = crate_graph.add_crate_root(
188 file_id, 188 file_id,
189 edition, 189 edition,
@@ -199,7 +199,7 @@ impl MockAnalysis {
199 .unwrap(); 199 .unwrap();
200 } 200 }
201 } 201 }
202 change.add_file(source_root, file_id, path, Arc::new(data.content().to_owned())); 202 change.add_file(source_root, file_id, path.into(), Arc::new(data.content().to_owned()));
203 } 203 }
204 change.set_crate_graph(crate_graph); 204 change.set_crate_graph(crate_graph);
205 host.apply_change(change); 205 host.apply_change(change);