Skip to content

Commit 53d7c25

Browse files
authored
Fix descriptions starting with multiple spaces removed (#41)
1 parent d983b33 commit 53d7c25

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 2025-10-21 - [0.3.2]
4+
5+
### Bug fixes
6+
7+
- Fix formatting of transactions with multiple spaces before description.
8+
39
## 2025-10-07 - [0.3.1]
410

511
### Bug fixes
@@ -156,6 +162,7 @@
156162

157163
First beta release
158164

165+
[0.3.2]: https://github.com/mondeja/hledger-fmt/compare/v0.3.1...v0.3.2
159166
[0.3.1]: https://github.com/mondeja/hledger-fmt/compare/v0.3.0...v0.3.1
160167
[0.3.0]: https://github.com/mondeja/hledger-fmt/compare/v0.2.11...v0.3.0
161168
[0.2.11]: https://github.com/mondeja/hledger-fmt/compare/v0.2.10...v0.2.11

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hledger-fmt"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
rust-version = "1.74.1"
55
edition = "2021"
66
description = "An opinionated hledger's journal files formatter."

src/formatter/tests.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,39 @@ tag foo
572572
"#,
573573
);
574574
}
575+
576+
// https://github.com/mondeja/hledger-fmt/issues/40
577+
#[test]
578+
fn transaction_with_multi_spaced_description() {
579+
assert_noop_format(
580+
r#"2025-10-10 Description after two spaces
581+
assets:A 10 EUR
582+
assets:B -10 EUR
583+
"#,
584+
);
585+
}
586+
587+
#[test]
588+
fn transaction_with_multi_spaced_description_and_valid_comment() {
589+
assert_format(
590+
r#"2025-10-10 Description after multiple spaces; comment
591+
assets:A 10 EUR
592+
assets:B -10 EUR
593+
"#,
594+
r#"2025-10-10 Description after multiple spaces ; comment
595+
assets:A 10 EUR
596+
assets:B -10 EUR
597+
"#,
598+
);
599+
600+
assert_format(
601+
r#"2025-10-10 Description after multiple spaces ; comment
602+
assets:A 10 EUR
603+
assets:B -10 EUR
604+
"#,
605+
r#"2025-10-10 Description after multiple spaces ; comment
606+
assets:A 10 EUR
607+
assets:B -10 EUR
608+
"#,
609+
);
610+
}

src/parser/mod.rs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -607,36 +607,22 @@ pub fn parse_content(content: &str) -> Result<JournalFile, errors::SyntaxError>
607607

608608
let mut transaction_title = String::with_capacity(64);
609609
transaction_title.push(c);
610-
let mut prev_was_whitespace = false;
611-
let mut is_periodic = false;
610+
let mut comment_prefix = None;
612611
for (_, c) in chars_iter.by_ref() {
613-
if c.is_whitespace() {
614-
if prev_was_whitespace {
615-
// periodic transactions (starts with "~ ") allow two
616-
// spaces between the period and the title
617-
if transaction_title.starts_with("~ ") && !is_periodic {
618-
is_periodic = true;
619-
} else {
620-
transaction_title.pop(); // remove previous whitespace
621-
break;
622-
}
623-
}
624-
prev_was_whitespace = true;
625-
} else {
626-
prev_was_whitespace = false;
612+
if c == ';' {
613+
comment_prefix = Some(CommentPrefix::Semicolon);
614+
break;
615+
} else if c == '#' {
616+
comment_prefix = Some(CommentPrefix::Hash);
617+
break;
627618
}
628619
transaction_title.push(c);
629620
}
630621

631-
data.transaction_title = transaction_title;
632-
633-
if !prev_was_whitespace {
634-
// end of line
635-
continue;
636-
}
622+
data.transaction_title = transaction_title.trim_end().to_string();
637623

638624
data.transaction_title_comment =
639-
parse_inline_comment(&mut chars_iter, lineno, 1, None);
625+
parse_inline_comment(&mut chars_iter, lineno, 1, comment_prefix);
640626
}
641627
}
642628
}

0 commit comments

Comments
 (0)