Skip to content

Exclude prefix colon from span when rewriting comment #1945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2313,35 +2313,45 @@ fn rewrite_generics_inner(
) -> Option<String> {
// FIXME: convert bounds to where clauses where they get too big or if
// there is a where clause at all.
let lifetimes: &[_] = &generics.lifetimes;
let tys: &[_] = &generics.ty_params;
if lifetimes.is_empty() && tys.is_empty() {
return Some(String::new());
}

// Strings for the generics.
let lt_strs = lifetimes.iter().map(|lt| lt.rewrite(context, shape));
let ty_strs = tys.iter().map(|ty_param| ty_param.rewrite(context, shape));
// Wrapper type
enum GenericsArg<'a> {
Lifetime(&'a ast::LifetimeDef),
TyParam(&'a ast::TyParam),
}
impl<'a> Rewrite for GenericsArg<'a> {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
match *self {
GenericsArg::Lifetime(ref lifetime) => lifetime.rewrite(context, shape),
GenericsArg::TyParam(ref ty) => ty.rewrite(context, shape),
}
}
}
impl<'a> Spanned for GenericsArg<'a> {
fn span(&self) -> Span {
match *self {
GenericsArg::Lifetime(ref lifetime) => lifetime.span(),
GenericsArg::TyParam(ref ty) => ty.span(),
}
}
}

// Extract comments between generics.
let lt_spans = lifetimes.iter().map(|l| {
let hi = if l.bounds.is_empty() {
l.lifetime.span.hi()
} else {
l.bounds[l.bounds.len() - 1].span.hi()
};
mk_sp(l.lifetime.span.lo(), hi)
});
let ty_spans = tys.iter().map(|ty| ty.span());
if generics.lifetimes.is_empty() && generics.ty_params.is_empty() {
return Some(String::new());
}

let generics_args = generics
.lifetimes
.iter()
.map(|lt| GenericsArg::Lifetime(lt))
.chain(generics.ty_params.iter().map(|ty| GenericsArg::TyParam(ty)));
let items = itemize_list(
context.codemap,
lt_spans.chain(ty_spans).zip(lt_strs.chain(ty_strs)),
generics_args,
">",
|&(sp, _)| sp.lo(),
|&(sp, _)| sp.hi(),
// FIXME: don't clone
|&(_, ref str)| str.clone(),
|arg| arg.span().lo(),
|arg| arg.span().hi(),
|arg| arg.rewrite(context, shape),
context.codemap.span_after(span, "<"),
span.hi(),
false,
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ impl Spanned for ast::TyParamBound {
}
}

impl Spanned for ast::LifetimeDef {
fn span(&self) -> Span {
let hi = if self.bounds.is_empty() {
self.lifetime.span.hi()
} else {
self.bounds[self.bounds.len() - 1].span.hi()
};
mk_sp(self.lifetime.span.lo(), hi)
}
}

impl Spanned for MacroArg {
fn span(&self) -> Span {
match *self {
Expand Down
2 changes: 1 addition & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ where
self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
let post_snippet = post_snippet[..comment_end].trim();

let post_snippet_trimmed = if post_snippet.starts_with(',') {
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
post_snippet[1..].trim_matches(white_space)
} else if post_snippet.ends_with(',') {
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
Expand Down
9 changes: 4 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,10 @@ impl Rewrite for ast::TyParam {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
let mut result = String::with_capacity(128);
// FIXME: If there are more than one attributes, this will force multiline.
let attr_str = match (&*self.attrs).rewrite(context, shape) {
Some(ref rw) if !rw.is_empty() => format!("{} ", rw),
_ => String::new(),
};
result.push_str(&attr_str);
match self.attrs.rewrite(context, shape) {
Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
_ => (),
}
result.push_str(&self.ident.to_string());
if !self.bounds.is_empty() {
result.push_str(type_bound_colon(context));
Expand Down
5 changes: 5 additions & 0 deletions tests/source/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ pub struct Foo {
pub i: TypeForPublicField
}

// #1095
struct S<T: /* comment */> {
t: T,
}

// #1029
pub struct Foo {
#[doc(hidden)]
Expand Down
5 changes: 5 additions & 0 deletions tests/target/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ pub struct Foo {
pub i: TypeForPublicField,
}

// #1095
struct S<T /* comment */> {
t: T,
}

// #1029
pub struct Foo {
#[doc(hidden)]
Expand Down