Preserve new line on exchanges (x) with empty hold spaces. (#259)

* fix: remove newline swap in exchange and swap content only

* fix: support non empty hold spaces and newline appended hold spaces

* nit: period in comment

* nit: useless comment removal

* chore: clarify newline swap logic for `x`
This commit is contained in:
Alexander Wang
2026-02-13 04:36:51 -05:00
committed by GitHub
parent c0c85e65fa
commit 22abe5ea6c
2 changed files with 16 additions and 1 deletions
+5 -1
View File
@@ -631,8 +631,12 @@ fn process_file(
'x' => {
// Exchange the contents of the pattern and hold spaces.
let (pat_content, pat_has_newline) = pattern.fields_mut()?;
// Swap newline if hold space is logically non-empty.
if !context.hold.content.is_empty() || context.hold.has_newline {
std::mem::swap(pat_has_newline, &mut context.hold.has_newline);
}
std::mem::swap(pat_content, &mut context.hold.content);
std::mem::swap(pat_has_newline, &mut context.hold.has_newline);
}
'y' => {
let trans = extract_variant!(command, Transliteration);
+11
View File
@@ -1195,3 +1195,14 @@ fn test_print_command_adds_newline() {
.succeeds()
.stdout_is("foo\nfoo");
}
////////////////////////////////////////////////////////////
// Test for issue #254: Missing newline in exchanged output with `2x`
#[test]
fn test_exchange_command_adds_newline() {
new_ucmd!()
.args(&["2x"])
.pipe_in("a\nb\nc\n")
.succeeds()
.stdout_is("a\n\nc\n");
}