diff options
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/display/short_label.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide/src/goto_definition.rs | 36 | ||||
-rw-r--r-- | crates/ra_ide/src/hover.rs | 49 | ||||
-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 | ||||
-rw-r--r-- | crates/ra_ide/test_data/highlight_extern_crate.html | 40 |
7 files changed, 142 insertions, 12 deletions
diff --git a/crates/ra_ide/src/display/short_label.rs b/crates/ra_ide/src/display/short_label.rs index 0fdf8e9a5..b5ff9fa2d 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 ") |
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..9c7348898 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), |
@@ -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. |
diff --git a/crates/ra_ide/test_data/highlight_extern_crate.html b/crates/ra_ide/test_data/highlight_extern_crate.html new file mode 100644 index 000000000..800d894c7 --- /dev/null +++ b/crates/ra_ide/test_data/highlight_extern_crate.html | |||
@@ -0,0 +1,40 @@ | |||
1 | |||
2 | <style> | ||
3 | body { margin: 0; } | ||
4 | pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; } | ||
5 | |||
6 | .lifetime { color: #DFAF8F; font-style: italic; } | ||
7 | .comment { color: #7F9F7F; } | ||
8 | .documentation { color: #629755; } | ||
9 | .injected { opacity: 0.65 ; } | ||
10 | .struct, .enum { color: #7CB8BB; } | ||
11 | .enum_variant { color: #BDE0F3; } | ||
12 | .string_literal { color: #CC9393; } | ||
13 | .field { color: #94BFF3; } | ||
14 | .function { color: #93E0E3; } | ||
15 | .function.unsafe { color: #BC8383; } | ||
16 | .operator.unsafe { color: #BC8383; } | ||
17 | .parameter { color: #94BFF3; } | ||
18 | .text { color: #DCDCCC; } | ||
19 | .type { color: #7CB8BB; } | ||
20 | .builtin_type { color: #8CD0D3; } | ||
21 | .type_param { color: #DFAF8F; } | ||
22 | .attribute { color: #94BFF3; } | ||
23 | .numeric_literal { color: #BFEBBF; } | ||
24 | .bool_literal { color: #BFE6EB; } | ||
25 | .macro { color: #94BFF3; } | ||
26 | .module { color: #AFD8AF; } | ||
27 | .value_param { color: #DCDCCC; } | ||
28 | .variable { color: #DCDCCC; } | ||
29 | .format_specifier { color: #CC696B; } | ||
30 | .mutable { text-decoration: underline; } | ||
31 | .escape_sequence { color: #94BFF3; } | ||
32 | .keyword { color: #F0DFAF; font-weight: bold; } | ||
33 | .keyword.unsafe { color: #BC8383; font-weight: bold; } | ||
34 | .control { font-style: italic; } | ||
35 | |||
36 | .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } | ||
37 | </style> | ||
38 | <pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module">std</span><span class="punctuation">;</span> | ||
39 | <span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module">alloc</span> <span class="keyword">as</span> <span class="module">abc</span><span class="punctuation">;</span> | ||
40 | </code></pre> \ No newline at end of file | ||