mhansen / hledger-vscode

VSCode plugin for HLedger accounting journal file
MIT License
37 stars 9 forks source link

[Feature Request] Journal Prettifier #232

Open the-solipsist opened 1 year ago

the-solipsist commented 1 year ago

One of the features that the Vim/Emacs ledger-language support add-ons have is reformatting of the transactions to make them look prettier (e.g., with transaction amounts aligned neatly, etc.)

While this is not a very important feature (since one can run hledger print to obtain somewhat similar results, though it is lossy since it doesn't preserve inter-transaction comments, etc.), it would be good to have.

mhansen commented 1 year ago

Right. This is a reasonable feature request, but its hard to do.

I have seen such formatters. I really wish it was built into hledger. Like gofmt is built into the go toolchain, and preserves comments. I don’t really want to build another hledger parser that will be bound to drift from the canonical parser.

The current parser we have for this extension is very weak: just a few regexps, not powerful enough for reformatting.

On Mon, 5 Sep 2022 at 19:46, Pranesh Prakash @.***> wrote:

One of the features that the Vim/Emacs ledger-language support add-ons have is reformatting of the transactions to make them look prettier (e.g., with transaction amounts aligned neatly, etc.)

While this is not a very important feature (since one can run hledger print to obtain somewhat similar results, though it is lossy since it doesn't preserve inter-transaction comments, etc.), it would be good to have.

— Reply to this email directly, view it on GitHub https://github.com/mhansen/hledger-vscode/issues/232, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZYOLCKNXQDOITXDDQXJ3V4W6QBANCNFSM6AAAAAAQE2G2G4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

simonmichael commented 1 year ago

What's wrong with piping through hledger print (and handling inter transaction content separately) ?

the-solipsist commented 1 year ago

What's wrong with piping through hledger print (and handling inter transaction content separately) ?

Nothing at all. It's just that doing so requires a separate script[1]. So it's just a matter of laziness vs. convenience.

At one point, I was using the vim-ledger plugin solely for reformatting of my file, since it made it very easy. Now that I've moved to vscode, I was hoping I could use hledger-vscode for the same purpose.

[1]: Here's a script that does the job for me, at least wrt to the include and default commands I use at the beginning of a file:

#!/bin/bash
sed '/^20[0-9][0-9]/Q' $1 > /tmp/j
tail -n +2 $1 | hledger -f- print -x -I >> /tmp/j
mv $1{,.bak}
mv /tmp/j $1
mhansen commented 1 year ago

What's wrong with piping through hledger print (and handling inter transaction content separately) ?

I suppose I'd want to be very careful not to lose data. So I'd want to be very confident that I'm not going to lose data before releasing anything like this.

I just tried hledger print on a file, and it complained about a balance assertion, which I could silence with -I. So that's one problem fixed.

I assumed hledger print wouldn't show comments inside transactions, or comments at the end of a transaction line. But it appears it does?

Let's try a test for a few language features:

$ cat test.hledger
; Comment
alias X = Y
commodity 1000.00 AUD
include /dev/null

; Comment after newline
2021-01-01 Description ; end of line comment
  ; Inside transaction comment
  Expenses:Post  200 AUD = 300 AUD ; End of post comment
  Assets:Bank  ; end of bank comment
  ; End of transaction comment
; Post transaction comment

Let's see how much it preserves:

$ hledger print -f test.hledger -I
2021-01-01 Description  ; end of line comment
    ; Inside transaction comment
    Expenses:Post      200.00 AUD = 300 AUD  ; End of post comment
    Assets:Bank                    ; end of bank comment
    ; End of transaction comment

It's good that some of the comments are preserved! But statements for alias, commodity, include, and inter-transaction comments have all gone missing. And I imagine the include will pull other files in during autoformatting. There's probably a bunch of other more esoteric language features that I don't use (timedot?) that I'd want to be confident in handling correctly before offering an autoformatter. I don't really feel confident in building a parser that will mirror what hledger does precisely (and as hledger changes in future too).

I believe these autoformatters best live alongside the canonical parser: that is, in hledger core, so that the autoformatter parser evolves along with the autoformatter (cf gofmt, rustfmt, clang-format).

the-solipsist commented 1 year ago

Ah. I misunderstood what @simonmichael meant. I mistakenly thought his question was aimed at me, rather than at @mhansen.

The things that hledger print (currently) doesn't handle are clearly defined:

  1. Directives (like alias, commodity, include, account, etc.)
  2. Inter-transaction comments

So intra-transaction comments (those attached to transactions and postings) are handled just fine. What aren't handled are inter-transaction comments.

And I imagine the include will pull other files in during autoformatting

Yes, hledger print respects include directives. That's why in my short script, I omit the include and default directives (which are in the top 2 lines of my file) before passing it on to hledger: tail -n +2 $1 | hledger -f- print -x -I.