leave the comment in parentheses of argumentless Fn (#3518)

This commit is contained in:
rChaser53 2019-04-24 08:21:04 +09:00 committed by Seiichi Uchida
parent 1f61286293
commit 05547d90b5
4 changed files with 128 additions and 38 deletions

View file

@ -11,6 +11,7 @@ pub trait SpanUtils {
fn span_after(&self, original: Span, needle: &str) -> BytePos;
fn span_after_last(&self, original: Span, needle: &str) -> BytePos;
fn span_before(&self, original: Span, needle: &str) -> BytePos;
fn span_before_last(&self, original: Span, needle: &str) -> BytePos;
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos>;
fn opt_span_before(&self, original: Span, needle: &str) -> Option<BytePos>;
}
@ -56,6 +57,17 @@ impl<'a> SpanUtils for SnippetProvider<'a> {
})
}
fn span_before_last(&self, original: Span, needle: &str) -> BytePos {
let snippet = self.span_to_snippet(original).unwrap();
let mut offset = 0;
while let Some(additional_offset) = snippet[offset..].find_uncommented(needle) {
offset += additional_offset + needle.len();
}
original.lo() + BytePos(offset as u32 - 1)
}
fn opt_span_after(&self, original: Span, needle: &str) -> Option<BytePos> {
self.opt_span_before(original, needle)
.map(|bytepos| bytepos + BytePos(needle.len() as u32))

View file

@ -8,7 +8,9 @@ use syntax::symbol::keywords;
use crate::config::lists::*;
use crate::config::{IndentStyle, TypeDensity};
use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::lists::{
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
};
use crate::macros::{rewrite_macro, MacroPosition};
use crate::overflow;
use crate::pairs::{rewrite_pair, PairParts};
@ -314,7 +316,27 @@ where
let offset = shape.indent + 1;
Shape::legacy(budget, offset)
};
let list_lo = context.snippet_provider.span_after(span, "(");
let (list_str, tactic) = if inputs.len() == 0 {
let tactic = get_tactics(&[], &output, shape);
let list_hi = context.snippet_provider.span_before_last(span, ")");
let comment = context
.snippet_provider
.span_to_snippet(mk_sp(list_lo, list_hi))?
.trim();
let comment = if comment.starts_with("//") {
format!(
"{}{}{}",
&list_shape.indent.to_string_with_newline(context.config),
comment,
&shape.block().indent.to_string_with_newline(context.config)
)
} else {
comment.to_string()
};
(comment, tactic)
} else {
let items = itemize_list(
context.snippet_provider,
inputs,
@ -329,19 +351,7 @@ where
);
let item_vec: Vec<_> = items.collect();
// If the return type is multi-lined, then force to use multiple lines for
// arguments as well.
let tactic = if output.contains('\n') {
DefinitiveListTactic::Vertical
} else {
definitive_tactic(
&*item_vec,
ListTactic::HorizontalVertical,
Separator::Comma,
shape.width.saturating_sub(2 + output.len()),
)
};
let tactic = get_tactics(&item_vec, &output, shape);
let trailing_separator = if !context.use_block_indent() || variadic {
SeparatorTactic::Never
} else {
@ -353,7 +363,8 @@ where
.trailing_separator(trailing_separator)
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
.preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?;
(write_list(&item_vec, &fmt)?, tactic)
};
let args = if tactic == DefinitiveListTactic::Horizontal || !context.use_block_indent() {
format!("({})", list_str)
@ -381,6 +392,22 @@ fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
colon_spaces(context.config)
}
// If the return type is multi-lined, then force to use multiple lines for
// arguments as well.
fn get_tactics(item_vec: &[ListItem], output: &str, shape: Shape) -> DefinitiveListTactic {
if output.contains('\n') {
DefinitiveListTactic::Vertical
} else {
definitive_tactic(
item_vec,
ListTactic::HorizontalVertical,
Separator::Comma,
// 2 is for the case of ',\n'
shape.width.saturating_sub(2 + output.len()),
)
}
}
impl Rewrite for ast::WherePredicate {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
// FIXME: dead spans?

View file

@ -0,0 +1,29 @@
fn foo<F>(foo2: F)
where
F: Fn(
// this comment is deleted
),
{
}
fn foo_block<F>(foo2: F)
where
F: Fn(
/* this comment is deleted */
),
{
}
fn bar(
bar2: impl Fn(
// this comment is deleted
),
) {
}
fn bar_block(
bar2: impl Fn(
/* this comment is deleted */
),
) {
}

View file

@ -0,0 +1,22 @@
fn foo<F>(foo2: F)
where
F: Fn(
// this comment is deleted
),
{
}
fn foo_block<F>(foo2: F)
where
F: Fn(/* this comment is deleted */),
{
}
fn bar(
bar2: impl Fn(
// this comment is deleted
),
) {
}
fn bar_block(bar2: impl Fn(/* this comment is deleted */)) {}