Reformat code to new struct lit style
This commit is contained in:
parent
7518f68861
commit
018fa85453
7 changed files with 112 additions and 84 deletions
|
@ -35,9 +35,11 @@ pub struct ChangeSet<'a> {
|
||||||
impl<'a> ChangeSet<'a> {
|
impl<'a> ChangeSet<'a> {
|
||||||
// Create a new ChangeSet for a given libsyntax CodeMap.
|
// Create a new ChangeSet for a given libsyntax CodeMap.
|
||||||
pub fn from_codemap(codemap: &'a CodeMap) -> ChangeSet<'a> {
|
pub fn from_codemap(codemap: &'a CodeMap) -> ChangeSet<'a> {
|
||||||
let mut result = ChangeSet { file_map: HashMap::new(),
|
let mut result = ChangeSet {
|
||||||
codemap: codemap,
|
file_map: HashMap::new(),
|
||||||
file_spans: Vec::with_capacity(codemap.files.borrow().len()), };
|
codemap: codemap,
|
||||||
|
file_spans: Vec::with_capacity(codemap.files.borrow().len()),
|
||||||
|
};
|
||||||
|
|
||||||
for f in codemap.files.borrow().iter() {
|
for f in codemap.files.borrow().iter() {
|
||||||
// Use the length of the file as a heuristic for how much space we
|
// Use the length of the file as a heuristic for how much space we
|
||||||
|
|
|
@ -24,13 +24,15 @@ pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, offset: usiz
|
||||||
let max_chars = width.checked_sub(closer.len()).unwrap_or(1)
|
let max_chars = width.checked_sub(closer.len()).unwrap_or(1)
|
||||||
.checked_sub(opener.len()).unwrap_or(1);
|
.checked_sub(opener.len()).unwrap_or(1);
|
||||||
|
|
||||||
let fmt = StringFormat { opener: "",
|
let fmt = StringFormat {
|
||||||
closer: "",
|
opener: "",
|
||||||
line_start: line_start,
|
closer: "",
|
||||||
line_end: "",
|
line_start: line_start,
|
||||||
width: max_chars,
|
line_end: "",
|
||||||
offset: offset + opener.len() - line_start.len(),
|
width: max_chars,
|
||||||
trim_end: true, };
|
offset: offset + opener.len() - line_start.len(),
|
||||||
|
trim_end: true,
|
||||||
|
};
|
||||||
|
|
||||||
let indent_str = make_indent(offset);
|
let indent_str = make_indent(offset);
|
||||||
let line_breaks = s.chars().filter(|&c| c == '\n').count();
|
let line_breaks = s.chars().filter(|&c| c == '\n').count();
|
||||||
|
|
64
src/expr.rs
64
src/expr.rs
|
@ -101,13 +101,15 @@ fn rewrite_string_lit(context: &RewriteContext,
|
||||||
if l_loc.line == r_loc.line && r_loc.col.to_usize() <= context.config.max_width {
|
if l_loc.line == r_loc.line && r_loc.col.to_usize() <= context.config.max_width {
|
||||||
return context.codemap.span_to_snippet(span).ok();
|
return context.codemap.span_to_snippet(span).ok();
|
||||||
}
|
}
|
||||||
let fmt = StringFormat { opener: "\"",
|
let fmt = StringFormat {
|
||||||
closer: "\"",
|
opener: "\"",
|
||||||
line_start: " ",
|
closer: "\"",
|
||||||
line_end: "\\",
|
line_start: " ",
|
||||||
width: width,
|
line_end: "\\",
|
||||||
offset: offset,
|
width: width,
|
||||||
trim_end: false, };
|
offset: offset,
|
||||||
|
trim_end: false,
|
||||||
|
};
|
||||||
|
|
||||||
Some(rewrite_string(&s.escape_default(), &fmt))
|
Some(rewrite_string(&s.escape_default(), &fmt))
|
||||||
}
|
}
|
||||||
|
@ -147,13 +149,15 @@ fn rewrite_call(context: &RewriteContext,
|
||||||
callee.span.hi + BytePos(1),
|
callee.span.hi + BytePos(1),
|
||||||
span.hi);
|
span.hi);
|
||||||
|
|
||||||
let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: ListTactic::HorizontalVertical,
|
||||||
trailing_separator: SeparatorTactic::Never,
|
separator: ",",
|
||||||
indent: offset,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
h_width: remaining_width,
|
indent: offset,
|
||||||
v_width: remaining_width,
|
h_width: remaining_width,
|
||||||
ends_with_newline: true, };
|
v_width: remaining_width,
|
||||||
|
ends_with_newline: true,
|
||||||
|
};
|
||||||
|
|
||||||
Some(format!("{}({})", callee_str, write_list(&items, &fmt)))
|
Some(format!("{}({})", callee_str, write_list(&items, &fmt)))
|
||||||
}
|
}
|
||||||
|
@ -239,17 +243,19 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
|
||||||
span_after(span, "{", context.codemap),
|
span_after(span, "{", context.codemap),
|
||||||
span.hi);
|
span.hi);
|
||||||
|
|
||||||
let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: ListTactic::HorizontalVertical,
|
||||||
trailing_separator: if base.is_some() {
|
separator: ",",
|
||||||
|
trailing_separator: if base.is_some() {
|
||||||
SeparatorTactic::Never
|
SeparatorTactic::Never
|
||||||
} else {
|
} else {
|
||||||
context.config.struct_lit_trailing_comma
|
context.config.struct_lit_trailing_comma
|
||||||
},
|
},
|
||||||
indent: indent,
|
indent: indent,
|
||||||
h_width: budget,
|
h_width: budget,
|
||||||
v_width: budget,
|
v_width: budget,
|
||||||
ends_with_newline: true, };
|
ends_with_newline: true,
|
||||||
|
};
|
||||||
let fields_str = write_list(&items, &fmt);
|
let fields_str = write_list(&items, &fmt);
|
||||||
|
|
||||||
match context.config.struct_lit_style {
|
match context.config.struct_lit_style {
|
||||||
|
@ -305,13 +311,15 @@ fn rewrite_tuple_lit(context: &RewriteContext,
|
||||||
span.lo + BytePos(1), // Remove parens
|
span.lo + BytePos(1), // Remove parens
|
||||||
span.hi - BytePos(1));
|
span.hi - BytePos(1));
|
||||||
|
|
||||||
let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: ListTactic::HorizontalVertical,
|
||||||
trailing_separator: SeparatorTactic::Never,
|
separator: ",",
|
||||||
indent: indent,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
h_width: width - 2,
|
indent: indent,
|
||||||
v_width: width - 2,
|
h_width: width - 2,
|
||||||
ends_with_newline: true, };
|
v_width: width - 2,
|
||||||
|
ends_with_newline: true,
|
||||||
|
};
|
||||||
|
|
||||||
Some(format!("({})", write_list(&items, &fmt)))
|
Some(format!("({})", write_list(&items, &fmt)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,13 +71,15 @@ impl<'a> FmtVisitor<'a> {
|
||||||
let remaining_line_budget = one_line_budget.checked_sub(used_width).unwrap_or(0);
|
let remaining_line_budget = one_line_budget.checked_sub(used_width).unwrap_or(0);
|
||||||
let remaining_multi_budget = multi_line_budget.checked_sub(used_width).unwrap_or(0);
|
let remaining_multi_budget = multi_line_budget.checked_sub(used_width).unwrap_or(0);
|
||||||
|
|
||||||
let fmt = ListFormatting { tactic: ListTactic::Mixed,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: ListTactic::Mixed,
|
||||||
trailing_separator: SeparatorTactic::Never,
|
separator: ",",
|
||||||
indent: block_indent + indent,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
h_width: remaining_line_budget,
|
indent: block_indent + indent,
|
||||||
v_width: remaining_multi_budget,
|
h_width: remaining_line_budget,
|
||||||
ends_with_newline: true, };
|
v_width: remaining_multi_budget,
|
||||||
|
ends_with_newline: true,
|
||||||
|
};
|
||||||
|
|
||||||
let mut items = itemize_list(self.codemap,
|
let mut items = itemize_list(self.codemap,
|
||||||
vec![ListItem::from_str("")], /* Dummy value, explanation
|
vec![ListItem::from_str("")], /* Dummy value, explanation
|
||||||
|
|
|
@ -96,9 +96,11 @@ pub struct BadIssueSeeker {
|
||||||
|
|
||||||
impl BadIssueSeeker {
|
impl BadIssueSeeker {
|
||||||
pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
|
pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
|
||||||
BadIssueSeeker { state: Seeking::Issue { todo_idx: 0, fixme_idx: 0 },
|
BadIssueSeeker {
|
||||||
report_todo: report_todo,
|
state: Seeking::Issue { todo_idx: 0, fixme_idx: 0 },
|
||||||
report_fixme: report_fixme, }
|
report_todo: report_todo,
|
||||||
|
report_fixme: report_fixme,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether or not the current char is conclusive evidence for an
|
// Check whether or not the current char is conclusive evidence for an
|
||||||
|
|
64
src/items.rs
64
src/items.rs
|
@ -305,13 +305,15 @@ impl<'a> FmtVisitor<'a> {
|
||||||
item.item = arg;
|
item.item = arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: ListTactic::HorizontalVertical,
|
||||||
trailing_separator: SeparatorTactic::Never,
|
separator: ",",
|
||||||
indent: arg_indent,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
h_width: one_line_budget,
|
indent: arg_indent,
|
||||||
v_width: multi_line_budget,
|
h_width: one_line_budget,
|
||||||
ends_with_newline: true, };
|
v_width: multi_line_budget,
|
||||||
|
ends_with_newline: true,
|
||||||
|
};
|
||||||
|
|
||||||
write_list(&arg_items, &fmt)
|
write_list(&arg_items, &fmt)
|
||||||
}
|
}
|
||||||
|
@ -566,13 +568,15 @@ impl<'a> FmtVisitor<'a> {
|
||||||
|
|
||||||
// 1 = ,
|
// 1 = ,
|
||||||
let budget = self.config.ideal_width - offset + self.config.tab_spaces - 1;
|
let budget = self.config.ideal_width - offset + self.config.tab_spaces - 1;
|
||||||
let fmt = ListFormatting { tactic: tactic,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: tactic,
|
||||||
trailing_separator: self.config.struct_trailing_comma,
|
separator: ",",
|
||||||
indent: offset + self.config.tab_spaces,
|
trailing_separator: self.config.struct_trailing_comma,
|
||||||
h_width: self.config.max_width,
|
indent: offset + self.config.tab_spaces,
|
||||||
v_width: budget,
|
h_width: self.config.max_width,
|
||||||
ends_with_newline: false, };
|
v_width: budget,
|
||||||
|
ends_with_newline: false,
|
||||||
|
};
|
||||||
|
|
||||||
result.push_str(&write_list(&items, &fmt));
|
result.push_str(&write_list(&items, &fmt));
|
||||||
|
|
||||||
|
@ -707,13 +711,15 @@ impl<'a> FmtVisitor<'a> {
|
||||||
item.item = ty;
|
item.item = ty;
|
||||||
}
|
}
|
||||||
|
|
||||||
let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: ListTactic::HorizontalVertical,
|
||||||
trailing_separator: SeparatorTactic::Never,
|
separator: ",",
|
||||||
indent: offset + 1,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
h_width: budget,
|
indent: offset + 1,
|
||||||
v_width: budget,
|
h_width: budget,
|
||||||
ends_with_newline: true, };
|
v_width: budget,
|
||||||
|
ends_with_newline: true,
|
||||||
|
};
|
||||||
result.push_str(&write_list(&items, &fmt));
|
result.push_str(&write_list(&items, &fmt));
|
||||||
|
|
||||||
result.push('>');
|
result.push('>');
|
||||||
|
@ -748,13 +754,15 @@ impl<'a> FmtVisitor<'a> {
|
||||||
span_end);
|
span_end);
|
||||||
|
|
||||||
let budget = self.config.ideal_width + self.config.leeway - indent - 10;
|
let budget = self.config.ideal_width + self.config.leeway - indent - 10;
|
||||||
let fmt = ListFormatting { tactic: ListTactic::Vertical,
|
let fmt = ListFormatting {
|
||||||
separator: ",",
|
tactic: ListTactic::Vertical,
|
||||||
trailing_separator: SeparatorTactic::Never,
|
separator: ",",
|
||||||
indent: indent + 10,
|
trailing_separator: SeparatorTactic::Never,
|
||||||
h_width: budget,
|
indent: indent + 10,
|
||||||
v_width: budget,
|
h_width: budget,
|
||||||
ends_with_newline: true, };
|
v_width: budget,
|
||||||
|
ends_with_newline: true,
|
||||||
|
};
|
||||||
result.push_str(&write_list(&items, &fmt));
|
result.push_str(&write_list(&items, &fmt));
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
|
@ -38,9 +38,11 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||||
self.codemap.lookup_char_pos(ex.span.hi));
|
self.codemap.lookup_char_pos(ex.span.hi));
|
||||||
self.format_missing(ex.span.lo);
|
self.format_missing(ex.span.lo);
|
||||||
let offset = self.changes.cur_offset_span(ex.span);
|
let offset = self.changes.cur_offset_span(ex.span);
|
||||||
let context = RewriteContext { codemap: self.codemap,
|
let context = RewriteContext {
|
||||||
config: self.config,
|
codemap: self.codemap,
|
||||||
block_indent: self.block_indent, };
|
config: self.config,
|
||||||
|
block_indent: self.block_indent,
|
||||||
|
};
|
||||||
let rewrite = ex.rewrite(&context, self.config.max_width - offset, offset);
|
let rewrite = ex.rewrite(&context, self.config.max_width - offset, offset);
|
||||||
|
|
||||||
if let Some(new_str) = rewrite {
|
if let Some(new_str) = rewrite {
|
||||||
|
@ -284,11 +286,13 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||||
|
|
||||||
impl<'a> FmtVisitor<'a> {
|
impl<'a> FmtVisitor<'a> {
|
||||||
pub fn from_codemap<'b>(codemap: &'b CodeMap, config: &'b Config) -> FmtVisitor<'b> {
|
pub fn from_codemap<'b>(codemap: &'b CodeMap, config: &'b Config) -> FmtVisitor<'b> {
|
||||||
FmtVisitor { codemap: codemap,
|
FmtVisitor {
|
||||||
changes: ChangeSet::from_codemap(codemap),
|
codemap: codemap,
|
||||||
last_pos: BytePos(0),
|
changes: ChangeSet::from_codemap(codemap),
|
||||||
block_indent: 0,
|
last_pos: BytePos(0),
|
||||||
config: config, }
|
block_indent: 0,
|
||||||
|
config: config,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn snippet(&self, span: Span) -> String {
|
pub fn snippet(&self, span: Span) -> String {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue