aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/completion/complete_scope.rs62
-rw-r--r--crates/ra_ide_api/src/display.rs2
-rw-r--r--crates/ra_ide_api/src/display/navigation_target.rs328
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs16
-rw-r--r--crates/ra_ide_api/src/goto_type_definition.rs4
-rw-r--r--crates/ra_ide_api/src/hover.rs46
-rw-r--r--crates/ra_ide_api/src/impls.rs11
-rw-r--r--crates/ra_ide_api/src/lib.rs4
-rw-r--r--crates/ra_ide_api/src/references.rs12
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs145
10 files changed, 362 insertions, 268 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs
index 4e56de3f5..3e205efd1 100644
--- a/crates/ra_ide_api/src/completion/complete_scope.rs
+++ b/crates/ra_ide_api/src/completion/complete_scope.rs
@@ -598,6 +598,68 @@ mod tests {
598 } 598 }
599 599
600 #[test] 600 #[test]
601 fn completes_std_prelude_if_core_is_defined() {
602 assert_debug_snapshot!(
603 do_reference_completion(
604 "
605 //- /main.rs
606 fn foo() { let x: <|> }
607
608 //- /core/lib.rs
609 #[prelude_import]
610 use prelude::*;
611
612 mod prelude {
613 struct Option;
614 }
615
616 //- /std/lib.rs
617 #[prelude_import]
618 use prelude::*;
619
620 mod prelude {
621 struct String;
622 }
623 "
624 ),
625 @r###"
626 [
627 CompletionItem {
628 label: "String",
629 source_range: [18; 18),
630 delete: [18; 18),
631 insert: "String",
632 kind: Struct,
633 },
634 CompletionItem {
635 label: "core",
636 source_range: [18; 18),
637 delete: [18; 18),
638 insert: "core",
639 kind: Module,
640 },
641 CompletionItem {
642 label: "foo()",
643 source_range: [18; 18),
644 delete: [18; 18),
645 insert: "foo()$0",
646 kind: Function,
647 lookup: "foo",
648 detail: "fn foo()",
649 },
650 CompletionItem {
651 label: "std",
652 source_range: [18; 18),
653 delete: [18; 18),
654 insert: "std",
655 kind: Module,
656 },
657 ]
658 "###
659 );
660 }
661
662 #[test]
601 fn completes_macros_as_value() { 663 fn completes_macros_as_value() {
602 assert_debug_snapshot!( 664 assert_debug_snapshot!(
603 do_reference_completion( 665 do_reference_completion(
diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs
index a980c56bc..30617412a 100644
--- a/crates/ra_ide_api/src/display.rs
+++ b/crates/ra_ide_api/src/display.rs
@@ -15,7 +15,7 @@ pub use function_signature::FunctionSignature;
15pub use navigation_target::NavigationTarget; 15pub use navigation_target::NavigationTarget;
16pub use structure::{file_structure, StructureNode}; 16pub use structure::{file_structure, StructureNode};
17 17
18pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol}; 18pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol, ToNav};
19pub(crate) use short_label::ShortLabel; 19pub(crate) use short_label::ShortLabel;
20 20
21pub(crate) fn function_label(node: &ast::FnDef) -> String { 21pub(crate) fn function_label(node: &ast::FnDef) -> String {
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs
index 1bf81e7d5..41d467564 100644
--- a/crates/ra_ide_api/src/display/navigation_target.rs
+++ b/crates/ra_ide_api/src/display/navigation_target.rs
@@ -29,19 +29,8 @@ pub struct NavigationTarget {
29 docs: Option<String>, 29 docs: Option<String>,
30} 30}
31 31
32fn find_range_from_node( 32pub(crate) trait ToNav {
33 db: &RootDatabase, 33 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
34 src: hir::HirFileId,
35 node: &SyntaxNode,
36) -> (FileId, TextRange) {
37 let text_range = node.text_range();
38 let (file_id, text_range) = src
39 .expansion_info(db)
40 .and_then(|expansion_info| expansion_info.find_range(text_range))
41 .unwrap_or((src, text_range));
42
43 // FIXME: handle recursive macro generated macro
44 (file_id.original_file(db), text_range)
45} 34}
46 35
47impl NavigationTarget { 36impl NavigationTarget {
@@ -95,19 +84,6 @@ impl NavigationTarget {
95 NavigationTarget::from_named(db, file_id.into(), pat, None, None) 84 NavigationTarget::from_named(db, file_id.into(), pat, None, None)
96 } 85 }
97 86
98 pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget {
99 NavigationTarget {
100 file_id: symbol.file_id,
101 name: symbol.name.clone(),
102 kind: symbol.ptr.kind(),
103 full_range: symbol.ptr.range(),
104 focus_range: symbol.name_range,
105 container_name: symbol.container_name.clone(),
106 description: description_from_symbol(db, &symbol),
107 docs: docs_from_symbol(db, &symbol),
108 }
109 }
110
111 pub(crate) fn from_pat( 87 pub(crate) fn from_pat(
112 db: &RootDatabase, 88 db: &RootDatabase,
113 file_id: FileId, 89 file_id: FileId,
@@ -136,39 +112,6 @@ impl NavigationTarget {
136 } 112 }
137 } 113 }
138 114
139 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
140 let src = module.definition_source(db);
141 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
142 match src.ast {
143 ModuleSource::SourceFile(node) => {
144 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
145
146 NavigationTarget::from_syntax(
147 file_id,
148 name,
149 None,
150 text_range,
151 node.syntax(),
152 None,
153 None,
154 )
155 }
156 ModuleSource::Module(node) => {
157 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
158
159 NavigationTarget::from_syntax(
160 file_id,
161 name,
162 None,
163 text_range,
164 node.syntax(),
165 node.doc_comment_text(),
166 node.short_label(),
167 )
168 }
169 }
170 }
171
172 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { 115 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
173 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); 116 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
174 if let Some(src) = module.declaration_source(db) { 117 if let Some(src) = module.declaration_source(db) {
@@ -183,55 +126,7 @@ impl NavigationTarget {
183 src.ast.short_label(), 126 src.ast.short_label(),
184 ); 127 );
185 } 128 }
186 NavigationTarget::from_module(db, module) 129 module.to_nav(db)
187 }
188
189 pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
190 let src = field.source(db);
191 match src.ast {
192 FieldSource::Named(it) => NavigationTarget::from_named(
193 db,
194 src.file_id,
195 &it,
196 it.doc_comment_text(),
197 it.short_label(),
198 ),
199 FieldSource::Pos(it) => {
200 let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax());
201 NavigationTarget::from_syntax(
202 file_id,
203 "".into(),
204 None,
205 text_range,
206 it.syntax(),
207 None,
208 None,
209 )
210 }
211 }
212 }
213
214 pub(crate) fn from_def_source<A, D>(db: &RootDatabase, def: D) -> NavigationTarget
215 where
216 D: HasSource<Ast = A>,
217 A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
218 {
219 let src = def.source(db);
220 NavigationTarget::from_named(
221 db,
222 src.file_id,
223 &src.ast,
224 src.ast.doc_comment_text(),
225 src.ast.short_label(),
226 )
227 }
228
229 pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::Adt) -> NavigationTarget {
230 match adt_def {
231 hir::Adt::Struct(it) => NavigationTarget::from_def_source(db, it),
232 hir::Adt::Union(it) => NavigationTarget::from_def_source(db, it),
233 hir::Adt::Enum(it) => NavigationTarget::from_def_source(db, it),
234 }
235 } 130 }
236 131
237 pub(crate) fn from_def( 132 pub(crate) fn from_def(
@@ -239,14 +134,14 @@ impl NavigationTarget {
239 module_def: hir::ModuleDef, 134 module_def: hir::ModuleDef,
240 ) -> Option<NavigationTarget> { 135 ) -> Option<NavigationTarget> {
241 let nav = match module_def { 136 let nav = match module_def {
242 hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), 137 hir::ModuleDef::Module(module) => module.to_nav(db),
243 hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func), 138 hir::ModuleDef::Function(it) => it.to_nav(db),
244 hir::ModuleDef::Adt(it) => NavigationTarget::from_adt_def(db, it), 139 hir::ModuleDef::Adt(it) => it.to_nav(db),
245 hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it), 140 hir::ModuleDef::Const(it) => it.to_nav(db),
246 hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it), 141 hir::ModuleDef::Static(it) => it.to_nav(db),
247 hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it), 142 hir::ModuleDef::EnumVariant(it) => it.to_nav(db),
248 hir::ModuleDef::Trait(it) => NavigationTarget::from_def_source(db, it), 143 hir::ModuleDef::Trait(it) => it.to_nav(db),
249 hir::ModuleDef::TypeAlias(it) => NavigationTarget::from_def_source(db, it), 144 hir::ModuleDef::TypeAlias(it) => it.to_nav(db),
250 hir::ModuleDef::BuiltinType(..) => { 145 hir::ModuleDef::BuiltinType(..) => {
251 return None; 146 return None;
252 } 147 }
@@ -254,41 +149,6 @@ impl NavigationTarget {
254 Some(nav) 149 Some(nav)
255 } 150 }
256 151
257 pub(crate) fn from_impl_block(
258 db: &RootDatabase,
259 impl_block: hir::ImplBlock,
260 ) -> NavigationTarget {
261 let src = impl_block.source(db);
262 let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax());
263
264 NavigationTarget::from_syntax(
265 file_id,
266 "impl".into(),
267 None,
268 text_range,
269 src.ast.syntax(),
270 None,
271 None,
272 )
273 }
274
275 pub(crate) fn from_assoc_item(
276 db: &RootDatabase,
277 assoc_item: hir::AssocItem,
278 ) -> NavigationTarget {
279 match assoc_item {
280 AssocItem::Function(it) => NavigationTarget::from_def_source(db, it),
281 AssocItem::Const(it) => NavigationTarget::from_def_source(db, it),
282 AssocItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it),
283 }
284 }
285
286 pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget {
287 let src = macro_call.source(db);
288 log::debug!("nav target {:#?}", src.ast.syntax());
289 NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None)
290 }
291
292 #[cfg(test)] 152 #[cfg(test)]
293 pub(crate) fn assert_match(&self, expected: &str) { 153 pub(crate) fn assert_match(&self, expected: &str) {
294 let actual = self.debug_render(); 154 let actual = self.debug_render();
@@ -359,6 +219,172 @@ impl NavigationTarget {
359 } 219 }
360} 220}
361 221
222impl ToNav for FileSymbol {
223 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
224 NavigationTarget {
225 file_id: self.file_id,
226 name: self.name.clone(),
227 kind: self.ptr.kind(),
228 full_range: self.ptr.range(),
229 focus_range: self.name_range,
230 container_name: self.container_name.clone(),
231 description: description_from_symbol(db, self),
232 docs: docs_from_symbol(db, self),
233 }
234 }
235}
236
237pub(crate) trait ToNavFromAst {}
238impl ToNavFromAst for hir::Function {}
239impl ToNavFromAst for hir::Const {}
240impl ToNavFromAst for hir::Static {}
241impl ToNavFromAst for hir::Struct {}
242impl ToNavFromAst for hir::Enum {}
243impl ToNavFromAst for hir::EnumVariant {}
244impl ToNavFromAst for hir::Union {}
245impl ToNavFromAst for hir::TypeAlias {}
246impl ToNavFromAst for hir::Trait {}
247
248impl<D> ToNav for D
249where
250 D: HasSource + ToNavFromAst + Copy,
251 D::Ast: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
252{
253 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
254 let src = self.source(db);
255 NavigationTarget::from_named(
256 db,
257 src.file_id,
258 &src.ast,
259 src.ast.doc_comment_text(),
260 src.ast.short_label(),
261 )
262 }
263}
264
265impl ToNav for hir::Module {
266 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
267 let src = self.definition_source(db);
268 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
269 match src.ast {
270 ModuleSource::SourceFile(node) => {
271 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
272
273 NavigationTarget::from_syntax(
274 file_id,
275 name,
276 None,
277 text_range,
278 node.syntax(),
279 None,
280 None,
281 )
282 }
283 ModuleSource::Module(node) => {
284 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
285
286 NavigationTarget::from_syntax(
287 file_id,
288 name,
289 None,
290 text_range,
291 node.syntax(),
292 node.doc_comment_text(),
293 node.short_label(),
294 )
295 }
296 }
297 }
298}
299
300impl ToNav for hir::ImplBlock {
301 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
302 let src = self.source(db);
303 let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax());
304
305 NavigationTarget::from_syntax(
306 file_id,
307 "impl".into(),
308 None,
309 text_range,
310 src.ast.syntax(),
311 None,
312 None,
313 )
314 }
315}
316
317impl ToNav for hir::StructField {
318 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
319 let src = self.source(db);
320
321 match src.ast {
322 FieldSource::Named(it) => NavigationTarget::from_named(
323 db,
324 src.file_id,
325 &it,
326 it.doc_comment_text(),
327 it.short_label(),
328 ),
329 FieldSource::Pos(it) => {
330 let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax());
331 NavigationTarget::from_syntax(
332 file_id,
333 "".into(),
334 None,
335 text_range,
336 it.syntax(),
337 None,
338 None,
339 )
340 }
341 }
342 }
343}
344
345impl ToNav for hir::MacroDef {
346 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
347 let src = self.source(db);
348 log::debug!("nav target {:#?}", src.ast.syntax());
349 NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None)
350 }
351}
352
353impl ToNav for hir::Adt {
354 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
355 match self {
356 hir::Adt::Struct(it) => it.to_nav(db),
357 hir::Adt::Union(it) => it.to_nav(db),
358 hir::Adt::Enum(it) => it.to_nav(db),
359 }
360 }
361}
362
363impl ToNav for hir::AssocItem {
364 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
365 match self {
366 AssocItem::Function(it) => it.to_nav(db),
367 AssocItem::Const(it) => it.to_nav(db),
368 AssocItem::TypeAlias(it) => it.to_nav(db),
369 }
370 }
371}
372
373fn find_range_from_node(
374 db: &RootDatabase,
375 src: hir::HirFileId,
376 node: &SyntaxNode,
377) -> (FileId, TextRange) {
378 let text_range = node.text_range();
379 let (file_id, text_range) = src
380 .expansion_info(db)
381 .and_then(|expansion_info| expansion_info.find_range(text_range))
382 .unwrap_or((src, text_range));
383
384 // FIXME: handle recursive macro generated macro
385 (file_id.original_file(db), text_range)
386}
387
362pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { 388pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
363 let parse = db.parse(symbol.file_id); 389 let parse = db.parse(symbol.file_id);
364 let node = symbol.ptr.to_node(parse.tree().syntax()); 390 let node = symbol.ptr.to_node(parse.tree().syntax());
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index afa59cbe3..713b61d5e 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -9,7 +9,7 @@ use ra_syntax::{
9 9
10use crate::{ 10use crate::{
11 db::RootDatabase, 11 db::RootDatabase,
12 display::ShortLabel, 12 display::{ShortLabel, ToNav},
13 references::{classify_name_ref, NameKind::*}, 13 references::{classify_name_ref, NameKind::*},
14 FilePosition, NavigationTarget, RangeInfo, 14 FilePosition, NavigationTarget, RangeInfo,
15}; 15};
@@ -56,16 +56,16 @@ pub(crate) fn reference_definition(
56 56
57 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); 57 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
58 match name_kind { 58 match name_kind {
59 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), 59 Some(Macro(mac)) => return Exact(mac.to_nav(db)),
60 Some(Field(field)) => return Exact(NavigationTarget::from_field(db, field)), 60 Some(Field(field)) => return Exact(field.to_nav(db)),
61 Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)), 61 Some(AssocItem(assoc)) => return Exact(assoc.to_nav(db)),
62 Some(Def(def)) => match NavigationTarget::from_def(db, def) { 62 Some(Def(def)) => match NavigationTarget::from_def(db, def) {
63 Some(nav) => return Exact(nav), 63 Some(nav) => return Exact(nav),
64 None => return Approximate(vec![]), 64 None => return Approximate(vec![]),
65 }, 65 },
66 Some(SelfType(ty)) => { 66 Some(SelfType(ty)) => {
67 if let Some((def_id, _)) = ty.as_adt() { 67 if let Some((adt, _)) = ty.as_adt() {
68 return Exact(NavigationTarget::from_adt_def(db, def_id)); 68 return Exact(adt.to_nav(db));
69 } 69 }
70 } 70 }
71 Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), 71 Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)),
@@ -79,7 +79,7 @@ pub(crate) fn reference_definition(
79 // Fallback index based approach: 79 // Fallback index based approach:
80 let navs = crate::symbol_index::index_resolve(db, name_ref) 80 let navs = crate::symbol_index::index_resolve(db, name_ref)
81 .into_iter() 81 .into_iter()
82 .map(|s| NavigationTarget::from_symbol(db, s)) 82 .map(|s| s.to_nav(db))
83 .collect(); 83 .collect();
84 Approximate(navs) 84 Approximate(navs)
85} 85}
@@ -95,7 +95,7 @@ pub(crate) fn name_definition(
95 if module.has_semi() { 95 if module.has_semi() {
96 let src = hir::Source { file_id: file_id.into(), ast: module }; 96 let src = hir::Source { file_id: file_id.into(), ast: module };
97 if let Some(child_module) = hir::Module::from_declaration(db, src) { 97 if let Some(child_module) = hir::Module::from_declaration(db, src) {
98 let nav = NavigationTarget::from_module(db, child_module); 98 let nav = child_module.to_nav(db);
99 return Some(vec![nav]); 99 return Some(vec![nav]);
100 } 100 }
101 } 101 }
diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs
index 059d80524..71146591d 100644
--- a/crates/ra_ide_api/src/goto_type_definition.rs
+++ b/crates/ra_ide_api/src/goto_type_definition.rs
@@ -3,7 +3,7 @@
3use ra_db::SourceDatabase; 3use ra_db::SourceDatabase;
4use ra_syntax::{ast, AstNode}; 4use ra_syntax::{ast, AstNode};
5 5
6use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo}; 6use crate::{db::RootDatabase, display::ToNav, FilePosition, NavigationTarget, RangeInfo};
7 7
8pub(crate) fn goto_type_definition( 8pub(crate) fn goto_type_definition(
9 db: &RootDatabase, 9 db: &RootDatabase,
@@ -33,7 +33,7 @@ pub(crate) fn goto_type_definition(
33 33
34 let adt_def = analyzer.autoderef(db, ty).find_map(|ty| ty.as_adt().map(|adt| adt.0))?; 34 let adt_def = analyzer.autoderef(db, ty).find_map(|ty| ty.as_adt().map(|adt| adt.0))?;
35 35
36 let nav = NavigationTarget::from_adt_def(db, adt_def); 36 let nav = adt_def.to_nav(db);
37 Some(RangeInfo::new(node.text_range(), vec![nav])) 37 Some(RangeInfo::new(node.text_range(), vec![nav]))
38} 38}
39 39
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index ba328efa1..cc41390b2 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -117,27 +117,23 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
117 hir::AssocItem::Const(it) => from_def_source(db, it), 117 hir::AssocItem::Const(it) => from_def_source(db, it),
118 hir::AssocItem::TypeAlias(it) => from_def_source(db, it), 118 hir::AssocItem::TypeAlias(it) => from_def_source(db, it),
119 }), 119 }),
120 Some(Def(it)) => { 120 Some(Def(it)) => match it {
121 match it { 121 hir::ModuleDef::Module(it) => {
122 hir::ModuleDef::Module(it) => { 122 if let hir::ModuleSource::Module(it) = it.definition_source(db).ast {
123 if let hir::ModuleSource::Module(it) = it.definition_source(db).ast { 123 res.extend(hover_text(it.doc_comment_text(), it.short_label()))
124 res.extend(hover_text(it.doc_comment_text(), it.short_label()))
125 }
126 }
127 hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)),
128 hir::ModuleDef::Adt(Adt::Struct(it)) => res.extend(from_def_source(db, it)),
129 hir::ModuleDef::Adt(Adt::Union(it)) => res.extend(from_def_source(db, it)),
130 hir::ModuleDef::Adt(Adt::Enum(it)) => res.extend(from_def_source(db, it)),
131 hir::ModuleDef::EnumVariant(it) => res.extend(from_def_source(db, it)),
132 hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)),
133 hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)),
134 hir::ModuleDef::Trait(it) => res.extend(from_def_source(db, it)),
135 hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)),
136 hir::ModuleDef::BuiltinType(_) => {
137 // FIXME: hover for builtin Type ?
138 } 124 }
139 } 125 }
140 } 126 hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)),
127 hir::ModuleDef::Adt(Adt::Struct(it)) => res.extend(from_def_source(db, it)),
128 hir::ModuleDef::Adt(Adt::Union(it)) => res.extend(from_def_source(db, it)),
129 hir::ModuleDef::Adt(Adt::Enum(it)) => res.extend(from_def_source(db, it)),
130 hir::ModuleDef::EnumVariant(it) => res.extend(from_def_source(db, it)),
131 hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)),
132 hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)),
133 hir::ModuleDef::Trait(it) => res.extend(from_def_source(db, it)),
134 hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)),
135 hir::ModuleDef::BuiltinType(it) => res.extend(Some(it.to_string())),
136 },
141 Some(SelfType(ty)) => { 137 Some(SelfType(ty)) => {
142 if let Some((adt_def, _)) = ty.as_adt() { 138 if let Some((adt_def, _)) = ty.as_adt() {
143 res.extend(match adt_def { 139 res.extend(match adt_def {
@@ -722,4 +718,16 @@ fn func(foo: i32) { if true { <|>foo; }; }
722 assert_eq!(trim_markup_opt(hover.info.first()), Some("macro_rules! foo")); 718 assert_eq!(trim_markup_opt(hover.info.first()), Some("macro_rules! foo"));
723 assert_eq!(hover.info.is_exact(), true); 719 assert_eq!(hover.info.is_exact(), true);
724 } 720 }
721
722 #[test]
723 fn test_hover_tuple_field() {
724 let (analysis, position) = single_file_with_position(
725 "
726 struct TS(String, i32<|>);
727 ",
728 );
729 let hover = analysis.hover(position).unwrap().unwrap();
730 assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
731 assert_eq!(hover.info.is_exact(), true);
732 }
725} 733}
diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs
index b899ed3a5..bc9b66550 100644
--- a/crates/ra_ide_api/src/impls.rs
+++ b/crates/ra_ide_api/src/impls.rs
@@ -4,7 +4,7 @@ use hir::{db::HirDatabase, ApplicationTy, FromSource, Ty, TypeCtor};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; 5use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
6 6
7use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo}; 7use crate::{db::RootDatabase, display::ToNav, FilePosition, NavigationTarget, RangeInfo};
8 8
9pub(crate) fn goto_implementation( 9pub(crate) fn goto_implementation(
10 db: &RootDatabase, 10 db: &RootDatabase,
@@ -58,7 +58,7 @@ fn impls_for_def(
58 impls 58 impls
59 .all_impls() 59 .all_impls()
60 .filter(|impl_block| is_equal_for_find_impls(&ty, &impl_block.target_ty(db))) 60 .filter(|impl_block| is_equal_for_find_impls(&ty, &impl_block.target_ty(db)))
61 .map(|imp| NavigationTarget::from_impl_block(db, imp)) 61 .map(|imp| imp.to_nav(db))
62 .collect(), 62 .collect(),
63 ) 63 )
64} 64}
@@ -75,12 +75,7 @@ fn impls_for_trait(
75 let krate = module.krate(); 75 let krate = module.krate();
76 let impls = db.impls_in_crate(krate); 76 let impls = db.impls_in_crate(krate);
77 77
78 Some( 78 Some(impls.lookup_impl_blocks_for_trait(tr).map(|imp| imp.to_nav(db)).collect())
79 impls
80 .lookup_impl_blocks_for_trait(tr)
81 .map(|imp| NavigationTarget::from_impl_block(db, imp))
82 .collect(),
83 )
84} 79}
85 80
86fn is_equal_for_find_impls(original_ty: &Ty, impl_ty: &Ty) -> bool { 81fn is_equal_for_find_impls(original_ty: &Ty, impl_ty: &Ty) -> bool {
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index d0188da44..484fbcc82 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -56,7 +56,7 @@ use ra_db::{
56}; 56};
57use ra_syntax::{SourceFile, TextRange, TextUnit}; 57use ra_syntax::{SourceFile, TextRange, TextUnit};
58 58
59use crate::{db::LineIndexDatabase, symbol_index::FileSymbol}; 59use crate::{db::LineIndexDatabase, display::ToNav, symbol_index::FileSymbol};
60 60
61pub use crate::{ 61pub use crate::{
62 assists::{Assist, AssistId}, 62 assists::{Assist, AssistId},
@@ -351,7 +351,7 @@ impl Analysis {
351 self.with_db(|db| { 351 self.with_db(|db| {
352 symbol_index::world_symbols(db, query) 352 symbol_index::world_symbols(db, query)
353 .into_iter() 353 .into_iter()
354 .map(|s| NavigationTarget::from_symbol(db, s)) 354 .map(|s| s.to_nav(db))
355 .collect::<Vec<_>>() 355 .collect::<Vec<_>>()
356 }) 356 })
357 } 357 }
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index b5b1c9a16..0a76bf484 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -19,7 +19,9 @@ use ra_db::{SourceDatabase, SourceDatabaseExt};
19use ra_prof::profile; 19use ra_prof::profile;
20use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit}; 20use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit};
21 21
22use crate::{db::RootDatabase, FilePosition, FileRange, NavigationTarget, RangeInfo}; 22use crate::{
23 db::RootDatabase, display::ToNav, FilePosition, FileRange, NavigationTarget, RangeInfo,
24};
23 25
24pub(crate) use self::{ 26pub(crate) use self::{
25 classify::{classify_name, classify_name_ref}, 27 classify::{classify_name, classify_name_ref},
@@ -76,12 +78,12 @@ pub(crate) fn find_all_refs(
76 let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?; 78 let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?;
77 79
78 let declaration = match def.kind { 80 let declaration = match def.kind {
79 NameKind::Macro(mac) => NavigationTarget::from_macro_def(db, mac), 81 NameKind::Macro(mac) => mac.to_nav(db),
80 NameKind::Field(field) => NavigationTarget::from_field(db, field), 82 NameKind::Field(field) => field.to_nav(db),
81 NameKind::AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc), 83 NameKind::AssocItem(assoc) => assoc.to_nav(db),
82 NameKind::Def(def) => NavigationTarget::from_def(db, def)?, 84 NameKind::Def(def) => NavigationTarget::from_def(db, def)?,
83 NameKind::SelfType(ref ty) => match ty.as_adt() { 85 NameKind::SelfType(ref ty) => match ty.as_adt() {
84 Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id), 86 Some((adt, _)) => adt.to_nav(db),
85 None => return None, 87 None => return None,
86 }, 88 },
87 NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat), 89 NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat),
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index 33f3caceb..1ee68abe2 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -9,12 +9,15 @@ use ra_syntax::{
9 ast::{self, NameOwner}, 9 ast::{self, NameOwner},
10 AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind, 10 AstNode, Direction, SmolStr, SyntaxElement, SyntaxKind,
11 SyntaxKind::*, 11 SyntaxKind::*,
12 TextRange, T, 12 SyntaxNode, TextRange, T,
13}; 13};
14 14
15use crate::{ 15use crate::{
16 db::RootDatabase, 16 db::RootDatabase,
17 references::{classify_name_ref, NameKind::*}, 17 references::{
18 classify_name, classify_name_ref,
19 NameKind::{self, *},
20 },
18 FileId, 21 FileId,
19}; 22};
20 23
@@ -100,81 +103,43 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
100 if node.ancestors().any(|it| it.kind() == ATTR) { 103 if node.ancestors().any(|it| it.kind() == ATTR) {
101 continue; 104 continue;
102 } 105 }
103 if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
104 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
105 match name_kind {
106 Some(Macro(_)) => "macro",
107 Some(Field(_)) => "field",
108 Some(AssocItem(hir::AssocItem::Function(_))) => "function",
109 Some(AssocItem(hir::AssocItem::Const(_))) => "constant",
110 Some(AssocItem(hir::AssocItem::TypeAlias(_))) => "type",
111 Some(Def(hir::ModuleDef::Module(_))) => "module",
112 Some(Def(hir::ModuleDef::Function(_))) => "function",
113 Some(Def(hir::ModuleDef::Adt(_))) => "type",
114 Some(Def(hir::ModuleDef::EnumVariant(_))) => "constant",
115 Some(Def(hir::ModuleDef::Const(_))) => "constant",
116 Some(Def(hir::ModuleDef::Static(_))) => "constant",
117 Some(Def(hir::ModuleDef::Trait(_))) => "type",
118 Some(Def(hir::ModuleDef::TypeAlias(_))) => "type",
119 Some(Def(hir::ModuleDef::BuiltinType(_))) => "type",
120 Some(SelfType(_)) => "type",
121 Some(Pat((_, ptr))) => {
122 let pat = ptr.to_node(&root);
123 if let Some(name) = pat.name() {
124 let text = name.text();
125 let shadow_count =
126 bindings_shadow_count.entry(text.clone()).or_default();
127 binding_hash =
128 Some(calc_binding_hash(file_id, &text, *shadow_count))
129 }
130 106
131 let analyzer = 107 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
132 hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); 108 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
133 if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) { 109
134 "variable.mut" 110 if let Some(Pat((_, ptr))) = &name_kind {
135 } else { 111 let pat = ptr.to_node(&root);
136 "variable" 112 if let Some(name) = pat.name() {
137 } 113 let text = name.text();
138 } 114 let shadow_count = bindings_shadow_count.entry(text.clone()).or_default();
139 Some(SelfParam(_)) => "type", 115 binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count))
140 Some(GenericParam(_)) => "type",
141 None => "text",
142 } 116 }
143 } else { 117 };
144 "text" 118
145 } 119 name_kind
120 .map_or("text", |it| highlight_name(db, file_id, name_ref.syntax(), &root, it))
146 } 121 }
147 NAME => { 122 NAME => {
148 if let Some(name) = node.as_node().cloned().and_then(ast::Name::cast) { 123 let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap();
149 let analyzer = hir::SourceAnalyzer::new(db, file_id, name.syntax(), None); 124 let name_kind = classify_name(db, file_id, &name).map(|d| d.kind);
150 if let Some(pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) { 125
151 if let Some(name) = pat.name() { 126 if let Some(Pat((_, ptr))) = &name_kind {
152 let text = name.text(); 127 let pat = ptr.to_node(&root);
153 let shadow_count = 128 if let Some(name) = pat.name() {
154 bindings_shadow_count.entry(text.clone()).or_default(); 129 let text = name.text();
155 *shadow_count += 1; 130 let shadow_count = bindings_shadow_count.entry(text.clone()).or_default();
156 binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count)) 131 *shadow_count += 1;
157 } 132 binding_hash = Some(calc_binding_hash(file_id, &text, *shadow_count))
158
159 if is_variable_mutable(db, &analyzer, pat) {
160 "variable.mut"
161 } else {
162 "variable"
163 }
164 } else {
165 name.syntax()
166 .parent()
167 .map(|x| match x.kind() {
168 TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => {
169 "type"
170 }
171 RECORD_FIELD_DEF => "field",
172 _ => "function",
173 })
174 .unwrap_or("function")
175 } 133 }
176 } else { 134 };
177 "text" 135
136 match name_kind {
137 Some(name_kind) => highlight_name(db, file_id, name.syntax(), &root, name_kind),
138 None => name.syntax().parent().map_or("function", |x| match x.kind() {
139 TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type",
140 RECORD_FIELD_DEF => "field",
141 _ => "function",
142 }),
178 } 143 }
179 } 144 }
180 INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", 145 INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
@@ -272,6 +237,42 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
272 buf 237 buf
273} 238}
274 239
240fn highlight_name(
241 db: &RootDatabase,
242 file_id: FileId,
243 node: &SyntaxNode,
244 root: &SyntaxNode,
245 name_kind: NameKind,
246) -> &'static str {
247 match name_kind {
248 Macro(_) => "macro",
249 Field(_) => "field",
250 AssocItem(hir::AssocItem::Function(_)) => "function",
251 AssocItem(hir::AssocItem::Const(_)) => "constant",
252 AssocItem(hir::AssocItem::TypeAlias(_)) => "type",
253 Def(hir::ModuleDef::Module(_)) => "module",
254 Def(hir::ModuleDef::Function(_)) => "function",
255 Def(hir::ModuleDef::Adt(_)) => "type",
256 Def(hir::ModuleDef::EnumVariant(_)) => "constant",
257 Def(hir::ModuleDef::Const(_)) => "constant",
258 Def(hir::ModuleDef::Static(_)) => "constant",
259 Def(hir::ModuleDef::Trait(_)) => "type",
260 Def(hir::ModuleDef::TypeAlias(_)) => "type",
261 Def(hir::ModuleDef::BuiltinType(_)) => "type",
262 SelfType(_) => "type",
263 SelfParam(_) => "type",
264 GenericParam(_) => "type",
265 Pat((_, ptr)) => {
266 let analyzer = hir::SourceAnalyzer::new(db, file_id, node, None);
267 if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) {
268 "variable.mut"
269 } else {
270 "variable"
271 }
272 }
273 }
274}
275
275//FIXME: like, real html escaping 276//FIXME: like, real html escaping
276fn html_escape(text: &str) -> String { 277fn html_escape(text: &str) -> String {
277 text.replace("<", "&lt;").replace(">", "&gt;") 278 text.replace("<", "&lt;").replace(">", "&gt;")