Skip to content

Commit 2ddbef7

Browse files
committed
config: Introduce RunConfig for config related to a single run
This commit introduces a `RunConfig` struct, and threads it through all libraries, binaries, and tests. While initially empty, this will be a place to store configuration related to a single run of `rustfmt`, as distinct from configuration read in from the `rustfmt.toml` file. Examples of such per-run settings include: - the `write_mode` override - the `--verbose` flag - the `--skip-children` flag - line ranges to restrict formatting to; see rust-lang#434 These will be added to `RunConfig` in later commits. refs rust-lang#434
1 parent e10b18d commit 2ddbef7

File tree

9 files changed

+111
-43
lines changed

9 files changed

+111
-43
lines changed

src/bin/rustfmt.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern crate getopts;
1919

2020
use rustfmt::{run, run_from_stdin};
2121
use rustfmt::config::{Config, WriteMode};
22+
use rustfmt::run_config::RunConfig;
2223

2324
use std::env;
2425
use std::fs::{self, File};
@@ -78,6 +79,10 @@ fn lookup_and_read_project_file(input_file: &Path) -> io::Result<(PathBuf, Strin
7879
Ok((path, toml))
7980
}
8081

82+
fn run_config_from_options(_matches: &Matches) -> RunConfig {
83+
RunConfig::default()
84+
}
85+
8186
fn update_config(config: &mut Config, matches: &Matches) {
8287
config.verbose = matches.opt_present("verbose");
8388
config.skip_children = matches.opt_present("skip-children");
@@ -131,8 +136,9 @@ fn execute() -> i32 {
131136
Ok((_, toml)) => Config::from_toml(&toml),
132137
Err(_) => Default::default(),
133138
};
139+
let run_config = run_config_from_options(&matches);
134140

135-
run_from_stdin(input, write_mode, &config);
141+
run_from_stdin(input, write_mode, &config, &run_config);
136142
0
137143
}
138144
Operation::Format(files, write_mode) => {
@@ -146,9 +152,10 @@ fn execute() -> i32 {
146152
}
147153
Err(_) => Default::default(),
148154
};
155+
let run_config = run_config_from_options(&matches);
149156

150157
update_config(&mut config, &matches);
151-
run(&file, write_mode, &config);
158+
run(&file, write_mode, &config, &run_config);
152159
}
153160
0
154161
}

src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,10 @@ impl Rewrite for ast::Block {
427427
return Some(user_str);
428428
}
429429

430-
let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config, None);
430+
let mut visitor = FmtVisitor::from_codemap(context.parse_session,
431+
context.config,
432+
context.run_config,
433+
None);
431434
visitor.block_indent = context.block_indent;
432435

