show rustfmt coverage!
This commit is contained in:
parent
29b244b67e
commit
d135217db2
7 changed files with 42 additions and 14 deletions
|
@ -108,7 +108,7 @@ fn determine_params<I>(args: I) -> Option<(PathBuf, WriteMode)>
|
||||||
opts.optopt("",
|
opts.optopt("",
|
||||||
"write-mode",
|
"write-mode",
|
||||||
"mode to write in",
|
"mode to write in",
|
||||||
"[replace|overwrite|display|diff]");
|
"[replace|overwrite|display|diff|coverage]");
|
||||||
let matches = match opts.parse(args) {
|
let matches = match opts.parse(args) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
|
@ -422,7 +422,7 @@ impl Rewrite for ast::Block {
|
||||||
return Some(user_str);
|
return Some(user_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config);
|
let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config, None);
|
||||||
visitor.block_indent = context.block_indent;
|
visitor.block_indent = context.block_indent;
|
||||||
|
|
||||||
let prefix = match self.rules {
|
let prefix = match self.rules {
|
||||||
|
@ -833,7 +833,7 @@ impl Rewrite for ast::Arm {
|
||||||
let attr_str = if !attrs.is_empty() {
|
let attr_str = if !attrs.is_empty() {
|
||||||
// We only use this visitor for the attributes, should we use it for
|
// We only use this visitor for the attributes, should we use it for
|
||||||
// more?
|
// more?
|
||||||
let mut attr_visitor = FmtVisitor::from_codemap(context.codemap, context.config);
|
let mut attr_visitor = FmtVisitor::from_codemap(context.codemap, context.config, None);
|
||||||
attr_visitor.block_indent = context.block_indent;
|
attr_visitor.block_indent = context.block_indent;
|
||||||
attr_visitor.last_pos = attrs[0].span.lo;
|
attr_visitor.last_pos = attrs[0].span.lo;
|
||||||
if attr_visitor.visit_attrs(attrs) {
|
if attr_visitor.visit_attrs(attrs) {
|
||||||
|
|
|
@ -100,7 +100,7 @@ fn write_file(text: &StringBuffer,
|
||||||
let file = try!(File::create(&filename));
|
let file = try!(File::create(&filename));
|
||||||
try!(write_system_newlines(file, text, config));
|
try!(write_system_newlines(file, text, config));
|
||||||
}
|
}
|
||||||
WriteMode::Display => {
|
WriteMode::Display | WriteMode::Coverage => {
|
||||||
println!("{}:\n", filename);
|
println!("{}:\n", filename);
|
||||||
let stdout = stdout();
|
let stdout = stdout();
|
||||||
let stdout_lock = stdout.lock();
|
let stdout_lock = stdout.lock();
|
||||||
|
|
13
src/lib.rs
13
src/lib.rs
|
@ -182,6 +182,8 @@ pub enum WriteMode {
|
||||||
Diff,
|
Diff,
|
||||||
// Return the result as a mapping from filenames to Strings.
|
// Return the result as a mapping from filenames to Strings.
|
||||||
Return,
|
Return,
|
||||||
|
// Display how much of the input file was processed
|
||||||
|
Coverage,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for WriteMode {
|
impl FromStr for WriteMode {
|
||||||
|
@ -193,6 +195,7 @@ impl FromStr for WriteMode {
|
||||||
"display" => Ok(WriteMode::Display),
|
"display" => Ok(WriteMode::Display),
|
||||||
"overwrite" => Ok(WriteMode::Overwrite),
|
"overwrite" => Ok(WriteMode::Overwrite),
|
||||||
"diff" => Ok(WriteMode::Diff),
|
"diff" => Ok(WriteMode::Diff),
|
||||||
|
"coverage" => Ok(WriteMode::Coverage),
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,11 +280,11 @@ impl fmt::Display for FormatReport {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatting which depends on the AST.
|
// Formatting which depends on the AST.
|
||||||
fn fmt_ast(krate: &ast::Crate, codemap: &CodeMap, config: &Config) -> FileMap {
|
fn fmt_ast(krate: &ast::Crate, codemap: &CodeMap, config: &Config, mode: WriteMode) -> FileMap {
|
||||||
let mut file_map = FileMap::new();
|
let mut file_map = FileMap::new();
|
||||||
for (path, module) in modules::list_files(krate, codemap) {
|
for (path, module) in modules::list_files(krate, codemap) {
|
||||||
let path = path.to_str().unwrap();
|
let path = path.to_str().unwrap();
|
||||||
let mut visitor = FmtVisitor::from_codemap(codemap, config);
|
let mut visitor = FmtVisitor::from_codemap(codemap, config, Some(mode));
|
||||||
visitor.format_separate_mod(module, path);
|
visitor.format_separate_mod(module, path);
|
||||||
file_map.insert(path.to_owned(), visitor.buffer);
|
file_map.insert(path.to_owned(), visitor.buffer);
|
||||||
}
|
}
|
||||||
|
@ -370,10 +373,10 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
|
||||||
report
|
report
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(file: &Path, config: &Config) -> FileMap {
|
pub fn format(file: &Path, config: &Config, mode: WriteMode) -> FileMap {
|
||||||
let parse_session = ParseSess::new();
|
let parse_session = ParseSess::new();
|
||||||
let krate = parse::parse_crate_from_file(file, Vec::new(), &parse_session);
|
let krate = parse::parse_crate_from_file(file, Vec::new(), &parse_session);
|
||||||
let mut file_map = fmt_ast(&krate, parse_session.codemap(), config);
|
let mut file_map = fmt_ast(&krate, parse_session.codemap(), config, mode);
|
||||||
|
|
||||||
// For some reason, the codemap does not include terminating
|
// For some reason, the codemap does not include terminating
|
||||||
// newlines so we must add one on for each file. This is sad.
|
// newlines so we must add one on for each file. This is sad.
|
||||||
|
@ -387,7 +390,7 @@ pub fn format(file: &Path, config: &Config) -> FileMap {
|
||||||
// write_mode determines what happens to the result of running rustfmt, see
|
// write_mode determines what happens to the result of running rustfmt, see
|
||||||
// WriteMode.
|
// WriteMode.
|
||||||
pub fn run(file: &Path, write_mode: WriteMode, config: &Config) {
|
pub fn run(file: &Path, write_mode: WriteMode, config: &Config) {
|
||||||
let mut result = format(file, config);
|
let mut result = format(file, config, write_mode);
|
||||||
|
|
||||||
println!("{}", fmt_lines(&mut result, config));
|
println!("{}", fmt_lines(&mut result, config));
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
use WriteMode;
|
||||||
use visitor::FmtVisitor;
|
use visitor::FmtVisitor;
|
||||||
|
|
||||||
use syntax::codemap::{self, BytePos, Span, Pos};
|
use syntax::codemap::{self, BytePos, Span, Pos};
|
||||||
use comment::{CodeCharKind, CommentCodeSlices, rewrite_comment};
|
use comment::{CodeCharKind, CommentCodeSlices, rewrite_comment};
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ impl<'a> FmtVisitor<'a> {
|
||||||
fn write_snippet_inner<F>(&mut self,
|
fn write_snippet_inner<F>(&mut self,
|
||||||
big_snippet: &str,
|
big_snippet: &str,
|
||||||
big_diff: usize,
|
big_diff: usize,
|
||||||
snippet: &str,
|
old_snippet: &str,
|
||||||
process_last_snippet: F)
|
process_last_snippet: F)
|
||||||
where F: Fn(&mut FmtVisitor, &str, &str)
|
where F: Fn(&mut FmtVisitor, &str, &str)
|
||||||
{
|
{
|
||||||
|
@ -91,6 +91,26 @@ impl<'a> FmtVisitor<'a> {
|
||||||
let mut last_wspace = None;
|
let mut last_wspace = None;
|
||||||
let mut rewrite_next_comment = true;
|
let mut rewrite_next_comment = true;
|
||||||
|
|
||||||
|
fn replace_chars(string: &str) -> String {
|
||||||
|
string.chars()
|
||||||
|
.map(|ch| {
|
||||||
|
match ch.is_whitespace() {
|
||||||
|
true => ch,
|
||||||
|
false => 'X',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
let replaced = match self.write_mode {
|
||||||
|
Some(mode) => match mode {
|
||||||
|
WriteMode::Coverage => replace_chars(old_snippet),
|
||||||
|
_ => old_snippet.to_owned(),
|
||||||
|
},
|
||||||
|
None => old_snippet.to_owned(),
|
||||||
|
};
|
||||||
|
let snippet = &*replaced;
|
||||||
|
|
||||||
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
|
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
|
||||||
if let CodeCharKind::Comment = kind {
|
if let CodeCharKind::Comment = kind {
|
||||||
let last_char = big_snippet[..(offset + big_diff)]
|
let last_char = big_snippet[..(offset + big_diff)]
|
||||||
|
|
|
@ -14,7 +14,7 @@ use syntax::visit;
|
||||||
|
|
||||||
use strings::string_buffer::StringBuffer;
|
use strings::string_buffer::StringBuffer;
|
||||||
|
|
||||||
use Indent;
|
use {Indent, WriteMode};
|
||||||
use utils;
|
use utils;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use rewrite::{Rewrite, RewriteContext};
|
use rewrite::{Rewrite, RewriteContext};
|
||||||
|
@ -29,6 +29,7 @@ pub struct FmtVisitor<'a> {
|
||||||
// TODO: RAII util for indenting
|
// TODO: RAII util for indenting
|
||||||
pub block_indent: Indent,
|
pub block_indent: Indent,
|
||||||
pub config: &'a Config,
|
pub config: &'a Config,
|
||||||
|
pub write_mode: Option<WriteMode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FmtVisitor<'a> {
|
impl<'a> FmtVisitor<'a> {
|
||||||
|
@ -356,7 +357,10 @@ impl<'a> FmtVisitor<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_codemap(codemap: &'a CodeMap, config: &'a Config) -> FmtVisitor<'a> {
|
pub fn from_codemap(codemap: &'a CodeMap,
|
||||||
|
config: &'a Config,
|
||||||
|
mode: Option<WriteMode>)
|
||||||
|
-> FmtVisitor<'a> {
|
||||||
FmtVisitor {
|
FmtVisitor {
|
||||||
codemap: codemap,
|
codemap: codemap,
|
||||||
buffer: StringBuffer::new(),
|
buffer: StringBuffer::new(),
|
||||||
|
@ -366,6 +370,7 @@ impl<'a> FmtVisitor<'a> {
|
||||||
alignment: 0,
|
alignment: 0,
|
||||||
},
|
},
|
||||||
config: config,
|
config: config,
|
||||||
|
write_mode: mode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -145,7 +145,7 @@ pub fn idempotent_check(filename: String) -> Result<FormatReport, HashMap<String
|
||||||
// Don't generate warnings for to-do items.
|
// Don't generate warnings for to-do items.
|
||||||
config.report_todo = ReportTactic::Never;
|
config.report_todo = ReportTactic::Never;
|
||||||
|
|
||||||
let mut file_map = format(Path::new(&filename), &config);
|
let mut file_map = format(Path::new(&filename), &config, WriteMode::Return);
|
||||||
let format_report = fmt_lines(&mut file_map, &config);
|
let format_report = fmt_lines(&mut file_map, &config);
|
||||||
|
|
||||||
// Won't panic, as we're not doing any IO.
|
// Won't panic, as we're not doing any IO.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue