ledger/vim-ledger

Syntax Highlighting for hledger

jman223 opened this issue · 8 comments

I've been using vim-ledger with ledger for many years, and I'm used to my tags and posting amounts being highlighted (blue and purple, respectively).

I recently migrated to hledger and reformatted my journal files to the hledger standard and noticed that my tags are no longer highlighted as they are when I use ledger formatted tags.

I have added the following to my .vimrc and executing let b: in vim shows that current_syntax is set to hledger.

let g:ledger_is_hledger = 1

I've noticed that if I have a single tag with a space after the tag, it will highlight correctly, but subsequent tags are not highlighted.

How do I get all tags to highlight?

Also, is there a way to highlight the color for description (yellow)?

ledger entry

2022/01/01 * (3091) ABC Council Inc  ;:tag1:tag2:
       ;Anywhere 2021 HOA Taxes
       Expenses:Tax:HOA                                          $555.00
       Assets:Banking:Chase Checking                            -$555.00

hledger entry - tag highlight works

2022/01/01 * (3091) ABC Council Inc | Anywhere 2021 HOA Taxes  ; tag1:<space>
       Expenses:Tax:HOA                                          $555.00
       Assets:Banking:Chase Checking                            -$555.00

hledger entry - tag highlight does NOT work

2022/01/01 * (3091) ABC Council Inc | Anywhere 2021 HOA Taxes  ; tag1:, Tag2:
       Expenses:Tax:HOA                                          $555.00
       Assets:Banking:Chase Checking                            -$555.00

The highlighting for multiple tags on the same line does appear to be broken. Lets call that a bug. I may or may not get a chance to look into it quickly so if anybody wants to jump in with a PR I'd be happy to facilitate it.

For your other points, first of all the way color schemes work in VIM the syntax plugin (like this one) just map syntax features to named syntax groups. Then color schemes are responsible for assigning a specific color to that group. So this plugin doesn't make anything yellow or blue or red or anything else, it just picks some group (see :help highlight-groups). If you want to change a specific thing's specific color what you'll want to do is figure out what syntax highlighting group that thing is assigned to and add a color override to your profile for that group.

I don't know off hand whether the description (which in HLedger is actually a two fields, payee and note) is being assigned a group at all, but if it is not we can probably arrange for it to be so. Maybe open a new issue about that though so we can track it separately from the tag issue.

It just occurred to me why I've never bumped into this bug. I was surprised to read it because I use hledger myself and often times multiple tags. It turns out I've been using an alternate syntax that works but isn't the documented way to use multiple tags. My way has just been to chain multiple comments together:

2022/01/01 * (3091) ABC Council Inc | Anywhere 2021 HOA Taxes  ; tag1:val ; Tag2:val
	Expenses:Tax:HOA                                          $555.00
	Assets:Banking:Chase Checking                            -$555.00

Note this also seems to work for me because I almost always use values in connection with my tags. That appears to be another aspect to this bug because it handles tags with and without values differently.

image

For reference here is the ledger cli docs on metadata. Comparing that with the hledger docs it looks like the tag syntax highlighting rules will need to branch so that ledger and hledger are parsed differently. Right now even with b:is_hledger enabled rules that more closely match ledger-cli parsing are being applied which is quite different from how hledger reads them.

The syntax code for this plugin is a DISASTER! Wow what a mess. There are so much simpler ways to do most of what it does. Okay rant over.

I took an initial stab at fixing hledger tag parsing in #142. You can checkout that branch and see how it works for you. I don't know if it covers all edges right yet but it does catch tags and values in at least a few basic cases.

Thanks for working on this Caleb.

The changes you made mostly work. There are two small issues:

  • If the tag contains the letter s, that letter isn't highlighted, although the rest of the tag is (see attached image).
  • The Identifier syntax group appears as normal text. I changed ledgerTag to Type to get around this issue. Based on your earlier note, I assume this is an issue with my Nord theme.

Fri Feb  3 10:56:18 AM CST 2023

The following change to line 91 fixed the the "s" issue:

Original
syn match ledgerTag /[^\s,:]\+/ contained nextgroup=ledgerTagDef

Modified
syn match ledgerTag /[^\,:]\+/ contained nextgroup=ledgerTagDef

I see what you mean about s matching wrong, but that was supposed to be \s the character class of spaces, not just \. Apparently character classes can't be nested in ranges though, hence why that wasn't working. I used a different range notation.

As for your colors, yes your color scheme is missing maps for Identifier. I wasn't sure that is a good match anyway, so I tried a different mapping for tags & values (to Type and Label respectively). See how that treats you.

See the updated PR.

It still isn't finished because it doesn't take into account quoted values or other edge cases, but it seems like a better start on hledger tag handling than before at least.

Latest changes are working well. Thanks again.