433436
let prefix = match self.rules {
@@ -956,6 +959,7 @@ impl Rewrite for ast::Arm {
956959
// more?
957960
let mut attr_visitor = FmtVisitor::from_codemap(context.parse_session,
958961
context.config,
962+
context.run_config,
959963
None);
960964
attr_visitor.block_indent = context.block_indent;
961965
attr_visitor.last_pos = attrs[0].span.lo;

src/filemap.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::fs::{self, File};
1818
use std::io::{self, Write, Read, stdout, BufWriter};
1919

2020
use config::{NewlineStyle, Config, WriteMode};
21+
use run_config::RunConfig;
2122
use rustfmt_diff::{make_diff, print_diff, Mismatch};
2223
use checkstyle::{output_header, output_footer, output_checkstyle_file};
2324

@@ -34,13 +35,19 @@ pub fn append_newlines(file_map: &mut FileMap) {
3435
pub fn write_all_files<T>(file_map: &FileMap,
3536
mut out: T,
3637
mode: WriteMode,
37-
config: &Config)
38+
config: &Config,
39+
run_config: &RunConfig)
3840
-> Result<(), io::Error>
3941
where T: Write
4042
{
4143
output_header(&mut out, mode).ok();
4244
for filename in file_map.keys() {
43-
try!(write_file(&file_map[filename], filename, &mut out, mode, config));
45+
try!(write_file(&file_map[filename],
46+
filename,
47+
&mut out,
48+
mode,
49+
config,
50+
run_config));
4451
}
4552
output_footer(&mut out, mode).ok();
4653

@@ -51,7 +58,8 @@ pub fn write_all_files<T>(file_map: &FileMap,
5158
// Prints all newlines either as `\n` or as `\r\n`.
5259
pub fn write_system_newlines<T>(writer: T,
5360
text: &StringBuffer,
54-
config: &Config)
61+
config: &Config,
62+
_run_config: &RunConfig)
5563
-> Result<(), io::Error>
5664
where T: Write
5765
{
@@ -88,35 +96,38 @@ pub fn write_file<T>(text: &StringBuffer,
8896
filename: &str,
8997
out: &mut T,
9098
mode: WriteMode,
91-
config: &Config)
99+
config: &Config,
100+
run_config: &RunConfig)
92101
-> Result<Option<String>, io::Error>
93102
where T: Write
94103
{
95104

96105
fn source_and_formatted_text(text: &StringBuffer,
97106
filename: &str,
98-
config: &Config)
107+
config: &Config,
108+
run_config: &RunConfig)
99109
-> Result<(String, String), io::Error> {
100110
let mut f = try!(File::open(filename));
101111
let mut ori_text = String::new();
102112
try!(f.read_to_string(&mut ori_text));
103113
let mut v = Vec::new();
104-
try!(write_system_newlines(&mut v, text, config));
114+
try!(write_system_newlines(&mut v, text, config, run_config));
105115
let fmt_text = String::from_utf8(v).unwrap();
106116
Ok((ori_text, fmt_text))
107117
}
108118

109119
fn create_diff(filename: &str,
110120
text: &StringBuffer,
111-
config: &Config)
121+
config: &Config,
122+
run_config: &RunConfig)
112123
-> Result<Vec<Mismatch>, io::Error> {
113-
let (ori, fmt) = try!(source_and_formatted_text(text, filename, config));
124+
let (ori, fmt) = try!(source_and_formatted_text(text, filename, config, run_config));
114125
Ok(make_diff(&ori, &fmt, 3))
115126
}
116127

117128
match mode {
118129
WriteMode::Replace => {
119-
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
130+
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config, run_config) {
120131
if fmt != ori {
121132
// Do a little dance to make writing safer - write to a temp file
122133
// rename the original to a .bk, then rename the temp file to the
@@ -126,7 +137,7 @@ pub fn write_file<T>(text: &StringBuffer,
126137
{
127138
// Write text to temp file
128139
let tmp_file = try!(File::create(&tmp_name));
129-
try!(write_system_newlines(tmp_file, text, config));
140+
try!(write_system_newlines(tmp_file, text, config, run_config));
130141
}
131142

132143
try!(fs::rename(filename, bk_name));
@@ -137,22 +148,22 @@ pub fn write_file<T>(text: &StringBuffer,
137148
WriteMode::Overwrite => {
138149
// Write text directly over original file.
139150
let file = try!(File::create(filename));
140-
try!(write_system_newlines(file, text, config));
151+
try!(write_system_newlines(file, text, config, run_config));
141152
}
142153
WriteMode::Plain => {
143154
let stdout = stdout();
144155
let stdout = stdout.lock();
145-
try!(write_system_newlines(stdout, text, config));
156+
try!(write_system_newlines(stdout, text, config, run_config));
146157
}
147158
WriteMode::Display | WriteMode::Coverage => {
148159
println!("{}:\n", filename);
149160
let stdout = stdout();
150161
let stdout = stdout.lock();
151-
try!(write_system_newlines(stdout, text, config));
162+
try!(write_system_newlines(stdout, text, config, run_config));
152163
}
153164
WriteMode::Diff => {
154165
println!("Diff of {}:\n", filename);
155-
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
166+
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config, run_config) {
156167
print_diff(make_diff(&ori, &fmt, 3),
157168
|line_num| format!("\nDiff at line {}:", line_num));
158169
}
@@ -161,7 +172,7 @@ pub fn write_file<T>(text: &StringBuffer,
161172
unreachable!("The WriteMode should NEVER Be default at this point!");
162173
}
163174
WriteMode::Checkstyle => {
164-
let diff = try!(create_diff(filename, text, config));
175+
let diff = try!(create_diff(filename, text, config, run_config));
165176
try!(output_checkstyle_file(out, filename, diff));
166177
}
167178
}

src/items.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,10 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
530530
let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
531531

532532
if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
533-
let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config, None);
533+
let mut visitor = FmtVisitor::from_codemap(context.parse_session,
534+
context.config,
535+
context.run_config,
536+
None);
534537
visitor.block_indent = context.block_indent.block_indent(context.config);
535538
visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
536539

src/lib.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use issues::{BadIssueSeeker, Issue};
4040
use filemap::FileMap;
4141
use visitor::FmtVisitor;
4242
use config::{Config, WriteMode};
43+
use run_config::RunConfig;
4344

4445
#[macro_use]
4546
mod utils;
@@ -55,6 +56,7 @@ mod expr;
5556
mod imports;
5657
mod issues;
5758
mod rewrite;
59+
pub mod run_config;
5860
mod string;
5961
mod comment;
6062
mod modules;
@@ -265,6 +267,7 @@ fn fmt_ast(krate: &ast::Crate,
265267
parse_session: &ParseSess,
266268
main_file: &Path,
267269
config: &Config,
270+
run_config: &RunConfig,
268271
mode: WriteMode)
269272
-> FileMap {
270273
let mut file_map = FileMap::new();
@@ -276,7 +279,7 @@ fn fmt_ast(krate: &ast::Crate,
276279
if config.verbose {
277280
println!("Formatting {}", path);
278281
}
279-
let mut visitor = FmtVisitor::from_codemap(parse_session, config, Some(mode));
282+
let mut visitor = FmtVisitor::from_codemap(parse_session, config, run_config, Some(mode));
280283
visitor.format_separate_mod(module);
281284
file_map.insert(path.to_owned(), visitor.buffer);
282285
}
@@ -286,7 +289,7 @@ fn fmt_ast(krate: &ast::Crate,
286289
// Formatting done on a char by char or line by line basis.
287290
// TODO(#209) warn on bad license
288291
// TODO(#20) other stuff for parity with make tidy
289-
pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
292+
pub fn fmt_lines(file_map: &mut FileMap, config: &Config, _run_config: &RunConfig) -> FormatReport {
290293
let mut truncate_todo = Vec::new();
291294
let mut report = FormatReport { file_error_map: HashMap::new() };
292295

@@ -366,7 +369,11 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
366369
report
367370
}
368371

369-
pub fn format_string(input: String, config: &Config, mode: WriteMode) -> FileMap {
372+
pub fn format_string(input: String,
373+
config: &Config,
374+
run_config: &RunConfig,
375+
mode: WriteMode)
376+
-> FileMap {
370377
let path = "stdin";
371378
let mut parse_session = ParseSess::new();
372379
let krate = parse::parse_crate_from_source_str(path.to_owned(),
@@ -383,7 +390,7 @@ pub fn format_string(input: String, config: &Config, mode: WriteMode) -> FileMap
383390
let mut file_map = FileMap::new();
384391

385392
// do the actual formatting
386-
let mut visitor = FmtVisitor::from_codemap(&parse_session, config, Some(mode));
393+
let mut visitor = FmtVisitor::from_codemap(&parse_session, config, run_config, Some(mode));
387394
visitor.format_separate_mod(&krate.module);
388395

389396
// append final newline
@@ -393,15 +400,15 @@ pub fn format_string(input: String, config: &Config, mode: WriteMode) -> FileMap
393400
file_map
394401
}
395402

396-
pub fn format(file: &Path, config: &Config, mode: WriteMode) -> FileMap {
403+
pub fn format(file: &Path, config: &Config, run_config: &RunConfig, mode: WriteMode) -> FileMap {
397404
let mut parse_session = ParseSess::new();
398405
let krate = parse::parse_crate_from_file(file, Vec::new(), &parse_session);
399406

400407
// Suppress error output after parsing.
401408
let emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), None));
402409
parse_session.span_diagnostic.handler = Handler::with_emitter(false, emitter);
403410

404-
let mut file_map = fmt_ast(&krate, &parse_session, file, config, mode);
411+
let mut file_map = fmt_ast(&krate, &parse_session, file, config, run_config, mode);
405412

406413
// For some reason, the codemap does not include terminating
407414
// newlines so we must add one on for each file. This is sad.
@@ -424,27 +431,35 @@ fn check_write_mode(arg: WriteMode, config: WriteMode) -> WriteMode {
424431
// to the compiler.
425432
// write_mode determines what happens to the result of running rustfmt, see
426433
// WriteMode.
427-
pub fn run(file: &Path, write_mode: WriteMode, config: &Config) {
434+
pub fn run(file: &Path, write_mode: WriteMode, config: &Config, run_config: &RunConfig) {
428435
let mode = check_write_mode(write_mode, config.write_mode);
429-
let mut result = format(file, config, mode);
436+
let mut result = format(file, config, run_config, mode);
430437

431-
print!("{}", fmt_lines(&mut result, config));
438+
print!("{}", fmt_lines(&mut result, config, run_config));
432439
let out = stdout();
433-
let write_result = filemap::write_all_files(&result, out, mode, config);
440+
let write_result = filemap::write_all_files(&result, out, mode, config, run_config);
434441

435442
if let Err(msg) = write_result {
436443
println!("Error writing files: {}", msg);
437444
}
438445
}
439446

440447
// Similar to run, but takes an input String instead of a file to format
441-
pub fn run_from_stdin(input: String, write_mode: WriteMode, config: &Config) {
448+
pub fn run_from_stdin(input: String,
449+
write_mode: WriteMode,
450+
config: &Config,
451+
run_config: &RunConfig) {
442452
let mode = check_write_mode(write_mode, config.write_mode);
443-
let mut result = format_string(input, config, mode);
444-
fmt_lines(&mut result, config);
453+
let mut result = format_string(input, config, run_config, mode);
454+
fmt_lines(&mut result, config, run_config);
445455

446456
let mut out = stdout();
447-
let write_result = filemap::write_file(&result["stdin"], "stdin", &mut out, mode, config);
457+
let write_result = filemap::write_file(&result["stdin"],
458+
"stdin",
459+
&mut out,
460+
mode,
461+
config,
462+
run_config);
448463

449464
if let Err(msg) = write_result {
450465
panic!("Error writing to stdout: {}", msg);

src/rewrite.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use syntax::parse::ParseSess;
1515

1616
use Indent;
1717
use config::Config;
18+
use run_config::RunConfig;
1819

1920
pub trait Rewrite {
2021
/// Rewrite self into offset and width.
@@ -31,6 +32,7 @@ pub struct RewriteContext<'a> {
3132
pub parse_session: &'a ParseSess,
3233
pub codemap: &'a CodeMap,
3334
pub config: &'a Config,
35+
pub run_config: &'a RunConfig,
3436
// Indentation due to nesting of blocks.
3537
pub block_indent: Indent,
3638
}
@@ -41,6 +43,7 @@ impl<'a> RewriteContext<'a> {
4143
parse_session: self.parse_session,
4244
codemap: self.codemap,
4345
config: self.config,
46+
run_config: self.run_config,
4447
block_indent: self.block_indent.block_indent(self.config),
4548
}
4649
}

src/run_config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/// Contains configuration specific to a single run of `rustfmt`.
12+
impl_default_and_override! {
13+
pub struct RunConfig {}
14+
}

0 commit comments

Comments
 (0)