Refactor - remove duplicates
replacing some functions with recover_missing_comments() and rewrite_missing_comments().
This commit is contained in:
parent
66b1642624
commit
6bbc6b54de
3 changed files with 30 additions and 73 deletions
|
|
@ -153,11 +153,10 @@ pub fn combine_strs_with_missing_comments(
|
|||
let mut one_line_width =
|
||||
last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
|
||||
|
||||
let original_snippet = context.snippet(span);
|
||||
let trimmed_snippet = original_snippet.trim();
|
||||
let indent_str = shape.indent.to_string(context.config);
|
||||
let missing_comment = try_opt!(rewrite_missing_comment(span, shape, context));
|
||||
|
||||
if trimmed_snippet.is_empty() {
|
||||
if missing_comment.is_empty() {
|
||||
if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
|
||||
return Some(format!("{}{}{}", prev_str, first_sep, next_str));
|
||||
} else {
|
||||
|
|
@ -175,18 +174,13 @@ pub fn combine_strs_with_missing_comments(
|
|||
// Peek the the original source code and find out whether there is a newline between the first
|
||||
// expression and the second expression or the missing comment. We will preserve the orginal
|
||||
// layout whenever possible.
|
||||
let original_snippet = context.snippet(span);
|
||||
let prefer_same_line = if let Some(pos) = original_snippet.chars().position(|c| c == '/') {
|
||||
!original_snippet[..pos].contains('\n')
|
||||
} else {
|
||||
!original_snippet.contains('\n')
|
||||
};
|
||||
|
||||
let missing_comment = try_opt!(rewrite_comment(
|
||||
trimmed_snippet,
|
||||
false,
|
||||
shape,
|
||||
context.config
|
||||
));
|
||||
one_line_width -= first_sep.len();
|
||||
let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
|
||||
String::new()
|
||||
|
|
|
|||
26
src/expr.rs
26
src/expr.rs
|
|
@ -20,7 +20,7 @@ use {Indent, Shape, Spanned};
|
|||
use chains::rewrite_chain;
|
||||
use codemap::{LineRangeUtils, SpanUtils};
|
||||
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
|
||||
rewrite_comment, FindUncommented};
|
||||
rewrite_comment, rewrite_missing_comment, FindUncommented};
|
||||
use config::{Config, ControlBraceStyle, IndentStyle, MultilineStyle, Style};
|
||||
use items::{span_hi_for_arg, span_lo_for_arg};
|
||||
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
|
||||
|
|
@ -1195,9 +1195,13 @@ impl<'a> ControlFlow<'a> {
|
|||
mk_sp(self.block.span.lo, self.block.span.lo)
|
||||
};
|
||||
|
||||
// for event in event
|
||||
// `for event in event`
|
||||
// Do not include label in the span.
|
||||
let lo = self.label.map_or(self.span.lo, |label| label.span.hi);
|
||||
let between_kwd_cond = mk_sp(
|
||||
context.codemap.span_after(self.span, self.keyword.trim()),
|
||||
context
|
||||
.codemap
|
||||
.span_after(mk_sp(lo, self.span.hi), self.keyword.trim()),
|
||||
self.pat
|
||||
.map_or(cond_span.lo, |p| if self.matcher.is_empty() {
|
||||
p.span.lo
|
||||
|
|
@ -1378,21 +1382,13 @@ fn rewrite_label(label: Option<ast::SpannedIdent>) -> String {
|
|||
}
|
||||
|
||||
fn extract_comment(span: Span, context: &RewriteContext, shape: Shape) -> Option<String> {
|
||||
let comment_str = context.snippet(span);
|
||||
if contains_comment(&comment_str) {
|
||||
let comment = try_opt!(rewrite_comment(
|
||||
comment_str.trim(),
|
||||
false,
|
||||
shape,
|
||||
context.config,
|
||||
));
|
||||
Some(format!(
|
||||
match rewrite_missing_comment(span, shape, context) {
|
||||
Some(ref comment) if !comment.is_empty() => Some(format!(
|
||||
"\n{indent}{}\n{indent}",
|
||||
comment,
|
||||
indent = shape.indent.to_string(context.config)
|
||||
))
|
||||
} else {
|
||||
None
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
65
src/items.rs
65
src/items.rs
|
|
@ -19,7 +19,7 @@ use syntax::codemap::{BytePos, Span};
|
|||
use {Indent, Shape, Spanned};
|
||||
use codemap::{LineRangeUtils, SpanUtils};
|
||||
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
|
||||
rewrite_comment, FindUncommented};
|
||||
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};
|
||||
use config::{BraceStyle, Config, Density, IndentStyle, ReturnIndent, Style};
|
||||
use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
|
||||
rewrite_call_inner, ExprType};
|
||||
|
|
@ -2006,33 +2006,17 @@ fn rewrite_fn_base(
|
|||
// args and `{`.
|
||||
if where_clause_str.is_empty() {
|
||||
if let ast::FunctionRetTy::Default(ret_span) = fd.output {
|
||||
let sp = mk_sp(args_span.hi, ret_span.hi);
|
||||
let missing_snippet = context.snippet(sp);
|
||||
let trimmed_snippet = missing_snippet.trim();
|
||||
let missing_comment = if trimmed_snippet.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
try_opt!(rewrite_comment(
|
||||
trimmed_snippet,
|
||||
false,
|
||||
Shape::indented(indent, context.config),
|
||||
context.config,
|
||||
))
|
||||
};
|
||||
if !missing_comment.is_empty() {
|
||||
let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
|
||||
// 1 = ` `
|
||||
let total_width = missing_comment.len() + last_line_width(&result) + 1;
|
||||
let force_new_line_before_comment = missing_snippet[..pos].contains('\n') ||
|
||||
total_width > context.config.max_width();
|
||||
let sep = if force_new_line_before_comment {
|
||||
format!("\n{}", indent.to_string(context.config))
|
||||
} else {
|
||||
String::from(" ")
|
||||
};
|
||||
result.push_str(&sep);
|
||||
result.push_str(&missing_comment);
|
||||
force_new_line_for_brace = true;
|
||||
match recover_missing_comment_in_span(
|
||||
mk_sp(args_span.hi, ret_span.hi),
|
||||
shape,
|
||||
context,
|
||||
last_line_width(&result),
|
||||
) {
|
||||
Some(ref missing_comment) if !missing_comment.is_empty() => {
|
||||
result.push_str(missing_comment);
|
||||
force_new_line_for_brace = true;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2684,34 +2668,17 @@ fn missing_span_before_after_where(
|
|||
(missing_span_before, missing_span_after)
|
||||
}
|
||||
|
||||
fn rewrite_missing_comment_in_where(
|
||||
context: &RewriteContext,
|
||||
comment: &str,
|
||||
shape: Shape,
|
||||
) -> Option<String> {
|
||||
let comment = comment.trim();
|
||||
if comment.is_empty() {
|
||||
Some(String::new())
|
||||
} else {
|
||||
rewrite_comment(comment, false, shape, context.config)
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_comments_before_after_where(
|
||||
context: &RewriteContext,
|
||||
span_before_where: Span,
|
||||
span_after_where: Span,
|
||||
shape: Shape,
|
||||
) -> Option<(String, String)> {
|
||||
let before_comment = try_opt!(rewrite_missing_comment_in_where(
|
||||
context,
|
||||
&context.snippet(span_before_where),
|
||||
shape,
|
||||
));
|
||||
let after_comment = try_opt!(rewrite_missing_comment_in_where(
|
||||
context,
|
||||
&context.snippet(span_after_where),
|
||||
let before_comment = try_opt!(rewrite_missing_comment(span_before_where, shape, context));
|
||||
let after_comment = try_opt!(rewrite_missing_comment(
|
||||
span_after_where,
|
||||
shape.block_indent(context.config.tab_spaces()),
|
||||
context,
|
||||
));
|
||||
Some((before_comment, after_comment))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue