Add count_newlines() utility function

This commit is contained in:
Seiichi Uchida 2017-12-05 15:17:40 +09:00
parent 89f27764ed
commit 228578b9c7
6 changed files with 17 additions and 16 deletions

View File

@ -18,7 +18,7 @@ use config::Config;
use rewrite::RewriteContext;
use shape::{Indent, Shape};
use string::{rewrite_string, StringFormat};
use utils::{first_line_width, last_line_width};
use utils::{count_newlines, first_line_width, last_line_width};
fn is_custom_comment(comment: &str) -> bool {
if !comment.starts_with("//") {
@ -296,7 +296,7 @@ fn rewrite_comment_inner(
config: config,
};
let line_breaks = orig.trim_right().chars().filter(|&c| c == '\n').count();
let line_breaks = count_newlines(orig.trim_right());
let lines = orig.lines()
.enumerate()
.map(|(i, mut line)| {

View File

@ -2712,12 +2712,8 @@ pub fn choose_rhs<R: Rewrite>(
}
fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str) -> bool {
fn count_line_breaks(src: &str) -> usize {
src.chars().filter(|&x| x == '\n').count()
}
!next_line_rhs.contains('\n')
|| count_line_breaks(orig_rhs) > count_line_breaks(next_line_rhs) + 1
use utils::count_newlines;
!next_line_rhs.contains('\n') || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
}
fn rewrite_expr_addrof(

View File

@ -17,7 +17,7 @@ use comment::{find_comment_end, rewrite_comment, FindUncommented};
use config::{Config, IndentStyle};
use rewrite::RewriteContext;
use shape::{Indent, Shape};
use utils::{first_line_width, last_line_width, mk_sp, starts_with_newline};
use utils::{count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline};
/// Formatting tactic for lists. This will be cast down to a
/// `DefinitiveListTactic` depending on the number and length of the items and
@ -651,7 +651,7 @@ where
// From the end of the first line of comments to the next non-whitespace char.
let test_snippet = &test_snippet[..first];
if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
if count_newlines(test_snippet) > 1 {
// There were multiple line breaks which got trimmed to nothing.
new_lines = true;
}

View File

@ -16,7 +16,7 @@ use syntax::codemap::{BytePos, Pos, Span};
use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
use config::WriteMode;
use shape::{Indent, Shape};
use utils::mk_sp;
use utils::{count_newlines, mk_sp};
use visitor::FmtVisitor;
impl<'a> FmtVisitor<'a> {
@ -86,7 +86,7 @@ impl<'a> FmtVisitor<'a> {
}
fn push_vertical_spaces(&mut self, original: &str) {
let mut newline_count = original.chars().filter(|&c| c == '\n').count();
let mut newline_count = count_newlines(original);
let newline_upper_bound = self.config.blank_lines_upper_bound() + 1;
let newline_lower_bound = self.config.blank_lines_lower_bound() + 1;
if newline_count > newline_upper_bound {
@ -171,7 +171,7 @@ impl<'a> FmtVisitor<'a> {
let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count();
let subslice_num_lines = count_newlines(subslice);
if rewrite_next_comment
&& !self.config.file_lines().intersects_range(

View File

@ -262,6 +262,11 @@ pub fn stmt_expr(stmt: &ast::Stmt) -> Option<&ast::Expr> {
}
}
#[inline]
pub fn count_newlines(input: &str) -> usize {
input.chars().filter(|&c| c == '\n').count()
}
#[inline]
pub fn trim_newlines(input: &str) -> &str {
match input.find(|c| c != '\n' && c != '\r') {

View File

@ -31,7 +31,7 @@ use macros::{rewrite_macro, MacroPosition};
use regex::Regex;
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
use utils::{self, contains_skip, inner_attributes, mk_sp, ptr_vec_to_ref_vec};
use utils::{self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec};
fn is_use_item(item: &ast::Item) -> bool {
match item.node {
@ -833,7 +833,7 @@ where
// Extract comments between two attributes.
let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo());
let snippet = context.snippet(span_between_attr);
if snippet.chars().filter(|c| *c == '\n').count() >= 2 || snippet.contains('/') {
if count_newlines(&snippet) >= 2 || snippet.contains('/') {
break;
}
}
@ -886,7 +886,7 @@ fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
// Look at before and after comment and see if there are any empty lines.
let comment_begin = comment.chars().position(|c| c == '/');
let len = comment_begin.unwrap_or_else(|| comment.len());
let mlb = comment.chars().take(len).filter(|c| *c == '\n').count() > 1;
let mlb = count_newlines(&comment[..len]) > 1;
let mla = if comment_begin.is_none() {
mlb
} else {