Skip to content

Added --format to rustc #7324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added format to rust
  • Loading branch information
MaikKlein committed Jun 23, 2013
commit ce4847e50f1b0421bcd96fd55d456e834befbf74
49 changes: 49 additions & 0 deletions src/librust/rust.rc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ static commands: &'static [Command<'static>] = &[
usage_line: "run a rust interpreter",
usage_full: UsgStr("\nUsage:\trusti"),
},
Command{cmd: "format",
action: Call(format_source_code),
usage_line: "Formats and overwrites the specified files",
usage_full:
UsgStr("usage: \"rust format file1.rs file2.rs\" aborts on error. Do
\"rust format --force file1.rs file2.rs\" if you want to contiue
formating even if an error is thrown")},
Command{
cmd: "help",
action: Call(cmd_help),
Expand All @@ -129,6 +136,48 @@ static commands: &'static [Command<'static>] = &[
)
}
];
pub fn format_source_code(args: &[~str]) -> ValidUsage {
let mut args = args.to_owned();

// true if all specified files are formatable
fn check_for_error(args: &[~str]) -> bool {
for args.iter().advance |p| {
let source_code =
core::run::process_output("rustc", &[copy *p, ~"--pretty"]);
if (source_code.output.is_empty()) { return false; }
}
true
}

fn write_output(args: &[~str]) {


// If we have a writer in our result then we write the source code to it.
for args.iter().advance |path| {
let source_code =
core::run::process_output("rustc", &[copy *path, ~"--pretty"]);
let path = core::path::Path(*path);
// We overwrite the input file.
let writer_result = io::buffered_file_writer(&path);
match writer_result {
core::result::Ok(writer) => writer.write(source_code.output),
core::result::Err(u) => ()
};
}
}

// rust format --force file1 file2
if (*args.head() == ~"--force") {
args.shift();
write_output(args);
// rust format file1 file2 --format_source_code
} else if (*args.last() == ~"--force") {
args.pop();
write_output(args);

} else if (check_for_error(args)) { write_output(args); } else { return Invalid }
Valid(0)
}

fn rustc_help() {
rustc::usage(copy os::args()[0])
Expand Down