Skip to content

Commit

Permalink
refactor: minor updates on pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Sep 24, 2019
1 parent 1cac774 commit 0531683
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::time::{Duration, Instant};

use syntax::ast;
use syntax::errors::emitter::{ColorConfig, Emitter, EmitterWriter};
use syntax::errors::{DiagnosticBuilder, Handler, HandlerFlags};
use syntax::errors::{DiagnosticBuilder, Handler};
use syntax::parse::{self, ParseSess};
use syntax::source_map::{FilePathMapping, SourceMap, Span, DUMMY_SP};

Expand Down Expand Up @@ -76,13 +76,13 @@ fn format_project<T: FormatHandler>(
}

// Parse the crate.
let can_reset = Rc::new(RefCell::new(false));
let can_reset_parser_errors = Rc::new(RefCell::new(false));
let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
let mut parse_session = make_parse_sess(
source_map.clone(),
config,
ignore_path_set.clone(),
can_reset.clone(),
Rc::clone(&ignore_path_set),
can_reset_parser_errors.clone(),
);
let mut report = FormatReport::new();
let directory_ownership = input.to_directory_ownership();
Expand All @@ -92,7 +92,7 @@ fn format_project<T: FormatHandler>(
config,
&mut report,
directory_ownership,
can_reset.clone(),
can_reset_parser_errors.clone(),
) {
Ok(krate) => krate,
// Surface parse error via Session (errors are merged there from report)
Expand Down Expand Up @@ -628,7 +628,7 @@ fn parse_crate(
config: &Config,
report: &mut FormatReport,
directory_ownership: Option<parse::DirectoryOwnership>,
parser_error_resetter: Rc<RefCell<bool>>,
can_reset_parser_errors: Rc<RefCell<bool>>,
) -> Result<ast::Crate, ErrorKind> {
let input_is_stdin = input.is_text();

Expand Down Expand Up @@ -681,7 +681,7 @@ fn parse_crate(
// occurred in files that are ignored, then reset
// the error count and continue.
// https://github.com/rust-lang/rustfmt/issues/3779
if *parser_error_resetter.borrow() {
if *can_reset_parser_errors.borrow() {
parse_session.span_diagnostic.reset_err_count();
return Ok(c);
}
Expand All @@ -705,9 +705,8 @@ struct SilentOnIgnoredFilesEmitter {
ignore_path_set: Rc<IgnorePathSet>,
source_map: Rc<SourceMap>,
emitter: EmitterWriter,
can_reset: bool,
has_non_ignorable_parser_errors: bool,
parser_error_resetter: Rc<RefCell<bool>>,
can_reset: Rc<RefCell<bool>>,
}

impl Emitter for SilentOnIgnoredFilesEmitter {
Expand All @@ -720,9 +719,8 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
.ignore_path_set
.is_match(&FileName::Real(path.to_path_buf()))
{
if !self.has_non_ignorable_parser_errors && !self.can_reset {
self.can_reset = true;
*self.parser_error_resetter.borrow_mut() = true;
if !self.has_non_ignorable_parser_errors {
*self.can_reset.borrow_mut() = true;
}
return;
}
Expand All @@ -732,10 +730,7 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
}

self.has_non_ignorable_parser_errors = true;
if self.can_reset {
*self.parser_error_resetter.borrow_mut() = false;
}
self.can_reset = false;
*self.can_reset.borrow_mut() = false;
self.emitter.emit_diagnostic(db);
}
}
Expand All @@ -755,7 +750,7 @@ fn make_parse_sess(
source_map: Rc<SourceMap>,
config: &Config,
ignore_path_set: Rc<IgnorePathSet>,
parser_error_resetter: Rc<RefCell<bool>>,
can_reset: Rc<RefCell<bool>>,
) -> ParseSess {
let tty_handler = if config.hide_parse_errors() {
let silent_emitter = silent_emitter();
Expand All @@ -772,20 +767,12 @@ fn make_parse_sess(
EmitterWriter::stderr(color_cfg, Some(source_map.clone()), false, false, None);
let emitter = Box::new(SilentOnIgnoredFilesEmitter {
has_non_ignorable_parser_errors: false,
can_reset: false,
ignore_path_set: ignore_path_set,
source_map: source_map.clone(),
source_map: Rc::clone(&source_map),
emitter: emitter_writer,
parser_error_resetter,
can_reset,
});
Handler::with_emitter_and_flags(
emitter,
HandlerFlags {
can_emit_warnings: false,
treat_err_as_bug: None,
..Default::default()
},
)
Handler::with_emitter(true, None, emitter)
};

ParseSess::with_span_handler(tty_handler, source_map)
Expand Down

0 comments on commit 0531683

Please sign in to comment.