2015-03-07 16:46:35 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
#![feature(box_patterns)]
|
|
|
|
#![feature(rustc_private)]
|
|
|
|
#![feature(collections)]
|
|
|
|
#![feature(exit_status)]
|
2015-04-13 20:00:46 -05:00
|
|
|
#![feature(str_char)]
|
2015-03-07 16:46:35 -06:00
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
// TODO we're going to allocate a whole bunch of temp Strings, is it worth
|
|
|
|
// keeping some scratch mem for this and running our own StrPool?
|
2015-04-13 20:00:46 -05:00
|
|
|
// TODO for lint violations of names, emit a refactor script
|
|
|
|
|
|
|
|
// TODO priorities
|
2015-04-20 16:38:16 -05:00
|
|
|
// Fix fns and methods properly
|
2015-04-20 19:02:30 -05:00
|
|
|
// dead spans
|
|
|
|
//
|
2015-04-13 20:00:46 -05:00
|
|
|
// Smoke testing till we can use it
|
2015-04-20 19:02:30 -05:00
|
|
|
// end of multi-line string has wspace
|
2015-04-20 23:28:10 -05:00
|
|
|
// no newline at the end of doc.rs
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-03-07 16:46:35 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
extern crate getopts;
|
|
|
|
extern crate rustc;
|
|
|
|
extern crate rustc_driver;
|
|
|
|
extern crate syntax;
|
|
|
|
|
2015-04-13 20:12:56 -05:00
|
|
|
extern crate strings;
|
|
|
|
|
2015-03-07 16:46:35 -06:00
|
|
|
use rustc::session::Session;
|
|
|
|
use rustc::session::config::{self, Input};
|
|
|
|
use rustc_driver::{driver, CompilerCalls, Compilation};
|
|
|
|
|
2015-04-20 23:47:15 -05:00
|
|
|
use syntax::{ast, ptr};
|
|
|
|
use syntax::codemap::{CodeMap, Span, Pos, BytePos};
|
2015-03-07 16:46:35 -06:00
|
|
|
use syntax::diagnostics;
|
|
|
|
use syntax::parse::token;
|
|
|
|
use syntax::print::pprust;
|
|
|
|
use syntax::visit;
|
|
|
|
|
2015-03-09 01:17:14 -05:00
|
|
|
use std::path::PathBuf;
|
2015-03-07 16:46:35 -06:00
|
|
|
|
|
|
|
use changes::ChangeSet;
|
2015-04-20 23:47:15 -05:00
|
|
|
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
|
2015-03-07 16:46:35 -06:00
|
|
|
|
|
|
|
mod changes;
|
2015-04-20 23:47:15 -05:00
|
|
|
mod functions;
|
|
|
|
mod missed_spans;
|
|
|
|
mod lists;
|
2015-03-07 16:46:35 -06:00
|
|
|
|
|
|
|
const IDEAL_WIDTH: usize = 80;
|
2015-03-08 23:18:48 -05:00
|
|
|
const LEEWAY: usize = 5;
|
2015-03-07 16:46:35 -06:00
|
|
|
const MAX_WIDTH: usize = 100;
|
|
|
|
const MIN_STRING: usize = 10;
|
2015-04-13 20:00:46 -05:00
|
|
|
const TAB_SPACES: usize = 4;
|
2015-04-20 19:02:30 -05:00
|
|
|
const FN_BRACE_STYLE: BraceStyle = BraceStyle::SameLineWhere;
|
|
|
|
const FN_RETURN_INDENT: ReturnIndent = ReturnIndent::WithArgs;
|
|
|
|
|
2015-04-20 23:28:10 -05:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum WriteMode {
|
|
|
|
Overwrite,
|
|
|
|
// str is the extension of the new file
|
|
|
|
NewFile(&'static str),
|
|
|
|
// Write the output to stdout.
|
|
|
|
Display,
|
|
|
|
}
|
|
|
|
|
2015-04-20 19:02:30 -05:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
enum BraceStyle {
|
|
|
|
AlwaysNextLine,
|
|
|
|
PreferSameLine,
|
|
|
|
// Prefer same line except where there is a where clause, in which case force
|
|
|
|
// the brace to the next line.
|
|
|
|
SameLineWhere,
|
|
|
|
}
|
|
|
|
|
|
|
|
// How to indent a function's return type.
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
enum ReturnIndent {
|
|
|
|
// Aligned with the arguments
|
|
|
|
WithArgs,
|
|
|
|
// Aligned with the where clause
|
|
|
|
WithWhereClause,
|
|
|
|
}
|
2015-03-07 16:46:35 -06:00
|
|
|
|
|
|
|
// Formatting which depends on the AST.
|
|
|
|
fn fmt_ast<'a>(krate: &ast::Crate, codemap: &'a CodeMap) -> ChangeSet<'a> {
|
2015-03-08 23:18:48 -05:00
|
|
|
let mut visitor = FmtVisitor::from_codemap(codemap);
|
2015-03-07 16:46:35 -06:00
|
|
|
visit::walk_crate(&mut visitor, krate);
|
2015-03-08 23:18:48 -05:00
|
|
|
let files = codemap.files.borrow();
|
|
|
|
if let Some(last) = files.last() {
|
|
|
|
visitor.format_missing(last.end_pos);
|
|
|
|
}
|
2015-03-07 16:46:35 -06:00
|
|
|
|
|
|
|
visitor.changes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formatting done on a char by char basis.
|
|
|
|
fn fmt_lines(changes: &mut ChangeSet) {
|
|
|
|
// Iterate over the chars in the change set.
|
|
|
|
for (f, text) in changes.text() {
|
|
|
|
let mut trims = vec![];
|
2015-04-13 20:00:46 -05:00
|
|
|
let mut last_wspace: Option<usize> = None;
|
2015-03-07 16:46:35 -06:00
|
|
|
let mut line_len = 0;
|
|
|
|
let mut cur_line = 1;
|
|
|
|
for (c, b) in text.chars() {
|
|
|
|
if c == '\n' { // TOOD test for \r too
|
|
|
|
// Check for (and record) trailing whitespace.
|
|
|
|
if let Some(lw) = last_wspace {
|
2015-03-08 23:18:48 -05:00
|
|
|
trims.push((cur_line, lw, b));
|
2015-03-07 16:46:35 -06:00
|
|
|
line_len -= b - lw;
|
|
|
|
}
|
|
|
|
// Check for any line width errors we couldn't correct.
|
|
|
|
if line_len > MAX_WIDTH {
|
|
|
|
// FIXME store the error rather than reporting immediately.
|
|
|
|
println!("Rustfmt couldn't fix (sorry). {}:{}: line longer than {} characters",
|
|
|
|
f, cur_line, MAX_WIDTH);
|
|
|
|
}
|
|
|
|
line_len = 0;
|
|
|
|
cur_line += 1;
|
|
|
|
last_wspace = None;
|
|
|
|
} else {
|
|
|
|
line_len += 1;
|
|
|
|
if c.is_whitespace() {
|
|
|
|
if last_wspace.is_none() {
|
|
|
|
last_wspace = Some(b);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
last_wspace = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
for &(l, _, _) in trims.iter() {
|
|
|
|
// FIXME store the error rather than reporting immediately.
|
|
|
|
println!("Rustfmt left trailing whitespace at {}:{} (sorry)", f, l);
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FmtVisitor<'a> {
|
|
|
|
codemap: &'a CodeMap,
|
|
|
|
changes: ChangeSet<'a>,
|
2015-03-08 23:18:48 -05:00
|
|
|
last_pos: BytePos,
|
2015-04-13 20:00:46 -05:00
|
|
|
// TODO RAII util for indenting
|
2015-03-08 23:18:48 -05:00
|
|
|
block_indent: usize,
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
|
|
|
fn visit_expr(&mut self, ex: &'v ast::Expr) {
|
2015-04-14 04:52:12 -05:00
|
|
|
debug!("visit_expr: {:?} {:?}",
|
|
|
|
self.codemap.lookup_char_pos(ex.span.lo),
|
|
|
|
self.codemap.lookup_char_pos(ex.span.hi));
|
2015-03-08 23:18:48 -05:00
|
|
|
self.format_missing(ex.span.lo);
|
|
|
|
let offset = self.changes.cur_offset_span(ex.span);
|
|
|
|
let new_str = self.rewrite_expr(ex, MAX_WIDTH - offset, offset);
|
|
|
|
self.changes.push_str_span(ex.span, &new_str);
|
|
|
|
self.last_pos = ex.span.hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_block(&mut self, b: &'v ast::Block) {
|
2015-04-14 04:52:12 -05:00
|
|
|
debug!("visit_block: {:?} {:?}",
|
|
|
|
self.codemap.lookup_char_pos(b.span.lo),
|
|
|
|
self.codemap.lookup_char_pos(b.span.hi));
|
2015-03-08 23:18:48 -05:00
|
|
|
self.format_missing(b.span.lo);
|
|
|
|
|
|
|
|
self.changes.push_str_span(b.span, "{");
|
|
|
|
self.last_pos = self.last_pos + BytePos(1);
|
2015-04-13 20:00:46 -05:00
|
|
|
self.block_indent += TAB_SPACES;
|
2015-03-08 23:18:48 -05:00
|
|
|
|
|
|
|
for stmt in &b.stmts {
|
|
|
|
self.format_missing_with_indent(stmt.span.lo);
|
2015-04-13 20:00:46 -05:00
|
|
|
self.visit_stmt(&stmt)
|
2015-03-08 23:18:48 -05:00
|
|
|
}
|
|
|
|
match b.expr {
|
|
|
|
Some(ref e) => {
|
|
|
|
self.format_missing_with_indent(e.span.lo);
|
|
|
|
self.visit_expr(e);
|
|
|
|
}
|
|
|
|
None => {}
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
self.block_indent -= TAB_SPACES;
|
2015-03-08 23:18:48 -05:00
|
|
|
// TODO we should compress any newlines here to just one
|
2015-04-21 02:59:48 -05:00
|
|
|
// TODO somewhere here we are preserving bogus whitespace
|
2015-03-08 23:18:48 -05:00
|
|
|
self.format_missing_with_indent(b.span.hi - BytePos(1));
|
|
|
|
self.changes.push_str_span(b.span, "}");
|
|
|
|
self.last_pos = b.span.hi;
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
2015-04-20 19:02:30 -05:00
|
|
|
// Note that this only gets called for function defintions. Required methods
|
|
|
|
// on traits do not get handled here.
|
2015-03-07 16:46:35 -06:00
|
|
|
fn visit_fn(&mut self,
|
|
|
|
fk: visit::FnKind<'v>,
|
|
|
|
fd: &'v ast::FnDecl,
|
|
|
|
b: &'v ast::Block,
|
|
|
|
s: Span,
|
|
|
|
_: ast::NodeId) {
|
2015-04-13 20:00:46 -05:00
|
|
|
self.format_missing(s.lo);
|
|
|
|
self.last_pos = s.lo;
|
|
|
|
|
|
|
|
// TODO need to check against expected indent
|
|
|
|
let indent = self.codemap.lookup_char_pos(s.lo).col.0;
|
|
|
|
match fk {
|
2015-04-20 16:38:16 -05:00
|
|
|
visit::FkItemFn(ident, ref generics, ref unsafety, ref abi, vis) => {
|
2015-04-13 20:00:46 -05:00
|
|
|
let new_fn = self.rewrite_fn(indent,
|
|
|
|
ident,
|
|
|
|
fd,
|
|
|
|
None,
|
|
|
|
generics,
|
|
|
|
unsafety,
|
|
|
|
abi,
|
2015-04-20 16:38:16 -05:00
|
|
|
vis);
|
2015-04-13 20:00:46 -05:00
|
|
|
self.changes.push_str_span(s, &new_fn);
|
|
|
|
}
|
2015-04-20 16:38:16 -05:00
|
|
|
visit::FkMethod(ident, ref sig, vis) => {
|
2015-04-13 20:00:46 -05:00
|
|
|
let new_fn = self.rewrite_fn(indent,
|
|
|
|
ident,
|
|
|
|
fd,
|
|
|
|
Some(&sig.explicit_self),
|
|
|
|
&sig.generics,
|
|
|
|
&sig.unsafety,
|
|
|
|
&sig.abi,
|
2015-04-20 16:38:16 -05:00
|
|
|
vis.unwrap_or(ast::Visibility::Inherited));
|
2015-04-13 20:00:46 -05:00
|
|
|
self.changes.push_str_span(s, &new_fn);
|
|
|
|
}
|
|
|
|
visit::FkFnBlock(..) => {}
|
2015-03-08 23:18:48 -05:00
|
|
|
}
|
2015-04-13 20:00:46 -05:00
|
|
|
|
|
|
|
self.last_pos = b.span.lo;
|
|
|
|
self.visit_block(b)
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(&mut self, item: &'v ast::Item) {
|
|
|
|
match item.node {
|
|
|
|
ast::Item_::ItemUse(ref vp) => {
|
|
|
|
match vp.node {
|
|
|
|
ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
|
2015-03-08 23:18:48 -05:00
|
|
|
self.format_missing(item.span.lo);
|
2015-04-13 20:00:46 -05:00
|
|
|
let new_str = self.rewrite_use_list(path, path_list, vp.span);
|
2015-03-08 23:18:48 -05:00
|
|
|
self.changes.push_str_span(item.span, &new_str);
|
|
|
|
self.last_pos = item.span.hi;
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
ast::ViewPath_::ViewPathGlob(_) => {
|
|
|
|
// FIXME convert to list?
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-03-08 23:18:48 -05:00
|
|
|
visit::walk_item(self, item);
|
|
|
|
}
|
|
|
|
ast::Item_::ItemImpl(..) => {
|
2015-04-13 20:00:46 -05:00
|
|
|
self.block_indent += TAB_SPACES;
|
2015-03-08 23:18:48 -05:00
|
|
|
visit::walk_item(self, item);
|
2015-04-13 20:00:46 -05:00
|
|
|
self.block_indent -= TAB_SPACES;
|
2015-03-08 23:18:48 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
visit::walk_item(self, item);
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-13 20:00:46 -05:00
|
|
|
|
|
|
|
fn visit_mac(&mut self, mac: &'v ast::Mac) {
|
|
|
|
visit::walk_mac(self, mac)
|
|
|
|
}
|
2015-04-14 04:52:12 -05:00
|
|
|
|
|
|
|
fn visit_mod(&mut self, m: &'v ast::Mod, s: Span, _: ast::NodeId) {
|
|
|
|
// Only visit inline mods here.
|
|
|
|
if self.codemap.lookup_char_pos(s.lo).file.name !=
|
|
|
|
self.codemap.lookup_char_pos(m.inner.lo).file.name {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
visit::walk_mod(self, m);
|
|
|
|
}
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FmtVisitor<'a> {
|
2015-03-08 23:18:48 -05:00
|
|
|
fn from_codemap<'b>(codemap: &'b CodeMap) -> FmtVisitor<'b> {
|
|
|
|
FmtVisitor {
|
|
|
|
codemap: codemap,
|
|
|
|
changes: ChangeSet::from_codemap(codemap),
|
|
|
|
last_pos: BytePos(0),
|
|
|
|
block_indent: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn snippet(&self, span: Span) -> String {
|
|
|
|
match self.codemap.span_to_snippet(span) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => {
|
2015-04-14 04:52:12 -05:00
|
|
|
println!("Couldn't make snippet for span {:?}->{:?}",
|
|
|
|
self.codemap.lookup_char_pos(span.lo),
|
|
|
|
self.codemap.lookup_char_pos(span.hi));
|
2015-03-08 23:18:48 -05:00
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 16:46:35 -06:00
|
|
|
// TODO NEEDS TESTS
|
2015-04-13 20:00:46 -05:00
|
|
|
fn rewrite_string_lit(&mut self, s: &str, span: Span, width: usize, offset: usize) -> String {
|
2015-03-07 16:46:35 -06:00
|
|
|
// FIXME I bet this stomps unicode escapes in the source string
|
|
|
|
|
|
|
|
// Check if there is anything to fix: we always try to fixup multi-line
|
|
|
|
// strings, or if the string is too long for the line.
|
|
|
|
let l_loc = self.codemap.lookup_char_pos(span.lo);
|
|
|
|
let r_loc = self.codemap.lookup_char_pos(span.hi);
|
|
|
|
if l_loc.line == r_loc.line && r_loc.col.to_usize() <= MAX_WIDTH {
|
2015-03-08 23:18:48 -05:00
|
|
|
return self.snippet(span);
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO if lo.col > IDEAL - 10, start a new line (need cur indent for that)
|
|
|
|
|
|
|
|
let s = s.escape_default();
|
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
let offset = offset + 1;
|
|
|
|
let indent = make_indent(offset);
|
2015-03-07 16:46:35 -06:00
|
|
|
let indent = &indent;
|
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
let max_chars = width - 1;
|
2015-03-07 16:46:35 -06:00
|
|
|
|
|
|
|
let mut cur_start = 0;
|
|
|
|
let mut result = String::new();
|
|
|
|
result.push('"');
|
|
|
|
loop {
|
|
|
|
let mut cur_end = cur_start + max_chars;
|
|
|
|
|
|
|
|
if cur_end >= s.len() {
|
|
|
|
result.push_str(&s[cur_start..]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we're on a char boundary.
|
|
|
|
cur_end = next_char(&s, cur_end);
|
|
|
|
|
|
|
|
// Push cur_end left until we reach whitespace
|
|
|
|
while !s.char_at(cur_end-1).is_whitespace() {
|
|
|
|
cur_end = prev_char(&s, cur_end);
|
|
|
|
|
|
|
|
if cur_end - cur_start < MIN_STRING {
|
|
|
|
// We can't break at whitespace, fall back to splitting
|
|
|
|
// anywhere that doesn't break an escape sequence
|
|
|
|
cur_end = next_char(&s, cur_start + max_chars);
|
|
|
|
while s.char_at(cur_end) == '\\' {
|
|
|
|
cur_end = prev_char(&s, cur_end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure there is no whitespace to the right of the break.
|
|
|
|
while cur_end < s.len() && s.char_at(cur_end).is_whitespace() {
|
|
|
|
cur_end = next_char(&s, cur_end+1);
|
|
|
|
}
|
|
|
|
result.push_str(&s[cur_start..cur_end]);
|
|
|
|
result.push_str("\\\n");
|
|
|
|
result.push_str(indent);
|
|
|
|
|
|
|
|
cur_start = cur_end;
|
|
|
|
}
|
|
|
|
result.push('"');
|
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
result
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Basically just pretty prints a multi-item import.
|
2015-04-13 20:00:46 -05:00
|
|
|
fn rewrite_use_list(&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
path_list: &[ast::PathListItem],
|
|
|
|
vp_span: Span) -> String {
|
2015-03-07 16:46:35 -06:00
|
|
|
// FIXME remove unused imports
|
|
|
|
|
|
|
|
// FIXME check indentation
|
|
|
|
let l_loc = self.codemap.lookup_char_pos(vp_span.lo);
|
2015-04-13 20:00:46 -05:00
|
|
|
|
2015-03-07 16:46:35 -06:00
|
|
|
let path_str = pprust::path_to_string(&path);
|
2015-04-13 20:00:46 -05:00
|
|
|
|
|
|
|
// 3 = :: + {
|
|
|
|
let indent = l_loc.col.0 + path_str.len() + 3;
|
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: ListTactic::Mixed,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: SeparatorTactic::Never,
|
|
|
|
indent: indent,
|
|
|
|
// 2 = } + ;
|
|
|
|
h_width: IDEAL_WIDTH - (indent + path_str.len() + 2),
|
|
|
|
v_width: IDEAL_WIDTH - (indent + path_str.len() + 2),
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO handle any comments inbetween items.
|
2015-03-07 16:46:35 -06:00
|
|
|
// If `self` is in the list, put it first.
|
2015-04-13 20:00:46 -05:00
|
|
|
let head = if path_list.iter().any(|vpi|
|
2015-03-07 16:46:35 -06:00
|
|
|
if let ast::PathListItem_::PathListMod{ .. } = vpi.node {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
) {
|
2015-04-13 20:00:46 -05:00
|
|
|
Some(("self".to_string(), String::new()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2015-03-07 16:46:35 -06:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
let items: Vec<_> = head.into_iter().chain(path_list.iter().filter_map(|vpi| {
|
2015-03-07 16:46:35 -06:00
|
|
|
match vpi.node {
|
|
|
|
ast::PathListItem_::PathListIdent{ name, .. } => {
|
2015-04-13 20:00:46 -05:00
|
|
|
Some((token::get_ident(name).to_string(), String::new()))
|
|
|
|
}
|
|
|
|
// Skip `self`, because we added it above.
|
|
|
|
ast::PathListItem_::PathListMod{ .. } => None,
|
|
|
|
}
|
|
|
|
})).collect();
|
2015-03-07 16:46:35 -06:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
format!("use {}::{{{}}};", path_str, write_list(&items, &fmt))
|
|
|
|
}
|
2015-03-07 16:46:35 -06:00
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
fn rewrite_pred(&self, predicate: &ast::WherePredicate) -> String
|
|
|
|
{
|
|
|
|
// TODO dead spans
|
|
|
|
// TODO assumes we'll always fit on one line...
|
|
|
|
match predicate {
|
|
|
|
&ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
|
|
|
|
ref bounded_ty,
|
|
|
|
ref bounds,
|
|
|
|
..}) => {
|
|
|
|
if bound_lifetimes.len() > 0 {
|
|
|
|
format!("for<{}> {}: {}",
|
|
|
|
bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
|
|
|
|
pprust::ty_to_string(bounded_ty),
|
|
|
|
bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect("+"))
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
} else {
|
|
|
|
format!("{}: {}",
|
|
|
|
pprust::ty_to_string(bounded_ty),
|
|
|
|
bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect("+"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
|
|
|
|
ref bounds,
|
|
|
|
..}) => {
|
|
|
|
format!("{}: {}",
|
|
|
|
pprust::lifetime_to_string(lifetime),
|
|
|
|
bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect("+"))
|
|
|
|
}
|
|
|
|
&ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
|
|
|
|
format!("{} = {}", pprust::path_to_string(path), pprust::ty_to_string(ty))
|
|
|
|
}
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
2015-04-13 20:00:46 -05:00
|
|
|
}
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
fn rewrite_lifetime_def(&self, lifetime: &ast::LifetimeDef) -> String
|
|
|
|
{
|
|
|
|
if lifetime.bounds.len() == 0 {
|
|
|
|
return pprust::lifetime_to_string(&lifetime.lifetime);
|
|
|
|
}
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
format!("{}: {}",
|
|
|
|
pprust::lifetime_to_string(&lifetime.lifetime),
|
|
|
|
lifetime.bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect("+"))
|
|
|
|
}
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
fn rewrite_ty_bound(&self, bound: &ast::TyParamBound) -> String
|
|
|
|
{
|
|
|
|
match *bound {
|
|
|
|
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
|
|
|
|
self.rewrite_poly_trait_ref(tref)
|
|
|
|
}
|
|
|
|
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
|
|
|
|
format!("?{}", self.rewrite_poly_trait_ref(tref))
|
|
|
|
}
|
|
|
|
ast::TyParamBound::RegionTyParamBound(ref l) => {
|
|
|
|
pprust::lifetime_to_string(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
fn rewrite_ty_param(&self, ty_param: &ast::TyParam) -> String
|
|
|
|
{
|
|
|
|
let mut result = String::with_capacity(128);
|
|
|
|
result.push_str(&token::get_ident(ty_param.ident));
|
|
|
|
if ty_param.bounds.len() > 0 {
|
|
|
|
result.push_str(": ");
|
|
|
|
result.push_str(&ty_param.bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(", "));
|
|
|
|
}
|
|
|
|
if let Some(ref def) = ty_param.default {
|
|
|
|
result.push_str(" = ");
|
|
|
|
result.push_str(&pprust::ty_to_string(&def));
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
2015-03-08 23:18:48 -05:00
|
|
|
|
2015-04-13 20:00:46 -05:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_poly_trait_ref(&self, t: &ast::PolyTraitRef) -> String
|
|
|
|
{
|
|
|
|
if t.bound_lifetimes.len() > 0 {
|
|
|
|
format!("for<{}> {}",
|
|
|
|
t.bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
|
|
|
|
pprust::path_to_string(&t.trait_ref.path))
|
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
} else {
|
2015-04-13 20:00:46 -05:00
|
|
|
pprust::path_to_string(&t.trait_ref.path)
|
2015-03-08 23:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-07 16:46:35 -06:00
|
|
|
|
2015-03-08 23:18:48 -05:00
|
|
|
fn rewrite_call(&mut self,
|
|
|
|
callee: &ast::Expr,
|
|
|
|
args: &[ptr::P<ast::Expr>],
|
|
|
|
width: usize,
|
|
|
|
offset: usize)
|
|
|
|
-> String
|
|
|
|
{
|
|
|
|
debug!("rewrite_call, width: {}, offset: {}", width, offset);
|
|
|
|
|
|
|
|
// TODO using byte lens instead of char lens (and probably all over the place too)
|
|
|
|
let callee_str = self.rewrite_expr(callee, width, offset);
|
|
|
|
debug!("rewrite_call, callee_str: `{}`", callee_str);
|
|
|
|
// 2 is for parens.
|
|
|
|
let remaining_width = width - callee_str.len() - 2;
|
|
|
|
let offset = callee_str.len() + 1 + offset;
|
|
|
|
let arg_count = args.len();
|
2015-04-13 20:00:46 -05:00
|
|
|
|
|
|
|
let args_str = if arg_count > 0 {
|
|
|
|
let args: Vec<_> = args.iter().map(|e| (self.rewrite_expr(e,
|
|
|
|
remaining_width,
|
|
|
|
offset), String::new())).collect();
|
|
|
|
// TODO move this into write_list
|
|
|
|
let tactics = if args.iter().any(|&(ref s, _)| s.contains('\n')) {
|
|
|
|
ListTactic::Vertical
|
|
|
|
} else {
|
|
|
|
ListTactic::HorizontalVertical
|
|
|
|
};
|
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: tactics,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: SeparatorTactic::Never,
|
|
|
|
indent: offset,
|
|
|
|
h_width: remaining_width,
|
|
|
|
v_width: remaining_width,
|
|
|
|
};
|
|
|
|
write_list(&args, &fmt)
|
2015-03-08 23:18:48 -05:00
|
|
|
} else {
|
2015-04-13 20:00:46 -05:00
|
|
|
String::new()
|
2015-03-08 23:18:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
format!("{}({})", callee_str, args_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
|
|
|
|
match expr.node {
|
|
|
|
ast::Expr_::ExprLit(ref l) => {
|
|
|
|
match l.node {
|
|
|
|
ast::Lit_::LitStr(ref is, _) => {
|
2015-04-13 20:00:46 -05:00
|
|
|
return self.rewrite_string_lit(&is, l.span, width, offset);
|
2015-03-08 23:18:48 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::Expr_::ExprCall(ref callee, ref args) => {
|
|
|
|
return self.rewrite_call(callee, args, width, offset);
|
|
|
|
}
|
|
|
|
_ => {}
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
2015-03-08 23:18:48 -05:00
|
|
|
|
|
|
|
let result = self.snippet(expr.span);
|
|
|
|
debug!("snippet: {}", result);
|
|
|
|
result
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn prev_char(s: &str, mut i: usize) -> usize {
|
|
|
|
if i == 0 { return 0; }
|
|
|
|
|
|
|
|
i -= 1;
|
|
|
|
while !s.is_char_boundary(i) {
|
|
|
|
i -= 1;
|
|
|
|
}
|
|
|
|
i
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next_char(s: &str, mut i: usize) -> usize {
|
|
|
|
if i >= s.len() { return s.len(); }
|
|
|
|
|
|
|
|
while !s.is_char_boundary(i) {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
i
|
|
|
|
}
|
|
|
|
|
2015-04-20 23:49:16 -05:00
|
|
|
#[inline]
|
|
|
|
fn make_indent(width: usize) -> String {
|
|
|
|
let mut indent = String::with_capacity(width);
|
|
|
|
for _ in 0..width {
|
|
|
|
indent.push(' ')
|
|
|
|
}
|
|
|
|
indent
|
|
|
|
}
|
|
|
|
|
2015-03-07 16:46:35 -06:00
|
|
|
struct RustFmtCalls {
|
2015-03-09 01:17:14 -05:00
|
|
|
input_path: Option<PathBuf>,
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CompilerCalls<'a> for RustFmtCalls {
|
|
|
|
fn early_callback(&mut self,
|
|
|
|
_: &getopts::Matches,
|
|
|
|
_: &diagnostics::registry::Registry)
|
|
|
|
-> Compilation {
|
|
|
|
Compilation::Continue
|
|
|
|
}
|
|
|
|
|
2015-04-20 23:49:16 -05:00
|
|
|
fn some_input(&mut self,
|
|
|
|
input: Input,
|
|
|
|
input_path: Option<PathBuf>)
|
|
|
|
-> (Input, Option<PathBuf>) {
|
2015-03-07 16:46:35 -06:00
|
|
|
match input_path {
|
|
|
|
Some(ref ip) => self.input_path = Some(ip.clone()),
|
|
|
|
_ => {
|
|
|
|
// FIXME should handle string input and write to stdout or something
|
|
|
|
panic!("No input path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(input, input_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn no_input(&mut self,
|
|
|
|
_: &getopts::Matches,
|
|
|
|
_: &config::Options,
|
2015-03-09 01:17:14 -05:00
|
|
|
_: &Option<PathBuf>,
|
|
|
|
_: &Option<PathBuf>,
|
2015-03-07 16:46:35 -06:00
|
|
|
_: &diagnostics::registry::Registry)
|
2015-03-09 01:17:14 -05:00
|
|
|
-> Option<(Input, Option<PathBuf>)> {
|
2015-03-07 16:46:35 -06:00
|
|
|
panic!("No input supplied to RustFmt");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn late_callback(&mut self,
|
|
|
|
_: &getopts::Matches,
|
|
|
|
_: &Session,
|
|
|
|
_: &Input,
|
2015-03-09 01:17:14 -05:00
|
|
|
_: &Option<PathBuf>,
|
|
|
|
_: &Option<PathBuf>)
|
2015-03-07 16:46:35 -06:00
|
|
|
-> Compilation {
|
|
|
|
Compilation::Continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_controller(&mut self, _: &Session) -> driver::CompileController<'a> {
|
|
|
|
let mut control = driver::CompileController::basic();
|
|
|
|
control.after_parse.stop = Compilation::Stop;
|
|
|
|
control.after_parse.callback = box |state| {
|
|
|
|
let krate = state.krate.unwrap();
|
|
|
|
let codemap = state.session.codemap();
|
|
|
|
let mut changes = fmt_ast(krate, codemap);
|
|
|
|
fmt_lines(&mut changes);
|
|
|
|
|
|
|
|
// FIXME(#5) Should be user specified whether to show or replace.
|
2015-04-20 23:28:10 -05:00
|
|
|
let result = changes.write_all_files(WriteMode::Display);
|
|
|
|
|
|
|
|
if let Err(msg) = result {
|
|
|
|
println!("Error writing files: {}", msg);
|
|
|
|
}
|
2015-03-07 16:46:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
control
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2015-04-13 20:00:46 -05:00
|
|
|
let args: Vec<_> = std::env::args().collect();
|
2015-03-07 16:46:35 -06:00
|
|
|
let mut call_ctxt = RustFmtCalls { input_path: None };
|
|
|
|
rustc_driver::run_compiler(&args, &mut call_ctxt);
|
|
|
|
std::env::set_exit_status(0);
|
2015-04-13 20:00:46 -05:00
|
|
|
|
|
|
|
// TODO unit tests
|
|
|
|
// let fmt = ListFormatting {
|
|
|
|
// tactic: ListTactic::Horizontal,
|
|
|
|
// separator: ",",
|
|
|
|
// trailing_separator: SeparatorTactic::Vertical,
|
|
|
|
// indent: 2,
|
|
|
|
// h_width: 80,
|
|
|
|
// v_width: 100,
|
|
|
|
// };
|
|
|
|
// let inputs = vec![(format!("foo"), String::new()),
|
|
|
|
// (format!("foo"), String::new()),
|
|
|
|
// (format!("foo"), String::new()),
|
|
|
|
// (format!("foo"), String::new()),
|
|
|
|
// (format!("foo"), String::new()),
|
|
|
|
// (format!("foo"), String::new()),
|
|
|
|
// (format!("foo"), String::new()),
|
|
|
|
// (format!("foo"), String::new())];
|
|
|
|
// let s = write_list(&inputs, &fmt);
|
|
|
|
// println!(" {}", s);
|
2015-03-07 16:46:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME comments
|
|
|
|
// comments aren't in the AST, which makes processing them difficult, but then
|
|
|
|
// comments are complicated anyway. I think I am happy putting off tackling them
|
|
|
|
// for now. Long term the soluton is for comments to be in the AST, but that means
|
|
|
|
// only the libsyntax AST, not the rustc one, which means waiting for the ASTs
|
|
|
|
// to diverge one day....
|
|
|
|
|
|
|
|
// Once we do have comments, we just have to implement a simple word wrapping
|
|
|
|
// algorithm to keep the width under IDEAL_WIDTH. We should also convert multiline
|
|
|
|
// /* ... */ comments to // and check doc comments are in the right place and of
|
|
|
|
// the right kind.
|
2015-03-08 23:18:48 -05:00
|
|
|
|
|
|
|
// Should also make sure comments have the right indent
|