mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-12 20:16:49 +00:00
Merge pull request #487 from marcusklaas/konsts-n-statix
Format constants and static variables
This commit is contained in:
commit
8a9bbd9d7c
59
src/items.rs
59
src/items.rs
@ -825,15 +825,15 @@ impl<'a> FmtVisitor<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn format_struct(&self,
|
||||
item_name: &str,
|
||||
ident: ast::Ident,
|
||||
vis: ast::Visibility,
|
||||
struct_def: &ast::VariantData,
|
||||
generics: Option<&ast::Generics>,
|
||||
span: Span,
|
||||
offset: Indent)
|
||||
-> Option<String> {
|
||||
pub fn format_struct(&self,
|
||||
item_name: &str,
|
||||
ident: ast::Ident,
|
||||
vis: ast::Visibility,
|
||||
struct_def: &ast::VariantData,
|
||||
generics: Option<&ast::Generics>,
|
||||
span: Span,
|
||||
offset: Indent)
|
||||
-> Option<String> {
|
||||
let mut result = String::with_capacity(1024);
|
||||
|
||||
let header_str = self.format_header(item_name, ident, vis);
|
||||
@ -929,27 +929,6 @@ impl<'a> FmtVisitor<'a> {
|
||||
Some(result)
|
||||
}
|
||||
|
||||
pub fn visit_struct(&mut self,
|
||||
ident: ast::Ident,
|
||||
vis: ast::Visibility,
|
||||
struct_def: &ast::VariantData,
|
||||
generics: &ast::Generics,
|
||||
span: Span) {
|
||||
let indent = self.block_indent;
|
||||
let result = self.format_struct("struct ",
|
||||
ident,
|
||||
vis,
|
||||
struct_def,
|
||||
Some(generics),
|
||||
span,
|
||||
indent);
|
||||
|
||||
if let Some(rewrite) = result {
|
||||
self.buffer.push_str(&rewrite);
|
||||
self.last_pos = span.hi;
|
||||
}
|
||||
}
|
||||
|
||||
fn format_header(&self, item_name: &str, ident: ast::Ident, vis: ast::Visibility) -> String {
|
||||
format!("{}{}{}", format_visibility(vis), item_name, ident)
|
||||
}
|
||||
@ -1138,6 +1117,26 @@ impl<'a> FmtVisitor<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rewrite_static(prefix: &str,
|
||||
ident: ast::Ident,
|
||||
ty: &ast::Ty,
|
||||
mutability: ast::Mutability,
|
||||
expr: &ast::Expr,
|
||||
context: &RewriteContext)
|
||||
-> Option<String> {
|
||||
let prefix = format!("{} {}{}: ", prefix, format_mutability(mutability), ident);
|
||||
// 2 = " =".len()
|
||||
let ty_str = try_opt!(ty.rewrite(context,
|
||||
context.config.max_width - context.block_indent.width() -
|
||||
prefix.len() - 2,
|
||||
context.block_indent));
|
||||
let lhs = format!("{}{} =", prefix, ty_str);
|
||||
|
||||
// 1 = ;
|
||||
let remaining_width = context.config.max_width - context.block_indent.width() - 1;
|
||||
rewrite_assign_rhs(context, lhs, expr, remaining_width, context.block_indent).map(|s| s + ";")
|
||||
}
|
||||
|
||||
impl Rewrite for ast::FunctionRetTy {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
match *self {
|
||||
|
105
src/visitor.rs
105
src/visitor.rs
@ -20,6 +20,7 @@ use config::Config;
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use comment::rewrite_comment;
|
||||
use macros::rewrite_macro;
|
||||
use items::rewrite_static;
|
||||
|
||||
pub struct FmtVisitor<'a> {
|
||||
pub codemap: &'a CodeMap,
|
||||
@ -31,22 +32,8 @@ pub struct FmtVisitor<'a> {
|
||||
}
|
||||
|
||||
impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
// FIXME: We'd rather not format expressions here, as we have little
|
||||
// context. How are we still reaching this?
|
||||
fn visit_expr(&mut self, ex: &'v ast::Expr) {
|
||||
debug!("visit_expr: {:?} {:?}",
|
||||
self.codemap.lookup_char_pos(ex.span.lo),
|
||||
self.codemap.lookup_char_pos(ex.span.hi));
|
||||
self.format_missing(ex.span.lo);
|
||||
|
||||
let rewrite = ex.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width(),
|
||||
self.block_indent);
|
||||
|
||||
if let Some(new_str) = rewrite {
|
||||
self.buffer.push_str(&new_str);
|
||||
self.last_pos = ex.span.hi;
|
||||
}
|
||||
fn visit_expr(&mut self, _: &'v ast::Expr) {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn visit_stmt(&mut self, stmt: &'v ast::Stmt) {
|
||||
@ -58,7 +45,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
}
|
||||
}
|
||||
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
|
||||
self.format_missing_with_indent(stmt.span.lo);
|
||||
let suffix = if semicolon_for_stmt(stmt) {
|
||||
";"
|
||||
} else {
|
||||
@ -67,13 +53,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
let rewrite = ex.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width() -
|
||||
suffix.len(),
|
||||
self.block_indent);
|
||||
|
||||
if let Some(new_str) = rewrite {
|
||||
self.buffer.push_str(&new_str);
|
||||
self.buffer.push_str(suffix);
|
||||
self.last_pos = stmt.span.hi;
|
||||
}
|
||||
self.block_indent)
|
||||
.map(|s| s + suffix);
|
||||
self.push_rewrite(stmt.span, rewrite);
|
||||
}
|
||||
ast::Stmt_::StmtMac(ref _mac, _macro_style) => {
|
||||
self.format_missing_with_indent(stmt.span.lo);
|
||||
@ -104,16 +86,19 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
self.visit_stmt(&stmt)
|
||||
}
|
||||
|
||||
match b.expr {
|
||||
Some(ref e) => {
|
||||
self.format_missing_with_indent(e.span.lo);
|
||||
self.visit_expr(e);
|
||||
if let Some(ref e) = b.expr {
|
||||
self.format_missing_with_indent(e.span.lo);
|
||||
let rewrite = e.rewrite(&self.get_context(),
|
||||
self.config.max_width - self.block_indent.width(),
|
||||
self.block_indent)
|
||||
.unwrap_or_else(|| self.snippet(e.span));
|
||||
|
||||
if semicolon_for_expr(e) {
|
||||
self.buffer.push_str(";");
|
||||
}
|
||||
self.buffer.push_str(&rewrite);
|
||||
self.last_pos = e.span.hi;
|
||||
|
||||
if semicolon_for_expr(e) {
|
||||
self.buffer.push_str(";");
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
self.block_indent = self.block_indent.block_unindent(self.config);
|
||||
@ -131,7 +116,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
b: &'v ast::Block,
|
||||
s: Span,
|
||||
_: ast::NodeId) {
|
||||
|
||||
let indent = self.block_indent;
|
||||
let rewrite = match fk {
|
||||
visit::FnKind::ItemFn(ident, ref generics, unsafety, constness, abi, vis) => {
|
||||
@ -190,6 +174,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
ast::Item_::ItemUse(ref vp) => {
|
||||
self.format_import(item.vis, vp, item.span);
|
||||
}
|
||||
// FIXME(#78): format traits and impl definitions.
|
||||
ast::Item_::ItemImpl(..) |
|
||||
ast::Item_::ItemTrait(..) => {
|
||||
self.block_indent = self.block_indent.block_indent(self.config);
|
||||
@ -203,8 +188,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
self.last_pos = item.span.hi;
|
||||
}
|
||||
ast::Item_::ItemStruct(ref def, ref generics) => {
|
||||
self.format_missing_with_indent(item.span.lo);
|
||||
self.visit_struct(item.ident, item.vis, def, generics, item.span);
|
||||
let indent = self.block_indent;
|
||||
let rewrite = self.format_struct("struct ",
|
||||
item.ident,
|
||||
item.vis,
|
||||
def,
|
||||
Some(generics),
|
||||
item.span,
|
||||
indent);
|
||||
self.push_rewrite(item.span, rewrite);
|
||||
}
|
||||
ast::Item_::ItemEnum(ref def, ref generics) => {
|
||||
self.format_missing_with_indent(item.span.lo);
|
||||
@ -225,7 +217,28 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
self.format_missing_with_indent(item.span.lo);
|
||||
self.format_foreign_mod(foreign_mod, item.span);
|
||||
}
|
||||
_ => {
|
||||
ast::Item_::ItemStatic(ref ty, mutability, ref expr) => {
|
||||
let rewrite = rewrite_static("static",
|
||||
item.ident,
|
||||
ty,
|
||||
mutability,
|
||||
expr,
|
||||
&self.get_context());
|
||||
self.push_rewrite(item.span, rewrite);
|
||||
}
|
||||
ast::Item_::ItemConst(ref ty, ref expr) => {
|
||||
let rewrite = rewrite_static("const",
|
||||
item.ident,
|
||||
ty,
|
||||
ast::Mutability::MutImmutable,
|
||||
expr,
|
||||
&self.get_context());
|
||||
self.push_rewrite(item.span, rewrite);
|
||||
}
|
||||
// FIXME(#486): format type aliases.
|
||||
ast::Item_::ItemDefaultImpl(..) |
|
||||
ast::Item_::ItemFn(..) |
|
||||
ast::Item_::ItemTy(..) => {
|
||||
visit::walk_item(self, item);
|
||||
}
|
||||
}
|
||||
@ -237,17 +250,10 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
}
|
||||
|
||||
if let ast::TraitItem_::MethodTraitItem(ref sig, None) = ti.node {
|
||||
self.format_missing_with_indent(ti.span.lo);
|
||||
|
||||
let indent = self.block_indent;
|
||||
let new_fn = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
|
||||
|
||||
if let Some(fn_str) = new_fn {
|
||||
self.buffer.push_str(&fn_str);
|
||||
self.last_pos = ti.span.hi;
|
||||
}
|
||||
let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
|
||||
self.push_rewrite(ti.span, rewrite);
|
||||
}
|
||||
// TODO(#78): format trait types.
|
||||
|
||||
visit::walk_trait_item(self, ti)
|
||||
}
|
||||
@ -272,6 +278,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||
}
|
||||
|
||||
impl<'a> FmtVisitor<'a> {
|
||||
fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
|
||||
self.format_missing_with_indent(span.lo);
|
||||
|
||||
if let Some(res) = rewrite {
|
||||
self.buffer.push_str(&res);
|
||||
self.last_pos = span.hi;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_codemap(codemap: &'a CodeMap, config: &'a Config) -> FmtVisitor<'a> {
|
||||
FmtVisitor {
|
||||
codemap: codemap,
|
||||
|
10
tests/source/static.rs
Normal file
10
tests/source/static.rs
Normal file
@ -0,0 +1,10 @@
|
||||
pub const FILE_GENERIC_READ: DWORD =
|
||||
STANDARD_RIGHTS_READ | FILE_READ_DATA |
|
||||
FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;
|
||||
|
||||
pub static boolnames: &'static[&'static str] = &["bw", "am", "xsb", "xhp", "xenl", "eo",
|
||||
"gn", "hc", "km", "hs", "in", "db", "da", "mir", "msgr", "os", "eslok", "xt", "hz", "ul", "xon",
|
||||
"nxon", "mc5i", "chts", "nrrmc", "npc", "ndscr", "ccc", "bce", "hls", "xhpa", "crxm", "daisy",
|
||||
"xvpa", "sam", "cpix", "lpix", "OTbs", "OTns", "OTnc", "OTMT", "OTNL", "OTpt", "OTxr"];
|
||||
|
||||
static mut name: SomeType = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
|
13
tests/target/static.rs
Normal file
13
tests/target/static.rs
Normal file
@ -0,0 +1,13 @@
|
||||
const FILE_GENERIC_READ: DWORD = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
|
||||
FILE_READ_EA | SYNCHRONIZE;
|
||||
|
||||
static boolnames: &'static [&'static str] = &["bw", "am", "xsb", "xhp", "xenl", "eo", "gn", "hc",
|
||||
"km", "hs", "in", "db", "da", "mir", "msgr", "os",
|
||||
"eslok", "xt", "hz", "ul", "xon", "nxon", "mc5i",
|
||||
"chts", "nrrmc", "npc", "ndscr", "ccc", "bce",
|
||||
"hls", "xhpa", "crxm", "daisy", "xvpa", "sam",
|
||||
"cpix", "lpix", "OTbs", "OTns", "OTnc", "OTMT",
|
||||
"OTNL", "OTpt", "OTxr"];
|
||||
|
||||
static mut name: SomeType =
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
|
Loading…
Reference in New Issue
Block a user