diff options
Diffstat (limited to 'crates/ra_ide_api/src/display')
-rw-r--r-- | crates/ra_ide_api/src/display/function_signature.rs | 9 | ||||
-rw-r--r-- | crates/ra_ide_api/src/display/navigation_target.rs | 200 |
2 files changed, 55 insertions, 154 deletions
diff --git a/crates/ra_ide_api/src/display/function_signature.rs b/crates/ra_ide_api/src/display/function_signature.rs index d09950bce..b0b0c7122 100644 --- a/crates/ra_ide_api/src/display/function_signature.rs +++ b/crates/ra_ide_api/src/display/function_signature.rs | |||
@@ -1,10 +1,11 @@ | |||
1 | use super::{where_predicates, generic_parameters}; | ||
2 | use crate::db; | ||
3 | use std::fmt::{self, Display}; | 1 | use std::fmt::{self, Display}; |
2 | |||
4 | use join_to_string::join; | 3 | use join_to_string::join; |
5 | use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; | 4 | use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; |
6 | use std::convert::From; | 5 | use std::convert::From; |
7 | use hir::{Docs, Documentation}; | 6 | use hir::{Docs, Documentation, HasSource}; |
7 | |||
8 | use crate::{db, display::{where_predicates, generic_parameters}}; | ||
8 | 9 | ||
9 | /// Contains information about a function signature | 10 | /// Contains information about a function signature |
10 | #[derive(Debug)] | 11 | #[derive(Debug)] |
@@ -33,7 +34,7 @@ impl FunctionSignature { | |||
33 | 34 | ||
34 | pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self { | 35 | pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self { |
35 | let doc = function.docs(db); | 36 | let doc = function.docs(db); |
36 | let (_, ast_node) = function.source(db); | 37 | let ast_node = function.source(db).ast; |
37 | FunctionSignature::from(&*ast_node).with_doc_opt(doc) | 38 | FunctionSignature::from(&*ast_node).with_doc_opt(doc) |
38 | } | 39 | } |
39 | } | 40 | } |
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index cfd3f5478..823cdaaf3 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use ra_db::{FileId, SourceDatabase}; | 1 | use ra_db::{FileId, SourceDatabase}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, | 3 | SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, TreeArc, |
4 | SyntaxKind::{self, NAME}, | 4 | SyntaxKind::{self, NAME}, |
5 | ast::{self, DocCommentsOwner}, | 5 | ast::{self, DocCommentsOwner}, |
6 | algo::visit::{visitor, Visitor}, | 6 | algo::visit::{visitor, Visitor}, |
7 | }; | 7 | }; |
8 | use hir::{ModuleSource, FieldSource, ImplItem}; | 8 | use hir::{ModuleSource, FieldSource, ImplItem, HasSource}; |
9 | 9 | ||
10 | use crate::{FileSymbol, db::RootDatabase}; | 10 | use crate::{FileSymbol, db::RootDatabase}; |
11 | use super::short_label::ShortLabel; | 11 | use super::short_label::ShortLabel; |
@@ -129,10 +129,10 @@ impl NavigationTarget { | |||
129 | } | 129 | } |
130 | 130 | ||
131 | pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget { | 131 | pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget { |
132 | let (file_id, source) = module.definition_source(db); | 132 | let src = module.definition_source(db); |
133 | let file_id = file_id.as_original_file(); | 133 | let file_id = src.file_id.as_original_file(); |
134 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); | 134 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); |
135 | match source { | 135 | match src.ast { |
136 | ModuleSource::SourceFile(node) => { | 136 | ModuleSource::SourceFile(node) => { |
137 | NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None) | 137 | NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None) |
138 | } | 138 | } |
@@ -149,34 +149,24 @@ impl NavigationTarget { | |||
149 | 149 | ||
150 | pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { | 150 | pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { |
151 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); | 151 | let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); |
152 | if let Some((file_id, source)) = module.declaration_source(db) { | 152 | if let Some(src) = module.declaration_source(db) { |
153 | let file_id = file_id.as_original_file(); | 153 | let file_id = src.file_id.as_original_file(); |
154 | return NavigationTarget::from_syntax( | 154 | return NavigationTarget::from_syntax( |
155 | file_id, | 155 | file_id, |
156 | name, | 156 | name, |
157 | None, | 157 | None, |
158 | source.syntax(), | 158 | src.ast.syntax(), |
159 | source.doc_comment_text(), | 159 | src.ast.doc_comment_text(), |
160 | source.short_label(), | 160 | src.ast.short_label(), |
161 | ); | 161 | ); |
162 | } | 162 | } |
163 | NavigationTarget::from_module(db, module) | 163 | NavigationTarget::from_module(db, module) |
164 | } | 164 | } |
165 | 165 | ||
166 | pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget { | ||
167 | let (file_id, fn_def) = func.source(db); | ||
168 | NavigationTarget::from_named( | ||
169 | file_id.original_file(db), | ||
170 | &*fn_def, | ||
171 | fn_def.doc_comment_text(), | ||
172 | fn_def.short_label(), | ||
173 | ) | ||
174 | } | ||
175 | |||
176 | pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { | 166 | pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { |
177 | let (file_id, field) = field.source(db); | 167 | let src = field.source(db); |
178 | let file_id = file_id.original_file(db); | 168 | let file_id = src.file_id.original_file(db); |
179 | match field { | 169 | match src.ast { |
180 | FieldSource::Named(it) => { | 170 | FieldSource::Named(it) => { |
181 | NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.short_label()) | 171 | NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.short_label()) |
182 | } | 172 | } |
@@ -186,35 +176,25 @@ impl NavigationTarget { | |||
186 | } | 176 | } |
187 | } | 177 | } |
188 | 178 | ||
179 | pub(crate) fn from_def_source<A, D>(db: &RootDatabase, def: D) -> NavigationTarget | ||
180 | where | ||
181 | D: HasSource<Ast = TreeArc<A>>, | ||
182 | A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, | ||
183 | { | ||
184 | let src = def.source(db); | ||
185 | NavigationTarget::from_named( | ||
186 | src.file_id.original_file(db), | ||
187 | &*src.ast, | ||
188 | src.ast.doc_comment_text(), | ||
189 | src.ast.short_label(), | ||
190 | ) | ||
191 | } | ||
192 | |||
189 | pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget { | 193 | pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget { |
190 | match adt_def { | 194 | match adt_def { |
191 | hir::AdtDef::Struct(s) => { | 195 | hir::AdtDef::Struct(it) => NavigationTarget::from_def_source(db, it), |
192 | let (file_id, node) = s.source(db); | 196 | hir::AdtDef::Union(it) => NavigationTarget::from_def_source(db, it), |
193 | NavigationTarget::from_named( | 197 | hir::AdtDef::Enum(it) => NavigationTarget::from_def_source(db, it), |
194 | file_id.original_file(db), | ||
195 | &*node, | ||
196 | node.doc_comment_text(), | ||
197 | node.short_label(), | ||
198 | ) | ||
199 | } | ||
200 | hir::AdtDef::Union(s) => { | ||
201 | let (file_id, node) = s.source(db); | ||
202 | NavigationTarget::from_named( | ||
203 | file_id.original_file(db), | ||
204 | &*node, | ||
205 | node.doc_comment_text(), | ||
206 | node.short_label(), | ||
207 | ) | ||
208 | } | ||
209 | hir::AdtDef::Enum(s) => { | ||
210 | let (file_id, node) = s.source(db); | ||
211 | NavigationTarget::from_named( | ||
212 | file_id.original_file(db), | ||
213 | &*node, | ||
214 | node.doc_comment_text(), | ||
215 | node.short_label(), | ||
216 | ) | ||
217 | } | ||
218 | } | 198 | } |
219 | } | 199 | } |
220 | 200 | ||
@@ -224,79 +204,15 @@ impl NavigationTarget { | |||
224 | ) -> Option<NavigationTarget> { | 204 | ) -> Option<NavigationTarget> { |
225 | let nav = match module_def { | 205 | let nav = match module_def { |
226 | hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), | 206 | hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), |
227 | hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func), | 207 | hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func), |
228 | hir::ModuleDef::Struct(s) => { | 208 | hir::ModuleDef::Struct(it) => NavigationTarget::from_adt_def(db, it.into()), |
229 | let (file_id, node) = s.source(db); | 209 | hir::ModuleDef::Enum(it) => NavigationTarget::from_adt_def(db, it.into()), |
230 | NavigationTarget::from_named( | 210 | hir::ModuleDef::Union(it) => NavigationTarget::from_adt_def(db, it.into()), |
231 | file_id.original_file(db), | 211 | hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it), |
232 | &*node, | 212 | hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it), |
233 | node.doc_comment_text(), | 213 | hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it), |
234 | node.short_label(), | 214 | hir::ModuleDef::Trait(it) => NavigationTarget::from_def_source(db, it), |
235 | ) | 215 | hir::ModuleDef::TypeAlias(it) => NavigationTarget::from_def_source(db, it), |
236 | } | ||
237 | hir::ModuleDef::Union(s) => { | ||
238 | let (file_id, node) = s.source(db); | ||
239 | NavigationTarget::from_named( | ||
240 | file_id.original_file(db), | ||
241 | &*node, | ||
242 | node.doc_comment_text(), | ||
243 | node.short_label(), | ||
244 | ) | ||
245 | } | ||
246 | hir::ModuleDef::Const(s) => { | ||
247 | let (file_id, node) = s.source(db); | ||
248 | NavigationTarget::from_named( | ||
249 | file_id.original_file(db), | ||
250 | &*node, | ||
251 | node.doc_comment_text(), | ||
252 | node.short_label(), | ||
253 | ) | ||
254 | } | ||
255 | hir::ModuleDef::Static(s) => { | ||
256 | let (file_id, node) = s.source(db); | ||
257 | NavigationTarget::from_named( | ||
258 | file_id.original_file(db), | ||
259 | &*node, | ||
260 | node.doc_comment_text(), | ||
261 | node.short_label(), | ||
262 | ) | ||
263 | } | ||
264 | hir::ModuleDef::Enum(e) => { | ||
265 | let (file_id, node) = e.source(db); | ||
266 | NavigationTarget::from_named( | ||
267 | file_id.original_file(db), | ||
268 | &*node, | ||
269 | node.doc_comment_text(), | ||
270 | node.short_label(), | ||
271 | ) | ||
272 | } | ||
273 | hir::ModuleDef::EnumVariant(var) => { | ||
274 | let (file_id, node) = var.source(db); | ||
275 | NavigationTarget::from_named( | ||
276 | file_id.original_file(db), | ||
277 | &*node, | ||
278 | node.doc_comment_text(), | ||
279 | node.short_label(), | ||
280 | ) | ||
281 | } | ||
282 | hir::ModuleDef::Trait(e) => { | ||
283 | let (file_id, node) = e.source(db); | ||
284 | NavigationTarget::from_named( | ||
285 | file_id.original_file(db), | ||
286 | &*node, | ||
287 | node.doc_comment_text(), | ||
288 | node.short_label(), | ||
289 | ) | ||
290 | } | ||
291 | hir::ModuleDef::TypeAlias(e) => { | ||
292 | let (file_id, node) = e.source(db); | ||
293 | NavigationTarget::from_named( | ||
294 | file_id.original_file(db), | ||
295 | &*node, | ||
296 | node.doc_comment_text(), | ||
297 | node.short_label(), | ||
298 | ) | ||
299 | } | ||
300 | hir::ModuleDef::BuiltinType(..) => { | 216 | hir::ModuleDef::BuiltinType(..) => { |
301 | return None; | 217 | return None; |
302 | } | 218 | } |
@@ -308,12 +224,12 @@ impl NavigationTarget { | |||
308 | db: &RootDatabase, | 224 | db: &RootDatabase, |
309 | impl_block: hir::ImplBlock, | 225 | impl_block: hir::ImplBlock, |
310 | ) -> NavigationTarget { | 226 | ) -> NavigationTarget { |
311 | let (file_id, node) = impl_block.source(db); | 227 | let src = impl_block.source(db); |
312 | NavigationTarget::from_syntax( | 228 | NavigationTarget::from_syntax( |
313 | file_id.as_original_file(), | 229 | src.file_id.as_original_file(), |
314 | "impl".into(), | 230 | "impl".into(), |
315 | None, | 231 | None, |
316 | node.syntax(), | 232 | src.ast.syntax(), |
317 | None, | 233 | None, |
318 | None, | 234 | None, |
319 | ) | 235 | ) |
@@ -321,35 +237,19 @@ impl NavigationTarget { | |||
321 | 237 | ||
322 | pub(crate) fn from_impl_item(db: &RootDatabase, impl_item: hir::ImplItem) -> NavigationTarget { | 238 | pub(crate) fn from_impl_item(db: &RootDatabase, impl_item: hir::ImplItem) -> NavigationTarget { |
323 | match impl_item { | 239 | match impl_item { |
324 | ImplItem::Method(f) => NavigationTarget::from_function(db, f), | 240 | ImplItem::Method(it) => NavigationTarget::from_def_source(db, it), |
325 | ImplItem::Const(c) => { | 241 | ImplItem::Const(it) => NavigationTarget::from_def_source(db, it), |
326 | let (file_id, node) = c.source(db); | 242 | ImplItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it), |
327 | NavigationTarget::from_named( | ||
328 | file_id.original_file(db), | ||
329 | &*node, | ||
330 | node.doc_comment_text(), | ||
331 | node.short_label(), | ||
332 | ) | ||
333 | } | ||
334 | ImplItem::TypeAlias(a) => { | ||
335 | let (file_id, node) = a.source(db); | ||
336 | NavigationTarget::from_named( | ||
337 | file_id.original_file(db), | ||
338 | &*node, | ||
339 | node.doc_comment_text(), | ||
340 | node.short_label(), | ||
341 | ) | ||
342 | } | ||
343 | } | 243 | } |
344 | } | 244 | } |
345 | 245 | ||
346 | pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { | 246 | pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { |
347 | let (file_id, node) = macro_call.source(db); | 247 | let src = macro_call.source(db); |
348 | log::debug!("nav target {}", node.syntax().debug_dump()); | 248 | log::debug!("nav target {}", src.ast.syntax().debug_dump()); |
349 | NavigationTarget::from_named( | 249 | NavigationTarget::from_named( |
350 | file_id.original_file(db), | 250 | src.file_id.original_file(db), |
351 | &*node, | 251 | &*src.ast, |
352 | node.doc_comment_text(), | 252 | src.ast.doc_comment_text(), |
353 | None, | 253 | None, |
354 | ) | 254 | ) |
355 | } | 255 | } |