Skip to content

Handle pub(restricted) #1013

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 3 commits into from
May 27, 2016
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
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ syntex_syntax = "0.32"
log = "0.3"
env_logger = "0.3"
getopts = "0.2"
itertools = "0.4.15"
43 changes: 15 additions & 28 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ 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 vis = format_visibility(&item.vis);
let mut_str = if is_mutable {
"mut "
} else {
Expand Down Expand Up @@ -305,11 +302,7 @@ impl<'a> FmtVisitor<'a> {
enum_def: &ast::EnumDef,
generics: &ast::Generics,
span: Span) {
let header_str = match format_header("enum ", ident, vis) {
Some(s) => s,
None => return,
};
self.buffer.push_str(&header_str);
self.buffer.push_str(&format_header("enum ", ident, vis));

let enum_snippet = self.snippet(span);
let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1);
Expand Down Expand Up @@ -453,7 +446,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(try_opt!(format_visibility(&item.vis)));
result.push_str(&*format_visibility(&item.vis));
result.push_str(format_unsafety(unsafety));
result.push_str("impl");

Expand Down Expand Up @@ -593,7 +586,7 @@ pub fn format_struct(context: &RewriteContext,
one_line_width: Option<usize>)
-> Option<String> {
match *struct_def {
ast::VariantData::Unit(..) => format_unit_struct(item_name, ident, vis),
ast::VariantData::Unit(..) => Some(format_unit_struct(item_name, ident, vis)),
ast::VariantData::Tuple(ref fields, _) => {
format_tuple_struct(context,
item_name,
Expand Down Expand Up @@ -623,7 +616,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
item.node {
let mut result = String::new();
let header = format!("{}{}trait {}",
try_opt!(format_visibility(&item.vis)),
format_visibility(&item.vis),
format_unsafety(unsafety),
item.ident);

Expand Down Expand Up @@ -744,14 +737,8 @@ 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 = try_opt!(format_header(item_name, ident, vis));
result.push_str(&header_str);
result.push(';');

Some(result)
fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
format!("{};", format_header(item_name, ident, vis))
}

fn format_struct_struct(context: &RewriteContext,
Expand All @@ -766,7 +753,7 @@ fn format_struct_struct(context: &RewriteContext,
-> Option<String> {
let mut result = String::with_capacity(1024);

let header_str = try_opt!(format_header(item_name, ident, vis));
let header_str = format_header(item_name, ident, vis);
result.push_str(&header_str);

let body_lo = context.codemap.span_after(span, "{");
Expand Down Expand Up @@ -859,7 +846,7 @@ fn format_tuple_struct(context: &RewriteContext,
-> Option<String> {
let mut result = String::with_capacity(1024);

let header_str = try_opt!(format_header(item_name, ident, vis));
let header_str = format_header(item_name, ident, vis);
result.push_str(&header_str);

// FIXME(#919): don't lose comments on empty tuple structs.
Expand Down Expand Up @@ -945,7 +932,7 @@ pub fn rewrite_type_alias(context: &RewriteContext,
-> Option<String> {
let mut result = String::new();

result.push_str(&try_opt!(format_visibility(&vis)));
result.push_str(&format_visibility(&vis));
result.push_str("type ");
result.push_str(&ident.to_string());

Expand Down Expand Up @@ -1013,7 +1000,7 @@ impl Rewrite for ast::StructField {
}

let name = self.ident;
let vis = try_opt!(format_visibility(&self.vis));
let vis = 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() {
Expand Down Expand Up @@ -1042,7 +1029,7 @@ pub fn rewrite_static(prefix: &str,
context: &RewriteContext)
-> Option<String> {
let prefix = format!("{}{} {}{}: ",
try_opt!(format_visibility(vis)),
format_visibility(vis),
prefix,
format_mutability(mutability),
ident);
Expand Down Expand Up @@ -1260,7 +1247,7 @@ fn rewrite_fn_base(context: &RewriteContext,

let mut result = String::with_capacity(1024);
// Vis unsafety abi.
result.push_str(try_opt!(format_visibility(vis)));
result.push_str(&*format_visibility(vis));

if let ast::Constness::Const = constness {
result.push_str("const ");
Expand Down Expand Up @@ -1816,8 +1803,8 @@ fn rewrite_where_clause(context: &RewriteContext,
}
}

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_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
format!("{}{}{}", format_visibility(vis), item_name, ident)
}

fn format_generics(context: &RewriteContext,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern crate unicode_segmentation;
extern crate regex;
extern crate diff;
extern crate term;
extern crate itertools;

use syntax::ast;
use syntax::codemap::{mk_sp, CodeMap, Span};
Expand Down
28 changes: 20 additions & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::borrow::Cow;
use std::cmp::Ordering;

use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind};
use itertools::Itertools;

use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind, Path};
use syntax::codemap::{CodeMap, Span, BytePos};
use syntax::abi;

Expand Down Expand Up @@ -66,14 +69,23 @@ pub fn extra_offset(text: &str, offset: Indent) -> usize {
}
}

#[inline]
pub fn format_visibility(vis: &Visibility) -> Option<&'static str> {
// Uses Cow to avoid allocating in the common cases.
pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> {
match *vis {
Visibility::Public => Some("pub "),
Visibility::Inherited => Some(""),
// FIXME(#970): Handle new visibility types.
Visibility::Crate(_) => None,
Visibility::Restricted { .. } => None,
Visibility::Public => Cow::from("pub "),
Visibility::Inherited => Cow::from(""),
Visibility::Crate(_) => Cow::from("pub(crate) "),
Visibility::Restricted { ref path, .. } => {
let Path { global, ref segments, .. } = **path;
let prefix = if global {
"::"
} else {
""
};
let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.as_str());

Cow::from(format!("pub({}{}) ", prefix, segments_iter.join("::")))
}
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,7 @@ 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(source!(self, m.inner));

if let Some(vis) = utils::format_visibility(vis) {
self.buffer.push_str(vis);
}
self.buffer.push_str(&*utils::format_visibility(vis));
self.buffer.push_str("mod ");
self.buffer.push_str(&ident.to_string());

Expand Down Expand Up @@ -540,10 +538,7 @@ impl<'a> FmtVisitor<'a> {
}

fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: Span) {
let vis = match utils::format_visibility(vis) {
Some(s) => s,
None => return,
};
let vis = utils::format_visibility(vis);
let mut offset = self.block_indent;
offset.alignment += vis.len() + "use ".len();
// 1 = ";"
Expand Down
51 changes: 51 additions & 0 deletions tests/source/pub-restricted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pub( super ) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}

pub( crate ) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}

pub( ::global:: path :: to::some_mod ) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}

pub( local:: path :: to::some_mod ) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}
51 changes: 51 additions & 0 deletions tests/target/pub-restricted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pub(super) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}

pub(crate) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}

pub(::global::path::to::some_mod) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}

pub(local::path::to::some_mod) enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}