-
-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement 'roc format <dir_or_files>' command
- Loading branch information
1 parent
047a11d
commit c114fb6
Showing
3 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::path::PathBuf; | ||
|
||
use bumpalo::collections::String; | ||
use bumpalo::Bump; | ||
use roc_fmt::def::fmt_def; | ||
use roc_fmt::module::fmt_module; | ||
use roc_parse::{ | ||
module::{self, module_defs}, | ||
parser::{Parser, State}, | ||
}; | ||
|
||
pub fn format(files: Vec<PathBuf>) { | ||
for file in files { | ||
let arena = Bump::new(); | ||
|
||
let src = std::fs::read_to_string(&file).unwrap(); | ||
|
||
match module::parse_header(&arena, State::new(src.as_bytes())) { | ||
Ok((result, state)) => { | ||
let mut buf = String::new_in(&arena); | ||
|
||
fmt_module(&mut buf, &result); | ||
|
||
match module_defs().parse(&arena, state) { | ||
Ok((_, loc_defs, _)) => { | ||
for loc_def in loc_defs { | ||
fmt_def(&mut buf, arena.alloc(loc_def.value), 0); | ||
} | ||
} | ||
Err(error) => panic!("Unexpected parse failure when parsing this for defs formatting:\n\n{:?}\n\nParse error was:\n\n{:?}\n\n", src, error) | ||
} | ||
|
||
std::fs::write(&file, buf).unwrap(); | ||
} | ||
Err(error) => panic!("Unexpected parse failure when parsing this for module header formatting:\n\n{:?}\n\nParse error was:\n\n{:?}\n\n", src, error) | ||
}; | ||
} | ||
// roc_format::generate_docs_html( | ||
// files, | ||
// roc_builtins::std::standard_stdlib(), | ||
// Path::new("./generated-docs"), | ||
// ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters