Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove version gates #3891

Merged
merged 15 commits into from
Dec 16, 2019
10 changes: 5 additions & 5 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use syntax::{ast, ptr};

use crate::attr::get_attrs_from_stmt;
use crate::config::lists::*;
use crate::config::{IndentStyle, SeparatorTactic, Version};
use crate::config::{IndentStyle, SeparatorTactic};
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
use crate::items::{span_hi_for_param, span_lo_for_param};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
Expand Down Expand Up @@ -409,19 +409,19 @@ fn is_block_closure_forced(context: &RewriteContext<'_>, expr: &ast::Expr) -> bo
if context.inside_macro() {
false
} else {
is_block_closure_forced_inner(expr, context.config.version())
is_block_closure_forced_inner(expr)
}
}

fn is_block_closure_forced_inner(expr: &ast::Expr, version: Version) -> bool {
fn is_block_closure_forced_inner(expr: &ast::Expr) -> bool {
match expr.kind {
ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop(..) => true,
ast::ExprKind::Loop(..) if version == Version::Two => true,
ast::ExprKind::Loop(..) => true,
ast::ExprKind::AddrOf(_, ref expr)
| ast::ExprKind::Box(ref expr)
| ast::ExprKind::Try(ref expr)
| ast::ExprKind::Unary(_, ref expr)
| ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced_inner(expr, version),
| ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced_inner(expr),
_ => false,
}
}
Expand Down
175 changes: 111 additions & 64 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::rewrite::RewriteContext;
use crate::shape::{Indent, Shape};
use crate::string::{rewrite_string, StringFormat};
use crate::utils::{
count_newlines, first_line_width, last_line_width, trim_left_preserve_layout, unicode_str_width,
count_newlines, first_line_width, last_line_width, tab_to_spaces, trim_left_preserve_layout,
unicode_str_width,
};
use crate::{ErrorKind, FormattingError};

Expand Down Expand Up @@ -1162,14 +1163,6 @@ impl FullCodeCharKind {
self == FullCodeCharKind::InStringCommented
|| self == FullCodeCharKind::StartStringCommented
}

fn to_codecharkind(self) -> CodeCharKind {
if self.is_comment() {
CodeCharKind::Comment
} else {
CodeCharKind::Normal
}
}
}

impl<T> CharClasses<T>
Expand Down Expand Up @@ -1484,16 +1477,31 @@ impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
/// functional text. Line style comments contain their ending newlines.
pub(crate) struct CommentCodeSlices<'a> {
slice: &'a str,
last_slice_kind: CodeCharKind,
last_slice_end: usize,
ungrouped_code_slices: MultiPeek<UngroupedCommentCodeSlices<'a>>,
offset: Option<usize>,
tab_spaces: usize,
}

impl<'a> CommentCodeSlices<'a> {
pub(crate) fn new(slice: &'a str) -> CommentCodeSlices<'a> {
CommentCodeSlices {
slice,
last_slice_kind: CodeCharKind::Comment,
last_slice_end: 0,
tab_spaces: 4,
ungrouped_code_slices: multipeek(UngroupedCommentCodeSlices::new(slice)),
offset: None,
}
}

pub(crate) fn with_offset(
slice: &'a str,
offset: usize,
tab_spaces: usize,
) -> CommentCodeSlices<'a> {
CommentCodeSlices {
slice,
tab_spaces,
ungrouped_code_slices: multipeek(UngroupedCommentCodeSlices::new(slice)),
offset: Some(offset).filter(|o| *o != 0),
}
}
}
Expand All @@ -1502,59 +1510,50 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
type Item = (CodeCharKind, usize, &'a str);

fn next(&mut self) -> Option<Self::Item> {
if self.last_slice_end == self.slice.len() {
return None;
}

let mut sub_slice_end = self.last_slice_end;
let mut first_whitespace = None;
let subslice = &self.slice[self.last_slice_end..];
let mut iter = CharClasses::new(subslice.char_indices());

for (kind, (i, c)) in &mut iter {
let is_comment_connector = self.last_slice_kind == CodeCharKind::Normal
&& &subslice[..2] == "//"
&& [' ', '\t'].contains(&c);

if is_comment_connector && first_whitespace.is_none() {
first_whitespace = Some(i);
let first_chunk = self.ungrouped_code_slices.next()?;
if first_chunk.0 == CodeCharKind::Normal {
if !first_chunk.2.trim().is_empty() {
self.offset = Some(last_line_width(first_chunk.2)).filter(|o| *o != 0);
}
return Some(first_chunk);
}

if kind.to_codecharkind() == self.last_slice_kind && !is_comment_connector {
let last_index = match first_whitespace {
Some(j) => j,
None => i,
};
sub_slice_end = self.last_slice_end + last_index;
break;
}
let mut comment_end_index = first_chunk.1 + first_chunk.2.len();
while let Some(&(k, i, s)) = self.ungrouped_code_slices.peek() {
match k {
CodeCharKind::Comment if self.offset.is_none() => {
comment_end_index = i + s.len();
self.ungrouped_code_slices.next()?;
}
CodeCharKind::Comment => break,
CodeCharKind::Normal if s.trim().is_empty() && count_newlines(s) == 0 => {
let indent_width = tab_to_spaces(s, self.tab_spaces);
if self.offset.map_or(false, |comment_offset| {
!(indent_width < comment_offset + 2 && comment_offset < indent_width + 2)
}) {
break;
}

if !is_comment_connector {
first_whitespace = None;
match self.ungrouped_code_slices.peek() {
Some((CodeCharKind::Comment, index, s)) => {
comment_end_index = index + s.len();
// Advance twice.
self.ungrouped_code_slices.next()?;
self.ungrouped_code_slices.next()?;
}
_ => break,
}
}
CodeCharKind::Normal => break,
}
}

if let (None, true) = (iter.next(), sub_slice_end == self.last_slice_end) {
// This was the last subslice.
sub_slice_end = match first_whitespace {
Some(i) => self.last_slice_end + i,
None => self.slice.len(),
};
}

let kind = match self.last_slice_kind {
CodeCharKind::Comment => CodeCharKind::Normal,
CodeCharKind::Normal => CodeCharKind::Comment,
};
let res = (
kind,
self.last_slice_end,
&self.slice[self.last_slice_end..sub_slice_end],
);
self.last_slice_end = sub_slice_end;
self.last_slice_kind = kind;

Some(res)
let comment_start_index = first_chunk.1;
Some((
CodeCharKind::Comment,
comment_start_index,
&self.slice[comment_start_index..comment_end_index],
))
}
}

Expand Down Expand Up @@ -1728,7 +1727,6 @@ mod test {
let input = "// comment\n test();";
let mut iter = CommentCodeSlices::new(input);

assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
assert_eq!(
(CodeCharKind::Comment, 0, "// comment\n"),
iter.next().unwrap()
Expand All @@ -1742,18 +1740,67 @@ mod test {

#[test]
fn comment_code_slices_three() {
let input = "1 // comment\n // comment2\n\n";
let input = "1 // comment\n // comment2\n\n";
let mut iter = CommentCodeSlices::new(input);

assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
assert_eq!(
(CodeCharKind::Comment, 2, "// comment\n // comment2\n"),
(CodeCharKind::Comment, 2, "// comment\n // comment2\n"),
iter.next().unwrap()
);
assert_eq!((CodeCharKind::Normal, 27, "\n"), iter.next().unwrap());
assert_eq!(None, iter.next());
}

#[test]
fn comment_code_slices_four() {
let input = r#"
if x == 3 {
x = 4;
} // if x == 3
// end of block
"#;
let mut iter = CommentCodeSlices::new(input);

assert_eq!(
(
CodeCharKind::Normal,
0,
r#"
if x == 3 {
x = 4;
} "#
),
iter.next().unwrap()
);
assert_eq!(
(CodeCharKind::Comment, 26, "// if x == 3\n",),
iter.next().unwrap()
);
assert_eq!(
(CodeCharKind::Comment, 39, "// end of block\n"),
iter.next().unwrap()
);
assert_eq!((CodeCharKind::Normal, 29, "\n"), iter.next().unwrap());
assert_eq!(None, iter.next());
}

#[test]
fn comment_code_slices_five() {
let input = "1 // comment\r\n\r\n // comment2\r\n";
let mut iter = CommentCodeSlices::new(input);

assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
assert_eq!(
(CodeCharKind::Comment, 2, "// comment\r\n"),
iter.next().unwrap()
);
assert_eq!((CodeCharKind::Normal, 14, "\r\n ",), iter.next().unwrap());
assert_eq!(
(CodeCharKind::Comment, 18, "// comment2\r\n"),
iter.next().unwrap()
);
assert_eq!(None, iter.next());
}
#[test]
#[rustfmt::skip]
fn format_doc_comments() {
Expand Down
3 changes: 1 addition & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::comment::{
rewrite_comment, rewrite_missing_comment, CharClasses, FindUncommented,
};
use crate::config::lists::*;
use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
use crate::config::{Config, ControlBraceStyle, IndentStyle};
use crate::lists::{
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
struct_lit_tactic, write_list, ListFormatting, Separator,
Expand Down Expand Up @@ -1238,7 +1238,6 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
.lines()
.dropping_back(1)
.all(|line| line.ends_with('\\'))
&& context.config.version() == Version::Two
{
return Some(string_lit.to_owned());
} else {
Expand Down
Loading