]> git.lizzy.rs Git - rust.git/blobdiff - src/config.rs
Support block indent for function calls
[rust.git] / src / config.rs
index 0fecdd49a087eac438d3a326cc57144bbdaa91e6..11816945f7131424883db2a232cb0f89909ce1e0 100644 (file)
@@ -10,6 +10,7 @@
 
 extern crate toml;
 
+use file_lines::FileLines;
 use lists::{SeparatorTactic, ListTactic};
 use std::io::Write;
 
@@ -24,6 +25,11 @@ pub enum $e {
     }
 }
 
+configuration_option_enum! { Style:
+    Rfc, // Follow the style RFCs style.
+    Default, // Follow the traditional Rustfmt style.
+}
+
 configuration_option_enum! { NewlineStyle:
     Windows, // \r\n
     Unix, // \n
@@ -38,6 +44,15 @@ pub enum $e {
     SameLineWhere,
 }
 
+configuration_option_enum! { ControlBraceStyle:
+    // K&R style, Rust community default
+    AlwaysSameLine,
+    // Stroustrup style
+    ClosingNextLine,
+    // Allman style
+    AlwaysNextLine,
+}
+
 // How to indent a function's return type.
 configuration_option_enum! { ReturnIndent:
     // Aligned with the arguments
@@ -46,34 +61,12 @@ pub enum $e {
     WithWhereClause,
 }
 
-// How to style a struct literal.
-configuration_option_enum! { StructLitStyle:
+configuration_option_enum! { IndentStyle:
     // 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.
-}
-
-// How to style fn args.
-configuration_option_enum! { FnArgLayoutStyle:
-    // First line on the same line as the opening brace, all lines aligned with
-    // the first line.
-    Visual,
-    // Put args on one line if they fit, or start a new line with block indent.
-    Block,
-    // First line is on a new line and all lines align with block indent.
-    BlockAlways,
-}
-
-configuration_option_enum! { BlockIndentStyle:
-    // Same level as parent.
-    Inherit,
-    // One level deeper than parent.
-    Tabbed,
-    // Aligned with block open.
-    Visual,
 }
 
 configuration_option_enum! { Density:
@@ -94,6 +87,7 @@ pub enum $e {
     Wide,
 }
 
+
 impl Density {
     pub fn to_list_tactic(self) -> ListTactic {
         match self {
@@ -137,46 +131,59 @@ pub fn to_list_tactic(self) -> ListTactic {
 }
 
 configuration_option_enum! { WriteMode:
-    // Backsup the original file and overwrites the orignal.
+    // Backs the original file up and overwrites the original.
     Replace,
     // Overwrites original file without backup.
     Overwrite,
-    // Write the output to stdout.
+    // Writes the output to stdout.
     Display,
-    // Write the diff to stdout.
+    // Writes the diff to stdout.
     Diff,
-    // Display how much of the input file was processed
+    // Displays how much of the input file was processed
     Coverage,
     // Unfancy stdout
     Plain,
-    // Output a checkstyle XML file.
+    // Outputs a checkstyle XML file.
     Checkstyle,
 }
 
-// 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;
+/// Trait for types that can be used in `Config`.
+pub trait ConfigType: Sized {
+    /// Returns hint text for use in `Config::print_docs()`. For enum types, this is a
+    /// pipe-separated list of variants; for other types it returns "<type>".
+    fn doc_hint() -> String;
 }
 
 impl ConfigType for bool {
-    fn get_variant_names() -> String {
+    fn doc_hint() -> String {
         String::from("<boolean>")
     }
 }
 
 impl ConfigType for usize {
-    fn get_variant_names() -> String {
+    fn doc_hint() -> String {
         String::from("<unsigned integer>")
     }
 }
 
+impl ConfigType for isize {
+    fn doc_hint() -> String {
+        String::from("<signed integer>")
+    }
+}
+
 impl ConfigType for String {
-    fn get_variant_names() -> String {
+    fn doc_hint() -> String {
         String::from("<string>")
     }
 }
 
+impl ConfigType for FileLines {
+    fn doc_hint() -> String {
+        String::from("<json>")
+    }
+}
+
 pub struct ConfigHelpItem {
     option_name: &'static str,
     doc_string: &'static str,
@@ -231,7 +238,15 @@ fn fill_from_parsed_config(mut self, parsed: ParsedConfig) -> Config {
             }
 
             pub fn from_toml(toml: &str) -> Config {
-                let parsed = toml.parse().expect("Could not parse TOML");
+                let parsed: toml::Value = toml.parse().expect("Could not parse TOML");
+                for (key, _) in parsed.as_table().expect("Parsed config was not table") {
+                    match &**key {
+                        $(
+                            stringify!($i) => (),
+                        )+
+                        _ => msg!("Warning: Unused configuration option {}", key),
+                    }
+                }
                 let parsed_config:ParsedConfig = match toml::decode(parsed) {
                     Some(decoded) => decoded,
                     None => {
@@ -278,7 +293,7 @@ pub fn print_docs() {
                     name_out.push(' ');
                     println!("{}{} Default: {:?}",
                              name_out,
-                             <$ty>::get_variant_names(),
+                             <$ty>::doc_hint(),
                              $def);
                     $(
                         println!("{}{}", space_str, $dstring);
@@ -303,65 +318,95 @@ fn default() -> Config {
 
 create_config! {
     verbose: bool, false, "Use verbose output";
+    disable_all_formatting: bool, false, "Don't reformat anything";
     skip_children: bool, false, "Don't reformat out of line modules";
+    file_lines: FileLines, FileLines::all(),
+        "Lines to format; this is not supported in rustfmt.toml, and can only be specified \
+         via the --file-lines option";
     max_width: usize, 100, "Maximum width of each line";
-    ideal_width: usize, 80, "Ideal width of each line";
+    error_on_line_overflow: bool, true, "Error if unable to get all lines within max_width";
     tab_spaces: usize, 4, "Number of spaces per tab";
     fn_call_width: usize, 60,
         "Maximum width of the args of a function call before falling back to vertical formatting";
-    struct_lit_width: usize, 16,
+    struct_lit_width: usize, 18,
         "Maximum width in the body of a struct lit before falling back to vertical formatting";
+    struct_variant_width: usize, 35,
+        "Maximum width in the body of a struct variant before falling back to vertical formatting";
+    force_explicit_abi: bool, true, "Always print the abi for extern items";
     newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
     fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
     item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
+    control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine,
+        "Brace style for control flow constructs";
     impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
+    trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
+        "How to handle trailing commas for lists";
     fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
     fn_single_line: bool, false, "Put single-expression functions on a single line";
     fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
         "Location of return type in function declaration";
     fn_args_paren_newline: bool, true, "If function argument parenthesis goes on a newline";
     fn_args_density: Density, Density::Tall, "Argument density in functions";
-    fn_args_layout: FnArgLayoutStyle, FnArgLayoutStyle::Visual, "Layout of function arguments";
-    fn_arg_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on function arguments";
+    fn_args_layout: IndentStyle, IndentStyle::Visual,
+        "Layout of function arguments and tuple structs";
+    array_layout: IndentStyle, IndentStyle::Visual, "Indent on arrays";
     type_punctuation_density: TypeDensity, TypeDensity::Wide,
         "Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
+    where_style: Style, Style::Default, "Overall strategy for where clauses";
     // 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::CompressedIfEmpty, "Density of a where clause";
     // Visual will be treated like Tabbed
-    where_indent: BlockIndentStyle, BlockIndentStyle::Tabbed, "Indentation of a where clause";
+    where_indent: IndentStyle, IndentStyle::Block, "Indentation of a where clause";
     where_layout: ListTactic, ListTactic::Vertical, "Element layout inside a where clause";
-    where_pred_indent: BlockIndentStyle, BlockIndentStyle::Visual,
+    where_pred_indent: IndentStyle, IndentStyle::Visual,
         "Indentation style of a where predicate";
-    where_trailing_comma: bool, false, "Put a trailing comma on where clauses";
-    generics_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation of generics";
-    struct_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
-        "If there is a trailing comma on structs";
-    struct_lit_trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
-        "If there is a trailing comma on literal structs";
-    struct_lit_style: StructLitStyle, StructLitStyle::Block, "Style of struct definition";
+    generics_style: Style, Style::Default, "Overall strategy for generics";
+    generics_indent: IndentStyle, IndentStyle::Visual, "Indentation of generics";
+    struct_lit_style: IndentStyle, IndentStyle::Block, "Style of struct definition";
     struct_lit_multiline_style: MultilineStyle, MultilineStyle::PreferSingle,
         "Multiline style on literal structs";
-    enum_trailing_comma: bool, true, "Put a trailing comma on enum declarations";
+    fn_call_style: IndentStyle, IndentStyle::Visual, "Indentation for function calls, etc.";
     report_todo: ReportTactic, ReportTactic::Never,
         "Report all, none or unnumbered occurrences of TODO in source file comments";
     report_fixme: ReportTactic, ReportTactic::Never,
         "Report all, none or unnumbered occurrences of FIXME in source file comments";
-    chain_base_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on chain base";
-    chain_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indentation of chain";
+    chain_indent: IndentStyle, IndentStyle::Block, "Indentation of chain";
+    chain_one_line_max: usize, 4, "Maximum number of elements in a chain to fit on a single line";
     reorder_imports: bool, false, "Reorder import statements alphabetically";
-    single_line_if_else: bool, false, "Put else on same line as closing brace for if statements";
-    format_strings: bool, true, "Format string literals where necessary";
+    reorder_imported_names: bool, false,
+        "Reorder lists of names in import statements alphabetically";
+    single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
+                                                expressions. A value of zero means always break \
+                                                if-else expressions.";
+    format_strings: bool, false, "Format string literals where necessary";
     force_format_strings: bool, false, "Always format string literals";
-    chains_overflow_last: bool, true, "Allow last call in method chain to break the line";
-    take_source_hints: bool, true, "Retain some formatting characteristics from the source code";
+    take_source_hints: bool, false, "Retain some formatting characteristics from the source code";
     hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
     wrap_comments: bool, false, "Break comments to fit on the line";
-    normalise_comments: bool, true, "Convert /* */ comments to // comments where possible";
+    comment_width: usize, 80, "Maximum length of comments. No effect unless wrap_comments = true";
+    normalize_comments: bool, false, "Convert /* */ comments to // comments where possible";
     wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
     match_block_trailing_comma: bool, false,
         "Put a trailing comma after a block based match arm (non-block arms are not affected)";
-    match_wildcard_trailing_comma: bool, true, "Put a trailing comma after a wildcard arm";
+    indent_match_arms: bool, true, "Indent match arms instead of keeping them at the same \
+                                    indentation level as the match keyword";
+    closure_block_indent_threshold: isize, 7, "How many lines a closure must have before it is \
+                                               block indented. -1 means never use block indent.";
+    space_before_type_annotation: bool, false,
+        "Leave a space before the colon in a type annotation";
+    space_after_type_annotation_colon: bool, true,
+        "Leave a space after the colon in a type annotation";
+    space_before_bound: bool, false, "Leave a space before the colon in a trait or lifetime bound";
+    space_after_bound_colon: bool, true,
+        "Leave a space after the colon in a trait or lifetime bound";
+    spaces_around_ranges: bool, false, "Put spaces around the  .. and ... range operators";
+    spaces_within_angle_brackets: bool, false, "Put spaces within non-empty generic arguments";
+    spaces_within_square_brackets: bool, false, "Put spaces within non-empty square brackets";
+    spaces_within_parens: bool, false, "Put spaces within non-empty parentheses";
+    use_try_shorthand: bool, false, "Replace uses of the try! macro by the ? shorthand";
     write_mode: WriteMode, WriteMode::Replace,
         "What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage";
+    condense_wildcard_suffices: bool, false, "Replace strings of _ wildcards by a single .. in \
+                                              tuple patterns"
 }