ledger/vim-ledger

comma in tag value should not change colour

mvhulten opened this issue · 4 comments

When I have a comment like this:

; price: 123,45 EUR

the ,45 EUR part gets highlighted differently.

I think the comma should not do that.

While I can understand your concern (I live in a country that uses comma as a decimal separator by default and have many currencies in my ledgers), but hledger made the choice to make commas a valid way to include multiple tags:

https://hledger.org/tags-tutorial.html#multiple-tags

I think the highlighting is doing what hledger is doing under the hood and showing you how it will be parsed. Based on the docs I don't think your price tag is going to be recorded properly.

Here is a proof of concept showing HLedger is not parsing your input the way your brain is and the syntax highlighting is correctly pointing out the error. Given this input as foo.ledger:

2023-03-03 foo
    Period  1  ; price: 1.1
    Comma   2  ; price: 2,2
    Out

this command shows that the price tag value for price does not include anything after the comma:

$ hledger -f foo.ledger bal --pivot price
                  -3
                   1  1.1
                   2  2
--------------------
                   0

I'd like to suggest that having a price: tag in the first place is probably a mistake. If you are concerned about recording unit prices, consider that there is a built in mechanism for that.

Of course your use case may not be for prices at all and that was just a bad choice of examples. If there is a use case that isn't afforded here feel free to comment. We can even ask upstream about how this could be parsed.

Though I have been using HLedger, I had not yet used --pivot, so I did not know about it. Thank you for the explanation.

One option is that HLedger would demand a space after the comma: , . However, I could imagine there are journals that follow the written rule "each tag separated by a comma", so forget that idea.

I was using lot prices. I have a @@ TOTALPRICE and wanted to have an additional unit price and thought ; price: UNITPRICE would be good. It seems redundant, but what is calculated (TOTALPRICE from UNITPRICE or vice versa) sometimes deviates slightly (because of rounding) from what is reported by the exchange.

If you are dealing with lot prices either use QTY @@ TOTALPRICE or QTY @ UNITPRICE. From there HLedger can derive the total or unit prices and there are special queries for that. Qty and price can have any commodity of course, and they don't have to be the same commodity.

If you're dealing with FX exchanges, then you can also just two entries, one in each currency.

2023-03-03 foo
    Assets:Cash    ₺18,67
    Assets:Cash    -$1.00

I'm going to close the issue for now but would be happy to re-evaluate if hledger has (or changes) any behavior we're missing. As it stands I think we're parsing tags correctly and the syntax highlighting is correctly pointing out what is being parsed (whether that is ideal for your use case or not).