rewrite_missing_comment, CharClasses, FindUncommented,
};
use crate::config::lists::*;
-use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
+use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, Version};
use crate::lists::{
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
struct_lit_tactic, write_list, ListFormatting, Separator,
}
_ => false,
},
- ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, &expr),
+ ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr),
_ => false,
}
}
prefix: &str,
shape: Shape,
) -> Option<String> {
- if block_has_statements(&block) {
+ if block_has_statements(block) {
return None;
}
let pat_string = pat.rewrite(context, pat_shape)?;
let comments_lo = context
.snippet_provider
- .span_after(self.span, self.connector.trim());
+ .span_after(self.span.with_lo(pat.span.hi()), self.connector.trim());
let comments_span = mk_sp(comments_lo, expr.span.lo());
return rewrite_assign_rhs_with_comments(
context,
block: &ast::Block,
attrs: Option<&[ast::Attribute]>,
) -> bool {
- !block_has_statements(&block)
+ !block_has_statements(block)
&& !block_contains_comment(context, block)
&& attrs.map_or(true, |a| inner_attributes(a).is_empty())
}
) -> Option<String> {
match l.kind {
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
+ ast::LitKind::Int(..) => rewrite_int_lit(context, l, shape),
_ => wrap_str(
context.snippet(l.span).to_owned(),
context.config.max_width(),
)
}
+fn rewrite_int_lit(context: &RewriteContext<'_>, lit: &ast::Lit, shape: Shape) -> Option<String> {
+ let span = lit.span;
+ let symbol = lit.token.symbol.as_str();
+
+ if let Some(symbol_stripped) = symbol.strip_prefix("0x") {
+ let hex_lit = match context.config.hex_literal_case() {
+ HexLiteralCase::Preserve => None,
+ HexLiteralCase::Upper => Some(symbol_stripped.to_ascii_uppercase()),
+ HexLiteralCase::Lower => Some(symbol_stripped.to_ascii_lowercase()),
+ };
+ if let Some(hex_lit) = hex_lit {
+ return wrap_str(
+ format!(
+ "0x{}{}",
+ hex_lit,
+ lit.token.suffix.map_or(String::new(), |s| s.to_string())
+ ),
+ context.config.max_width(),
+ shape,
+ );
+ }
+ }
+
+ wrap_str(
+ context.snippet(span).to_owned(),
+ context.config.max_width(),
+ shape,
+ )
+}
+
fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {
if context.inside_macro() {
if span_ends_with_comma(context, span) {
let path_shape = shape.sub_width(2)?;
let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;
- let has_base = match struct_rest {
+ let has_base_or_rest = match struct_rest {
ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)),
ast::StructRest::Rest(_) if fields.is_empty() => {
return Some(format!("{} {{ .. }}", path_str));
}
- ast::StructRest::Base(_) => true,
+ ast::StructRest::Rest(_) | ast::StructRest::Base(_) => true,
_ => false,
};
let one_line_width = h_shape.map_or(0, |shape| shape.width);
let body_lo = context.snippet_provider.span_after(span, "{");
- let fields_str = if struct_lit_can_be_aligned(fields, has_base)
+ let fields_str = if struct_lit_can_be_aligned(fields, has_base_or_rest)
&& context.config.struct_field_align_threshold() > 0
{
rewrite_with_alignment(
nested_shape,
tactic,
context,
- force_no_trailing_comma
- || has_base
- || !context.use_block_indent()
- || matches!(struct_rest, ast::StructRest::Rest(_)),
+ force_no_trailing_comma || has_base_or_rest || !context.use_block_indent(),
);
write_list(&item_vec, &fmt)?
};
let fields_str =
- wrap_struct_field(context, &attrs, &fields_str, shape, v_shape, one_line_width)?;
+ wrap_struct_field(context, attrs, &fields_str, shape, v_shape, one_line_width)?;
Some(format!("{} {{{}}}", path_str, fields_str))
// FIXME if context.config.indent_style() == Visual, but we run out
shape: Shape,
rhs_tactics: RhsTactics,
) -> Option<String> {
- let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
+ let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
0
if contains_comment {
let rhs = rhs.trim_start();
- combine_strs_with_missing_comments(context, &lhs, &rhs, between_span, shape, allow_extend)
+ combine_strs_with_missing_comments(context, &lhs, rhs, between_span, shape, allow_extend)
} else {
Some(lhs + &rhs)
}
has_rhs_comment: bool,
) -> Option<String> {
match orig_rhs {
+ Some(ref new_str) if new_str.is_empty() => {
+ return Some(String::new());
+ }
Some(ref new_str)
if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
{