Skip to content

Commit 7e1a92f

Browse files
committed
extract push_vertical_spaces into a stand alone function
To better reuse the logic in `FmtVisitor::push_vertical_spaces` it was extracted into its own function. `FmtVisitor::push_vertical_spaces` now calls the stand alone function in it's implementation.
1 parent 2403f82 commit 7e1a92f

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/missed_spans.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::shape::{Indent, Shape};
99
use crate::source_map::LineRangeUtils;
1010
use crate::utils::{count_lf_crlf, count_newlines, last_line_width, mk_sp};
1111
use crate::visitor::FmtVisitor;
12+
use crate::Config;
1213

1314
struct SnippetStatus {
1415
/// An offset to the current line from the beginning of the original snippet.
@@ -112,27 +113,9 @@ impl<'a> FmtVisitor<'a> {
112113
}
113114
}
114115

115-
fn push_vertical_spaces(&mut self, mut newline_count: usize) {
116-
let offset = self.buffer.chars().rev().take_while(|c| *c == '\n').count();
117-
let newline_upper_bound = self.config.blank_lines_upper_bound() + 1;
118-
let newline_lower_bound = self.config.blank_lines_lower_bound() + 1;
119-
120-
if newline_count + offset > newline_upper_bound {
121-
if offset >= newline_upper_bound {
122-
newline_count = 0;
123-
} else {
124-
newline_count = newline_upper_bound - offset;
125-
}
126-
} else if newline_count + offset < newline_lower_bound {
127-
if offset >= newline_lower_bound {
128-
newline_count = 0;
129-
} else {
130-
newline_count = newline_lower_bound - offset;
131-
}
132-
}
133-
134-
let blank_lines = "\n".repeat(newline_count);
135-
self.push_str(&blank_lines);
116+
fn push_vertical_spaces(&mut self, newline_count: usize) {
117+
let newlines_pushed = push_vertical_spaces(&mut self.buffer, self.config, newline_count);
118+
self.line_number += newlines_pushed;
136119
}
137120

138121
fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
@@ -361,3 +344,31 @@ impl<'a> FmtVisitor<'a> {
361344
}
362345
}
363346
}
347+
348+
pub(crate) fn push_vertical_spaces(
349+
buffer: &mut String,
350+
config: &Config,
351+
mut newline_count: usize,
352+
) -> usize {
353+
let offset = buffer.chars().rev().take_while(|c| *c == '\n').count();
354+
let newline_upper_bound = config.blank_lines_upper_bound() + 1;
355+
let newline_lower_bound = config.blank_lines_lower_bound() + 1;
356+
357+
if newline_count + offset > newline_upper_bound {
358+
if offset >= newline_upper_bound {
359+
newline_count = 0;
360+
} else {
361+
newline_count = newline_upper_bound - offset;
362+
}
363+
} else if newline_count + offset < newline_lower_bound {
364+
if offset >= newline_lower_bound {
365+
newline_count = 0;
366+
} else {
367+
newline_count = newline_lower_bound - offset;
368+
}
369+
}
370+
371+
let blank_lines = "\n".repeat(newline_count);
372+
buffer.push_str(&blank_lines);
373+
newline_count
374+
}

0 commit comments

Comments
 (0)