extern crate serde_derive;
extern crate serde_json;
extern crate syntax;
+extern crate syntax_pos;
extern crate toml;
extern crate unicode_segmentation;
mod missed_spans;
pub(crate) mod modules;
mod overflow;
+mod pairs;
mod patterns;
mod reorder;
mod rewrite;
ParseError,
/// The user mandated a version and the current version of Rustfmt does not
/// satisfy that requirement.
- #[fail(display = "Version mismatch")]
+ #[fail(display = "version mismatch")]
VersionMismatch,
+ /// If we had formatted the given node, then we would have lost a comment.
+ #[fail(display = "not formatted because a comment would be lost")]
+ LostComment,
+}
+
+impl ErrorKind {
+ fn is_comment(&self) -> bool {
+ match self {
+ ErrorKind::LostComment => true,
+ _ => false,
+ }
+ }
}
impl From<io::Error> for ErrorKind {
fn from_span(span: &Span, codemap: &CodeMap, kind: ErrorKind) -> FormattingError {
FormattingError {
line: codemap.lookup_char_pos(span.lo()).line,
+ is_comment: kind.is_comment(),
kind,
- is_comment: false,
is_string: false,
line_buffer: codemap
.span_to_lines(*span)
ErrorKind::LineOverflow(..)
| ErrorKind::TrailingWhitespace
| ErrorKind::IoError(_)
- | ErrorKind::ParseError => "internal error:",
+ | ErrorKind::ParseError
+ | ErrorKind::LostComment => "internal error:",
ErrorKind::LicenseCheck | ErrorKind::BadAttr | ErrorKind::VersionMismatch => "error:",
ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => "warning:",
}
fn format_len(&self) -> (usize, usize) {
match self.kind {
ErrorKind::LineOverflow(found, max) => (max, found - max),
- ErrorKind::TrailingWhitespace | ErrorKind::DeprecatedAttr | ErrorKind::BadAttr => {
+ ErrorKind::TrailingWhitespace
+ | ErrorKind::DeprecatedAttr
+ | ErrorKind::BadAttr
+ | ErrorKind::LostComment => {
let trailing_ws_start = self
.line_buffer
.rfind(|c: char| !c.is_whitespace())
is_string: bool,
error_kind: &ErrorKind,
) -> bool {
- let allow_error_report = if char_kind.is_comment() || is_string {
+ let allow_error_report = if char_kind.is_comment() || is_string || error_kind.is_comment() {
config.error_on_unformatted()
} else {
true
match error_kind {
ErrorKind::LineOverflow(..) => config.error_on_line_overflow() && allow_error_report,
- ErrorKind::TrailingWhitespace => allow_error_report,
+ ErrorKind::TrailingWhitespace | ErrorKind::LostComment => allow_error_report,
_ => true,
}
}
config: &Config,
mut out: Option<&mut T>,
) -> Result<(Summary, FileMap, FormatReport), (ErrorKind, Summary)> {
+ syntax_pos::hygiene::set_default_edition(config.edition().to_libsyntax_pos_edition());
let mut summary = Summary::default();
if config.disable_all_formatting() {
// When the input is from stdin, echo back the input.
}
}
-pub fn replace_with_system_newlines(text: &mut String, config: &Config) -> () {
+fn replace_with_system_newlines(text: &mut String, config: &Config) -> () {
let style = if config.newline_style() == NewlineStyle::Native {
if cfg!(windows) {
NewlineStyle::Windows
#[test]
fn test_format_snippet() {
let snippet = "fn main() { println!(\"hello, world\"); }";
+ #[cfg(not(windows))]
let expected = "fn main() {\n \
println!(\"hello, world\");\n\
}\n";
+ #[cfg(windows)]
+ let expected = "fn main() {\r\n \
+ println!(\"hello, world\");\r\n\
+ }\r\n";
assert!(test_format_inner(format_snippet, snippet, expected));
}