Skip to content

Commit c5b9bf7

Browse files
authored
Document the code (#196)
1 parent a421121 commit c5b9bf7

24 files changed

Lines changed: 612 additions & 81 deletions

rust-code-analysis-cli/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,14 @@ fn act_on_file(language: Option<LANG>, path: PathBuf, cfg: &Config) -> std::io::
112112
action::<Function>(&language, source, &path, pr, cfg)
113113
} else if !cfg.find_filter.is_empty() {
114114
let cfg = FindCfg {
115-
path: Some(path.clone()),
115+
path: path.clone(),
116116
filters: cfg.find_filter.clone(),
117117
line_start: cfg.line_start,
118118
line_end: cfg.line_end,
119119
};
120120
action::<Find>(&language, source, &path, pr, cfg)
121121
} else if cfg.count_lock.is_some() {
122122
let cfg = CountCfg {
123-
path: Some(path.clone()),
124123
filters: cfg.count_filter.clone(),
125124
stats: cfg.count_lock.as_ref().unwrap().clone(),
126125
};

src/ast.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,52 @@ use serde::{Deserialize, Serialize};
33

44
use crate::*;
55

6+
/// Start and end positions of a node in a code in terms of rows and columns.
7+
///
8+
/// The first and second fields represent the row and column associated to
9+
/// the start position of a node.
10+
///
11+
/// The third and fourth fields represent the row and column associated to
12+
/// the end position of a node.
613
pub type Span = Option<(usize, usize, usize, usize)>;
714

15+
/// The payload of an `Ast` request.
816
#[derive(Debug, Deserialize, Serialize)]
917
pub struct AstPayload {
18+
/// The id associated to a request for an `AST`
1019
pub id: String,
20+
/// The filename associated to a source code file
1121
pub file_name: String,
22+
/// The code to be represented as an `AST`
1223
pub code: String,
24+
/// If `true`, nodes representing comments are ignored
1325
pub comment: bool,
26+
/// If `true`, the start and end positions of a node in a code
27+
/// are considered
1428
pub span: bool,
1529
}
1630

31+
/// The response of an `AST` request.
1732
#[derive(Debug, Serialize)]
1833
pub struct AstResponse {
34+
/// The id associated to a request for an `AST`
1935
id: String,
36+
/// The root node of an `AST`
37+
///
38+
/// If `None`, an error has occurred
2039
root: Option<AstNode>,
2140
}
2241

42+
/// Information on an `AST` node.
2343
#[derive(Debug)]
2444
pub struct AstNode {
45+
/// The type of node
2546
pub r#type: &'static str,
47+
/// The code associated to a node
2648
pub value: String,
49+
/// The start and end positions of a node in a code
2750
pub span: Span,
51+
/// The children of a node
2852
pub children: Vec<AstNode>,
2953
}
3054

@@ -99,11 +123,18 @@ fn build<T: TSParserTrait>(parser: &T, span: bool, comment: bool) -> Option<AstN
99123
}
100124
}
101125

102-
pub struct AstCallback {}
126+
pub struct AstCallback {
127+
_guard: (),
128+
}
103129

130+
/// Configuration options for retrieving the nodes of an `AST`.
104131
pub struct AstCfg {
132+
/// The id associated to a request for an `AST`
105133
pub id: String,
134+
/// If `true`, nodes representing comments are ignored
106135
pub comment: bool,
136+
/// If `true`, the start and end positions of a node in a code
137+
/// are considered
107138
pub span: bool,
108139
}
109140

src/asttools.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn get_parent<'a>(node: &'a Node<'a>, level: usize) -> Option<Node<'a>> {
1616
Some(node)
1717
}
1818

