update combine_strs_with_missing_comments
This commit is contained in:
parent
5325b9e98c
commit
3d468e2d92
38
src/attr.rs
38
src/attr.rs
@ -11,7 +11,7 @@ use crate::config::IndentStyle;
|
||||
use crate::expr::rewrite_literal;
|
||||
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
|
||||
use crate::overflow;
|
||||
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
|
||||
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
|
||||
use crate::shape::Shape;
|
||||
use crate::source_map::SpanUtils;
|
||||
use crate::types::{rewrite_path, PathContext};
|
||||
@ -217,9 +217,9 @@ fn rewrite_initial_doc_comments(
|
||||
context: &RewriteContext<'_>,
|
||||
attrs: &[ast::Attribute],
|
||||
shape: Shape,
|
||||
) -> Option<(usize, Option<String>)> {
|
||||
) -> Result<(usize, Option<String>), RewriteError> {
|
||||
if attrs.is_empty() {
|
||||
return Some((0, None));
|
||||
return Ok((0, None));
|
||||
}
|
||||
// Rewrite doc comments
|
||||
let sugared_docs = take_while_with_pred(context, attrs, |a| a.is_doc_comment());
|
||||
@ -229,7 +229,7 @@ fn rewrite_initial_doc_comments(
|
||||
.map(|a| context.snippet(a.span))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
return Some((
|
||||
return Ok((
|
||||
sugared_docs.len(),
|
||||
Some(rewrite_doc_comment(
|
||||
&snippet,
|
||||
@ -239,7 +239,7 @@ fn rewrite_initial_doc_comments(
|
||||
));
|
||||
}
|
||||
|
||||
Some((0, None))
|
||||
Ok((0, None))
|
||||
}
|
||||
|
||||
impl Rewrite for ast::NestedMetaItem {
|
||||
@ -328,6 +328,10 @@ impl Rewrite for ast::MetaItem {
|
||||
|
||||
impl Rewrite for ast::Attribute {
|
||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
self.rewrite_result(context, shape).ok()
|
||||
}
|
||||
|
||||
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
|
||||
let snippet = context.snippet(self.span);
|
||||
if self.is_doc_comment() {
|
||||
rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
|
||||
@ -339,7 +343,7 @@ impl Rewrite for ast::Attribute {
|
||||
let prefix = attr_prefix(self);
|
||||
|
||||
if should_skip || contains_comment(snippet) {
|
||||
return Some(snippet.to_owned());
|
||||
return Ok(snippet.to_owned());
|
||||
}
|
||||
|
||||
if let Some(ref meta) = self.meta() {
|
||||
@ -364,9 +368,11 @@ impl Rewrite for ast::Attribute {
|
||||
}
|
||||
|
||||
// 1 = `[`
|
||||
let shape = shape.offset_left(prefix.len() + 1)?;
|
||||
Some(meta.rewrite(context, shape).map_or_else(
|
||||
|| snippet.to_owned(),
|
||||
let shape = shape
|
||||
.offset_left(prefix.len() + 1)
|
||||
.max_width_error(shape.width, self.span)?;
|
||||
Ok(meta.rewrite_result(context, shape).map_or_else(
|
||||
|_| snippet.to_owned(),
|
||||
|rw| match &self.kind {
|
||||
ast::AttrKind::Normal(normal_attr) => match normal_attr.item.unsafety {
|
||||
// For #![feature(unsafe_attributes)]
|
||||
@ -378,7 +384,7 @@ impl Rewrite for ast::Attribute {
|
||||
},
|
||||
))
|
||||
} else {
|
||||
Some(snippet.to_owned())
|
||||
Ok(snippet.to_owned())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -386,8 +392,12 @@ impl Rewrite for ast::Attribute {
|
||||
|
||||
impl Rewrite for [ast::Attribute] {
|
||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
self.rewrite_result(context, shape).ok()
|
||||
}
|
||||
|
||||
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
|
||||
if self.is_empty() {
|
||||
return Some(String::new());
|
||||
return Ok(String::new());
|
||||
}
|
||||
|
||||
// The current remaining attributes.
|
||||
@ -403,7 +413,7 @@ impl Rewrite for [ast::Attribute] {
|
||||
// merging derives into a single attribute.
|
||||
loop {
|
||||
if attrs.is_empty() {
|
||||
return Some(result);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// Handle doc comments.
|
||||
@ -442,7 +452,7 @@ impl Rewrite for [ast::Attribute] {
|
||||
// Handle derives if we will merge them.
|
||||
if !skip_derives && context.config.merge_derives() && is_derive(&attrs[0]) {
|
||||
let derives = take_while_with_pred(context, attrs, is_derive);
|
||||
let derive_str = format_derive(derives, shape, context)?;
|
||||
let derive_str = format_derive(derives, shape, context).unknown_error()?;
|
||||
result.push_str(&derive_str);
|
||||
|
||||
let missing_span = attrs
|
||||
@ -475,7 +485,7 @@ impl Rewrite for [ast::Attribute] {
|
||||
// If we get here, then we have a regular attribute, just handle one
|
||||
// at a time.
|
||||
|
||||
let formatted_attr = attrs[0].rewrite(context, shape)?;
|
||||
let formatted_attr = attrs[0].rewrite_result(context, shape)?;
|
||||
result.push_str(&formatted_attr);
|
||||
|
||||
let missing_span = attrs
|
||||
|
@ -267,6 +267,7 @@ impl ChainItemKind {
|
||||
}
|
||||
|
||||
impl Rewrite for ChainItem {
|
||||
// TODO impl rewrite_result after rebase
|
||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
let shape = shape.sub_width(self.tries)?;
|
||||
let rewrite = match self.kind {
|
||||
@ -294,7 +295,7 @@ impl Rewrite for ChainItem {
|
||||
),
|
||||
ChainItemKind::Await => ".await".to_owned(),
|
||||
ChainItemKind::Comment(ref comment, _) => {
|
||||
rewrite_comment(comment, false, shape, context.config)?
|
||||
rewrite_comment(comment, false, shape, context.config).ok()?
|
||||
}
|
||||
};
|
||||
Some(format!("{rewrite}{}", "?".repeat(self.tries)))
|
||||
|
@ -6,7 +6,7 @@ use itertools::{multipeek, MultiPeek};
|
||||
use rustc_span::Span;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::rewrite::RewriteContext;
|
||||
use crate::rewrite::{RewriteContext, RewriteErrorExt, RewriteResult};
|
||||
use crate::shape::{Indent, Shape};
|
||||
use crate::string::{rewrite_string, StringFormat};
|
||||
use crate::utils::{
|
||||
@ -157,7 +157,7 @@ pub(crate) fn combine_strs_with_missing_comments(
|
||||
span: Span,
|
||||
shape: Shape,
|
||||
allow_extend: bool,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
trace!(
|
||||
"combine_strs_with_missing_comments `{}` `{}` {:?} {:?}",
|
||||
prev_str, next_str, span, shape
|
||||
@ -187,7 +187,7 @@ pub(crate) fn combine_strs_with_missing_comments(
|
||||
result.push_str(&indent.to_string_with_newline(config))
|
||||
}
|
||||
result.push_str(next_str);
|
||||
return Some(result);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// We have a missing comment between the first expression and the second expression.
|
||||
@ -232,10 +232,10 @@ pub(crate) fn combine_strs_with_missing_comments(
|
||||
result.push_str(&second_sep);
|
||||
result.push_str(next_str);
|
||||
|
||||
Some(result)
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
|
||||
pub(crate) fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> RewriteResult {
|
||||
identify_comment(orig, false, shape, config, true)
|
||||
}
|
||||
|
||||
@ -244,7 +244,7 @@ pub(crate) fn rewrite_comment(
|
||||
block_style: bool,
|
||||
shape: Shape,
|
||||
config: &Config,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
identify_comment(orig, block_style, shape, config, false)
|
||||
}
|
||||
|
||||
@ -254,7 +254,7 @@ fn identify_comment(
|
||||
shape: Shape,
|
||||
config: &Config,
|
||||
is_doc_comment: bool,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
let style = comment_style(orig, false);
|
||||
|
||||
// Computes the byte length of line taking into account a newline if the line is part of a
|
||||
@ -346,7 +346,7 @@ fn identify_comment(
|
||||
let (first_group, rest) = orig.split_at(first_group_ending);
|
||||
let rewritten_first_group =
|
||||
if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
|
||||
trim_left_preserve_layout(first_group, shape.indent, config)?
|
||||
trim_left_preserve_layout(first_group, shape.indent, config).unknown_error()?
|
||||
} else if !config.normalize_comments()
|
||||
&& !config.wrap_comments()
|
||||
&& !(
|
||||
@ -367,7 +367,7 @@ fn identify_comment(
|
||||
)?
|
||||
};
|
||||
if rest.is_empty() {
|
||||
Some(rewritten_first_group)
|
||||
Ok(rewritten_first_group)
|
||||
} else {
|
||||
identify_comment(
|
||||
rest.trim_start(),
|
||||
@ -899,7 +899,7 @@ fn rewrite_comment_inner(
|
||||
shape: Shape,
|
||||
config: &Config,
|
||||
is_doc_comment: bool,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);
|
||||
|
||||
let line_breaks = count_newlines(orig.trim_end());
|
||||
@ -933,7 +933,7 @@ fn rewrite_comment_inner(
|
||||
}
|
||||
}
|
||||
|
||||
Some(rewriter.finish())
|
||||
Ok(rewriter.finish())
|
||||
}
|
||||
|
||||
const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
|
||||
@ -998,7 +998,7 @@ pub(crate) fn rewrite_missing_comment(
|
||||
span: Span,
|
||||
shape: Shape,
|
||||
context: &RewriteContext<'_>,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
let missing_snippet = context.snippet(span);
|
||||
let trimmed_snippet = missing_snippet.trim();
|
||||
// check the span starts with a comment
|
||||
@ -1006,7 +1006,7 @@ pub(crate) fn rewrite_missing_comment(
|
||||
if !trimmed_snippet.is_empty() && pos.is_some() {
|
||||
rewrite_comment(trimmed_snippet, false, shape, context.config)
|
||||
} else {
|
||||
Some(String::new())
|
||||
Ok(String::new())
|
||||
}
|
||||
}
|
||||
|
||||
@ -1018,13 +1018,13 @@ pub(crate) fn recover_missing_comment_in_span(
|
||||
shape: Shape,
|
||||
context: &RewriteContext<'_>,
|
||||
used_width: usize,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
let missing_comment = rewrite_missing_comment(span, shape, context)?;
|
||||
if missing_comment.is_empty() {
|
||||
Some(String::new())
|
||||
Ok(String::new())
|
||||
} else {
|
||||
let missing_snippet = context.snippet(span);
|
||||
let pos = missing_snippet.find('/')?;
|
||||
let pos = missing_snippet.find('/').unknown_error()?;
|
||||
// 1 = ` `
|
||||
let total_width = missing_comment.len() + used_width + 1;
|
||||
let force_new_line_before_comment =
|
||||
@ -1034,7 +1034,7 @@ pub(crate) fn recover_missing_comment_in_span(
|
||||
} else {
|
||||
Cow::from(" ")
|
||||
};
|
||||
Some(format!("{sep}{missing_comment}"))
|
||||
Ok(format!("{sep}{missing_comment}"))
|
||||
}
|
||||
}
|
||||
|
||||
|
57
src/expr.rs
57
src/expr.rs
@ -136,7 +136,9 @@ pub(crate) fn format_expr(
|
||||
ast::ExprKind::Tup(ref items) => {
|
||||
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1).ok()
|
||||
}
|
||||
ast::ExprKind::Let(ref pat, ref expr, _span, _) => rewrite_let(context, shape, pat, expr),
|
||||
ast::ExprKind::Let(ref pat, ref expr, _span, _) => {
|
||||
rewrite_let(context, shape, pat, expr).ok()
|
||||
}
|
||||
ast::ExprKind::If(..)
|
||||
| ast::ExprKind::ForLoop { .. }
|
||||
| ast::ExprKind::Loop(..)
|
||||
@ -166,7 +168,7 @@ pub(crate) fn format_expr(
|
||||
// Rewrite block without trying to put it in a single line.
|
||||
rw
|
||||
} else {
|
||||
let prefix = block_prefix(context, block, shape)?;
|
||||
let prefix = block_prefix(context, block, shape).ok()?;
|
||||
|
||||
rewrite_block_with_visitor(
|
||||
context,
|
||||
@ -437,6 +439,7 @@ pub(crate) fn format_expr(
|
||||
expr.span.lo(),
|
||||
);
|
||||
combine_strs_with_missing_comments(context, &attrs_str, &expr_str, span, shape, false)
|
||||
.ok()
|
||||
})
|
||||
}
|
||||
|
||||
@ -498,17 +501,20 @@ fn rewrite_empty_block(
|
||||
None
|
||||
}
|
||||
|
||||
fn block_prefix(context: &RewriteContext<'_>, block: &ast::Block, shape: Shape) -> Option<String> {
|
||||
Some(match block.rules {
|
||||
fn block_prefix(context: &RewriteContext<'_>, block: &ast::Block, shape: Shape) -> RewriteResult {
|
||||
Ok(match block.rules {
|
||||
ast::BlockCheckMode::Unsafe(..) => {
|
||||
let snippet = context.snippet(block.span);
|
||||
let open_pos = snippet.find_uncommented("{")?;
|
||||
let open_pos = snippet.find_uncommented("{").unknown_error()?;
|
||||
// Extract comment between unsafe and block start.
|
||||
let trimmed = &snippet[6..open_pos].trim();
|
||||
|
||||
if !trimmed.is_empty() {
|
||||
// 9 = "unsafe {".len(), 7 = "unsafe ".len()
|
||||
let budget = shape.width.checked_sub(9)?;
|
||||
let budget = shape
|
||||
.width
|
||||
.checked_sub(9)
|
||||
.max_width_error(shape.width, block.span)?;
|
||||
format!(
|
||||
"unsafe {} ",
|
||||
rewrite_comment(
|
||||
@ -612,7 +618,7 @@ fn rewrite_block_inner(
|
||||
context: &RewriteContext<'_>,
|
||||
shape: Shape,
|
||||
) -> RewriteResult {
|
||||
let prefix = block_prefix(context, block, shape).unknown_error()?;
|
||||
let prefix = block_prefix(context, block, shape)?;
|
||||
|
||||
// shape.width is used only for the single line case: either the empty block `{}`,
|
||||
// or an unsafe expression `unsafe { e }`.
|
||||
@ -912,7 +918,8 @@ impl<'a> ControlFlow<'a> {
|
||||
RhsTactics::Default,
|
||||
comments_span,
|
||||
true,
|
||||
);
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
|
||||
let expr_rw = expr.rewrite(context, cond_shape);
|
||||
@ -1191,7 +1198,7 @@ fn rewrite_label(opt_label: Option<ast::Label>) -> Cow<'static, str> {
|
||||
|
||||
fn extract_comment(span: Span, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
match rewrite_missing_comment(span, shape, context) {
|
||||
Some(ref comment) if !comment.is_empty() => Some(format!(
|
||||
Ok(ref comment) if !comment.is_empty() => Some(format!(
|
||||
"{indent}{comment}{indent}",
|
||||
indent = shape.indent.to_string_with_newline(context.config)
|
||||
)),
|
||||
@ -1300,7 +1307,7 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
|
||||
&StringFormat::new(shape.visual_indent(0), context.config),
|
||||
shape.width.saturating_sub(2),
|
||||
)
|
||||
.max_width_error(shape.width, span) // - 2 ?
|
||||
.max_width_error(shape.width, span)
|
||||
}
|
||||
|
||||
fn rewrite_int_lit(
|
||||
@ -1490,8 +1497,8 @@ pub(crate) fn rewrite_paren(
|
||||
// 1 = "(" or ")"
|
||||
pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span().lo());
|
||||
post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
|
||||
pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
|
||||
post_comment = rewrite_missing_comment(post_span, shape, context)?;
|
||||
pre_comment = rewrite_missing_comment(pre_span, shape, context).ok()?;
|
||||
post_comment = rewrite_missing_comment(post_span, shape, context).ok()?;
|
||||
|
||||
// Remove nested parens if there are no comments.
|
||||
if let ast::ExprKind::Paren(ref subsubexpr) = subexpr.kind {
|
||||
@ -1525,8 +1532,8 @@ fn rewrite_paren_in_multi_line(
|
||||
) -> Option<String> {
|
||||
let nested_indent = shape.indent.block_indent(context.config);
|
||||
let nested_shape = Shape::indented(nested_indent, context.config);
|
||||
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context)?;
|
||||
let post_comment = rewrite_missing_comment(post_span, nested_shape, context)?;
|
||||
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context).ok()?;
|
||||
let post_comment = rewrite_missing_comment(post_span, nested_shape, context).ok()?;
|
||||
let subexpr_str = subexpr.rewrite(context, nested_shape)?;
|
||||
|
||||
let mut result = String::with_capacity(subexpr_str.len() * 2);
|
||||
@ -1894,14 +1901,16 @@ fn rewrite_let(
|
||||
shape: Shape,
|
||||
pat: &ast::Pat,
|
||||
expr: &ast::Expr,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
let mut result = "let ".to_owned();
|
||||
|
||||
// TODO(ytmimi) comments could appear between `let` and the `pat`
|
||||
|
||||
// 4 = "let ".len()
|
||||
let pat_shape = shape.offset_left(4)?;
|
||||
let pat_str = pat.rewrite(context, pat_shape)?;
|
||||
let pat_shape = shape
|
||||
.offset_left(4)
|
||||
.max_width_error(shape.width, pat.span)?;
|
||||
let pat_str = pat.rewrite_result(context, pat_shape)?;
|
||||
result.push_str(&pat_str);
|
||||
|
||||
// TODO(ytmimi) comments could appear between `pat` and `=`
|
||||
@ -2121,7 +2130,7 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
|
||||
Some(lhs + &rhs)
|
||||
}
|
||||
|
||||
pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
|
||||
pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite + Spanned>(
|
||||
context: &RewriteContext<'_>,
|
||||
lhs: S,
|
||||
ex: &R,
|
||||
@ -2130,21 +2139,23 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
|
||||
rhs_tactics: RhsTactics,
|
||||
between_span: Span,
|
||||
allow_extend: bool,
|
||||
) -> Option<String> {
|
||||
) -> RewriteResult {
|
||||
let lhs = lhs.into();
|
||||
let contains_comment = contains_comment(context.snippet(between_span));
|
||||
let shape = if contains_comment {
|
||||
shape.block_left(context.config.tab_spaces())?
|
||||
shape
|
||||
.block_left(context.config.tab_spaces())
|
||||
.max_width_error(shape.width, between_span.with_hi(ex.span().hi()))?
|
||||
} else {
|
||||
shape
|
||||
};
|
||||
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
|
||||
|
||||
let rhs =
|
||||
rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics).unknown_error()?;
|
||||
if contains_comment {
|
||||
let rhs = rhs.trim_start();
|
||||
combine_strs_with_missing_comments(context, &lhs, rhs, between_span, shape, allow_extend)
|
||||
} else {
|
||||
Some(lhs + &rhs)
|
||||
Ok(lhs + &rhs)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -367,6 +367,7 @@ impl UseTree {
|
||||
shape,
|
||||
allow_extend,
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
_ => Some(use_str),
|
||||
}
|
||||
|
56
src/items.rs
56
src/items.rs
@ -77,8 +77,7 @@ impl Rewrite for ast::Local {
|
||||
),
|
||||
shape,
|
||||
false,
|
||||
)
|
||||
.unknown_error()?
|
||||
)?
|
||||
};
|
||||
let let_kw_offset = result.len() - "let ".len();
|
||||
|
||||
@ -718,6 +717,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
};
|
||||
|
||||
combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
|
||||
.ok()
|
||||
}
|
||||
|
||||
fn visit_impl_items(&mut self, items: &[ptr::P<ast::AssocItem>]) {
|
||||
@ -853,7 +853,7 @@ pub(crate) fn format_impl(
|
||||
context,
|
||||
last_line_width(&result),
|
||||
) {
|
||||
Some(ref missing_comment) if !missing_comment.is_empty() => {
|
||||
Ok(ref missing_comment) if !missing_comment.is_empty() => {
|
||||
result.push_str(missing_comment);
|
||||
}
|
||||
_ => (),
|
||||
@ -1260,7 +1260,7 @@ pub(crate) fn format_trait(
|
||||
context,
|
||||
last_line_width(&result),
|
||||
) {
|
||||
Some(ref missing_comment) if !missing_comment.is_empty() => {
|
||||
Ok(ref missing_comment) if !missing_comment.is_empty() => {
|
||||
result.push_str(missing_comment);
|
||||
}
|
||||
_ => (),
|
||||
@ -1550,8 +1550,8 @@ fn format_empty_struct_or_tuple(
|
||||
// indented shape for proper indenting of multi-line comments
|
||||
let shape = Shape::indented(offset.block_indent(context.config), context.config);
|
||||
match rewrite_missing_comment(span, shape, context) {
|
||||
Some(ref s) if s.is_empty() => (),
|
||||
Some(ref s) => {
|
||||
Ok(ref s) if s.is_empty() => (),
|
||||
Ok(ref s) => {
|
||||
let is_multi_line = !is_single_line(s);
|
||||
if is_multi_line || first_line_contains_single_line_comment(s) {
|
||||
let nested_indent_str = offset
|
||||
@ -1564,7 +1564,7 @@ fn format_empty_struct_or_tuple(
|
||||
result.push_str(&offset.to_string_with_newline(context.config));
|
||||
}
|
||||
}
|
||||
None => result.push_str(context.snippet(span)),
|
||||
Err(_) => result.push_str(context.snippet(span)),
|
||||
}
|
||||
result.push_str(closer);
|
||||
}
|
||||
@ -1827,7 +1827,8 @@ fn rewrite_ty<R: Rewrite>(
|
||||
comment_span,
|
||||
comment_shape,
|
||||
true,
|
||||
)?
|
||||
)
|
||||
.ok()?
|
||||
}
|
||||
_ => format!("{result}="),
|
||||
};
|
||||
@ -1907,8 +1908,7 @@ pub(crate) fn rewrite_struct_field(
|
||||
missing_span,
|
||||
shape,
|
||||
attrs_extendable,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
let overhead = trimmed_last_line_width(&attr_prefix);
|
||||
let lhs_offset = lhs_max_width.saturating_sub(overhead);
|
||||
for _ in 0..lhs_offset {
|
||||
@ -1940,7 +1940,6 @@ pub(crate) fn rewrite_struct_field(
|
||||
&field_str
|
||||
};
|
||||
combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false)
|
||||
.unknown_error()
|
||||
}
|
||||
|
||||
pub(crate) struct StaticParts<'a> {
|
||||
@ -2074,6 +2073,7 @@ fn rewrite_static(
|
||||
comments_span,
|
||||
true,
|
||||
)
|
||||
.ok()
|
||||
.and_then(|res| recover_comment_removed(res, static_parts.span, context))
|
||||
.map(|s| if s.ends_with(';') { s } else { s + ";" })
|
||||
} else {
|
||||
@ -2170,9 +2170,11 @@ fn get_missing_param_comments(
|
||||
};
|
||||
|
||||
let comment_before_colon = rewrite_missing_comment(span_before_colon, shape, context)
|
||||
.ok()
|
||||
.filter(|comment| !comment.is_empty())
|
||||
.map_or(String::new(), |comment| format!(" {}", comment));
|
||||
let comment_after_colon = rewrite_missing_comment(span_after_colon, shape, context)
|
||||
.ok()
|
||||
.filter(|comment| !comment.is_empty())
|
||||
.map_or(String::new(), |comment| format!("{} ", comment));
|
||||
(comment_before_colon, comment_after_colon)
|
||||
@ -2220,8 +2222,7 @@ impl Rewrite for ast::Param {
|
||||
span,
|
||||
shape,
|
||||
!has_multiple_attr_lines && !has_doc_comments,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
|
||||
if !is_empty_infer(&*self.ty, self.pat.span) {
|
||||
let (before_comment, after_comment) =
|
||||
@ -2253,8 +2254,7 @@ impl Rewrite for ast::Param {
|
||||
span,
|
||||
shape,
|
||||
!has_multiple_attr_lines,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
result.push_str(&before_comment);
|
||||
result.push_str(colon_spaces(context.config));
|
||||
result.push_str(&after_comment);
|
||||
@ -2301,8 +2301,7 @@ fn rewrite_explicit_self(
|
||||
span,
|
||||
shape,
|
||||
!has_multiple_attr_lines,
|
||||
)
|
||||
.unknown_error()?)
|
||||
)?)
|
||||
}
|
||||
None => Ok(combine_strs_with_missing_comments(
|
||||
context,
|
||||
@ -2311,8 +2310,7 @@ fn rewrite_explicit_self(
|
||||
span,
|
||||
shape,
|
||||
!has_multiple_attr_lines,
|
||||
)
|
||||
.unknown_error()?),
|
||||
)?),
|
||||
}
|
||||
}
|
||||
ast::SelfKind::Explicit(ref ty, mutability) => {
|
||||
@ -2328,8 +2326,7 @@ fn rewrite_explicit_self(
|
||||
span,
|
||||
shape,
|
||||
!has_multiple_attr_lines,
|
||||
)
|
||||
.unknown_error()?)
|
||||
)?)
|
||||
}
|
||||
ast::SelfKind::Value(mutability) => Ok(combine_strs_with_missing_comments(
|
||||
context,
|
||||
@ -2338,8 +2335,7 @@ fn rewrite_explicit_self(
|
||||
span,
|
||||
shape,
|
||||
!has_multiple_attr_lines,
|
||||
)
|
||||
.unknown_error()?),
|
||||
)?),
|
||||
}
|
||||
}
|
||||
|
||||
@ -2677,7 +2673,7 @@ fn rewrite_fn_base(
|
||||
context,
|
||||
last_line_width(&result),
|
||||
) {
|
||||
Some(ref missing_comment) if !missing_comment.is_empty() => {
|
||||
Ok(ref missing_comment) if !missing_comment.is_empty() => {
|
||||
result.push_str(missing_comment);
|
||||
force_new_line_for_brace = true;
|
||||
}
|
||||
@ -3201,12 +3197,13 @@ fn rewrite_comments_before_after_where(
|
||||
span_after_where: Span,
|
||||
shape: Shape,
|
||||
) -> Option<(String, String)> {
|
||||
let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
|
||||
let before_comment = rewrite_missing_comment(span_before_where, shape, context).ok()?;
|
||||
let after_comment = rewrite_missing_comment(
|
||||
span_after_where,
|
||||
shape.block_indent(context.config.tab_spaces()),
|
||||
context,
|
||||
)?;
|
||||
)
|
||||
.ok()?;
|
||||
Some((before_comment, after_comment))
|
||||
}
|
||||
|
||||
@ -3229,7 +3226,7 @@ fn format_header(
|
||||
.opt_span_before(mk_sp(vis.span.lo(), ident.span.hi()), item_name.trim())
|
||||
{
|
||||
let missing_span = mk_sp(after_vis, before_item_name);
|
||||
if let Some(result_with_comment) = combine_strs_with_missing_comments(
|
||||
if let Ok(result_with_comment) = combine_strs_with_missing_comments(
|
||||
context,
|
||||
&result,
|
||||
item_name,
|
||||
@ -3313,7 +3310,8 @@ fn format_generics(
|
||||
),
|
||||
shape,
|
||||
context,
|
||||
),
|
||||
)
|
||||
.ok(),
|
||||
)
|
||||
};
|
||||
// add missing comments
|
||||
@ -3446,6 +3444,7 @@ impl Rewrite for ast::ForeignItem {
|
||||
shape,
|
||||
false,
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
@ -3481,6 +3480,7 @@ fn rewrite_attrs(
|
||||
shape,
|
||||
allow_extend,
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Rewrite an inline mod.
|
||||
|
15
src/lists.rs
15
src/lists.rs
@ -8,7 +8,7 @@ use rustc_span::BytePos;
|
||||
use crate::comment::{find_comment_end, rewrite_comment, FindUncommented};
|
||||
use crate::config::lists::*;
|
||||
use crate::config::{Config, IndentStyle};
|
||||
use crate::rewrite::{RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
|
||||
use crate::rewrite::{RewriteContext, RewriteError, RewriteResult};
|
||||
use crate::shape::{Indent, Shape};
|
||||
use crate::utils::{
|
||||
count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline,
|
||||
@ -366,8 +366,8 @@ where
|
||||
// Block style in non-vertical mode.
|
||||
let block_mode = tactic == DefinitiveListTactic::Horizontal;
|
||||
// Width restriction is only relevant in vertical mode.
|
||||
let comment = rewrite_comment(comment, block_mode, formatting.shape, formatting.config)
|
||||
.unknown_error()?;
|
||||
let comment =
|
||||
rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?;
|
||||
result.push_str(&comment);
|
||||
|
||||
if !inner_item.is_empty() {
|
||||
@ -413,8 +413,7 @@ where
|
||||
true,
|
||||
Shape::legacy(formatting.shape.width, Indent::empty()),
|
||||
formatting.config,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
|
||||
result.push(' ');
|
||||
result.push_str(&formatted_comment);
|
||||
@ -465,8 +464,7 @@ where
|
||||
)
|
||||
};
|
||||
|
||||
let mut formatted_comment =
|
||||
rewrite_post_comment(&mut item_max_width).unknown_error()?;
|
||||
let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
|
||||
|
||||
if !starts_with_newline(comment) {
|
||||
if formatting.align_comments {
|
||||
@ -479,8 +477,7 @@ where
|
||||
> formatting.config.max_width()
|
||||
{
|
||||
item_max_width = None;
|
||||
formatted_comment =
|
||||
rewrite_post_comment(&mut item_max_width).unknown_error()?;
|
||||
formatted_comment = rewrite_post_comment(&mut item_max_width)?;
|
||||
comment_alignment =
|
||||
post_comment_alignment(item_max_width, unicode_str_width(inner_item));
|
||||
}
|
||||
|
@ -310,8 +310,7 @@ fn rewrite_match_arm(
|
||||
missing_span,
|
||||
shape,
|
||||
false,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
|
||||
let arrow_span = mk_sp(
|
||||
arm.pat.span.hi(),
|
||||
@ -448,7 +447,7 @@ fn rewrite_match_body(
|
||||
if comment_str.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
rewrite_comment(comment_str, false, shape, context.config).unknown_error()?
|
||||
rewrite_comment(comment_str, false, shape, context.config)?
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -284,13 +284,13 @@ impl<'a> FmtVisitor<'a> {
|
||||
let other_lines = &subslice[offset + 1..];
|
||||
let comment_str =
|
||||
rewrite_comment(other_lines, false, comment_shape, self.config)
|
||||
.unwrap_or_else(|| String::from(other_lines));
|
||||
.unwrap_or_else(|_| String::from(other_lines));
|
||||
self.push_str(&comment_str);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
|
||||
.unwrap_or_else(|| String::from(subslice));
|
||||
.unwrap_or_else(|_| String::from(subslice));
|
||||
self.push_str(&comment_str);
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,8 @@ impl Rewrite for Pat {
|
||||
mk_sp(lo, p.span.lo()),
|
||||
shape,
|
||||
true,
|
||||
)?
|
||||
)
|
||||
.ok()?
|
||||
}
|
||||
None => "".to_owned(),
|
||||
};
|
||||
@ -152,7 +153,8 @@ impl Rewrite for Pat {
|
||||
mk_sp(lo, hi),
|
||||
shape,
|
||||
true,
|
||||
)?,
|
||||
)
|
||||
.ok()?,
|
||||
)
|
||||
}
|
||||
(false, true) => (
|
||||
@ -181,7 +183,8 @@ impl Rewrite for Pat {
|
||||
mk_sp(lo, hi),
|
||||
shape,
|
||||
true,
|
||||
)?,
|
||||
)
|
||||
.ok()?,
|
||||
)
|
||||
}
|
||||
(false, true) => (first_lo, first),
|
||||
@ -198,7 +201,8 @@ impl Rewrite for Pat {
|
||||
mk_sp(ident.span.hi(), hi),
|
||||
shape,
|
||||
true,
|
||||
)?
|
||||
)
|
||||
.ok()?
|
||||
} else {
|
||||
id_str.to_owned()
|
||||
};
|
||||
@ -211,6 +215,7 @@ impl Rewrite for Pat {
|
||||
shape,
|
||||
true,
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
PatKind::Wild => {
|
||||
if 1 <= shape.width {
|
||||
@ -408,6 +413,10 @@ fn rewrite_struct_pat(
|
||||
|
||||
impl Rewrite for PatField {
|
||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
self.rewrite_result(context, shape).ok()
|
||||
}
|
||||
|
||||
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
|
||||
let hi_pos = if let Some(last) = self.attrs.last() {
|
||||
last.span.hi()
|
||||
} else {
|
||||
@ -417,10 +426,10 @@ impl Rewrite for PatField {
|
||||
let attrs_str = if self.attrs.is_empty() {
|
||||
String::from("")
|
||||
} else {
|
||||
self.attrs.rewrite(context, shape)?
|
||||
self.attrs.rewrite_result(context, shape)?
|
||||
};
|
||||
|
||||
let pat_str = self.pat.rewrite(context, shape)?;
|
||||
let pat_str = self.pat.rewrite_result(context, shape)?;
|
||||
if self.is_shorthand {
|
||||
combine_strs_with_missing_comments(
|
||||
context,
|
||||
@ -441,7 +450,7 @@ impl Rewrite for PatField {
|
||||
"{}:\n{}{}",
|
||||
id_str,
|
||||
nested_shape.indent.to_string(context.config),
|
||||
self.pat.rewrite(context, nested_shape)?
|
||||
self.pat.rewrite_result(context, nested_shape)?
|
||||
)
|
||||
};
|
||||
combine_strs_with_missing_comments(
|
||||
|
16
src/types.rs
16
src/types.rs
@ -713,8 +713,7 @@ impl Rewrite for ast::GenericParam {
|
||||
mk_sp(last_attr.span.hi(), param_start),
|
||||
shape,
|
||||
!last_attr.is_doc_comment(),
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
} else {
|
||||
// When rewriting generic params, an extra newline should be put
|
||||
// if the attributes end with a doc comment
|
||||
@ -831,8 +830,7 @@ impl Rewrite for ast::Ty {
|
||||
before_lt_span,
|
||||
shape,
|
||||
true,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
} else {
|
||||
result.push_str(<_str);
|
||||
}
|
||||
@ -851,8 +849,7 @@ impl Rewrite for ast::Ty {
|
||||
before_mut_span,
|
||||
shape,
|
||||
true,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
} else {
|
||||
result.push_str(mut_str);
|
||||
}
|
||||
@ -868,8 +865,7 @@ impl Rewrite for ast::Ty {
|
||||
before_ty_span,
|
||||
shape,
|
||||
true,
|
||||
)
|
||||
.unknown_error()?;
|
||||
)?;
|
||||
} else {
|
||||
let used_width = last_line_width(&result);
|
||||
let budget = shape
|
||||
@ -1182,8 +1178,7 @@ fn join_bounds_inner(
|
||||
is_bound_extendable(bound_str, item),
|
||||
combine_strs_with_missing_comments(
|
||||
context, joiner, bound_str, ls, shape, true,
|
||||
)
|
||||
.unknown_error()?,
|
||||
)?,
|
||||
),
|
||||
_ => (
|
||||
is_bound_extendable(bound_str, item),
|
||||
@ -1200,7 +1195,6 @@ fn join_bounds_inner(
|
||||
shape,
|
||||
true,
|
||||
)
|
||||
.unknown_error()
|
||||
.map(|v| (v, trailing_span, extendable)),
|
||||
_ => Ok((strs + &trailing_str, trailing_span, extendable)),
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ use crate::utils::{
|
||||
pub(crate) trait AlignedItem {
|
||||
fn skip(&self) -> bool;
|
||||
fn get_span(&self) -> Span;
|
||||
fn rewrite_prefix(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>;
|
||||
fn rewrite_prefix(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult;
|
||||
fn rewrite_aligned_item(
|
||||
&self,
|
||||
context: &RewriteContext<'_>,
|
||||
@ -42,15 +42,15 @@ impl AlignedItem for ast::FieldDef {
|
||||
self.span()
|
||||
}
|
||||
|
||||
fn rewrite_prefix(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
let attrs_str = self.attrs.rewrite(context, shape)?;
|
||||
fn rewrite_prefix(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
|
||||
let attrs_str = self.attrs.rewrite_result(context, shape)?;
|
||||
let missing_span = if self.attrs.is_empty() {
|
||||
mk_sp(self.span.lo(), self.span.lo())
|
||||
} else {
|
||||
mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
|
||||
};
|
||||
let attrs_extendable = self.ident.is_none() && is_attributes_extendable(&attrs_str);
|
||||
let field_str = rewrite_struct_field_prefix(context, self).ok()?;
|
||||
let field_str = rewrite_struct_field_prefix(context, self)?;
|
||||
combine_strs_with_missing_comments(
|
||||
context,
|
||||
&attrs_str,
|
||||
@ -80,8 +80,8 @@ impl AlignedItem for ast::ExprField {
|
||||
self.span()
|
||||
}
|
||||
|
||||
fn rewrite_prefix(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
let attrs_str = self.attrs.rewrite(context, shape)?;
|
||||
fn rewrite_prefix(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
|
||||
let attrs_str = self.attrs.rewrite_result(context, shape)?;
|
||||
let name = rewrite_ident(context, self.ident);
|
||||
let missing_span = if self.attrs.is_empty() {
|
||||
mk_sp(self.span.lo(), self.span.lo())
|
||||
@ -198,7 +198,7 @@ fn struct_field_prefix_max_min_width<T: AlignedItem>(
|
||||
.rewrite_prefix(context, shape)
|
||||
.map(|field_str| trimmed_last_line_width(&field_str))
|
||||
})
|
||||
.fold_options((0, ::std::usize::MAX), |(max_len, min_len), len| {
|
||||
.fold_ok((0, ::std::usize::MAX), |(max_len, min_len), len| {
|
||||
(cmp::max(max_len, len), cmp::min(min_len, len))
|
||||
})
|
||||
.unwrap_or((0, 0))
|
||||
|
@ -312,8 +312,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
let comment_str =
|
||||
rewrite_comment(other_lines, false, comment_shape, config);
|
||||
match comment_str {
|
||||
Some(ref s) => self.push_str(s),
|
||||
None => self.push_str(other_lines),
|
||||
Ok(ref s) => self.push_str(s),
|
||||
Err(_) => self.push_str(other_lines),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -342,8 +342,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
|
||||
let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
|
||||
match comment_str {
|
||||
Some(ref s) => self.push_str(s),
|
||||
None => self.push_str(&sub_slice),
|
||||
Ok(ref s) => self.push_str(s),
|
||||
Err(_) => self.push_str(&sub_slice),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user