Skip to content

Commit 0ce89d6

Browse files
committed
feat(Extracted): Parsing of bashdocs from text now includes the line number of the function extract
1 parent 80fd862 commit 0ce89d6

File tree

3 files changed

+95
-43
lines changed

3 files changed

+95
-43
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ serde_json = "1.0.33"
1515
toml = "0.4.8"
1616
nom = "4.1.1"
1717
handlebars = "1.1.0"
18-
notify = "4.0.6"
18+
notify = "4.0.6"
19+
nom_locate = "0.3.1"

example/zshrc.json

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,96 @@
77
"long_description": "re-initialize .zshrc file",
88
"descriptors": [],
99
"params": [],
10-
"returns": []
10+
"returns": [],
11+
"position": 26
1112
},
1213
{
1314
"short_description": "zshconfig",
1415
"long_description": "open .zshrc in VSCode",
1516
"descriptors": [],
1617
"params": [],
17-
"returns": []
18+
"returns": [],
19+
"position": 32
1820
},
1921
{
2022
"short_description": "vimconfig",
2123
"long_description": "open .vimrc in vim",
2224
"descriptors": [],
2325
"params": [],
24-
"returns": []
26+
"returns": [],
27+
"position": 38
2528
},
2629
{
2730
"short_description": "docs",
2831
"long_description": "shortcut to ~/Documents folder",
2932
"descriptors": [],
3033
"params": [],
31-
"returns": []
34+
"returns": [],
35+
"position": 47
3236
},
3337
{
3438
"short_description": "svenv",
3539
"long_description": "activate virtual python environment",
3640
"descriptors": [],
3741
"params": [],
38-
"returns": []
42+
"returns": [],
43+
"position": 53
3944
},
4045
{
4146
"short_description": "update",
4247
"long_description": "update Operating System, Brew, NPM, Applications, and Pip",
4348
"descriptors": [],
4449
"params": [],
45-
"returns": []
50+
"returns": [],
51+
"position": 63
4652
},
4753
{
4854
"short_description": "cleanup",
4955
"long_description": "remove .DS_Store files from current directory",
5056
"descriptors": [],
5157
"params": [],
52-
"returns": []
58+
"returns": [],
59+
"position": 69
5360
},
5461
{
5562
"short_description": "emptytrash",
5663
"long_description": "clean the trash",
5764
"descriptors": [],
5865
"params": [],
59-
"returns": []
66+
"returns": [],
67+
"position": 75
6068
},
6169
{
6270
"short_description": "helpme",
6371
"long_description": "access cht.sh for help with commands and languages",
6472
"descriptors": [],
6573
"params": [],
66-
"returns": []
74+
"returns": [],
75+
"position": 82
6776
},
6877
{
6978
"short_description": "cloud",
7079
"long_description": "shortcut to iCloud documents",
7180
"descriptors": [],
7281
"params": [],
73-
"returns": []
82+
"returns": [],
83+
"position": 88
7484
},
7585
{
7686
"short_description": "notes",
7787
"long_description": "shortcut to exported Notes",
7888
"descriptors": [],
7989
"params": [],
80-
"returns": []
90+
"returns": [],
91+
"position": 94
8192
},
8293
{
8394
"short_description": "gits",
8495
"long_description": "Shortcut to ~/Documents/Gits",
8596
"descriptors": [],
8697
"params": [],
87-
"returns": []
98+
"returns": [],
99+
"position": 100
88100
},
89101
{
90102
"short_description": "glsnips()",
@@ -96,14 +108,16 @@
96108
"value": "Optional keyword to fuzzy search"
97109
}
98110
],
99-
"returns": []
111+
"returns": [],
112+
"position": 125
100113
},
101114
{
102115
"short_description": "bip()",
103116
"long_description": "Fuzzy search brew",
104117
"descriptors": [],
105118
"params": [],
106-
"returns": []
119+
"returns": [],
120+
"position": 151
107121
},
108122
{
109123
"short_description": "allgits()",
@@ -115,7 +129,8 @@
115129
"value": "commit message"
116130
}
117131
],
118-
"returns": []
132+
"returns": [],
133+
"position": 166
119134
},
120135
{
121136
"short_description": "xi()",
@@ -127,7 +142,8 @@
127142
"value": "file path to open"
128143
}
129144
],
130-
"returns": []
145+
"returns": [],
146+
"position": 175
131147
},
132148
{
133149
"short_description": "fs()",
@@ -159,7 +175,8 @@
159175
}
160176
],
161177
"params": [],
162-
"returns": []
178+
"returns": [],
179+
"position": 200
163180
},
164181
{
165182
"short_description": "2mp4()",
@@ -171,7 +188,8 @@
171188
"value": "mp4 or gif"
172189
}
173190
],
174-
"returns": []
191+
"returns": [],
192+
"position": 246
175193
},
176194
{
177195
"short_description": "gitrm()",
@@ -183,35 +201,40 @@
183201
"value": "file type to be removed from this folder"
184202
}
185203
],
186-
"returns": []
204+
"returns": [],
205+
"position": 263
187206
},
188207
{
189208
"short_description": "pygnore()",
190209
"long_description": "Generate a .gitignore file for python projects",
191210
"descriptors": [],
192211
"params": [],
193-
"returns": []
212+
"returns": [],
213+
"position": 272
194214
},
195215
{
196216
"short_description": "fbr()",
197217
"long_description": "Fuzzy search git branches",
198218
"descriptors": [],
199219
"params": [],
200-
"returns": []
220+
"returns": [],
221+
"position": 281
201222
},
202223
{
203224
"short_description": "fshow()",
204225
"long_description": "Preview git tree",
205226
"descriptors": [],
206227
"params": [],
207-
"returns": []
228+
"returns": [],
229+
"position": 294
208230
},
209231
{
210232
"short_description": "fstat()",
211233
"long_description": "Show uncommitted files",
212234
"descriptors": [],
213235
"params": [],
214-
"returns": []
236+
"returns": [],
237+
"position": 311
215238
},
216239
{
217240
"short_description": "gi()",
@@ -223,7 +246,8 @@
223246
"value": "list of types to generator gitignore of"
224247
}
225248
],
226-
"returns": []
249+
"returns": [],
250+
"position": 333
227251
},
228252
{
229253
"short_description": "setgit()",
@@ -235,7 +259,8 @@
235259
"value": "url of git repo push to"
236260
}
237261
],
238-
"returns": []
262+
"returns": [],
263+
"position": 343
239264
},
240265
{
241266
"short_description": "gup()",
@@ -247,7 +272,8 @@
247272
"value": "name of branch"
248273
}
249274
],
250-
"returns": []
275+
"returns": [],
276+
"position": 352
251277
},
252278
{
253279
"short_description": "psman()",
@@ -259,10 +285,11 @@
259285
"value": "command line function to call man on"
260286
}
261287
],
262-
"returns": []
288+
"returns": [],
289+
"position": 362
263290
}
264291
],
265-
"filename": ".zshrc"
292+
"filename": "zshrc"
266293
}
267294
]
268295
}

