2015-05-23 22:25:36 +12: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.
|
|
|
|
|
|
|
|
extern crate toml;
|
|
|
|
|
2015-09-01 17:06:41 +12:00
|
|
|
use lists::{SeparatorTactic, ListTactic};
|
2015-09-17 20:21:06 +02:00
|
|
|
pub use issues::ReportTactic;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum NewlineStyle {
|
|
|
|
Windows, // \r\n
|
|
|
|
Unix, // \n
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_enum_decodable!(NewlineStyle, Windows, Unix);
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_enum_decodable!(BraceStyle, AlwaysNextLine, PreferSameLine, SameLineWhere);
|
|
|
|
|
|
|
|
// How to indent a function's return type.
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum ReturnIndent {
|
|
|
|
// Aligned with the arguments
|
|
|
|
WithArgs,
|
|
|
|
// Aligned with the where clause
|
|
|
|
WithWhereClause,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_enum_decodable!(ReturnIndent, WithArgs, WithWhereClause);
|
|
|
|
|
|
|
|
// How to stle a struct literal.
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum StructLitStyle {
|
|
|
|
// First line on the same line as the opening brace, all lines aligned with
|
|
|
|
// the first line.
|
|
|
|
Visual,
|
|
|
|
// First line is on a new line and all lines align with block indent.
|
|
|
|
Block,
|
|
|
|
// FIXME Maybe we should also have an option to align types.
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_enum_decodable!(StructLitStyle, Visual, Block);
|
2015-06-09 01:42:29 +02:00
|
|
|
|
2015-07-20 23:29:25 +02:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum BlockIndentStyle {
|
|
|
|
// Same level as parent.
|
|
|
|
Inherit,
|
|
|
|
// One level deeper than parent.
|
|
|
|
Tabbed,
|
|
|
|
// Aligned with block open.
|
|
|
|
Visual,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_enum_decodable!(BlockIndentStyle, Inherit, Tabbed, Visual);
|
|
|
|
|
2015-09-01 17:06:41 +12:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum Density {
|
|
|
|
// Fit as much on one line as possible.
|
|
|
|
Compressed,
|
|
|
|
// Use more lines.
|
|
|
|
Tall,
|
2015-09-28 08:46:05 +13:00
|
|
|
// Try to compress if the body is empty.
|
|
|
|
CompressedIfEmpty,
|
2015-09-01 17:06:41 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_enum_decodable!(Density, Compressed, Tall);
|
|
|
|
|
|
|
|
impl Density {
|
|
|
|
pub fn to_list_tactic(self) -> ListTactic {
|
|
|
|
match self {
|
|
|
|
Density::Compressed => ListTactic::Mixed,
|
2015-09-28 08:46:05 +13:00
|
|
|
Density::Tall | Density::CompressedIfEmpty => ListTactic::HorizontalVertical,
|
2015-09-01 17:06:41 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-05 01:31:39 +02:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum MultilineStyle {
|
|
|
|
// Use horizontal layout if it fits in one line, fall back to vertical
|
|
|
|
PreferSingle,
|
|
|
|
// Use vertical layout
|
|
|
|
ForceMulti,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl_enum_decodable!(MultilineStyle, PreferSingle, ForceMulti);
|
|
|
|
|
|
|
|
impl MultilineStyle {
|
|
|
|
pub fn to_list_tactic(self) -> ListTactic {
|
|
|
|
match self {
|
|
|
|
MultilineStyle::PreferSingle => ListTactic::HorizontalVertical,
|
|
|
|
MultilineStyle::ForceMulti => ListTactic::Vertical,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 21:41:19 +02:00
|
|
|
macro_rules! create_config {
|
2015-09-14 00:29:15 -07:00
|
|
|
($($i:ident: $ty:ty, $dstring: tt),+ $(,)*) => (
|
2015-08-19 21:41:19 +02:00
|
|
|
#[derive(RustcDecodable, Clone)]
|
|
|
|
pub struct Config {
|
|
|
|
$(pub $i: $ty),+
|
|
|
|
}
|
|
|
|
|
2015-09-04 23:01:20 +02:00
|
|
|
// Just like the Config struct but with each property wrapped
|
|
|
|
// as Option<T>. This is used to parse a rustfmt.toml that doesn't
|
|
|
|
// specity all properties of `Config`.
|
|
|
|
// We first parse into `ParsedConfig`, then create a default `Config`
|
|
|
|
// and overwrite the properties with corresponding values from `ParsedConfig`
|
|
|
|
#[derive(RustcDecodable, Clone)]
|
|
|
|
pub struct ParsedConfig {
|
|
|
|
$(pub $i: Option<$ty>),+
|
|
|
|
}
|
|
|
|
|
2015-09-14 13:43:55 -07:00
|
|
|
// This trait and the following impl blocks are there so that we an use
|
|
|
|
// UCFS inside the get_docs() function on types for configs.
|
|
|
|
pub trait ConfigType {
|
|
|
|
fn get_variant_names() -> String;
|
2015-09-14 00:29:15 -07:00
|
|
|
}
|
|
|
|
|
2015-09-14 13:43:55 -07:00
|
|
|
impl ConfigType for bool {
|
|
|
|
fn get_variant_names() -> String {
|
|
|
|
String::from("<boolean>")
|
2015-09-14 00:29:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 13:43:55 -07:00
|
|
|
impl ConfigType for usize {
|
|
|
|
fn get_variant_names() -> String {
|
|
|
|
String::from("<unsigned integer>")
|
2015-09-14 00:29:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConfigHelpItem {
|
|
|
|
option_name: &'static str,
|
|
|
|
doc_string : &'static str,
|
2015-09-14 13:43:55 -07:00
|
|
|
variant_names: String,
|
2015-09-14 00:29:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigHelpItem {
|
|
|
|
pub fn option_name(&self) -> &'static str {
|
|
|
|
self.option_name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn doc_string(&self) -> &'static str {
|
|
|
|
self.doc_string
|
|
|
|
}
|
|
|
|
|
2015-09-14 13:43:55 -07:00
|
|
|
pub fn variant_names(&self) -> &String {
|
2015-09-14 00:29:15 -07:00
|
|
|
&self.variant_names
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 21:41:19 +02:00
|
|
|
impl Config {
|
2015-09-04 23:01:20 +02:00
|
|
|
|
|
|
|
fn fill_from_parsed_config(mut self, parsed: &ParsedConfig) -> Config {
|
|
|
|
$(
|
|
|
|
if let Some(val) = parsed.$i {
|
|
|
|
self.$i = val;
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-08-19 21:41:19 +02:00
|
|
|
pub fn from_toml(toml: &str) -> Config {
|
|
|
|
let parsed = toml.parse().unwrap();
|
2015-09-04 23:01:20 +02:00
|
|
|
let parsed_config:ParsedConfig = match toml::decode(parsed) {
|
2015-08-19 21:41:19 +02:00
|
|
|
Some(decoded) => decoded,
|
|
|
|
None => {
|
|
|
|
println!("Decoding config file failed. Config:\n{}", toml);
|
|
|
|
let parsed: toml::Value = toml.parse().unwrap();
|
|
|
|
println!("\n\nParsed:\n{:?}", parsed);
|
|
|
|
panic!();
|
|
|
|
}
|
2015-09-04 23:01:20 +02:00
|
|
|
};
|
|
|
|
Config::default().fill_from_parsed_config(&parsed_config)
|
2015-08-19 21:41:19 +02:00
|
|
|
}
|
2015-05-23 22:25:36 +12:00
|
|
|
|
2015-08-19 21:41:19 +02:00
|
|
|
pub fn override_value(&mut self, key: &str, val: &str) {
|
|
|
|
match key {
|
|
|
|
$(
|
|
|
|
stringify!($i) => {
|
|
|
|
self.$i = val.parse::<$ty>().unwrap();
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
_ => panic!("Bad config key!")
|
|
|
|
}
|
2015-07-16 10:44:43 +12:00
|
|
|
}
|
2015-09-14 00:29:15 -07:00
|
|
|
|
|
|
|
pub fn get_docs() -> Vec<ConfigHelpItem> {
|
|
|
|
let mut options: Vec<ConfigHelpItem> = Vec::new();
|
|
|
|
$(
|
|
|
|
options.push(ConfigHelpItem {
|
|
|
|
option_name: stringify!($i),
|
|
|
|
doc_string: stringify!($dstring),
|
2015-09-14 13:43:55 -07:00
|
|
|
variant_names: <$ty>::get_variant_names(),
|
2015-09-14 00:29:15 -07:00
|
|
|
});
|
|
|
|
)+
|
|
|
|
options
|
|
|
|
}
|
2015-07-16 10:44:43 +12:00
|
|
|
}
|
2015-08-19 21:41:19 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
create_config! {
|
2015-09-14 00:29:15 -07:00
|
|
|
max_width: usize, "Maximum width of each line",
|
2015-09-24 11:00:14 +12:00
|
|
|
ideal_width: usize, "Ideal width of each line (only used for comments)",
|
2015-09-14 00:29:15 -07:00
|
|
|
tab_spaces: usize, "Number of spaces per tab",
|
2015-09-26 18:25:41 +12:00
|
|
|
fn_call_width: usize, "Maximum width of the args of a function call\
|
|
|
|
before faling back to vertical formatting",
|
|
|
|
struct_lit_width: usize, "Maximum width in the body of a struct lit\
|
|
|
|
before faling back to vertical formatting",
|
2015-09-14 00:29:15 -07:00
|
|
|
newline_style: NewlineStyle, "Unix or Windows line endings",
|
|
|
|
fn_brace_style: BraceStyle, "Brace style for functions",
|
|
|
|
fn_return_indent: ReturnIndent, "Location of return type in function declaration",
|
|
|
|
fn_args_paren_newline: bool, "If function argument parenthases goes on a newline",
|
|
|
|
fn_args_density: Density, "Argument density in functions",
|
|
|
|
fn_args_layout: StructLitStyle, "Layout of function arguments",
|
|
|
|
fn_arg_indent: BlockIndentStyle, "Indent on function arguments",
|
2015-09-17 20:21:06 +02:00
|
|
|
// Should we at least try to put the where clause on the same line as the rest of the
|
|
|
|
// function decl?
|
|
|
|
where_density: Density, "Density of a where clause",
|
|
|
|
// Visual will be treated like Tabbed
|
|
|
|
where_indent: BlockIndentStyle, "Indentation of a where clause",
|
2015-09-14 00:29:15 -07:00
|
|
|
where_layout: ListTactic, "Element layout inside a where clause",
|
|
|
|
where_pred_indent: BlockIndentStyle, "Indentation style of a where predicate",
|
|
|
|
generics_indent: BlockIndentStyle, "Indentation of generics",
|
|
|
|
struct_trailing_comma: SeparatorTactic, "If there is a trailing comma on structs",
|
|
|
|
struct_lit_trailing_comma: SeparatorTactic, "If there is a trailing comma on literal structs",
|
|
|
|
struct_lit_style: StructLitStyle, "Style of struct definition",
|
|
|
|
struct_lit_multiline_style: MultilineStyle, "Multilline style on literal structs",
|
|
|
|
enum_trailing_comma: bool, "Put a trailing comma on enum declarations",
|
|
|
|
report_todo: ReportTactic, "Report all occurences of TODO in source file comments",
|
|
|
|
report_fixme: ReportTactic, "Report all occurences of FIXME in source file comments",
|
2015-09-17 20:21:06 +02:00
|
|
|
// Alphabetically, case sensitive.
|
|
|
|
reorder_imports: bool, "Reorder import statements alphabetically",
|
2015-09-14 00:29:15 -07:00
|
|
|
single_line_if_else: bool, "Put else on same line as closing brace for if statements",
|
|
|
|
format_strings: bool, "Format string literals, or leave as is",
|
2015-09-15 21:15:46 -07:00
|
|
|
chains_overflow_last: bool, "Allow last call in method chain to break the line",
|
2015-09-14 00:29:15 -07:00
|
|
|
take_source_hints: bool, "Retain some formatting characteristics from the source code",
|
2015-09-05 22:39:28 -07:00
|
|
|
hard_tabs: bool, "Use tab characters for indentation, spaces for alignment",
|
2015-05-23 22:25:36 +12:00
|
|
|
}
|
2015-07-31 19:21:44 -04:00
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
max_width: 100,
|
|
|
|
ideal_width: 80,
|
|
|
|
tab_spaces: 4,
|
2015-09-26 18:25:41 +12:00
|
|
|
fn_call_width: 50,
|
|
|
|
struct_lit_width: 12,
|
2015-07-31 19:21:44 -04:00
|
|
|
newline_style: NewlineStyle::Unix,
|
|
|
|
fn_brace_style: BraceStyle::SameLineWhere,
|
|
|
|
fn_return_indent: ReturnIndent::WithArgs,
|
|
|
|
fn_args_paren_newline: true,
|
2015-09-02 08:56:37 +12:00
|
|
|
fn_args_density: Density::Tall,
|
2015-09-02 09:41:08 +12:00
|
|
|
fn_args_layout: StructLitStyle::Visual,
|
2015-09-01 17:22:00 +12:00
|
|
|
fn_arg_indent: BlockIndentStyle::Visual,
|
2015-09-28 08:46:05 +13:00
|
|
|
where_density: Density::CompressedIfEmpty,
|
2015-09-01 18:38:12 +12:00
|
|
|
where_indent: BlockIndentStyle::Tabbed,
|
2015-09-01 18:53:16 +12:00
|
|
|
where_layout: ListTactic::Vertical,
|
2015-09-01 19:04:41 +12:00
|
|
|
where_pred_indent: BlockIndentStyle::Visual,
|
2015-09-01 18:20:17 +12:00
|
|
|
generics_indent: BlockIndentStyle::Visual,
|
2015-07-31 19:21:44 -04:00
|
|
|
struct_trailing_comma: SeparatorTactic::Vertical,
|
|
|
|
struct_lit_trailing_comma: SeparatorTactic::Vertical,
|
2015-09-02 09:41:08 +12:00
|
|
|
struct_lit_style: StructLitStyle::Block,
|
2015-09-05 01:31:39 +02:00
|
|
|
struct_lit_multiline_style: MultilineStyle::PreferSingle,
|
2015-07-31 19:21:44 -04:00
|
|
|
enum_trailing_comma: true,
|
|
|
|
report_todo: ReportTactic::Always,
|
|
|
|
report_fixme: ReportTactic::Never,
|
|
|
|
reorder_imports: false,
|
|
|
|
single_line_if_else: false,
|
2015-09-01 08:14:52 -04:00
|
|
|
format_strings: true,
|
2015-09-09 23:17:31 +02:00
|
|
|
chains_overflow_last: true,
|
|
|
|
take_source_hints: true,
|
2015-09-05 22:39:28 -07:00
|
|
|
hard_tabs: false,
|
2015-07-31 19:21:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|