Skip to content

Commit 5a41452

Browse files
committed
allow completion from declared accounts
When using ledger with the `--strict` option, you need to declare all your used accounts. These declarations can additionally be used to complete account names. ledger#declared_accounts() works a lot like ledger#transactions(), on which it is based. s:collect_completion_data() can then take the list of declared accounts as an initial value to which it then adds the accounts used in transactions. Signed-off-by: Roland Hieber <[email protected]>
1 parent 1c3551b commit 5a41452

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

autoload/ledger.vim

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,50 @@ endf "}}}
267267

268268
" == helper functions ==
269269

270+
" get a list of declared accounts in the buffer
271+
function! ledger#declared_accounts(...)
272+
if a:0 == 2
273+
let lnum = a:1
274+
let lend = a:2
275+
elseif a:0 == 0
276+
let lnum = 1
277+
let lend = line('$')
278+
else
279+
throw "wrong number of arguments for ledger#declared_accounts()"
280+
return []
281+
endif
282+
283+
" save view / position
284+
let view = winsaveview()
285+
let fe = &foldenable
286+
set nofoldenable
287+
288+
let accounts = []
289+
call cursor(lnum, 0)
290+
while 1
291+
let lnum = search('^account\s', 'cW', lend)
292+
if !lnum || lnum > lend
293+
break
294+
endif
295+
296+
" remove comments at the end and "account" at the front
297+
let line = split(getline(lnum), '\s\+;')[0]
298+
let line = matchlist(line, 'account\s\+\(.\+\)')[1]
299+
300+
if len(line) > 1
301+
call add(accounts, line)
302+
endif
303+
304+
call cursor(lnum+1,0)
305+
endw
306+
307+
" restore view / position
308+
let &foldenable = fe
309+
call winrestview(view)
310+
311+
return accounts
312+
endf
313+
270314
function! s:get_transaction_extents(lnum)
271315
if ! (indent(a:lnum) || getline(a:lnum) =~ '^[~=[:digit:]]')
272316
" only do something if lnum is in a transaction

ftplugin/ledger.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ unlet s:old s:new s:fun
330330
function! s:collect_completion_data() "{{{1
331331
let transactions = ledger#transactions()
332332
let cache = {'descriptions': [], 'tags': {}, 'accounts': {}}
333-
let accounts = []
333+
let accounts = ledger#declared_accounts()
334334
for xact in transactions
335335
" collect descriptions
336336
if has_key(xact, 'description') && index(cache.descriptions, xact['description']) < 0

0 commit comments

Comments
 (0)