diff options
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r-- | crates/ra_ide/src/display/short_label.rs | 12 | ||||
-rw-r--r-- | crates/ra_ide/src/goto_definition.rs | 36 | ||||
-rw-r--r-- | crates/ra_ide/src/hover.rs | 57 | ||||
-rw-r--r-- | crates/ra_ide/src/references.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 17 |
6 files changed, 111 insertions, 17 deletions
diff --git a/crates/ra_ide/src/display/short_label.rs b/crates/ra_ide/src/display/short_label.rs index 0fdf8e9a5..010c34705 100644 --- a/crates/ra_ide/src/display/short_label.rs +++ b/crates/ra_ide/src/display/short_label.rs | |||
@@ -47,6 +47,12 @@ impl ShortLabel for ast::Module { | |||
47 | } | 47 | } |
48 | } | 48 | } |
49 | 49 | ||
50 | impl ShortLabel for ast::SourceFile { | ||
51 | fn short_label(&self) -> Option<String> { | ||
52 | None | ||
53 | } | ||
54 | } | ||
55 | |||
50 | impl ShortLabel for ast::TypeAlias { | 56 | impl ShortLabel for ast::TypeAlias { |
51 | fn short_label(&self) -> Option<String> { | 57 | fn short_label(&self) -> Option<String> { |
52 | short_label_from_node(self, "type ") | 58 | short_label_from_node(self, "type ") |
@@ -55,7 +61,11 @@ impl ShortLabel for ast::TypeAlias { | |||
55 | 61 | ||
56 | impl ShortLabel for ast::Const { | 62 | impl ShortLabel for ast::Const { |
57 | fn short_label(&self) -> Option<String> { | 63 | fn short_label(&self) -> Option<String> { |
58 | short_label_from_ty(self, self.ty(), "const ") | 64 | let mut new_buf = short_label_from_ty(self, self.ty(), "const ")?; |
65 | if let Some(expr) = self.body() { | ||
66 | format_to!(new_buf, " = {}", expr.syntax()); | ||
67 | } | ||
68 | Some(new_buf) | ||
59 | } | 69 | } |
60 | } | 70 | } |
61 | 71 | ||
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 4e3f428fa..45389fd23 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use hir::Semantics; | 1 | use hir::Semantics; |
2 | use ra_ide_db::{ | 2 | use ra_ide_db::{ |
3 | defs::{classify_name, classify_name_ref, NameClass}, | 3 | defs::{classify_name, classify_name_ref}, |
4 | symbol_index, RootDatabase, | 4 | symbol_index, RootDatabase, |
5 | }; | 5 | }; |
6 | use ra_syntax::{ | 6 | use ra_syntax::{ |
@@ -40,10 +40,7 @@ pub(crate) fn goto_definition( | |||
40 | reference_definition(&sema, &name_ref).to_vec() | 40 | reference_definition(&sema, &name_ref).to_vec() |
41 | }, | 41 | }, |
42 | ast::Name(name) => { | 42 | ast::Name(name) => { |
43 | let def = match classify_name(&sema, &name)? { | 43 | let def = classify_name(&sema, &name)?.definition(sema.db); |
44 | NameClass::Definition(def) | NameClass::ConstReference(def) => def, | ||
45 | NameClass::FieldShorthand { local: _, field } => field, | ||
46 | }; | ||
47 | let nav = def.try_to_nav(sema.db)?; | 44 | let nav = def.try_to_nav(sema.db)?; |
48 | vec![nav] | 45 | vec![nav] |
49 | }, | 46 | }, |
@@ -86,8 +83,7 @@ pub(crate) fn reference_definition( | |||
86 | ) -> ReferenceResult { | 83 | ) -> ReferenceResult { |
87 | let name_kind = classify_name_ref(sema, name_ref); | 84 | let name_kind = classify_name_ref(sema, name_ref); |
88 | if let Some(def) = name_kind { | 85 | if let Some(def) = name_kind { |
89 | let def = def.definition(); | 86 | let def = def.definition(sema.db); |
90 | |||
91 | return match def.try_to_nav(sema.db) { | 87 | return match def.try_to_nav(sema.db) { |
92 | Some(nav) => ReferenceResult::Exact(nav), | 88 | Some(nav) => ReferenceResult::Exact(nav), |
93 | None => ReferenceResult::Approximate(Vec::new()), | 89 | None => ReferenceResult::Approximate(Vec::new()), |
@@ -134,6 +130,32 @@ mod tests { | |||
134 | } | 130 | } |
135 | 131 | ||
136 | #[test] | 132 | #[test] |
133 | fn goto_def_for_extern_crate() { | ||
134 | check( | ||
135 | r#" | ||
136 | //- /main.rs | ||
137 | extern crate std<|>; | ||
138 | //- /std/lib.rs | ||
139 | // empty | ||
140 | //^ file | ||
141 | "#, | ||
142 | ) | ||
143 | } | ||
144 | |||
145 | #[test] | ||
146 | fn goto_def_for_renamed_extern_crate() { | ||
147 | check( | ||
148 | r#" | ||
149 | //- /main.rs | ||
150 | extern crate std as abc<|>; | ||
151 | //- /std/lib.rs | ||
152 | // empty | ||
153 | //^ file | ||
154 | "#, | ||
155 | ) | ||
156 | } | ||
157 | |||
158 | #[test] | ||
137 | fn goto_def_in_items() { | 159 | fn goto_def_in_items() { |
138 | check( | 160 | check( |
139 | r#" | 161 | r#" |
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 385e3e64e..f66f62bfb 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs | |||
@@ -85,8 +85,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn | |||
85 | let node = token.parent(); | 85 | let node = token.parent(); |
86 | let definition = match_ast! { | 86 | let definition = match_ast! { |
87 | match node { | 87 | match node { |
88 | ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).map(|d| d.definition()), | 88 | ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).map(|d| d.definition(sema.db)), |
89 | ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition()), | 89 | ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition(sema.db)), |
90 | _ => None, | 90 | _ => None, |
91 | } | 91 | } |
92 | }; | 92 | }; |
@@ -304,7 +304,10 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> { | |||
304 | let docs = Documentation::from_ast(&it).map(Into::into); | 304 | let docs = Documentation::from_ast(&it).map(Into::into); |
305 | hover_markup(docs, it.short_label(), mod_path) | 305 | hover_markup(docs, it.short_label(), mod_path) |
306 | } | 306 | } |
307 | _ => None, | 307 | ModuleSource::SourceFile(it) => { |
308 | let docs = Documentation::from_ast(&it).map(Into::into); | ||
309 | hover_markup(docs, it.short_label(), mod_path) | ||
310 | } | ||
308 | }, | 311 | }, |
309 | ModuleDef::Function(it) => from_def_source(db, it, mod_path), | 312 | ModuleDef::Function(it) => from_def_source(db, it, mod_path), |
310 | ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path), | 313 | ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path), |
@@ -587,16 +590,16 @@ fn main() { | |||
587 | #[test] | 590 | #[test] |
588 | fn hover_const_static() { | 591 | fn hover_const_static() { |
589 | check( | 592 | check( |
590 | r#"const foo<|>: u32 = 0;"#, | 593 | r#"const foo<|>: u32 = 123;"#, |
591 | expect![[r#" | 594 | expect![[r#" |
592 | *foo* | 595 | *foo* |
593 | ```rust | 596 | ```rust |
594 | const foo: u32 | 597 | const foo: u32 = 123 |
595 | ``` | 598 | ``` |
596 | "#]], | 599 | "#]], |
597 | ); | 600 | ); |
598 | check( | 601 | check( |
599 | r#"static foo<|>: u32 = 0;"#, | 602 | r#"static foo<|>: u32 = 456;"#, |
600 | expect![[r#" | 603 | expect![[r#" |
601 | *foo* | 604 | *foo* |
602 | ```rust | 605 | ```rust |
@@ -831,7 +834,7 @@ fn main() { | |||
831 | expect![[r#" | 834 | expect![[r#" |
832 | *C* | 835 | *C* |
833 | ```rust | 836 | ```rust |
834 | const C: u32 | 837 | const C: u32 = 1 |
835 | ``` | 838 | ``` |
836 | "#]], | 839 | "#]], |
837 | ) | 840 | ) |
@@ -1138,6 +1141,46 @@ fn bar() { fo<|>o(); } | |||
1138 | } | 1141 | } |
1139 | 1142 | ||
1140 | #[test] | 1143 | #[test] |
1144 | fn test_hover_extern_crate() { | ||
1145 | check( | ||
1146 | r#" | ||
1147 | //- /main.rs | ||
1148 | extern crate st<|>d; | ||
1149 | //- /std/lib.rs | ||
1150 | //! Standard library for this test | ||
1151 | //! | ||
1152 | //! Printed? | ||
1153 | //! abc123 | ||
1154 | "#, | ||
1155 | expect![[r#" | ||
1156 | *std* | ||
1157 | Standard library for this test | ||
1158 | |||
1159 | Printed? | ||
1160 | abc123 | ||
1161 | "#]], | ||
1162 | ); | ||
1163 | check( | ||
1164 | r#" | ||
1165 | //- /main.rs | ||
1166 | extern crate std as ab<|>c; | ||
1167 | //- /std/lib.rs | ||
1168 | //! Standard library for this test | ||
1169 | //! | ||
1170 | //! Printed? | ||
1171 | //! abc123 | ||
1172 | "#, | ||
1173 | expect![[r#" | ||
1174 | *abc* | ||
1175 | Standard library for this test | ||
1176 | |||
1177 | Printed? | ||
1178 | abc123 | ||
1179 | "#]], | ||
1180 | ); | ||
1181 | } | ||
1182 | |||
1183 | #[test] | ||
1141 | fn test_hover_mod_with_same_name_as_function() { | 1184 | fn test_hover_mod_with_same_name_as_function() { |
1142 | check( | 1185 | check( |
1143 | r#" | 1186 | r#" |
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index cf456630a..453985de3 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs | |||
@@ -130,13 +130,13 @@ fn find_name( | |||
130 | opt_name: Option<ast::Name>, | 130 | opt_name: Option<ast::Name>, |
131 | ) -> Option<RangeInfo<Definition>> { | 131 | ) -> Option<RangeInfo<Definition>> { |
132 | if let Some(name) = opt_name { | 132 | if let Some(name) = opt_name { |
133 | let def = classify_name(sema, &name)?.definition(); | 133 | let def = classify_name(sema, &name)?.definition(sema.db); |
134 | let range = name.syntax().text_range(); | 134 | let range = name.syntax().text_range(); |
135 | return Some(RangeInfo::new(range, def)); | 135 | return Some(RangeInfo::new(range, def)); |
136 | } | 136 | } |
137 | let name_ref = | 137 | let name_ref = |
138 | sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?; | 138 | sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?; |
139 | let def = classify_name_ref(sema, &name_ref)?.definition(); | 139 | let def = classify_name_ref(sema, &name_ref)?.definition(sema.db); |
140 | let range = name_ref.syntax().text_range(); | 140 | let range = name_ref.syntax().text_range(); |
141 | Some(RangeInfo::new(range, def)) | 141 | Some(RangeInfo::new(range, def)) |
142 | } | 142 | } |
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index f71b804fe..6b7874460 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs | |||
@@ -495,6 +495,7 @@ fn highlight_element( | |||
495 | }; | 495 | }; |
496 | 496 | ||
497 | match name_kind { | 497 | match name_kind { |
498 | Some(NameClass::ExternCrate(_)) => HighlightTag::Module.into(), | ||
498 | Some(NameClass::Definition(def)) => { | 499 | Some(NameClass::Definition(def)) => { |
499 | highlight_name(db, def, false) | HighlightModifier::Definition | 500 | highlight_name(db, def, false) | HighlightModifier::Definition |
500 | } | 501 | } |
@@ -522,6 +523,7 @@ fn highlight_element( | |||
522 | let possibly_unsafe = is_possibly_unsafe(&name_ref); | 523 | let possibly_unsafe = is_possibly_unsafe(&name_ref); |
523 | match classify_name_ref(sema, &name_ref) { | 524 | match classify_name_ref(sema, &name_ref) { |
524 | Some(name_kind) => match name_kind { | 525 | Some(name_kind) => match name_kind { |
526 | NameRefClass::ExternCrate(_) => HighlightTag::Module.into(), | ||
525 | NameRefClass::Definition(def) => { | 527 | NameRefClass::Definition(def) => { |
526 | if let Definition::Local(local) = &def { | 528 | if let Definition::Local(local) = &def { |
527 | if let Some(name) = local.name(db) { | 529 | if let Some(name) = local.name(db) { |
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 730efff0d..09062c38e 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs | |||
@@ -391,6 +391,23 @@ macro_rules! noop { | |||
391 | ); | 391 | ); |
392 | } | 392 | } |
393 | 393 | ||
394 | #[test] | ||
395 | fn test_extern_crate() { | ||
396 | check_highlighting( | ||
397 | r#" | ||
398 | //- /main.rs | ||
399 | extern crate std; | ||
400 | extern crate alloc as abc; | ||
401 | //- /std/lib.rs | ||
402 | pub struct S; | ||
403 | //- /alloc/lib.rs | ||
404 | pub struct A | ||
405 | "#, | ||
406 | expect_file!["crates/ra_ide/test_data/highlight_extern_crate.html"], | ||
407 | false, | ||
408 | ); | ||
409 | } | ||
410 | |||
394 | /// Highlights the code given by the `ra_fixture` argument, renders the | 411 | /// Highlights the code given by the `ra_fixture` argument, renders the |
395 | /// result as HTML, and compares it with the HTML file given as `snapshot`. | 412 | /// result as HTML, and compares it with the HTML file given as `snapshot`. |
396 | /// Note that the `snapshot` file is overwritten by the rendered HTML. | 413 | /// Note that the `snapshot` file is overwritten by the rendered HTML. |