Skip to content

Commit 7d8f3a3

Browse files
committed
feat: Reuse data collection options for command line completions
Closes #85
1 parent 7fd0d8f commit 7d8f3a3

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

ftplugin/ledger.vim

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -241,26 +241,16 @@ endfunction
241241
function! s:collect_completion_data()
242242
let transactions = ledger#transactions()
243243
let cache = {'descriptions': [], 'tags': {}, 'accounts': {}, 'flat_accounts': []}
244-
if b:ledger_bin !=# v:false
245-
let accounts = split(system(b:ledger_accounts_cmd), '\n')
246-
else
247-
let accounts = ledger#declared_accounts()
248-
endif
244+
245+
let accounts = s:get_accounts_list()
249246
let cache.flat_accounts = accounts
250-
if b:ledger_bin !=# v:false
251-
let cache.descriptions = split(system(b:ledger_descriptions_cmd), '\n')
252-
endif
247+
let cache.descriptions = s:get_descriptions_list()
248+
253249
for xact in transactions
254-
if b:ledger_bin ==# v:false
255-
" collect descriptions
256-
if has_key(xact, 'description') && index(cache.descriptions, xact['description']) < 0
257-
call add(cache.descriptions, xact['description'])
258-
endif
259-
endif
260250
let [t, postings] = xact.parse_body()
261251
let tagdicts = [t]
262252

263-
" collect account names
253+
" collect account names (only when not using ledger binary)
264254
if b:ledger_bin ==# v:false
265255
for posting in postings
266256
if has_key(posting, 'tags')
@@ -297,6 +287,29 @@ function! s:collect_completion_data()
297287
return cache
298288
endfunction
299289

290+
function! s:get_accounts_list()
291+
if b:ledger_bin !=# v:false
292+
return split(system(b:ledger_accounts_cmd), '\n')
293+
else
294+
return ledger#declared_accounts()
295+
endif
296+
endfunction
297+
298+
function! s:get_descriptions_list()
299+
if b:ledger_bin !=# v:false
300+
return split(system(b:ledger_descriptions_cmd), '\n')
301+
else
302+
let transactions = ledger#transactions()
303+
let descriptions = []
304+
for xact in transactions
305+
if has_key(xact, 'description') && index(descriptions, xact['description']) < 0
306+
call add(descriptions, xact['description'])
307+
endif
308+
endfor
309+
return descriptions
310+
endif
311+
endfunction
312+
300313
" Helper functions
301314

302315
" get # of visible/usable columns in current window
@@ -333,12 +346,16 @@ function! s:count_expression(text, expression)
333346
endfunction
334347

335348
function! s:autocomplete_account_or_payee(argLead, cmdLine, cursorPos)
336-
return (a:argLead =~# '^@') ?
337-
\ map(filter(split(system(b:ledger_bin . ' -f ' . shellescape(expand(b:ledger_main)) . ' payees'), '\n'),
338-
\ "v:val =~? '" . strpart(a:argLead, 1) . "' && v:val !~? '^Warning: '"), '"@" . escape(v:val, " ")')
339-
\ :
340-
\ map(filter(split(system(b:ledger_bin . ' -f ' . shellescape(expand(b:ledger_main)) . ' accounts'), '\n'),
341-
\ "v:val =~? '" . a:argLead . "' && v:val !~? '^Warning: '"), 'escape(v:val, " ")')
349+
if a:argLead =~# '^@'
350+
let payees = s:get_descriptions_list()
351+
let pattern = strpart(a:argLead, 1)
352+
return map(filter(payees, "v:val =~? '" . pattern . "' && v:val !~? '^Warning: '"),
353+
\ '"@" . escape(v:val, " ")')
354+
else
355+
let accounts = s:get_accounts_list()
356+
return map(filter(accounts, "v:val =~? '" . a:argLead . "' && v:val !~? '^Warning: '"),
357+
\ 'escape(v:val, " ")')
358+
endif
342359
endfunction
343360

344361
function! s:reconcile(file, account)

0 commit comments

Comments
 (0)