src/docs.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use colored::*;
33
use dirs::home_dir;
44
use glob::glob;
55
use handlebars::{to_json, Handlebars};
6+
use nom::types::CompleteStr;
67
use nom::*;
8+
use nom_locate::{position, LocatedSpan};
79
use rayon::prelude::*;
810
use serde_derive::*;
911
use std::collections::HashMap;
@@ -13,6 +15,8 @@ use std::fs::File;
1315
use std::io::prelude::*;
1416
use std::path::Path;
1517

18+
type Span<'a> = LocatedSpan<CompleteStr<'a>>;
19+
1620
/// Represents a simple Key, Value pair
1721
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
1822
struct KV {
@@ -34,13 +38,14 @@ impl KV {
3438
}
3539

3640
/// Represents a docstring
37-
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
41+
#[derive(Debug, Serialize, Deserialize, Clone)]
3842
pub struct Doc {
3943
short_description: String,
4044
long_description: String,
4145
descriptors: Vec<KV>,
4246
params: Vec<KV>,
4347
returns: Vec<KV>,
48+
position: u32,
4449
}
4550

4651
impl PartialEq for Doc {
@@ -119,17 +124,20 @@ fn parse_doc<'a>(input: &'a str, delims: Delimiters) -> IResult<&'a str, Doc> {
119124
descriptors: desc.unwrap_or_default(),
120125
params: par.unwrap_or_default(),
121126
returns: ret.unwrap_or_default(),
127+
position: 0
122128
})
123129
)
124130
}
125131

