aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-06-11 15:22:14 +0100
committerAleksey Kladov <[email protected]>2019-06-11 16:28:51 +0100
commit647c8f3df8e5f798f940ce0b93e5e0ccd59b0e7f (patch)
treeb8e4deaa77f4648c5645fe6caa8e66e298b0d2b8 /crates/ra_ide_api/src
parent46bc8675edd403ffcd8963e4f26447b283e9d09e (diff)
reduce duplication in hover
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r--crates/ra_ide_api/src/hover.rs80
1 files changed, 24 insertions, 56 deletions
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index fe108a589..0f68cacca 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -1,6 +1,7 @@
1use ra_db::SourceDatabase; 1use ra_db::SourceDatabase;
2use ra_syntax::{ 2use 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};
6use hir::HirDisplay; 7use hir::HirDisplay;
@@ -95,10 +96,7 @@ 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 src = it.source(db);
100 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()));
101 }
102 Some(Macro(it)) => { 100 Some(Macro(it)) => {
103 let it = it.source(db).1; 101 let it = it.source(db).1;
104 res.extend(hover_text(it.doc_comment_text(), None)); 102 res.extend(hover_text(it.doc_comment_text(), None));
@@ -110,14 +108,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
110 } 108 }
111 } 109 }
112 Some(AssocItem(it)) => match it { 110 Some(AssocItem(it)) => match it {
113 hir::ImplItem::Method(it) => { 111 hir::ImplItem::Method(it) => res.extend(from_def_source(db, it)),
114 let src = it.source(db); 112 hir::ImplItem::Const(it) => res.extend(from_def_source(db, it)),
115 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
116 }
117 hir::ImplItem::Const(it) => {
118 let src = it.source(db);
119 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
120 }
121 hir::ImplItem::TypeAlias(it) => { 113 hir::ImplItem::TypeAlias(it) => {
122 let it = it.source(db).1; 114 let it = it.source(db).1;
123 res.extend(hover_text(it.doc_comment_text(), it.short_label())) 115 res.extend(hover_text(it.doc_comment_text(), it.short_label()))
@@ -131,34 +123,16 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
131 res.extend(hover_text(it.doc_comment_text(), it.short_label())) 123 res.extend(hover_text(it.doc_comment_text(), it.short_label()))
132 } 124 }
133 } 125 }
134 hir::ModuleDef::Function(it) => { 126 hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)),
135 let src = it.source(db); 127 hir::ModuleDef::Struct(it) => res.extend(from_def_source(db, it)),
136 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) 128 hir::ModuleDef::Union(it) => res.extend(from_def_source(db, it)),
137 } 129 hir::ModuleDef::Enum(it) => res.extend(from_def_source(db, it)),
138 hir::ModuleDef::Struct(it) => {
139 let src = it.source(db);
140 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
141 }
142 hir::ModuleDef::Union(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 src = it.source(db);
148 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
149 }
150 hir::ModuleDef::EnumVariant(it) => { 130 hir::ModuleDef::EnumVariant(it) => {
151 let src = it.source(db); 131 let src = it.source(db);
152 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) 132 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
153 } 133 }
154 hir::ModuleDef::Const(it) => { 134 hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)),
155 let src = it.source(db); 135 hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)),
156 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
157 }
158 hir::ModuleDef::Static(it) => {
159 let src = it.source(db);
160 res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
161 }
162 hir::ModuleDef::Trait(it) => { 136 hir::ModuleDef::Trait(it) => {
163 let it = it.source(db).1; 137 let it = it.source(db).1;
164 res.extend(hover_text(it.doc_comment_text(), it.short_label())) 138 res.extend(hover_text(it.doc_comment_text(), it.short_label()))
@@ -175,24 +149,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
175 Some(SelfType(ty)) => { 149 Some(SelfType(ty)) => {
176 if let Some((adt_def, _)) = ty.as_adt() { 150 if let Some((adt_def, _)) = ty.as_adt() {
177 match adt_def { 151 match adt_def {
178 hir::AdtDef::Struct(it) => { 152 hir::AdtDef::Struct(it) => res.extend(from_def_source(db, it)),
179 let src = it.source(db); 153 hir::AdtDef::Union(it) => res.extend(from_def_source(db, it)),
180 res.extend(hover_text( 154 hir::AdtDef::Enum(it) => res.extend(from_def_source(db, it)),
181 src.ast.doc_comment_text(),
182 src.ast.short_label(),
183 ))
184 }
185 hir::AdtDef::Union(it) => {
186 let it = it.source(db).1;
187 res.extend(hover_text(it.doc_comment_text(), it.short_label()))
188 }
189 hir::AdtDef::Enum(it) => {
190 let src = it.source(db);
191 res.extend(hover_text(
192 src.ast.doc_comment_text(),
193 src.ast.short_label(),
194 ))
195 }
196 } 155 }
197 } 156 }
198 } 157 }
@@ -276,7 +235,16 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
276 return None; 235 return None;
277 } 236 }
278 let res = RangeInfo::new(range, res); 237 let res = RangeInfo::new(range, res);
279 Some(res) 238 return Some(res);
239
240 fn from_def_source<A, D>(db: &RootDatabase, def: D) -> Option<String>
241 where
242 D: hir::HasSource<Ast = TreeArc<A>>,
243 A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
244 {
245 let src = def.source(db);
246 hover_text(src.ast.doc_comment_text(), src.ast.short_label())
247 }
280} 248}
281 249
282pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> { 250pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {