diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-11 16:29:42 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-11 16:29:42 +0100 |
commit | 80aa9d5f9f55341d2a51176e385d8aa6d2d2cec8 (patch) | |
tree | b6fee9faf5f82ba6ee063667605e39ac3bcbe37c /crates/ra_ide_api/src/hover.rs | |
parent | e505fe9d7b96f3454711e923c70d763c5cee5f47 (diff) | |
parent | ff6f6b3a5223ddab81c7357a3c59bdb09936a552 (diff) |
Merge #1395
1395: move docs under code model r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/hover.rs')
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 115 |
1 files changed, 38 insertions, 77 deletions
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index fbabeb194..ad00abe49 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs | |||
@@ -1,9 +1,10 @@ | |||
1 | use ra_db::SourceDatabase; | 1 | use ra_db::SourceDatabase; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | AstNode, ast::{self, DocCommentsOwner}, | 3 | AstNode, TreeArc, |
4 | ast::{self, DocCommentsOwner}, | ||
4 | algo::{find_covering_element, find_node_at_offset, ancestors_at_offset, visit::{visitor, Visitor}}, | 5 | algo::{find_covering_element, find_node_at_offset, ancestors_at_offset, visit::{visitor, Visitor}}, |
5 | }; | 6 | }; |
6 | use hir::HirDisplay; | 7 | use hir::{HirDisplay, HasSource}; |
7 | 8 | ||
8 | use crate::{ | 9 | use crate::{ |
9 | db::RootDatabase, | 10 | db::RootDatabase, |
@@ -95,78 +96,38 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn | |||
95 | let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); | 96 | let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); |
96 | 97 | ||
97 | match classify_name_ref(db, &analyzer, name_ref) { | 98 | match classify_name_ref(db, &analyzer, name_ref) { |
98 | Some(Method(it)) => { | 99 | Some(Method(it)) => res.extend(from_def_source(db, it)), |
99 | let it = it.source(db).1; | ||
100 | res.extend(hover_text(it.doc_comment_text(), it.short_label())); | ||
101 | } | ||
102 | Some(Macro(it)) => { | 100 | Some(Macro(it)) => { |
103 | let it = it.source(db).1; | 101 | let src = it.source(db); |
104 | res.extend(hover_text(it.doc_comment_text(), None)); | 102 | res.extend(hover_text(src.ast.doc_comment_text(), None)); |
105 | } | 103 | } |
106 | Some(FieldAccess(it)) => { | 104 | Some(FieldAccess(it)) => { |
107 | let it = it.source(db).1; | 105 | let src = it.source(db); |
108 | if let hir::FieldSource::Named(it) = it { | 106 | if let hir::FieldSource::Named(it) = src.ast { |
109 | res.extend(hover_text(it.doc_comment_text(), it.short_label())); | 107 | res.extend(hover_text(it.doc_comment_text(), it.short_label())); |
110 | } | 108 | } |
111 | } | 109 | } |
112 | Some(AssocItem(it)) => match it { | 110 | Some(AssocItem(it)) => res.extend(match it { |
113 | hir::ImplItem::Method(it) => { | 111 | hir::ImplItem::Method(it) => from_def_source(db, it), |
114 | let it = it.source(db).1; | 112 | hir::ImplItem::Const(it) => from_def_source(db, it), |
115 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | 113 | hir::ImplItem::TypeAlias(it) => from_def_source(db, it), |
116 | } | 114 | }), |
117 | hir::ImplItem::Const(it) => { | ||
118 | let it = it.source(db).1; | ||
119 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
120 | } | ||
121 | hir::ImplItem::TypeAlias(it) => { | ||
122 | let it = it.source(db).1; | ||
123 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
124 | } | ||
125 | }, | ||
126 | Some(Def(it)) => { | 115 | Some(Def(it)) => { |
127 | match it { | 116 | match it { |
128 | hir::ModuleDef::Module(it) => { | 117 | hir::ModuleDef::Module(it) => { |
129 | let it = it.definition_source(db).1; | 118 | if let hir::ModuleSource::Module(it) = it.definition_source(db).ast { |
130 | if let hir::ModuleSource::Module(it) = it { | ||
131 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | 119 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) |
132 | } | 120 | } |
133 | } | 121 | } |
134 | hir::ModuleDef::Function(it) => { | 122 | hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)), |
135 | let it = it.source(db).1; | 123 | hir::ModuleDef::Struct(it) => res.extend(from_def_source(db, it)), |
136 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | 124 | hir::ModuleDef::Union(it) => res.extend(from_def_source(db, it)), |
137 | } | 125 | hir::ModuleDef::Enum(it) => res.extend(from_def_source(db, it)), |
138 | hir::ModuleDef::Struct(it) => { | 126 | hir::ModuleDef::EnumVariant(it) => res.extend(from_def_source(db, it)), |
139 | let it = it.source(db).1; | 127 | hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)), |
140 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | 128 | hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)), |
141 | } | 129 | hir::ModuleDef::Trait(it) => res.extend(from_def_source(db, it)), |
142 | hir::ModuleDef::Union(it) => { | 130 | hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)), |
143 | let it = it.source(db).1; | ||
144 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
145 | } | ||
146 | hir::ModuleDef::Enum(it) => { | ||
147 | let it = it.source(db).1; | ||
148 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
149 | } | ||
150 | hir::ModuleDef::EnumVariant(it) => { | ||
151 | let it = it.source(db).1; | ||
152 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
153 | } | ||
154 | hir::ModuleDef::Const(it) => { | ||
155 | let it = it.source(db).1; | ||
156 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
157 | } | ||
158 | hir::ModuleDef::Static(it) => { | ||
159 | let it = it.source(db).1; | ||
160 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
161 | } | ||
162 | hir::ModuleDef::Trait(it) => { | ||
163 | let it = it.source(db).1; | ||
164 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
165 | } | ||
166 | hir::ModuleDef::TypeAlias(it) => { | ||
167 | let it = it.source(db).1; | ||
168 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
169 | } | ||
170 | hir::ModuleDef::BuiltinType(_) => { | 131 | hir::ModuleDef::BuiltinType(_) => { |
171 | // FIXME: hover for builtin Type ? | 132 | // FIXME: hover for builtin Type ? |
172 | } | 133 | } |
@@ -174,20 +135,11 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn | |||
174 | } | 135 | } |
175 | Some(SelfType(ty)) => { | 136 | Some(SelfType(ty)) => { |
176 | if let Some((adt_def, _)) = ty.as_adt() { | 137 | if let Some((adt_def, _)) = ty.as_adt() { |
177 | match adt_def { | 138 | res.extend(match adt_def { |
178 | hir::AdtDef::Struct(it) => { | 139 | hir::AdtDef::Struct(it) => from_def_source(db, it), |
179 | let it = it.source(db).1; | 140 | hir::AdtDef::Union(it) => from_def_source(db, it), |
180 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | 141 | hir::AdtDef::Enum(it) => from_def_source(db, it), |
181 | } | 142 | }) |
182 | hir::AdtDef::Union(it) => { | ||
183 | let it = it.source(db).1; | ||
184 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
185 | } | ||
186 | hir::AdtDef::Enum(it) => { | ||
187 | let it = it.source(db).1; | ||
188 | res.extend(hover_text(it.doc_comment_text(), it.short_label())) | ||
189 | } | ||
190 | } | ||
191 | } | 143 | } |
192 | } | 144 | } |
193 | Some(Pat(_)) => { | 145 | Some(Pat(_)) => { |
@@ -270,7 +222,16 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn | |||
270 | return None; | 222 | return None; |
271 | } | 223 | } |
272 | let res = RangeInfo::new(range, res); | 224 | let res = RangeInfo::new(range, res); |
273 | Some(res) | 225 | return Some(res); |
226 | |||
227 | fn from_def_source<A, D>(db: &RootDatabase, def: D) -> Option<String> | ||
228 | where | ||
229 | D: HasSource<Ast = TreeArc<A>>, | ||
230 | A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, | ||
231 | { | ||
232 | let src = def.source(db); | ||
233 | hover_text(src.ast.doc_comment_text(), src.ast.short_label()) | ||
234 | } | ||
274 | } | 235 | } |
275 | 236 | ||
276 | pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> { | 237 | pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> { |