Skip to content

Commit 2290bf3

Browse files
authored
Sort functions by name in documentation (#470)
* Minor refactoring. * Use absolute or relative output directory for documentation. * Sort functions by name. * Fix Clippy compiler error. * Rename and refactor docs item name function.
1 parent 6dc17df commit 2290bf3

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/compiler.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ impl AmberCompiler {
128128
meta: &ParserMetadata,
129129
) -> Vec<(String, Block)> {
130130
let imports_sorted = meta.import_cache.topological_sort();
131-
let imports_blocks = meta
132-
.import_cache
133-
.files
134-
.iter()
131+
let imports_blocks = meta.import_cache.files.iter()
135132
.map(|file| {
136133
file.metadata
137134
.as_ref()
@@ -186,32 +183,29 @@ impl AmberCompiler {
186183
}
187184

188185
pub fn document(&self, block: Block, meta: ParserMetadata, output: String) {
189-
let base_path = PathBuf::from(
190-
meta.get_path()
191-
.expect("Input file must exist in docs generation"),
192-
);
186+
let base_path = meta.get_path()
187+
.map(PathBuf::from)
188+
.expect("Input file must exist in docs generation");
193189
let base_dir = fs::canonicalize(base_path).map(|val| {
194190
val.parent()
195191
.expect("Parent dir must exist in docs generation")
196192
.to_owned()
197193
.clone()
198194
});
199-
if let Err(err) = base_dir {
195+
let base_dir = base_dir.unwrap_or_else(|err| {
200196
Message::new_err_msg("Couldn't get the absolute path to the provided input file")
201197
.comment(err.to_string())
202198
.show();
203199
std::process::exit(1);
204-
}
205-
let base_dir = base_dir.unwrap();
200+
});
206201
let ast_forest = self.get_sorted_ast_forest(block, &meta);
207202
let mut paths = vec![];
208203
for (path, block) in ast_forest {
209204
let dep_path = {
210-
let dep_path = fs::canonicalize(PathBuf::from(path.clone()));
211-
if dep_path.is_err() {
212-
continue;
213-
}
214-
let dep_path = dep_path.unwrap();
205+
let dep_path = match fs::canonicalize(PathBuf::from(path)) {
206+
Ok(path) => path,
207+
Err(_) => continue,
208+
};
215209

216210
if !dep_path.starts_with(&base_dir) {
217211
continue;
@@ -220,22 +214,23 @@ impl AmberCompiler {
220214
dep_path
221215
};
222216
let document = block.document(&meta);
223-
// Save to file
217+
// Save to file; replace the base directory if the output
218+
// path is absolute, otherwise append the output path.
224219
let dir_path = {
225-
let file_dir = dep_path.strip_prefix(&base_dir).unwrap();
226-
let parent = file_dir.parent().unwrap().display();
227-
format!("{}/{output}/{}", base_dir.to_string_lossy(), parent)
220+
let file_path = dep_path.strip_prefix(&base_dir).unwrap();
221+
let file_dir = file_path.parent().unwrap();
222+
base_dir.join(&output).join(file_dir)
228223
};
229224
if let Err(err) = fs::create_dir_all(dir_path.clone()) {
230225
Message::new_err_msg(format!(
231-
"Couldn't create directory `{dir_path}`. Do you have sufficient permissions?"
226+
"Couldn't create directory `{}`. Do you have sufficient permissions?", dir_path.display()
232227
))
233228
.comment(err.to_string())
234229
.show();
235230
std::process::exit(1);
236231
}
237232
let filename = dep_path.file_stem().unwrap().to_string_lossy();
238-
let path = PathBuf::from(dir_path).join(format!("{filename}.md"));
233+
let path = dir_path.join(format!("{filename}.md"));
239234
let mut file = File::create(path.clone()).unwrap();
240235
file.write_all(document.as_bytes()).unwrap();
241236
paths.push(String::from(path.to_string_lossy()));

src/modules/block.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::collections::VecDeque;
2+
use std::ops::Index;
23

34
use heraclitus_compiler::prelude::*;
5+
use itertools::Itertools;
46
use crate::docs::module::DocumentationModule;
57
use crate::utils::{metadata::ParserMetadata, TranslateMetadata};
68
use crate::translate::module::TranslateModule;
@@ -82,8 +84,16 @@ impl TranslateModule for Block {
8284

8385
impl DocumentationModule for Block {
8486
fn document(&self, meta: &ParserMetadata) -> String {
85-
self.statements.iter()
87+
let indices = self.statements.iter()
88+
.enumerate()
89+
.map(|(index, statement)| (index, statement.get_docs_item_name()))
90+
.filter_map(|(index, name)| name.map(|n| (n, index)))
91+
.sorted()
92+
.collect::<Vec<_>>();
93+
indices.iter()
94+
.map(|(_, index)| self.statements.index(*index))
8695
.map(|statement| statement.document(meta))
87-
.collect::<Vec<_>>().join("")
96+
.collect::<Vec<_>>()
97+
.join("")
8898
}
8999
}

src/modules/statement/stmt.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ impl Statement {
114114
Err(details) => Err(details)
115115
}
116116
}
117+
118+
pub fn get_docs_item_name(&self) -> Option<String> {
119+
match &self.value {
120+
Some(StatementType::FunctionDeclaration(inner)) => Some(inner.name.clone()),
121+
_ => None,
122+
}
123+
}
117124
}
118125

119126
impl SyntaxModule<ParserMetadata> for Statement {

0 commit comments

Comments
 (0)