mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
keep indent state in fmt Buf
This commit is contained in:
parent
e872d00b13
commit
e3f2f8d9a4
11 changed files with 313 additions and 182 deletions
|
@ -1,9 +1,10 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use bumpalo::collections::{String, Vec};
|
||||
use bumpalo::collections::Vec;
|
||||
use bumpalo::Bump;
|
||||
use roc_fmt::def::fmt_def;
|
||||
use roc_fmt::module::fmt_module;
|
||||
use roc_fmt::Buf;
|
||||
use roc_module::called_via::{BinOp, UnaryOp};
|
||||
use roc_parse::ast::{
|
||||
AssignedField, Collection, Expr, Pattern, StrLiteral, StrSegment, Tag, TypeAnnotation,
|
||||
|
@ -30,13 +31,13 @@ pub fn format(files: std::vec::Vec<PathBuf>) {
|
|||
let ast = arena.alloc(parse_all(&arena, &src).unwrap_or_else(|e| {
|
||||
user_error!("Unexpected parse failure when parsing this formatting:\n\n{:?}\n\nParse error was:\n\n{:?}\n\n", src, e)
|
||||
}));
|
||||
let mut buf = String::new_in(&arena);
|
||||
let mut buf = Buf::new_in(&arena);
|
||||
fmt_all(&arena, &mut buf, ast);
|
||||
|
||||
let reparsed_ast = arena.alloc(parse_all(&arena, &buf).unwrap_or_else(|e| {
|
||||
let reparsed_ast = arena.alloc(parse_all(&arena, buf.as_str()).unwrap_or_else(|e| {
|
||||
let mut fail_file = file.clone();
|
||||
fail_file.set_extension("roc-format-failed");
|
||||
std::fs::write(&fail_file, &buf).unwrap();
|
||||
std::fs::write(&fail_file, buf.as_str()).unwrap();
|
||||
internal_error!(
|
||||
"Formatting bug; formatted code isn't valid\n\n\
|
||||
I wrote the incorrect result to this file for debugging purposes:\n{}\n\n\
|
||||
|
@ -57,7 +58,7 @@ pub fn format(files: std::vec::Vec<PathBuf>) {
|
|||
if format!("{:?}", ast_normalized) != format!("{:?}", reparsed_ast_normalized) {
|
||||
let mut fail_file = file.clone();
|
||||
fail_file.set_extension("roc-format-failed");
|
||||
std::fs::write(&fail_file, &buf).unwrap();
|
||||
std::fs::write(&fail_file, buf.as_str()).unwrap();
|
||||
|
||||
let mut before_file = file.clone();
|
||||
before_file.set_extension("roc-format-failed-ast-before");
|
||||
|
@ -77,16 +78,16 @@ pub fn format(files: std::vec::Vec<PathBuf>) {
|
|||
}
|
||||
|
||||
// Now verify that the resultant formatting is _stable_ - i.e. that it doesn't change again if re-formatted
|
||||
let mut reformatted_buf = String::new_in(&arena);
|
||||
let mut reformatted_buf = Buf::new_in(&arena);
|
||||
fmt_all(&arena, &mut reformatted_buf, reparsed_ast);
|
||||
if buf.as_str() != reformatted_buf.as_str() {
|
||||
let mut unstable_1_file = file.clone();
|
||||
unstable_1_file.set_extension("roc-format-unstable-1");
|
||||
std::fs::write(&unstable_1_file, &buf).unwrap();
|
||||
std::fs::write(&unstable_1_file, buf.as_str()).unwrap();
|
||||
|
||||
let mut unstable_2_file = file.clone();
|
||||
unstable_2_file.set_extension("roc-format-unstable-2");
|
||||
std::fs::write(&unstable_2_file, &reformatted_buf).unwrap();
|
||||
std::fs::write(&unstable_2_file, reformatted_buf.as_str()).unwrap();
|
||||
|
||||
internal_error!(
|
||||
"Formatting bug; formatting is not stable. Reformatting the formatted file changed it again.\n\n\
|
||||
|
@ -97,7 +98,7 @@ pub fn format(files: std::vec::Vec<PathBuf>) {
|
|||
}
|
||||
|
||||
// If all the checks above passed, actually write out the new file.
|
||||
std::fs::write(&file, &buf).unwrap();
|
||||
std::fs::write(&file, buf.as_str()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,7 +117,7 @@ fn parse_all<'a>(arena: &'a Bump, src: &'a str) -> Result<Ast<'a>, SyntaxError<'
|
|||
Ok(Ast { module, defs })
|
||||
}
|
||||
|
||||
fn fmt_all<'a>(arena: &'a Bump, buf: &mut String<'a>, ast: &'a Ast) {
|
||||
fn fmt_all<'a>(arena: &'a Bump, buf: &mut Buf<'a>, ast: &'a Ast) {
|
||||
fmt_module(buf, &ast.module);
|
||||
for def in &ast.defs {
|
||||
fmt_def(buf, arena.alloc(def.value), 0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue