Skip to content

Commit d04ab9e

Browse files
authored
Merge pull request #1630 from topecongiro/issue-1115
Allow comments after attributes on enum fields
2 parents 14a1ea8 + d7de5b7 commit d04ab9e

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

src/items.rs

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,30 @@ impl<'a> FmtVisitor<'a> {
456456
return Some(self.snippet(span));
457457
}
458458

459+
let context = self.get_context();
459460
let indent = self.block_indent;
460-
let mut result = try_opt!(field.node.attrs.rewrite(&self.get_context(),
461-
Shape::indented(indent, self.config)));
461+
let mut result = try_opt!(field
462+
.node
463+
.attrs
464+
.rewrite(&context, Shape::indented(indent, self.config)));
462465
if !result.is_empty() {
463-
result.push('\n');
464-
result.push_str(&indent.to_string(self.config));
466+
let shape = Shape {
467+
width: context.config.max_width(),
468+
indent: self.block_indent,
469+
offset: self.block_indent.alignment,
470+
};
471+
let missing_comment =
472+
rewrite_missing_comment_on_field(&context,
473+
shape,
474+
field.node.attrs[field.node.attrs.len() - 1]
475+
.span
476+
.hi,
477+
field.span.lo,
478+
&mut result)
479+
.unwrap_or(String::new());
480+
result.push_str(&missing_comment);
465481
}
466482

467-
let context = self.get_context();
468483
let variant_body = match field.node.data {
469484
ast::VariantData::Tuple(..) |
470485
ast::VariantData::Struct(..) => {
@@ -1194,6 +1209,31 @@ fn type_annotation_spacing(config: &Config) -> (&str, &str) {
11941209
})
11951210
}
11961211

1212+
fn rewrite_missing_comment_on_field(context: &RewriteContext,
1213+
shape: Shape,
1214+
lo: BytePos,
1215+
hi: BytePos,
1216+
result: &mut String)
1217+
-> Option<String> {
1218+
let possibly_comment_snippet = context.snippet(mk_sp(lo, hi));
1219+
let newline_index = possibly_comment_snippet.find('\n');
1220+
let comment_index = possibly_comment_snippet.find('/');
1221+
match (newline_index, comment_index) {
1222+
(Some(i), Some(j)) if i > j => result.push(' '),
1223+
_ => {
1224+
result.push('\n');
1225+
result.push_str(&shape.indent.to_string(context.config));
1226+
}
1227+
}
1228+
let trimmed = possibly_comment_snippet.trim();
1229+
if trimmed.is_empty() {
1230+
None
1231+
} else {
1232+
rewrite_comment(trimmed, false, shape, context.config)
1233+
.map(|s| format!("{}\n{}", s, shape.indent.to_string(context.config)))
1234+
}
1235+
}
1236+
11971237
impl Rewrite for ast::StructField {
11981238
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
11991239
if contains_skip(&self.attrs) {
@@ -1208,25 +1248,12 @@ impl Rewrite for ast::StructField {
12081248
context.config)));
12091249
// Try format missing comments after attributes
12101250
let missing_comment = if !self.attrs.is_empty() {
1211-
let possibly_comment_snippet =
1212-
context.snippet(mk_sp(self.attrs[self.attrs.len() - 1].span.hi, self.span.lo));
1213-
let newline_index = possibly_comment_snippet.find('\n');
1214-
let comment_index = possibly_comment_snippet.find('/');
1215-
match (newline_index, comment_index) {
1216-
(Some(i), Some(j)) if i > j => attr_str.push(' '),
1217-
_ => {
1218-
attr_str.push('\n');
1219-
attr_str.push_str(&shape.indent.to_string(context.config));
1220-
}
1221-
}
1222-
let trimmed = possibly_comment_snippet.trim();
1223-
if trimmed.is_empty() {
1224-
String::new()
1225-
} else {
1226-
rewrite_comment(trimmed, false, shape, context.config).map_or(String::new(), |s| {
1227-
format!("{}\n{}", s, shape.indent.to_string(context.config))
1228-
})
1229-
}
1251+
rewrite_missing_comment_on_field(context,
1252+
shape,
1253+
self.attrs[self.attrs.len() - 1].span.hi,
1254+
self.span.lo,
1255+
&mut attr_str)
1256+
.unwrap_or(String::new())
12301257
} else {
12311258
String::new()
12321259
};

tests/source/enum.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,14 @@ fn nested_enum_test() {
9898
pub struct EmtpyWithComment {
9999
// FIXME: Implement this struct
100100
}
101+
102+
// #1115
103+
pub enum Bencoding<'i> {
104+
Str(&'i [u8]),
105+
Int(i64),
106+
List(Vec<Bencoding<'i>>),
107+
/// A bencoded dict value. The first element the slice of bytes in the source that the dict is
108+
/// composed of. The second is the dict, decoded into an ordered map.
109+
// TODO make Dict "structlike" AKA name the two values.
110+
Dict(&'i [u8], BTreeMap<&'i [u8], Bencoding<'i>>),
111+
}

tests/target/enum.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,15 @@ fn nested_enum_test() {
127127
pub struct EmtpyWithComment {
128128
// FIXME: Implement this struct
129129
}
130+
131+
// #1115
132+
pub enum Bencoding<'i> {
133+
Str(&'i [u8]),
134+
Int(i64),
135+
List(Vec<Bencoding<'i>>),
136+
/// A bencoded dict value. The first element the slice of bytes in the
137+
/// source that the dict is
138+
/// composed of. The second is the dict, decoded into an ordered map.
139+
// TODO make Dict "structlike" AKA name the two values.
140+
Dict(&'i [u8], BTreeMap<&'i [u8], Bencoding<'i>>),
141+
}

0 commit comments

Comments
 (0)