126132
impl Doc {
127133
/// Build a `Doc` from an array of strings
128134
/// Parse `Doc` fields.
129-
pub fn make_doc(vector: String, delims: Delimiters) -> Doc {
135+
fn make_doc(vector: &Extracted, delims: Delimiters) -> Doc {
130136
// println!("{:#?}", vector);
131-
let result = parse_doc(&vector, delims);
132-
result.expect("Parsing error.").1
137+
let parsed = parse_doc(&vector.content, delims);
138+
let mut result = parsed.expect("Parsing error.").1;
139+
result.position = vector.position.line + 1;
140+
result
133141
}
134142
}
135143

@@ -148,14 +156,30 @@ impl DocFile {
148156
}
149157
}
150158

159+
struct Extracted<'a> {
160+
position: Span<'a>,
161+
content: String,
162+
}
163+
151164
/// Nom function to extract all docstring from a file.
152-
fn getinfo(input: &'static str, delims: Delimiters) -> IResult<&'static str, Vec<&'static str>> {
165+
fn getinfo(
166+
input: Span<'static>,
167+
delims: Delimiters,
168+
) -> IResult<Span<'static>, Vec<Extracted<'static>>> {
153169
many0!(
154170
input,
155-
complete!(preceded!(
156-
take_until_and_consume!(delims.start),
157-
take_until_and_consume!(delims.end)
158-
))
171+
do_parse!(
172+
content:
173+
complete!(preceded!(
174+
take_until_and_consume!(delims.start),
175+
take_until_and_consume!(delims.end)
176+
))
177+
>> pos: position!()
178+
>> (Extracted {
179+
position: pos,
180+
content: content.to_string()
181+
})
182+
)
159183
)
160184
}
161185

@@ -165,25 +189,25 @@ fn getinfo(input: &'static str, delims: Delimiters) -> IResult<&'static str, Vec
165189
/// and adds every line to a `Vec` until the end delimiter.
166190
///
167191
/// A final `Vec` of the collected comment strings is returned.
168-
fn get_info<'a>(p: &Path, delims: Delimiters) -> Vec<&'a str> {
192+
fn get_info<'a>(p: &Path, delims: Delimiters) -> Vec<Extracted<'a>> {
169193
let mut f = File::open(&p).expect("file not found.");
170194
let mut buffer = String::new();
171195
f.read_to_string(&mut buffer).unwrap();
172196
let used = Box::leak(buffer.into_boxed_str());
173197
// println!("{:#?}", used);
174-
let result = getinfo(used, delims);
198+
let result = getinfo(Span::new(CompleteStr(used)), delims);
175199
// println!("{:#?}", result);
176200
result.unwrap().1
177201
}
178202

179203
/// Given a `Vec<str>` make a `DocFile`
180-
fn generate_doc_file(docs: &[&str], fname: String, delims: Delimiters) -> DocFile {
204+
fn generate_doc_file(docs: &Vec<Extracted<'static>>, fname: String, delims: Delimiters) -> DocFile {
181205
let mut all_docs: DocFile = Default::default();
182206
all_docs.filename = fname;
183207
let collected: Vec<Doc> = docs
184208
.par_iter()
185-
.filter(|x| !x.is_empty())
186-
.map(|x| Doc::make_doc(x.to_string(), delims))
209+
.filter(|x| !x.content.is_empty())
210+
.map(|x| Doc::make_doc(x, delims))
187211
.collect();
188212
all_docs.thedocs = collected;
189213
all_docs

0 commit comments

Comments
 (0)