Skip to content

Commit

Permalink
Merge pull request #15 from LaunchPlatform/fix-13-bug-handling-tailin…
Browse files Browse the repository at this point in the history
…g-comments

Fix #13 tailing comments causing error throwing issue. Get rid of standalone comments group
  • Loading branch information
fangpenlin authored Mar 10, 2023
2 parents 13b485f + 0360c52 commit d8ea7fd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 16 deletions.
21 changes: 6 additions & 15 deletions beancount_black/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,17 +518,6 @@ def format_statement_group(self, group: StatementGroup) -> str:
else:
raise ValueError(f"Unexpected token {first_child.type}")
else:
if comments and comments[-1].line + 1 != statement.meta.line:
# Standalone comment group
entry = Entry(
type=EntryType.COMMENTS,
comments=comments,
statement=statement,
metadata=[],
postings=[],
)
entries.append(entry)
comments = []
if first_child.data == "posting":
last_entry = entries[-1]
if last_entry.type != EntryType.TXN:
Expand Down Expand Up @@ -558,18 +547,16 @@ def format_statement_group(self, group: StatementGroup) -> str:
entries.append(entry)
comments = []

tailing_comments_entry: typing.Optional[Entry] = None
if comments:
entry = Entry(
tailing_comments_entry = Entry(
type=EntryType.COMMENTS,
comments=comments,
# TODO: maybe pass None makes more sense here?
statement=statement,
metadata=[],
postings=[],
)
entries.append(entry)
comments = []
# TODO: or add to tail comments?

# breaking down entries into groups by leading entry type, comments or
# None (means doesn't belong to the leading groups or comments)
Expand Down Expand Up @@ -598,6 +585,10 @@ def format_statement_group(self, group: StatementGroup) -> str:
for entry in remain_entries:
sections.append(self.format_entry(entry))

# Dealing with the special case when there's tailing comments in the file
if tailing_comments_entry is not None:
sections.append(self.format_entry(tailing_comments_entry))

return "\n\n".join(sections)

def calculate_column_widths(self, tree: ParseTree):
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/expected_output/sections.bean
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
; standalone comment
; line 2
; line 3

2022-05-01 note Assets:Cash "0"

2022-05-02 note Assets:Cash "1"
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/expected_output/tailing_comment.bean
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
2000-01-01 open Assets:Cash
2000-01-01 open Expenses:Food

;; comment OK
2010-01-01 txn "P. Escobar" "White flour"
Assets:Cash 1,000.00 USD
Expenses:Food

;; this comment blows up
12 changes: 12 additions & 0 deletions tests/fixtures/expected_output/tailing_comments.bean
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
2000-01-01 open Assets:Cash
2000-01-01 open Expenses:Food

;; comment OK
2010-01-01 txn "P. Escobar" "White flour"
Assets:Cash 1,000.00 USD
Expenses:Food

;; comment 1
;; comment 2
;; comment 3
;; comment 4
10 changes: 10 additions & 0 deletions tests/fixtures/input/tailing_comment.bean
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
2000-01-01 open Assets:Cash
2000-01-01 open Expenses:Food

;; comment OK

2010-01-01 txn "P. Escobar" "White flour"
Assets:Cash 1000.00 USD
Expenses:Food

;; this comment blows up
15 changes: 15 additions & 0 deletions tests/fixtures/input/tailing_comments.bean
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2000-01-01 open Assets:Cash
2000-01-01 open Expenses:Food

;; comment OK

2010-01-01 txn "P. Escobar" "White flour"
Assets:Cash 1000.00 USD
Expenses:Food

;; comment 1
;; comment 2

;; comment 3

;; comment 4
2 changes: 2 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def test_create_backup_with_conflicts(tmp_path: pathlib.Path):
("column_width.bean", "column_width.bean"),
("txn.bean", "txn.bean"),
("number_expr.bean", "number_expr.bean"),
("tailing_comment.bean", "tailing_comment.bean"),
("tailing_comments.bean", "tailing_comments.bean"),
],
)
@pytest.mark.parametrize("stdin_mode", [False, True])
Expand Down

0 comments on commit d8ea7fd

Please sign in to comment.