From 09d66c5fc1a6e3c97a133b9efa48968df706d379 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 4 Aug 2022 19:05:52 +0100 Subject: [PATCH] fix: account display order is now tracked across all files (fix #1909) --- hledger-lib/Hledger/Data/Journal.hs | 12 +++++++++++- hledger-lib/Hledger/Read/JournalReader.hs | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 9c8264312..908f7d124 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -199,7 +199,9 @@ instance Show Journal where -- Note that (<>) is right-biased, so nulljournal is only a left identity. -- In particular, this prevents Journal from being a monoid. instance Semigroup Journal where - j1 <> j2 = Journal { + j1 <> j2 = + journalRenumberAccountDeclarations $ + Journal { jparsedefaultyear = jparsedefaultyear j2 ,jparsedefaultcommodity = jparsedefaultcommodity j2 ,jparsedecimalmark = jparsedecimalmark j2 @@ -226,6 +228,14 @@ instance Semigroup Journal where ,jlastreadtime = max (jlastreadtime j1) (jlastreadtime j2) } +-- | Renumber all the account declarations. Call this after combining two journals into one, +-- so that account declarations have a total order again. +journalRenumberAccountDeclarations :: Journal -> Journal +journalRenumberAccountDeclarations j = j{jdeclaredaccounts=jdas'} + where + jdas' = [(a, adi{adideclarationorder=n}) | (n, (a,adi)) <- zip [1..] $ jdeclaredaccounts j] + -- XXX the per-file declaration order saved during parsing is discarded; it seems unneeded + instance Default Journal where def = nulljournal diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 6377a9835..d28d4db6a 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -401,6 +401,9 @@ addAccountDeclaration (a,cmt,tags) = adicomment = cmt ,aditags = tags ,adideclarationorder = length decls + 1 + -- this restarts from 1 in each file, which is not that useful + -- when there are multiple files; so it gets renumbered + -- automatically when combining Journals with <> }) in j{jdeclaredaccounts = d:decls})