19+
#[doc(hidden)]
1920
#[macro_export]
2021
macro_rules! has_ancestors {
2122
($node:expr, $( $typs:pat )|*, $( $typ:pat ),+) => {{

src/comment_rm.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::traits::*;
77

88
const CR: [u8; 8192] = [b'\n'; 8192];
99

10+
/// Removes comments from a code.
1011
pub fn rm_comments<T: TSParserTrait>(parser: &T) -> Option<Vec<u8>> {
1112
let node = parser.get_root();
1213
let mut stack = Vec::new();
@@ -59,12 +60,17 @@ fn remove_from_code(code: &[u8], mut spans: Vec<(usize, usize, usize)>) -> Vec<u
5960
new_code
6061
}
6162

63+
/// Configuration options for removing comments from a code.
6264
pub struct CommentRmCfg {
65+
/// If `true`, the modified code is saved on a file
6366
pub in_place: bool,
67+
/// Path to output file
6468
pub path: PathBuf,
6569
}
6670

67-
pub struct CommentRm {}
71+
pub struct CommentRm {
72+
_guard: (),
73+
}
6874

6975
impl Callback for CommentRm {
7076
type Res = std::io::Result<()>;

src/count.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ extern crate num_format;
22

33
use num_format::{Locale, ToFormattedString};
44
use std::fmt;
5-
use std::path::PathBuf;
65
use std::sync::{Arc, Mutex};
76

87
use crate::traits::*;
98

9+
/// Counts the types of nodes specified in the input slice
10+
/// and the number of nodes in a code.
1011
pub fn count<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> (usize, usize) {
1112
let filters = parser.get_filters(filters);
1213
let node = parser.get_root();
@@ -35,15 +36,21 @@ pub fn count<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> (usize,
3536
(good, total)
3637
}
3738

39+
/// Configuration options for counting different
40+
/// types of nodes in a code.
3841
pub struct CountCfg {
39-
pub path: Option<PathBuf>,
42+
/// Types of nodes to count
4043
pub filters: Vec<String>,
44+
/// Number of nodes of a certain type counted by each thread
4145
pub stats: Arc<Mutex<Count>>,
4246
}
4347

48+
/// Count of different types of nodes in a code.
4449
#[derive(Debug, Default)]
4550
pub struct Count {
51+
/// The number of specific types of nodes searched in a code
4652
pub good: usize,
53+
/// The total number of nodes in a code
4754
pub total: usize,
4855
}
4956

src/find.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use tree_sitter::Node;
44
use crate::dump::*;
55
use crate::traits::*;
66

7+
/// Finds the types of nodes specified in the input slice.
78
pub fn find<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> Option<Vec<Node<'a>>> {
89
let filters = parser.get_filters(filters);
910
let node = parser.get_root();
@@ -34,14 +35,28 @@ pub fn find<'a, T: TSParserTrait>(parser: &'a T, filters: &[String]) -> Option<V
3435
Some(good)
3536
}
3637

38+
/// Configuration options for finding different
39+
/// types of nodes in a code.
3740
pub struct FindCfg {
38-
pub path: Option<PathBuf>,
41+
/// Path to the file containing the code
42+
pub path: PathBuf,
43+
/// Types of nodes to find
3944
pub filters: Vec<String>,
45+
/// The first line of code considered in the search
46+
///
47+
/// If `None`, the search starts from the
48+
/// first line of code in a file
4049
pub line_start: Option<usize>,
50+
/// The end line of code considered in the search
51+
///
52+
/// If `None`, the search ends at the
53+
/// last line of code in a file
4154
pub line_end: Option<usize>,
4255
}
4356

44-
pub struct Find {}
57+
pub struct Find {
58+
_guard: (),
59+
}
4560

4661
impl Callback for Find {
4762
type Res = std::io::Result<()>;
@@ -50,7 +65,7 @@ impl Callback for Find {
5065
fn call<T: TSParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res {
5166
if let Some(good) = find(parser, &cfg.filters) {
5267
if !good.is_empty() {
53-
println!("In file {:?}", cfg.path);
68+
println!("In file {}", cfg.path.to_str().unwrap());
5469
for node in good {
5570
dump_node(parser.get_code(), &node, 1, cfg.line_start, cfg.line_end)?;
5671
}

src/function.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@ use crate::traits::*;
77
use crate::checker::Checker;
88
use crate::getter::Getter;
99

10+
/// Function span data.
1011
#[derive(Debug, Serialize)]
1112
pub struct FunctionSpan {
13+
/// The function name
1214
pub name: String,
15+
/// The first line of a function
1316
pub start_line: usize,
17+
/// The last line of a function
1418
pub end_line: usize,
19+
/// If `true`, an error is occured in determining the span
20+
/// of a function
1521
pub error: bool,
1622
}
1723

24+
/// Detects the span of each function in a code.
25+
///
26+
/// Returns a vector containing the [`FunctionSpan`] of each function
27+
///
28+
/// [`FunctionSpan`]: struct.FunctionSpan.html
1829
pub fn function<T: TSParserTrait>(parser: &T) -> Vec<FunctionSpan> {
1930
let root = parser.get_root();
2031
let code = parser.get_code();
@@ -96,11 +107,16 @@ fn dump_spans(mut spans: Vec<FunctionSpan>, path: PathBuf) -> std::io::Result<()
96107
Ok(())
97108
}
98109

110+
/// Configuration options for detecting the span of
111+
/// each function in a code.
99112
pub struct FunctionCfg {
113+
/// Path to the file containing the code
100114
pub path: PathBuf,
101115
}
102116

103-
pub struct Function {}
117+
pub struct Function {
118+
_guard: (),
119+
}
104120

105121
impl Callback for Function {
106122
type Res = std::io::Result<()>;

src/langs.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ use crate::*;
88

99
mk_langs!(
1010
// 1) Name for enum
11-
// 2) Display name
12-
// 3) Empty struct name to implement
13-
// 4) Parser name
14-
// 5) tree-sitter function to call to get a Language
15-
// 6) file extensions
16-
// 7) emacs modes
11+
// 2) Language description
12+
// 3) Display name
13+
// 4) Empty struct name to implement
14+
// 5) Parser name
15+
// 6) tree-sitter function to call to get a Language
16+
// 7) file extensions
17+
// 8) emacs modes
1718
(
1819
Mozjs,
20+
"The `Mozjs` language is variant of the `JavaScript` language",
1921
"javascript",
2022
MozjsCode,
2123
MozjsParser,
@@ -25,6 +27,7 @@ mk_langs!(
2527
),
2628
(
2729
Javascript,
30+
"The `JavaScript` language",
2831
"javascript",
2932
JavascriptCode,
3033
JavascriptParser,
@@ -34,16 +37,27 @@ mk_langs!(
3437
),
3538
(
3639
Java,
40+
"The `Java` language",
3741
"java",
3842
JavaCode,
3943
JavaParser,
4044
tree_sitter_java,
4145
[java],
4246
["java"]
4347
),
44-
(Go, "go", GoCode, GoParser, tree_sitter_go, [go], ["go"]),
48+
(
49+
Go,
50+
"The `Go` language",
51+
"go",
52+
GoCode,
53+
GoParser,
54+
tree_sitter_go,
55+
[go],
56+
["go"]
57+
),
4558
(
4659
Html,
60+
"The `HTML` language",
4761
"html",
4862
HtmlCode,
4963
HtmlParser,
@@ -53,6 +67,7 @@ mk_langs!(
5367
),
5468
(
5569
CSharp,
70+
"The `C#` language",
5671
"c#",
5772
CSharpCode,
5873
CSharpParser,
@@ -62,6 +77,7 @@ mk_langs!(
6277
),
6378
(
6479
Rust,
80+
"The `Rust` language",
6581
"rust",
6682
RustCode,
6783
RustParser,
@@ -71,6 +87,7 @@ mk_langs!(
7187
),
7288
(
7389
Css,
90+
"The `CSS` language",
7491
"css",
7592
CssCode,
7693
CssParser,
@@ -80,6 +97,7 @@ mk_langs!(
8097
),
8198
(
8299
Cpp,
100+
"The `C/C++` language",
83101
"c/c++",
84102
CppCode,
85103
CppParser,
@@ -89,6 +107,7 @@ mk_langs!(
89107
),
90108
(
91109
Python,
110+
"The `Python` language",
92111
"python",
93112
PythonCode,
94113
PythonParser,
@@ -98,6 +117,7 @@ mk_langs!(
98117
),
99118
(
100119
Tsx,
120+
"The `Tsx` language incorporates the `JSX` syntax inside `TypeScript`",
101121
"typescript",
102122
TsxCode,
103123
TsxParser,
@@ -107,6 +127,7 @@ mk_langs!(
107127
),
108128
(
109129
Typescript,
130+
"The `TypeScript` language",
110131
"typescript",
111132
TypescriptCode,
112133
TypescriptParser,
@@ -116,6 +137,7 @@ mk_langs!(
116137
),
117138
(
118139
Ccomment,
140+
"The `Ccomment` language is a variant of the `C` language focused on comments",
119141
"ccomment",
120142
CcommentCode,
121143
CcommentParser,
@@ -125,6 +147,7 @@ mk_langs!(
125147
),
126148
(
127149
Preproc,
150+
"The `PreProc` language is a variant of the `C/C++` language focused on macros",
128151
"preproc",
129152
PreprocCode,
130153
PreprocParser,

0 commit comments

Comments
 (0)