Skip to content

Commit babb13c

Browse files
authored
Merge pull request #59 from rohieb/completion-from-account-stmts
Allow completion from declared accounts
2 parents 0068e6f + 5a41452 commit babb13c

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
@@ -333,7 +333,7 @@ unlet s:old s:new s:fun
333333
function! s:collect_completion_data() "{{{1
334334
let transactions = ledger#transactions()
335335
let cache = {'descriptions': [], 'tags': {}, 'accounts': {}}
336-
let accounts = []
336+
let accounts = ledger#declared_accounts()
337337
for xact in transactions
338338
" collect descriptions
339339
if has_key(xact, 'description') && index(cache.descriptions, xact['description']) < 0

0 commit comments

Comments
 (0)