Skip to content

Commit 435ab86

Browse files
authored
Merge pull request #1945 from topecongiro/issue-1095
Exclude prefix colon from span when rewriting comment
2 parents 84e7ef0 + c720a3a commit 435ab86

File tree

6 files changed

+59
-29
lines changed

6 files changed

+59
-29
lines changed

src/items.rs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,35 +2313,45 @@ fn rewrite_generics_inner(
23132313
) -> Option<String> {
23142314
// FIXME: convert bounds to where clauses where they get too big or if
23152315
// there is a where clause at all.
2316-
let lifetimes: &[_] = &generics.lifetimes;
2317-
let tys: &[_] = &generics.ty_params;
2318-
if lifetimes.is_empty() && tys.is_empty() {
2319-
return Some(String::new());
2320-
}
23212316

2322-
// Strings for the generics.
2323-
let lt_strs = lifetimes.iter().map(|lt| lt.rewrite(context, shape));
2324-
let ty_strs = tys.iter().map(|ty_param| ty_param.rewrite(context, shape));
2317+
// Wrapper type
2318+
enum GenericsArg<'a> {
2319+
Lifetime(&'a ast::LifetimeDef),
2320+
TyParam(&'a ast::TyParam),
2321+
}
2322+
impl<'a> Rewrite for GenericsArg<'a> {
2323+
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
2324+
match *self {
2325+
GenericsArg::Lifetime(ref lifetime) => lifetime.rewrite(context, shape),
2326+
GenericsArg::TyParam(ref ty) => ty.rewrite(context, shape),
2327+
}
2328+
}
2329+
}
2330+
impl<'a> Spanned for GenericsArg<'a> {
2331+
fn span(&self) -> Span {
2332+
match *self {
2333+
GenericsArg::Lifetime(ref lifetime) => lifetime.span(),
2334+
GenericsArg::TyParam(ref ty) => ty.span(),
2335+
}
2336+
}
2337+
}
23252338

2326-
// Extract comments between generics.
2327-
let lt_spans = lifetimes.iter().map(|l| {
2328-
let hi = if l.bounds.is_empty() {
2329-
l.lifetime.span.hi()
2330-
} else {
2331-
l.bounds[l.bounds.len() - 1].span.hi()
2332-
};
2333-
mk_sp(l.lifetime.span.lo(), hi)
2334-
});
2335-
let ty_spans = tys.iter().map(|ty| ty.span());
2339+
if generics.lifetimes.is_empty() && generics.ty_params.is_empty() {
2340+
return Some(String::new());
2341+
}
23362342

2343+
let generics_args = generics
2344+
.lifetimes
2345+
.iter()
2346+
.map(|lt| GenericsArg::Lifetime(lt))
2347+
.chain(generics.ty_params.iter().map(|ty| GenericsArg::TyParam(ty)));
23372348
let items = itemize_list(
23382349
context.codemap,
2339-
lt_spans.chain(ty_spans).zip(lt_strs.chain(ty_strs)),
2350+
generics_args,
23402351
">",
2341-
|&(sp, _)| sp.lo(),
2342-
|&(sp, _)| sp.hi(),
2343-
// FIXME: don't clone
2344-
|&(_, ref str)| str.clone(),
2352+
|arg| arg.span().lo(),
2353+
|arg| arg.span().hi(),
2354+
|arg| arg.rewrite(context, shape),
23452355
context.codemap.span_after(span, "<"),
23462356
span.hi(),
23472357
false,

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ impl Spanned for ast::TyParamBound {
217217
}
218218
}
219219

220+
impl Spanned for ast::LifetimeDef {
221+
fn span(&self) -> Span {
222+
let hi = if self.bounds.is_empty() {
223+
self.lifetime.span.hi()
224+
} else {
225+
self.bounds[self.bounds.len() - 1].span.hi()
226+
};
227+
mk_sp(self.lifetime.span.lo(), hi)
228+
}
229+
}
230+
220231
impl Spanned for MacroArg {
221232
fn span(&self) -> Span {
222233
match *self {

src/lists.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ where
641641
self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
642642
let post_snippet = post_snippet[..comment_end].trim();
643643

644-
let post_snippet_trimmed = if post_snippet.starts_with(',') {
644+
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
645645
post_snippet[1..].trim_matches(white_space)
646646
} else if post_snippet.ends_with(',') {
647647
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)

src/types.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,10 @@ impl Rewrite for ast::TyParam {
584584
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
585585
let mut result = String::with_capacity(128);
586586
// FIXME: If there are more than one attributes, this will force multiline.
587-
let attr_str = match (&*self.attrs).rewrite(context, shape) {
588-
Some(ref rw) if !rw.is_empty() => format!("{} ", rw),
589-
_ => String::new(),
590-
};
591-
result.push_str(&attr_str);
587+
match self.attrs.rewrite(context, shape) {
588+
Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
589+
_ => (),
590+
}
592591
result.push_str(&self.ident.to_string());
593592
if !self.bounds.is_empty() {
594593
result.push_str(type_bound_colon(context));

tests/source/structs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ pub struct Foo {
1515
pub i: TypeForPublicField
1616
}
1717

18+
// #1095
19+
struct S<T: /* comment */> {
20+
t: T,
21+
}
22+
1823
// #1029
1924
pub struct Foo {
2025
#[doc(hidden)]

tests/target/structs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ pub struct Foo {
1414
pub i: TypeForPublicField,
1515
}
1616

17+
// #1095
18+
struct S<T /* comment */> {
19+
t: T,
20+
}
21+
1722
// #1029
1823
pub struct Foo {
1924
#[doc(hidden)]

0 commit comments

Comments
 (0)