Handle new visibility types more gracefully

This commit is contained in:
Kamal Marhubi 2016-05-02 01:05:23 -04:00
parent fd38acb86f
commit 6285d53885
3 changed files with 33 additions and 24 deletions

View File

@ -142,15 +142,16 @@ impl<'a> FmtVisitor<'a> {
ast::ForeignItemKind::Static(ref ty, is_mutable) => {
// FIXME(#21): we're dropping potential comments in between the
// function keywords here.
let vis = match format_visibility(&item.vis) {
Some(s) => s,
None => return,
};
let mut_str = if is_mutable {
"mut "
} else {
""
};
let prefix = format!("{}static {}{}: ",
format_visibility(&item.vis),
mut_str,
item.ident);
let prefix = format!("{}static {}{}: ", vis, mut_str, item.ident);
let offset = self.block_indent + prefix.len();
// 1 = ;
let width = self.config.max_width - offset.width() - 1;
@ -307,7 +308,10 @@ impl<'a> FmtVisitor<'a> {
enum_def: &ast::EnumDef,
generics: &ast::Generics,
span: Span) {
let header_str = format_header("enum ", ident, vis);
let header_str = match format_header("enum ", ident, vis) {
Some(s) => s,
None => return,
};
self.buffer.push_str(&header_str);
let enum_snippet = self.snippet(span);
@ -451,7 +455,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
ref self_ty,
ref items) = item.node {
let mut result = String::new();
result.push_str(format_visibility(&item.vis));
result.push_str(try_opt!(format_visibility(&item.vis)));
result.push_str(format_unsafety(unsafety));
result.push_str("impl");
@ -619,7 +623,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
item.node {
let mut result = String::new();
let header = format!("{}{}trait {}",
format_visibility(&item.vis),
try_opt!(format_visibility(&item.vis)),
format_unsafety(unsafety),
item.ident);
@ -744,7 +748,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> Option<String> {
let mut result = String::with_capacity(1024);
let header_str = format_header(item_name, ident, vis);
let header_str = try_opt!(format_header(item_name, ident, vis));
result.push_str(&header_str);
result.push(';');
@ -762,7 +766,7 @@ fn format_struct_struct(context: &RewriteContext,
-> Option<String> {
let mut result = String::with_capacity(1024);
let header_str = format_header(item_name, ident, vis);
let header_str = try_opt!(format_header(item_name, ident, vis));
result.push_str(&header_str);
let body_lo = context.codemap.span_after(span, "{");
@ -843,7 +847,7 @@ fn format_tuple_struct(context: &RewriteContext,
-> Option<String> {
let mut result = String::with_capacity(1024);
let header_str = format_header(item_name, ident, vis);
let header_str = try_opt!(format_header(item_name, ident, vis));
result.push_str(&header_str);
// FIXME(#919): don't lose comments on empty tuple structs.
@ -929,7 +933,7 @@ pub fn rewrite_type_alias(context: &RewriteContext,
-> Option<String> {
let mut result = String::new();
result.push_str(&format_visibility(&vis));
result.push_str(&try_opt!(format_visibility(&vis)));
result.push_str("type ");
result.push_str(&ident.to_string());
@ -997,7 +1001,7 @@ impl Rewrite for ast::StructField {
}
let name = self.ident;
let vis = format_visibility(&self.vis);
let vis = try_opt!(format_visibility(&self.vis));
let mut attr_str = try_opt!(self.attrs
.rewrite(context, context.config.max_width - offset.width(), offset));
if !attr_str.is_empty() {
@ -1026,7 +1030,7 @@ pub fn rewrite_static(prefix: &str,
context: &RewriteContext)
-> Option<String> {
let prefix = format!("{}{} {}{}: ",
format_visibility(vis),
try_opt!(format_visibility(vis)),
prefix,
format_mutability(mutability),
ident);
@ -1245,7 +1249,7 @@ fn rewrite_fn_base(context: &RewriteContext,
let mut result = String::with_capacity(1024);
// Vis unsafety abi.
result.push_str(format_visibility(vis));
result.push_str(try_opt!(format_visibility(vis)));
if let ast::Constness::Const = constness {
result.push_str("const ");
@ -1801,8 +1805,8 @@ fn rewrite_where_clause(context: &RewriteContext,
}
}
fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
format!("{}{}{}", format_visibility(vis), item_name, ident)
fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> Option<String> {
Some(format!("{}{}{}", try_opt!(format_visibility(vis)), item_name, ident))
}
fn format_generics(context: &RewriteContext,

View File

@ -67,13 +67,13 @@ pub fn extra_offset(text: &str, offset: Indent) -> usize {
}
#[inline]
pub fn format_visibility(vis: &Visibility) -> &'static str {
pub fn format_visibility(vis: &Visibility) -> Option<&'static str> {
match *vis {
Visibility::Public => "pub ",
Visibility::Inherited => "",
// TODO(#970): Handle new visibility types.
Visibility::Crate => unimplemented!(),
Visibility::Restricted { .. } => unimplemented!(),
Visibility::Public => Some("pub "),
Visibility::Inherited => Some(""),
// FIXME(#970): Handle new visibility types.
Visibility::Crate => None,
Visibility::Restricted { .. } => None,
}
}

View File

@ -467,7 +467,9 @@ impl<'a> FmtVisitor<'a> {
let local_file_name = self.codemap.span_to_filename(s);
let is_internal = local_file_name == self.codemap.span_to_filename(m.inner);
self.buffer.push_str(utils::format_visibility(vis));
if let Some(vis) = utils::format_visibility(vis) {
self.buffer.push_str(vis);
}
self.buffer.push_str("mod ");
self.buffer.push_str(&ident.to_string());
@ -502,7 +504,10 @@ impl<'a> FmtVisitor<'a> {
}
fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: Span) {
let vis = utils::format_visibility(vis);
let vis = match utils::format_visibility(vis) {
Some(s) => s,
None => return,
};
let mut offset = self.block_indent;
offset.alignment += vis.len() + "use ".len();
// 1 = ";"