Handle attributes on modules (#968)

* Handle attributes (including doc comments) on inline modules

Closes #22
Closes #684

* Tweak the rules for changing indentation in comments (to do it less often).
This commit is contained in:
Nick Cameron 2016-05-02 20:54:25 +12:00 committed by Marcus Klaas de Vries
parent 5092eec081
commit c696dcf8da
4 changed files with 31 additions and 17 deletions

View File

@ -82,7 +82,7 @@ pub fn rewrite_comment(orig: &str,
})
.map(left_trim_comment_line)
.map(|line| {
if line_breaks == 0 {
if orig.starts_with("/*") && line_breaks == 0 {
line.trim_left()
} else {
line
@ -529,10 +529,10 @@ pub fn recover_comment_removed(new: String,
/// Return true if the two strings of code have the same payload of comments.
/// The payload of comments is everything in the string except:
/// - actual code (not comments)
/// - comment start/end marks
/// - whitespace
/// - '*' at the beginning of lines in block comments
/// - actual code (not comments)
/// - comment start/end marks
/// - whitespace
/// - '*' at the beginning of lines in block comments
fn changed_comment_content(orig: &str, new: &str) -> bool {
// Cannot write this as a fn since we cannot return types containing closures
let code_comment_content = |code| {

View File

@ -177,14 +177,19 @@ impl<'a> FmtVisitor<'a> {
}
fn visit_item(&mut self, item: &ast::Item) {
// Don't look at attributes for modules (except for rustfmt_skip).
// We want to avoid looking at attributes in another file, which the AST
// doesn't distinguish.
// FIXME This is overly conservative and means we miss attributes on
// inline modules.
// Only look at attributes for modules (except for rustfmt_skip) if the
// module is inline. We want to avoid looking at attributes in another
// file, which the AST doesn't distinguish.
match item.node {
ast::ItemKind::Mod(_) => {
if utils::contains_skip(&item.attrs) {
ast::ItemKind::Mod(ref m) => {
let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
if outer_file.name == inner_file.name {
if self.visit_attrs(&item.attrs) {
self.push_rewrite(item.span, None);
return;
}
} else if utils::contains_skip(&item.attrs) {
return;
}
}
@ -551,7 +556,7 @@ impl<'a> Rewrite for [ast::Attribute] {
let indent = offset.to_string(context.config);
for (i, a) in self.iter().enumerate() {
let a_str = context.snippet(a.span);
let mut a_str = context.snippet(a.span);
// Write comments and blank lines between attributes.
if i > 0 {
@ -564,7 +569,7 @@ impl<'a> Rewrite for [ast::Attribute] {
if !comment.is_empty() {
let comment = try_opt!(rewrite_comment(comment,
false,
context.config.max_width -
context.config.ideal_width -
offset.width(),
offset,
context.config));
@ -577,6 +582,14 @@ impl<'a> Rewrite for [ast::Attribute] {
result.push_str(&indent);
}
if a_str.starts_with("//") {
a_str = try_opt!(rewrite_comment(&a_str,
false,
context.config.ideal_width - offset.width(),
offset,
context.config));
}
// Write the attribute itself.
result.push_str(&a_str);

View File

@ -33,8 +33,8 @@ impl Bar {
#[attrib1]
/// Blah blah bing.
#[attrib2]
// Another comment that needs rewrite because it's tooooooooooooooooooooooooooooooo
// loooooooooooong.
// Another comment that needs rewrite because it's
// tooooooooooooooooooooooooooooooo loooooooooooong.
/// Blah blah bing.
fn f4(self) -> Cat {}
}

View File

@ -96,7 +96,8 @@ pub struct State<F: FnMut()> {
}
struct Palette {
/// A map of indizes in the palette to a count of pixels in approximately that color
/// A map of indizes in the palette to a count of pixels in approximately
/// that color
foo: i32,
}