Catch attributes before comments

Closes #1280
Closes #1284
This commit is contained in:
Nick Cameron 2017-01-26 15:10:47 +13:00
parent b5d4421a83
commit 2261947f6e
5 changed files with 27 additions and 2 deletions

2
Cargo.lock generated
View File

@ -1,6 +1,6 @@
[root]
name = "rustfmt"
version = "0.7.0"
version = "0.7.1"
dependencies = [
"diff 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -84,6 +84,8 @@ impl<'a> FmtVisitor<'a> {
let big_diff = (span.lo - big_span_lo).to_usize();
let snippet = self.snippet(span);
debug!("write_snippet `{}`", snippet);
self.write_snippet_inner(big_snippet, big_diff, &snippet, process_last_snippet);
}
@ -114,6 +116,8 @@ impl<'a> FmtVisitor<'a> {
let snippet = &*replaced;
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
debug!("{:?}: {:?}", kind, subslice);
if let CodeCharKind::Comment = kind {
let last_char = big_snippet[..(offset + big_diff)]
.chars()
@ -199,6 +203,13 @@ impl<'a> FmtVisitor<'a> {
last_wspace = None;
}
}
let remaining = snippet[line_start..subslice.len() + offset].trim();
if !remaining.is_empty() {
self.buffer.push_str(remaining);
line_start = subslice.len() + offset;
rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
}
}
process_last_snippet(self, &snippet[line_start..], snippet);

View File

@ -1,3 +1,5 @@
#![allow(dead_code)] // bar
//! Doc comment
fn test() {
// comment
@ -44,3 +46,8 @@ fn debug_function() {
println!("hello");
}
// */
#[link_section=".vectors"]
#[no_mangle] // Test this attribute is preserved.
#[cfg_attr(rustfmt, rustfmt_skip)]
pub static ISSUE_1284: [i32; 16] = [];

View File

@ -1,3 +1,5 @@
#![allow(dead_code)] // bar
//! Doc comment
fn test() {
// comment
@ -43,3 +45,8 @@ fn debug_function() {
println!("hello");
}
// */
#[link_section=".vectors"]
#[no_mangle] // Test this attribute is preserved.
#[cfg_attr(rustfmt, rustfmt_skip)]
pub static ISSUE_1284: [i32; 16] = [];

View File

@ -86,7 +86,7 @@ fn ______________________baz(a: i32)
*mut ::std::option::Option<extern "C" fn(arg1: i32,
_____________________a: i32,
arg3: i32)
-> ()> {
-> ()>{
}
pub fn check_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,