Allow comments after where clause

This commit is contained in:
topecongiro 2017-05-27 02:03:22 +09:00
parent b43958d96e
commit dab14b4485
3 changed files with 27 additions and 3 deletions

View file

@ -500,7 +500,11 @@ impl<'a> FmtVisitor<'a> {
}
}
pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
pub fn format_impl(context: &RewriteContext,
item: &ast::Item,
offset: Indent,
where_span_end: Option<BytePos>)
-> Option<String> {
if let ast::ItemKind::Impl(_, _, ref generics, ref trait_ref, _, ref items) = item.node {
let mut result = String::new();
// First try to format the ref and type without a split at the 'for'.
@ -527,7 +531,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
"{",
false,
last_line_width(&ref_and_type) == 1,
None));
where_span_end));
if try_opt!(is_impl_single_line(context, &items, &result, &where_clause_str, &item)) {
result.push_str(&where_clause_str);

View file

@ -19,6 +19,7 @@ use strings::string_buffer::StringBuffer;
use {Indent, Shape};
use utils;
use codemap::{LineRangeUtils, SpanUtils};
use comment::FindUncommented;
use config::Config;
use rewrite::{Rewrite, RewriteContext};
use comment::rewrite_comment;
@ -249,7 +250,15 @@ impl<'a> FmtVisitor<'a> {
}
ast::ItemKind::Impl(..) => {
self.format_missing_with_indent(source!(self, item.span).lo);
if let Some(impl_str) = format_impl(&self.get_context(), item, self.block_indent) {
let snippet = self.get_context().snippet(item.span);
let where_span_end =
snippet
.find_uncommented("{")
.map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
if let Some(impl_str) = format_impl(&self.get_context(),
item,
self.block_indent,
where_span_end) {
self.buffer.push_str(&impl_str);
self.last_pos = source!(self, item.span).hi;
}

View file

@ -5,3 +5,14 @@ impl<T> JSTraceable for SmallVec<[T; 1]> {}
impl<K, V, NodeRef: Deref<Target = Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
// Keep this.
}
impl<V> Test<V>
where V: Clone // This comment is NOT removed by formating!
{
pub fn new(value: V) -> Self {
Test {
cloned_value: value.clone(),
value: value,
}
}
}