Simplify CallInfo label and documentation

This commit is contained in:
Jeremy A. Kolb 2019-01-22 16:28:02 -05:00
parent cc6898acd1
commit 9582a439e1

View file

@ -1,5 +1,3 @@
use std::cmp::{max, min};
use ra_db::SyntaxDatabase; use ra_db::SyntaxDatabase;
use ra_syntax::{ use ra_syntax::{
AstNode, SyntaxNode, TextUnit, TextRange, AstNode, SyntaxNode, TextUnit, TextRange,
@ -107,15 +105,13 @@ impl<'a> FnCallNode<'a> {
impl CallInfo { impl CallInfo {
fn new(node: &ast::FnDef) -> Option<Self> { fn new(node: &ast::FnDef) -> Option<Self> {
let mut doc = None; let label: String = if let Some(body) = node.body() {
// Strip the body out for the label.
let mut label: String = if let Some(body) = node.body() {
let body_range = body.syntax().range(); let body_range = body.syntax().range();
let label: String = node let label: String = node
.syntax() .syntax()
.children() .children()
.filter(|child| !child.range().is_subrange(&body_range)) .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body
.filter(|child| ast::Comment::cast(child).is_none()) // Filter out doc comments
.map(|node| node.text().to_string()) .map(|node| node.text().to_string())
.collect(); .collect();
label label
@ -123,16 +119,9 @@ impl CallInfo {
node.syntax().text().to_string() node.syntax().text().to_string()
}; };
if let Some((comment_range, docs)) = extract_doc_comments(node) { let mut doc = None;
let comment_range = comment_range let docs = node.doc_comment_text();
.checked_sub(node.syntax().range().start()) if !docs.is_empty() {
.unwrap();
let start = comment_range.start().to_usize();
let end = comment_range.end().to_usize();
// Remove the comment from the label
label.replace_range(start..end, "");
// Massage markdown // Massage markdown
let mut processed_lines = Vec::new(); let mut processed_lines = Vec::new();
let mut in_code_block = false; let mut in_code_block = false;
@ -150,10 +139,8 @@ impl CallInfo {
processed_lines.push(line); processed_lines.push(line);
} }
if !processed_lines.is_empty() {
doc = Some(processed_lines.join("\n")); doc = Some(processed_lines.join("\n"));
} }
}
Some(CallInfo { Some(CallInfo {
parameters: param_list(node), parameters: param_list(node),
@ -164,26 +151,6 @@ impl CallInfo {
} }
} }
fn extract_doc_comments(node: &ast::FnDef) -> Option<(TextRange, String)> {
if node.doc_comments().count() == 0 {
return None;
}
let comment_text = node.doc_comment_text();
let (begin, end) = node
.doc_comments()
.map(|comment| comment.syntax().range())
.map(|range| (range.start().to_usize(), range.end().to_usize()))
.fold((std::usize::MAX, std::usize::MIN), |acc, range| {
(min(acc.0, range.0), max(acc.1, range.1))
});
let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end));
Some((range, comment_text))
}
fn param_list(node: &ast::FnDef) -> Vec<String> { fn param_list(node: &ast::FnDef) -> Vec<String> {
let mut res = vec![]; let mut res = vec![];
if let Some(param_list) = node.param_list() { if let Some(param_list) = node.param_list() {