Skip to content

Commit

Permalink
feat: Account name completion using matchfuzzy (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshayg committed May 7, 2024
1 parent da40d73 commit 7b97c87
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Include the following let-statements somewhere in your `.vimrc` to modify the be

let g:ledger_detailed_first = 1

* If you want account completion based on fuzzy matching instead of the default sub-level completion, include the following line:

let g:ledger_fuzzy_account_completion = 1

* By default vim will fold ledger transactions, leaving surrounding blank lines unfolded.
You can use `g:ledger_fold_blanks` to hide blank lines following a transaction.

Expand All @@ -107,7 +111,7 @@ Omni completion is implemented for transactions descriptions and posting account

### Accounts

Account names are matched by the start of every sub-level.
By default, account names are matched by the start of every sub-level.
When you insert an account name like this:

Asse<C-X><C-O>
Expand All @@ -120,6 +124,16 @@ Go ahead and try something like:

When you have an account like this, 'Assets:Bank:Checking' should show up.

If fuzzy matching based account completion is enabled, the matches are
loaded based on string similarity and without regard for the sub-levels.

In the previous example, with fuzzy matching enabled, you could load up
matches by doing something like:

Chec<C-X><C-O>

Notice that we did not need to write the initial account components.

When you want to complete on a virtual transaction, it's currently best to keep the cursor in front of the closing bracket.
Of course you can insert the closing bracket after calling the completion, too.

Expand Down
19 changes: 17 additions & 2 deletions doc/ledger.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ behaviour of the ledger filetype.

let g:ledger_fillstring = ' -'

* If you want account completion based on fuzzy matching instead of the
default sub-level completion, include the following line:

let g:ledger_fuzzy_account_completion = 1

* If you want the account completion to be sorted by level of detail/depth
instead of alphabetical, include the following line:

Expand Down Expand Up @@ -447,8 +452,8 @@ Omni completion is currently implemented for account names only.

### Accounts

Account names are matched by the start of every sub-level. When you
insert an account name like this:
By default, account names are matched by the start of every sub-level. When
you insert an account name like this:

Asse<C-X><C-O>

Expand All @@ -460,6 +465,16 @@ Go ahead and try something like:

When you have an account like this, 'Assets:Bank:Checking' should show up.

If fuzzy matching based account completion is enabled, the matches are
loaded based on string similarity and without regard for the sub-levels.

In the previous example, with fuzzy matching enabled, you could load up
matches by doing something like:

Chec<C-X><C-O>

Notice that we did not need to write the initial account components.

When you want to complete on a virtual transaction, it's currently best
to keep the cursor in front of the closing bracket. Of course you can
insert the closing bracket after calling the completion, too.
Expand Down
7 changes: 5 additions & 2 deletions ftplugin/ledger.vim
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ function! LedgerComplete(findstart, base) "{{{1

call map(results, 'v:val[0]')

if g:ledger_detailed_first
if get(g:, "ledger_fuzzy_account_completion", 0)
let results = matchfuzzy(b:compl_cache.flat_accounts, a:base, {'matchseq':1})
elseif g:ledger_detailed_first
let results = reverse(sort(results, 's:sort_accounts_by_depth'))
else
let results = sort(results)
Expand Down Expand Up @@ -403,12 +405,13 @@ unlet s:old s:new s:fun

function! s:collect_completion_data() "{{{1
let transactions = ledger#transactions()
let cache = {'descriptions': [], 'tags': {}, 'accounts': {}}
let cache = {'descriptions': [], 'tags': {}, 'accounts': {}, 'flat_accounts': []}
if exists('g:ledger_accounts_cmd')
let accounts = split(system(g:ledger_accounts_cmd), '\n')
else
let accounts = ledger#declared_accounts()
endif
let cache.flat_accounts = accounts
if exists('g:ledger_descriptions_cmd')
let cache.descriptions = split(system(g:ledger_descriptions_cmd), '\n')
endif
Expand Down

0 comments on commit 7b97c87

Please sign in to comment.