Merge pull request #314 from alobb/configDocumentation

Fixed https://github.com/nrc/rustfmt/issues/291
This commit is contained in:
cassiersg 2015-09-17 23:13:25 +02:00
commit 9c5dc29416
3 changed files with 95 additions and 31 deletions

View File

@ -85,6 +85,10 @@ fn print_usage<S: Into<String>>(reason: S) {
println!("{}\n\r usage: rustfmt [-h Help] [--write-mode=[replace|overwrite|display|diff]] \ println!("{}\n\r usage: rustfmt [-h Help] [--write-mode=[replace|overwrite|display|diff]] \
<file_name>", <file_name>",
reason.into()); reason.into());
for option in Config::get_docs() {
println!("{}, {}, Possible values: {}", option.option_name(), option.doc_string(), option.variant_names());
}
} }
fn determine_params<I>(args: I) -> Option<(Vec<String>, WriteMode)> fn determine_params<I>(args: I) -> Option<(Vec<String>, WriteMode)>

View File

@ -66,7 +66,7 @@ impl MultilineStyle {
} }
macro_rules! create_config { macro_rules! create_config {
($($i:ident: $ty:ty),+ $(,)*) => ( ($($i:ident: $ty:ty, $dstring: tt),+ $(,)*) => (
#[derive(RustcDecodable, Clone)] #[derive(RustcDecodable, Clone)]
pub struct Config { pub struct Config {
$(pub $i: $ty),+ $(pub $i: $ty),+
@ -82,6 +82,44 @@ macro_rules! create_config {
$(pub $i: Option<$ty>),+ $(pub $i: Option<$ty>),+
} }
// 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;
}
impl ConfigType for bool {
fn get_variant_names() -> String {
String::from("<boolean>")
}
}
impl ConfigType for usize {
fn get_variant_names() -> String {
String::from("<unsigned integer>")
}
}
pub struct ConfigHelpItem {
option_name: &'static str,
doc_string : &'static str,
variant_names: String,
}
impl ConfigHelpItem {
pub fn option_name(&self) -> &'static str {
self.option_name
}
pub fn doc_string(&self) -> &'static str {
self.doc_string
}
pub fn variant_names(&self) -> &String {
&self.variant_names
}
}
impl Config { impl Config {
fn fill_from_parsed_config(mut self, parsed: &ParsedConfig) -> Config { fn fill_from_parsed_config(mut self, parsed: &ParsedConfig) -> Config {
@ -117,41 +155,52 @@ macro_rules! create_config {
_ => panic!("Bad config key!") _ => panic!("Bad config key!")
} }
} }
pub fn get_docs() -> Vec<ConfigHelpItem> {
let mut options: Vec<ConfigHelpItem> = Vec::new();
$(
options.push(ConfigHelpItem {
option_name: stringify!($i),
doc_string: stringify!($dstring),
variant_names: <$ty>::get_variant_names(),
});
)+
options
}
} }
) )
} }
create_config! { create_config! {
max_width: usize, max_width: usize, "Maximum width of each line",
ideal_width: usize, ideal_width: usize, "Ideal width of each line",
leeway: usize, leeway: usize, "Leeway of line width",
tab_spaces: usize, tab_spaces: usize, "Number of spaces per tab",
newline_style: NewlineStyle, newline_style: NewlineStyle, "Unix or Windows line endings",
fn_brace_style: BraceStyle, fn_brace_style: BraceStyle, "Brace style for functions",
fn_return_indent: ReturnIndent, fn_return_indent: ReturnIndent, "Location of return type in function declaration",
fn_args_paren_newline: bool, fn_args_paren_newline: bool, "If function argument parenthases goes on a newline",
fn_args_density: Density, fn_args_density: Density, "Argument density in functions",
fn_args_layout: StructLitStyle, fn_args_layout: StructLitStyle, "Layout of function arguments",
fn_arg_indent: BlockIndentStyle, fn_arg_indent: BlockIndentStyle, "Indent on function arguments",
where_density: Density, // Should we at least try to put the where clause on where_density: Density, "Density of a where clause", // Should we at least try to put the where clause on the same line as
// the same line as the rest of the function decl? // the rest of the function decl?
where_indent: BlockIndentStyle, // Visual will be treated like Tabbed where_indent: BlockIndentStyle, "Indentation of a where clause", // Visual will be treated like Tabbed
where_layout: ListTactic, where_layout: ListTactic, "Element layout inside a where clause",
where_pred_indent: BlockIndentStyle, where_pred_indent: BlockIndentStyle, "Indentation style of a where predicate",
generics_indent: BlockIndentStyle, generics_indent: BlockIndentStyle, "Indentation of generics",
struct_trailing_comma: SeparatorTactic, struct_trailing_comma: SeparatorTactic, "If there is a trailing comma on structs",
struct_lit_trailing_comma: SeparatorTactic, struct_lit_trailing_comma: SeparatorTactic, "If there is a trailing comma on literal structs",
struct_lit_style: StructLitStyle, struct_lit_style: StructLitStyle, "Style of struct definition",
struct_lit_multiline_style: MultilineStyle, struct_lit_multiline_style: MultilineStyle, "Multilline style on literal structs",
enum_trailing_comma: bool, enum_trailing_comma: bool, "Put a trailing comma on enum declarations",
report_todo: ReportTactic, report_todo: ReportTactic, "Report all occurences of TODO in source file comments",
report_fixme: ReportTactic, report_fixme: ReportTactic, "Report all occurences of FIXME in source file comments",
reorder_imports: bool, // Alphabetically, case sensitive. reorder_imports: bool, "Reorder import statements alphabetically", // Alphabetically, case sensitive.
single_line_if_else: bool, single_line_if_else: bool, "Put else on same line as closing brace for if statements",
format_strings: bool, format_strings: bool, "Format string literals, or leave as is",
chains_overflow_last: bool, chains_overflow_last: bool, "Allow last call in method chain to break the line",
take_source_hints: bool, // Retain some formatting characteristics from take_source_hints: bool, "Retain some formatting characteristics from the source code",
// the source code.
} }
impl Default for Config { impl Default for Config {

View File

@ -161,6 +161,17 @@ macro_rules! impl_enum_decodable {
} }
} }
} }
impl ::config::ConfigType for $e {
fn get_variant_names() -> String {
let mut variants = Vec::new();
$(
variants.push(stringify!($x));
)*
variants.join(", ")
}
}
}; };
} }