ledger / vim-ledger

Vim plugin for Ledger
GNU General Public License v2.0
370 stars 56 forks source link

Aligning all lines #113

Closed sudoforge closed 3 years ago

sudoforge commented 3 years ago

LedgerAlign() currently takes an optional range of lines, defaulting to the current line. From a usability perspective, I think it would be useful to default to align all lines unless a range is given.

sudoforge commented 3 years ago

In #114, I add a new command called LedgerAlignAll which invokes ledger#align_all_comodity().

alerque commented 3 years ago

LedgerAlign() currently takes an optional range of lines, defaulting to the current line. From a usability perspective, I think it would be useful to default to align all lines unless a range is given.

This is how almost all VIM commands whether bultin or from Plugins work: they operate on the smallest logical bit (at cursor for characterwise actions, current word for wordwise, current line for linewise, etc.). As a simple example, search and replace is a linewise action and defaults to the current line. You can specify a larger range, but it never defaults to the whole file.

You're welcome to setup a binding or alias that runs a linewise command over a whole file, but I am not going to approve or LedgerAlign() becoming a buffer wide command, it is linewise and can take arguments to cover a larger range.

alerque commented 3 years ago

Sorry I hadn't seen your comment in the PR yet ... but yes you were right to conclude that would break existing workflow(s) — and I don't think there is any justification for that. The alternative you actually submitted there is probably workable instead.

sudoforge commented 3 years ago

Yeah, the desire for this functionality comes about due to the "autoformat" approach taken by different tools such as go fmt, terraform fmt, black, etc; whose integration into vim typically includes a format-the-entire-buffer command. In this vein, it would perhaps be more idiomatic to contribute a full fledged formatter to the CLI project; however, I've been using a solution similar to what I submitted locally for a bit and decided it may be useful for others.

I'll take a look at your suggestions on the PR itself and we can go from there.

alerque commented 3 years ago

Just for reference in case anybody runs across this discussion while trying to sort out their own preferred workflow, here is some info on the most common ways I use this are alignment stuff.

First, I have a VIM keymap that aligns a paragraph (i.e. transaction):

" Binding to align the current transaction
autocmd FileType ledger noremap <buffer> <Leader>h vip:LedgerAlign<Cr>

" Actually I use this, which first normalizes the transaction formatting with `hledger print`, then aligns it with this plugin
autocmd FileType ledger noremap <buffer> <Leader>h vip:!hledger -f- print -x<Cr> <bar> vip:LedgerAlign<Cr>

And perhaps even more importantly, all my projects hame Makefiles with normalize targets using a combination of tools including hledger print but also some sort and merge tooling that cleans up postings, some sed scripting, per project stuff, aliases, and of course this plugin to align the result. Here is a simplified overview of such a normalize step:

LEDGERS = target.ledger

.PHONY: normalize
normalize: REBUILD=force
normalize: $(LEDGERS)

$(LEDGERS): %.ledger: $(REBUILD)
  hledger -f <(cat aliases.ledger; sortandmergepostings.awk $@) print |\
    sed -e '' |\
    sponge $@
  nvim -n --headless $@ "+%call ledger#align_commodity()" "+x"

.PHONY: force
force:;

Note this only works because I don't keep anything in these ledger files that isn't output by print. No extra comments, no aliases, nothing out of order, etc. I keep other ledgers that are not normalized in this way with such data.

alerque commented 3 years ago

And yes, having the CLI do the formatting would be a much better solution. I'm just not as happy with hledger's output as I am with the alignment from this plugin.

See also: https://github.com/simonmichael/hledger/issues/1045

Ideally both ledger and hledger would have "format" modes that just cleanup and normalize content without things like loosing comment placements, but neither of them are currently well suited to this.