diff options
Diffstat (limited to 'crates/ra_ide_api/src/call_info.rs')
-rw-r--r-- | crates/ra_ide_api/src/call_info.rs | 66 |
1 files changed, 18 insertions, 48 deletions
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index ee1e13799..2eb388e0e 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs | |||
@@ -3,9 +3,10 @@ use ra_db::SourceDatabase; | |||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | AstNode, SyntaxNode, TextUnit, TextRange, | 4 | AstNode, SyntaxNode, TextUnit, TextRange, |
5 | SyntaxKind::FN_DEF, | 5 | SyntaxKind::FN_DEF, |
6 | ast::{self, ArgListOwner, DocCommentsOwner}, | 6 | ast::{self, ArgListOwner}, |
7 | algo::find_node_at_offset, | 7 | algo::find_node_at_offset, |
8 | }; | 8 | }; |
9 | use hir::Docs; | ||
9 | 10 | ||
10 | use crate::{FilePosition, CallInfo, db::RootDatabase}; | 11 | use crate::{FilePosition, CallInfo, db::RootDatabase}; |
11 | 12 | ||
@@ -26,7 +27,9 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal | |||
26 | let fn_file = db.parse(symbol.file_id); | 27 | let fn_file = db.parse(symbol.file_id); |
27 | let fn_def = symbol.ptr.to_node(&fn_file); | 28 | let fn_def = symbol.ptr.to_node(&fn_file); |
28 | let fn_def = ast::FnDef::cast(fn_def).unwrap(); | 29 | let fn_def = ast::FnDef::cast(fn_def).unwrap(); |
29 | let mut call_info = CallInfo::new(fn_def)?; | 30 | let function = hir::source_binder::function_from_source(db, symbol.file_id, fn_def)?; |
31 | |||
32 | let mut call_info = CallInfo::new(db, function, fn_def)?; | ||
30 | // If we have a calling expression let's find which argument we are on | 33 | // If we have a calling expression let's find which argument we are on |
31 | let num_params = call_info.parameters.len(); | 34 | let num_params = call_info.parameters.len(); |
32 | let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some(); | 35 | let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some(); |
@@ -110,46 +113,13 @@ impl<'a> FnCallNode<'a> { | |||
110 | } | 113 | } |
111 | 114 | ||
112 | impl CallInfo { | 115 | impl CallInfo { |
113 | fn new(node: &ast::FnDef) -> Option<Self> { | 116 | fn new(db: &RootDatabase, function: hir::Function, node: &ast::FnDef) -> Option<Self> { |
114 | let label: String = if let Some(body) = node.body() { | 117 | let label = crate::completion::function_label(node)?; |
115 | let body_range = body.syntax().range(); | 118 | let doc = function.docs(db); |
116 | let label: String = node | ||
117 | .syntax() | ||
118 | .children() | ||
119 | .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body | ||
120 | .filter(|child| ast::Comment::cast(child).is_none()) // Filter out doc comments | ||
121 | .map(|node| node.text().to_string()) | ||
122 | .collect(); | ||
123 | label | ||
124 | } else { | ||
125 | node.syntax().text().to_string() | ||
126 | }; | ||
127 | |||
128 | let mut doc = None; | ||
129 | if let Some(docs) = node.doc_comment_text() { | ||
130 | // Massage markdown | ||
131 | let mut processed_lines = Vec::new(); | ||
132 | let mut in_code_block = false; | ||
133 | for line in docs.lines() { | ||
134 | if line.starts_with("```") { | ||
135 | in_code_block = !in_code_block; | ||
136 | } | ||
137 | |||
138 | let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { | ||
139 | "```rust".into() | ||
140 | } else { | ||
141 | line.to_string() | ||
142 | }; | ||
143 | |||
144 | processed_lines.push(line); | ||
145 | } | ||
146 | |||
147 | doc = Some(processed_lines.join("\n")); | ||
148 | } | ||
149 | 119 | ||
150 | Some(CallInfo { | 120 | Some(CallInfo { |
151 | parameters: param_list(node), | 121 | parameters: param_list(node), |
152 | label: label.trim().to_owned(), | 122 | label, |
153 | doc, | 123 | doc, |
154 | active_parameter: None, | 124 | active_parameter: None, |
155 | }) | 125 | }) |
@@ -284,7 +254,7 @@ fn bar() { | |||
284 | assert_eq!(info.parameters, vec!["j".to_string()]); | 254 | assert_eq!(info.parameters, vec!["j".to_string()]); |
285 | assert_eq!(info.active_parameter, Some(0)); | 255 | assert_eq!(info.active_parameter, Some(0)); |
286 | assert_eq!(info.label, "fn foo(j: u32) -> u32".to_string()); | 256 | assert_eq!(info.label, "fn foo(j: u32) -> u32".to_string()); |
287 | assert_eq!(info.doc, Some("test".into())); | 257 | assert_eq!(info.doc.map(|it| it.into()), Some("test".to_string())); |
288 | } | 258 | } |
289 | 259 | ||
290 | #[test] | 260 | #[test] |
@@ -313,18 +283,18 @@ pub fn do() { | |||
313 | assert_eq!(info.active_parameter, Some(0)); | 283 | assert_eq!(info.active_parameter, Some(0)); |
314 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); | 284 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); |
315 | assert_eq!( | 285 | assert_eq!( |
316 | info.doc, | 286 | info.doc.map(|it| it.into()), |
317 | Some( | 287 | Some( |
318 | r#"Adds one to the number given. | 288 | r#"Adds one to the number given. |
319 | 289 | ||
320 | # Examples | 290 | # Examples |
321 | 291 | ||
322 | ```rust | 292 | ``` |
323 | let five = 5; | 293 | let five = 5; |
324 | 294 | ||
325 | assert_eq!(6, my_crate::add_one(5)); | 295 | assert_eq!(6, my_crate::add_one(5)); |
326 | ```"# | 296 | ```"# |
327 | .into() | 297 | .to_string() |
328 | ) | 298 | ) |
329 | ); | 299 | ); |
330 | } | 300 | } |
@@ -359,18 +329,18 @@ pub fn do_it() { | |||
359 | assert_eq!(info.active_parameter, Some(0)); | 329 | assert_eq!(info.active_parameter, Some(0)); |
360 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); | 330 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); |
361 | assert_eq!( | 331 | assert_eq!( |
362 | info.doc, | 332 | info.doc.map(|it| it.into()), |
363 | Some( | 333 | Some( |
364 | r#"Adds one to the number given. | 334 | r#"Adds one to the number given. |
365 | 335 | ||
366 | # Examples | 336 | # Examples |
367 | 337 | ||
368 | ```rust | 338 | ``` |
369 | let five = 5; | 339 | let five = 5; |
370 | 340 | ||
371 | assert_eq!(6, my_crate::add_one(5)); | 341 | assert_eq!(6, my_crate::add_one(5)); |
372 | ```"# | 342 | ```"# |
373 | .into() | 343 | .to_string() |
374 | ) | 344 | ) |
375 | ); | 345 | ); |
376 | } | 346 | } |
@@ -414,12 +384,12 @@ pub fn foo() { | |||
414 | ); | 384 | ); |
415 | assert_eq!(info.active_parameter, Some(1)); | 385 | assert_eq!(info.active_parameter, Some(1)); |
416 | assert_eq!( | 386 | assert_eq!( |
417 | info.doc, | 387 | info.doc.map(|it| it.into()), |
418 | Some( | 388 | Some( |
419 | r#"Method is called when writer finishes. | 389 | r#"Method is called when writer finishes. |
420 | 390 | ||
421 | By default this method stops actor's `Context`."# | 391 | By default this method stops actor's `Context`."# |
422 | .into() | 392 | .to_string() |
423 | ) | 393 | ) |
424 | ); | 394 | ); |
425 | } | 395 | } |