Skip to content

Commit 7b97c87

Browse files
authored
feat: Account name completion using matchfuzzy (#156)
1 parent da40d73 commit 7b97c87

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ Include the following let-statements somewhere in your `.vimrc` to modify the be
9191

9292
let g:ledger_detailed_first = 1
9393

94+
* If you want account completion based on fuzzy matching instead of the default sub-level completion, include the following line:
95+
96+
let g:ledger_fuzzy_account_completion = 1
97+
9498
* By default vim will fold ledger transactions, leaving surrounding blank lines unfolded.
9599
You can use `g:ledger_fold_blanks` to hide blank lines following a transaction.
96100

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

108112
### Accounts
109113

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

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

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

127+
If fuzzy matching based account completion is enabled, the matches are
128+
loaded based on string similarity and without regard for the sub-levels.
129+
130+
In the previous example, with fuzzy matching enabled, you could load up
131+
matches by doing something like:
132+
133+
Chec<C-X><C-O>
134+
135+
Notice that we did not need to write the initial account components.
136+
123137
When you want to complete on a virtual transaction, it's currently best to keep the cursor in front of the closing bracket.
124138
Of course you can insert the closing bracket after calling the completion, too.
125139

doc/ledger.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ behaviour of the ledger filetype.
325325

326326
let g:ledger_fillstring = ' -'
327327

328+
* If you want account completion based on fuzzy matching instead of the
329+
default sub-level completion, include the following line:
330+
331+
let g:ledger_fuzzy_account_completion = 1
332+
328333
* If you want the account completion to be sorted by level of detail/depth
329334
instead of alphabetical, include the following line:
330335

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

448453
### Accounts
449454

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

453458
Asse<C-X><C-O>
454459

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

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

468+
If fuzzy matching based account completion is enabled, the matches are
469+
loaded based on string similarity and without regard for the sub-levels.
470+
471+
In the previous example, with fuzzy matching enabled, you could load up
472+
matches by doing something like:
473+
474+
Chec<C-X><C-O>
475+
476+
Notice that we did not need to write the initial account components.
477+
463478
When you want to complete on a virtual transaction, it's currently best
464479
to keep the cursor in front of the closing bracket. Of course you can
465480
insert the closing bracket after calling the completion, too.

ftplugin/ledger.vim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ function! LedgerComplete(findstart, base) "{{{1
354354

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

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

404406
function! s:collect_completion_data() "{{{1
405407
let transactions = ledger#transactions()
406-
let cache = {'descriptions': [], 'tags': {}, 'accounts': {}}
408+
let cache = {'descriptions': [], 'tags': {}, 'accounts': {}, 'flat_accounts': []}
407409
if exists('g:ledger_accounts_cmd')
408410
let accounts = split(system(g:ledger_accounts_cmd), '\n')
409411
else
410412
let accounts = ledger#declared_accounts()
411413
endif
414+
let cache.flat_accounts = accounts
412415
if exists('g:ledger_descriptions_cmd')
413416
let cache.descriptions = split(system(g:ledger_descriptions_cmd), '\n')
414417
endif

0 commit comments

Comments
 (0)