Skip to content

Commit 081d7ca

Browse files
committed
added doc comments attributes
1 parent f40bb64 commit 081d7ca

File tree

2 files changed

+133
-128
lines changed

2 files changed

+133
-128
lines changed

git-function-history-lib/src/lib.rs

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
clippy::missing_errors_doc,
1414
clippy::return_self_not_must_use
1515
)]
16-
1716
/// Different types that can extracted from the result of `get_function_history`.
1817
pub mod types;
1918
use ra_ap_syntax::{
20-
ast::{self, HasGenericParams, HasName},
19+
ast::{self, HasDocComments, HasGenericParams, HasName},
2120
AstNode, SourceFile, SyntaxKind,
2221
};
2322

@@ -288,6 +287,7 @@ fn find_function_in_commit(
288287
ast::Fn::cast(p.clone()).map_or_else(
289288
|| {
290289
if let Some(block) = ast::Impl::cast(p.clone()) {
290+
let attr = get_doc_comments_and_attrs(&block);
291291
let stuff = get_stuff(&block, &file_contents, &map);
292292
let generics = get_genrerics_and_lifetime(&block);
293293
parent_block = Some(Block {
@@ -298,8 +298,11 @@ fn find_function_in_commit(
298298
bottom: stuff.1 .1,
299299
block_type: BlockType::Impl,
300300
lines: (stuff.0 .0, stuff.0 .1),
301+
attributes: attr.1,
302+
doc_comments: attr.0,
301303
});
302304
} else if let Some(block) = ast::Trait::cast(p.clone()) {
305+
let attr = get_doc_comments_and_attrs(&block);
303306
let stuff = get_stuff(&block, &file_contents, &map);
304307
let generics = get_genrerics_and_lifetime(&block);
305308
parent_block = Some(Block {
@@ -310,24 +313,29 @@ fn find_function_in_commit(
310313
bottom: stuff.1 .1,
311314
block_type: BlockType::Trait,
312315
lines: (stuff.0 .0, stuff.0 .1),
316+
attributes: attr.1,
317+
doc_comments: attr.0,
313318
});
314319
} else if let Some(block) = ast::ExternBlock::cast(p.clone()) {
320+
let attr = get_doc_comments_and_attrs(&block);
315321
let stuff = get_stuff(&block, &file_contents, &map);
316322
parent_block = Some(Block {
317323
name: block.abi().map(|ty| ty.to_string()),
318-
lifetime: None,
319-
generics: None,
324+
lifetime: Vec::new(),
325+
generics: Vec::new(),
320326
top: stuff.1 .0,
321327
bottom: stuff.1 .1,
322328
block_type: BlockType::Extern,
323329
lines: (stuff.0 .0, stuff.0 .1),
330+
attributes: attr.1,
331+
doc_comments: attr.0,
324332
});
325333
}
326334
},
327335
|function| {
328336
let stuff = get_stuff(&function, &file_contents, &map);
329337
let generics = get_genrerics_and_lifetime(&function);
330-
// TODO: get the functionblock and add it to the parent_fn
338+
let attr = get_doc_comments_and_attrs(&function);
331339
parent_fn.push(FunctionBlock {
332340
name: function.name().unwrap().to_string(),
333341
lifetime: generics.1,
@@ -336,22 +344,34 @@ fn find_function_in_commit(
336344
bottom: stuff.1 .1,
337345
lines: (stuff.0 .0, stuff.0 .1),
338346
return_type: function.ret_type().map(|ty| ty.to_string()),
339-
arguments: function.param_list().map(|args| {
340-
args.params()
347+
arguments: match function.param_list() {
348+
Some(args) => args
349+
.params()
341350
.map(|arg| arg.to_string())
342-
.collect::<Vec<String>>()
343-
}),
351+
.collect::<Vec<String>>(),
352+
None => Vec::new(),
353+
},
354+
attributes: attr.1,
355+
doc_comments: attr.0,
344356
});
345357
},
346358
);
347359
parent = p.parent();
348360
}
349-
361+
let attr = get_doc_comments_and_attrs(&f);
350362
let mut start = stuff.0 .0;
351363
let bb = match map[&start] {
352364
0 => 0,
353365
x => x + 1,
354366
};
367+
let ttt = f
368+
.doc_comments_and_attrs()
369+
.into_iter()
370+
.map(|c| c.to_string())
371+
.collect::<String>();
372+
if !ttt.is_empty() {
373+
println!("{}", ttt);
374+
}
355375
let contents: String = file_contents[bb..f.syntax().text_range().end().into()]
356376
.to_string()
357377
.lines()
@@ -365,19 +385,20 @@ fn find_function_in_commit(
365385
name: f.name().unwrap().to_string(),
366386
contents,
367387
block: parent_block,
368-
function: match parent_fn {
369-
x if x.is_empty() => None,
370-
x => Some(x),
371-
},
388+
function: parent_fn,
372389
return_type: f.ret_type().map(|ty| ty.to_string()),
373-
arguments: f.param_list().map(|args| {
374-
args.params()
390+
arguments: match f.param_list() {
391+
Some(args) => args
392+
.params()
375393
.map(|arg| arg.to_string())
376-
.collect::<Vec<String>>()
377-
}),
394+
.collect::<Vec<String>>(),
395+
None => Vec::new(),
396+
},
378397
lifetime: generics.1,
379398
generics: generics.0,
380399
lines: (stuff.0 .0, stuff.0 .1),
400+
attributes: attr.1,
401+
doc_comments: attr.0,
381402
};
382403
hist.push(function);
383404
}
@@ -436,10 +457,11 @@ fn get_stuff<T: AstNode>(
436457
}
437458
let start = map[&start_line];
438459
let mut start_lines = start_line;
439-
let mut content: String = file[*start..found_start_brace].to_string();
460+
let mut content: String = file[(*start)..=found_start_brace].to_string();
440461
if &content[..1] == "\n" {
441462
content = content[1..].to_string();
442-
} (
463+
}
464+
(
443465
(start_line, end_line),
444466
(
445467
content
@@ -454,33 +476,46 @@ fn get_stuff<T: AstNode>(
454476
format!(
455477
"\n{}: {}",
456478
end_line,
457-
file.lines().nth(if end_line == file.lines().count()-1 {end_line} else {end_line - 1}).unwrap_or("")
479+
file.lines()
480+
.nth(if end_line == file.lines().count() - 1 {
481+
end_line
482+
} else {
483+
end_line - 1
484+
})
485+
.unwrap_or("")
458486
),
459487
),
460488
(starts, end_line),
461489
)
462490
}
463491

464-
fn get_genrerics_and_lifetime<T: HasGenericParams>(
465-
block: &T,
466-
) -> (Option<Vec<String>>, Option<Vec<String>>) {
492+
fn get_genrerics_and_lifetime<T: HasGenericParams>(block: &T) -> (Vec<String>, Vec<String>) {
467493
match block.generic_param_list() {
468-
None => (None, None),
494+
None => (vec![], vec![]),
469495
Some(gt) => (
470-
Some(
471-
gt.generic_params()
472-
.map(|gt| gt.to_string())
473-
.collect::<Vec<String>>(),
474-
),
475-
Some(
476-
gt.lifetime_params()
477-
.map(|lt| lt.to_string())
478-
.collect::<Vec<String>>(),
479-
),
496+
gt.generic_params()
497+
.map(|gt| gt.to_string())
498+
.collect::<Vec<String>>(),
499+
gt.lifetime_params()
500+
.map(|lt| lt.to_string())
501+
.collect::<Vec<String>>(),
480502
),
481503
}
482504
}
483505

506+
fn get_doc_comments_and_attrs<T: HasDocComments>(block: &T) -> (Vec<String>, Vec<String>) {
507+
(
508+
block
509+
.doc_comments()
510+
.map(|c| c.to_string())
511+
.collect::<Vec<String>>(),
512+
block
513+
.attrs()
514+
.map(|c| c.to_string())
515+
.collect::<Vec<String>>(),
516+
)
517+
}
518+
484519
fn find_function_in_commit_with_filetype(
485520
commit: &str,
486521
name: &str,
@@ -616,7 +651,7 @@ mod tests {
616651
println!("time taken: {}", after.num_seconds());
617652
match &output {
618653
Ok(functions) => {
619-
println!("{:?}", functions);
654+
println!("{}", functions);
620655
}
621656
Err(e) => println!("-{}-", e),
622657
}

0 commit comments

Comments
 (0)