Skip to content

Commit

Permalink
Merge pull request #7307 from joshuawarner32/merge-MigrationFlags
Browse files Browse the repository at this point in the history
Move MigrationFlags from an argument on format* to a field on Buf
  • Loading branch information
smores56 authored Dec 5, 2024
2 parents 0274d9b + 120e9be commit 54e78e8
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 812 deletions.
32 changes: 14 additions & 18 deletions crates/cli/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::path::{Path, PathBuf};

use bumpalo::Bump;
use roc_error_macros::{internal_error, user_error};
use roc_fmt::annotation::MigrationFlags;
use roc_fmt::def::fmt_defs;
use roc_fmt::header::fmt_header;
use roc_fmt::Buf;
use roc_fmt::MigrationFlags;
use roc_parse::ast::{FullAst, SpacesBefore};
use roc_parse::header::parse_module_defs;
use roc_parse::normalize::Normalize;
Expand Down Expand Up @@ -67,7 +67,7 @@ fn is_roc_file(path: &Path) -> bool {
pub fn format_files(
files: std::vec::Vec<PathBuf>,
mode: FormatMode,
flags: &MigrationFlags,
flags: MigrationFlags,
) -> Result<(), String> {
let arena = Bump::new();
let mut files_to_reformat = Vec::new(); // to track which files failed `roc format --check`
Expand Down Expand Up @@ -188,16 +188,12 @@ pub enum FormatProblem {
},
}

pub fn format_src(
arena: &Bump,
src: &str,
flags: &MigrationFlags,
) -> Result<String, FormatProblem> {
pub fn format_src(arena: &Bump, src: &str, flags: MigrationFlags) -> Result<String, FormatProblem> {
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 = Buf::new_in(arena);
fmt_all(&mut buf, ast, flags);
let mut buf = Buf::new_in(arena, flags);
fmt_all(&mut buf, ast);

let reparsed_ast = match arena.alloc(parse_all(arena, buf.as_str())) {
Ok(ast) => ast,
Expand Down Expand Up @@ -228,9 +224,9 @@ pub fn format_src(
}

// Now verify that the resultant formatting is _stable_ - i.e. that it doesn't change again if re-formatted
let mut reformatted_buf = Buf::new_in(arena);
let mut reformatted_buf = Buf::new_in(arena, flags);

fmt_all(&mut reformatted_buf, reparsed_ast, flags);
fmt_all(&mut reformatted_buf, reparsed_ast);

if buf.as_str() != reformatted_buf.as_str() {
return Err(FormatProblem::ReformattingUnstable {
Expand Down Expand Up @@ -259,10 +255,10 @@ fn parse_all<'a>(arena: &'a Bump, src: &'a str) -> Result<FullAst<'a>, SyntaxErr
})
}

fn fmt_all<'a>(buf: &mut Buf<'a>, ast: &'a FullAst, flags: &MigrationFlags) {
fmt_header(buf, &ast.header, flags);
fn fmt_all<'a>(buf: &mut Buf<'a>, ast: &'a FullAst) {
fmt_header(buf, &ast.header);

fmt_defs(buf, &ast.defs, flags, 0);
fmt_defs(buf, &ast.defs, 0);

buf.fmt_end_of_file();
}
Expand Down Expand Up @@ -311,7 +307,7 @@ main =
let file_path = setup_test_file(dir.path(), "test1.roc", UNFORMATTED_ROC);
let flags = MigrationFlags::new(false);

let result = format_files(vec![file_path.clone()], FormatMode::CheckOnly, &flags);
let result = format_files(vec![file_path.clone()], FormatMode::CheckOnly, flags);
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
Expand All @@ -331,7 +327,7 @@ main =
let file2 = setup_test_file(dir.path(), "test2.roc", UNFORMATTED_ROC);
let flags = MigrationFlags::new(false);

let result = format_files(vec![file1, file2], FormatMode::CheckOnly, &flags);
let result = format_files(vec![file1, file2], FormatMode::CheckOnly, flags);
assert!(result.is_err());
let error_message = result.unwrap_err();
assert!(error_message.contains("test1.roc") && error_message.contains("test2.roc"));
Expand All @@ -345,7 +341,7 @@ main =
let file_path = setup_test_file(dir.path(), "formatted.roc", FORMATTED_ROC);
let flags = MigrationFlags::new(false);

let result = format_files(vec![file_path], FormatMode::CheckOnly, &flags);
let result = format_files(vec![file_path], FormatMode::CheckOnly, flags);
assert!(result.is_ok());

cleanup_temp_dir(dir);
Expand All @@ -362,7 +358,7 @@ main =
let result = format_files(
vec![file_formatted, file1_unformated, file2_unformated],
FormatMode::CheckOnly,
&flags,
flags,
);
assert!(result.is_err());
let error_message = result.unwrap_err();
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use roc_cli::{
};
use roc_docs::generate_docs_html;
use roc_error_macros::user_error;
use roc_fmt::annotation::MigrationFlags;
use roc_fmt::MigrationFlags;
use roc_gen_dev::AssemblyBackendMode;
use roc_gen_llvm::llvm::build::LlvmBackendMode;
use roc_load::{LoadingProblem, Threading};
Expand Down Expand Up @@ -374,7 +374,7 @@ fn main() -> io::Result<()> {
std::process::exit(1);
});

match format_src(&arena, src, &flags) {
match format_src(&arena, src, flags) {
Ok(formatted_src) => {
match format_mode {
FormatMode::CheckOnly => {
Expand Down Expand Up @@ -406,7 +406,7 @@ fn main() -> io::Result<()> {
}
}
} else {
match format_files(roc_files, format_mode, &flags) {
match format_files(roc_files, format_mode, flags) {
Ok(()) => 0,
Err(message) => {
eprintln!("{message}");
Expand Down
Loading

0 comments on commit 54e78e8

Please sign in to comment.