Skip to content

Commit ac4c2d0

Browse files
og-orcasylvestre
authored andcommitted
fmt: only keep the paragraph's last word off its own line
Apply the orphan penalty only when the word left alone is the last word of the paragraph, not at every sentence end. GNU fmt lets a sentence that ends mid-paragraph start a line.
1 parent 0a1c996 commit ac4c2d0

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

‎src/uu/fmt/src/linebreak.rs‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ fn find_kp_breakpoints<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(
241241
.peek()
242242
.is_some_and(|w| w.sentence_start || (w.new_line && first_punct));
243243
while let Some(w) = iter.next() {
244-
let next_word_sentence_final = is_next_word_sentence_final(iter.clone());
244+
// is the word after this one the last word of the paragraph?
245+
let next_is_last_word = iter.clone().nth(1).is_none();
245246
// if this is the last word, we don't add additional demerits for this break
246247
let (is_last_word, is_sentence_end) = match iter.peek() {
247248
None => (true, true),
@@ -280,7 +281,7 @@ fn find_kp_breakpoints<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(
280281
w.word_nchars,
281282
active.prev_rat,
282283
);
283-
if is_sentence_end {
284+
if is_last_word {
284285
new_demerits = new_demerits.saturating_add(ORPHAN_BREAK_PENALTY);
285286
}
286287
let total_demerits = active.demerits.saturating_add(new_demerits);
@@ -329,7 +330,7 @@ fn find_kp_breakpoints<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(
329330
)
330331
};
331332

332-
if !is_last_word && next_word_sentence_final {
333+
if !is_last_word && next_is_last_word {
333334
new_demerits = new_demerits.saturating_add(ORPHAN_BREAK_PENALTY);
334335
}
335336

@@ -409,23 +410,9 @@ const BAD_MULT: f32 = 200.0;
409410
const DR_MULT: f32 = 600.0;
410411
// DL_MULT is penalty multiplier for short words at end of line
411412
const DL_MULT: f32 = 10.0;
412-
// Penalize breaks that leave the first word on the next line as the sentence-final word.
413+
// Penalize breaks that leave the last word of the paragraph alone on its line.
413414
const ORPHAN_BREAK_PENALTY: i64 = 4_000_000_000;
414415

415-
fn is_word_sentence_final(current: &WordInfo, next: Option<&WordInfo>) -> bool {
416-
match next {
417-
None => true,
418-
Some(next_word) => next_word.sentence_start || (next_word.new_line && current.ends_punct),
419-
}
420-
}
421-
422-
fn is_next_word_sentence_final<'a, T: Iterator<Item = &'a WordInfo<'a>>>(mut iter: T) -> bool {
423-
let Some(next_word) = iter.next() else {
424-
return false;
425-
};
426-
is_word_sentence_final(next_word, iter.next())
427-
}
428-
429416
fn compute_demerits(delta_len: isize, stretch: usize, wlen: usize, prev_rat: f32) -> (i64, f32) {
430417
// how much stretch are we using?
431418
let ratio = if delta_len == 0 {

‎tests/by-util/test_fmt.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,21 @@ fn test_fmt_balances_lines_before_a_lone_last_word() {
843843
.stdout_only(expected);
844844
}
845845
}
846+
847+
#[test]
848+
fn test_fmt_default_width_sentence_end_mid_paragraph() {
849+
// Only the last word of the paragraph is kept off a line of its own. A
850+
// sentence that ends mid-paragraph may start a line, as in GNU fmt.
851+
new_ucmd!()
852+
.pipe_in(
853+
"The coreutils should be very reliable. This means that we should never panic. \
854+
Therefore, you should avoid using unwrap and expect in the code. \
855+
Sometimes the use of unreachable can be justified with a comment.\n",
856+
)
857+
.succeeds()
858+
.stdout_only(
859+
"The coreutils should be very reliable. This means that we should never\n\
860+
panic. Therefore, you should avoid using unwrap and expect in the code.\n\
861+
Sometimes the use of unreachable can be justified with a comment.\n",
862+
);
863+
}

0 commit comments

Comments
 (0)