robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
710431c696
master
1" Vim completion script 2" Language: beancount 3" Maintainer: Nathan Grigg 4" Latest Revision: 2021-03-06 5 6let s: using_python3 = has ( 'python3' ) || has ( 'python3/dyn' ) 7 8" Equivalent to python's startswith 9" Matches based on user's ignorecase preference 10function ! s: startswith ( string , prefix ) abort 11return strpart ( a: string , 0 , strlen ( a: prefix )) == a: prefix 12endfunction 13 14function ! s: count_expression ( text , expression ) abort 15return len ( split ( a: text , a: expression , 1 )) - 1 16endfunction 17 18function ! s: sort_accounts_by_depth ( name1 , name2 ) abort 19let l: depth1 = s: count_expression ( a: name1 , ':' ) 20let l: depth2 = s: count_expression ( a: name2 , ':' ) 21return l: depth1 == l: depth2 ? 0 : l: depth1 > l: depth2 ? 1 : -1 22endfunction 23 24let s: directives = [ 'open' , 'close' , 'commodity' , 'txn' , 'balance' , 'pad' , 'note' , 'document' , 'price' , 'event' , 'query' , 'custom' ] 25 26" ------------------------------ 27" Completion functions 28" ------------------------------ 29function ! beancountcomplete#complete ( findstart , base ) abort 30if a: findstart 31let l: col = searchpos ( '\s' , 'bn' , line ( '.' ))[ 1 ] 32if l: col == 0 33return -1 34else 35return l: col 36endif 37endif 38 39let l: partial_line = strpart ( getline ( '.' ), 0 , getpos ( '.' )[ 2 ] - 1 ) 40" Match directive types 41if l: partial_line =~# '^\d\d\d\d\(-\|/\)\d\d\1\d\d $' 42return beancountcomplete#complete_basic ( s: directives , a: base , '' ) 43endif 44 45" If we are using python3, now is a good time to load everything 46call beancountcomplete#load_everything () 47 48" Split out the first character (for cases where we don't want to match the 49" leading character: ", #, etc) 50let l: first = strpart ( a: base , 0 , 1 ) 51let l: rest = strpart ( a: base , 1 ) 52 53if l: partial_line =~# '^\d\d\d\d\(-\|/\)\d\d\1\d\d event $' && l: first ==# '"' 54return beancountcomplete#complete_basic ( b: beancount_events , l: rest , '"' ) 55endif 56 57let l: two_tokens = searchpos ( '\S\+\s' , 'bn' , line ( '.' ))[ 1 ] 58let l: prev_token = strpart ( getline ( '.' ), l: two_tokens , getpos ( '.' )[ 2 ] - l: two_tokens ) 59" Match curriences if previous token is number 60if l: prev_token =~# '^\d\+\([\.,]\d\+\)*' 61call beancountcomplete#load_currencies () 62return beancountcomplete#complete_basic ( b: beancount_currencies , a: base , '' ) 63endif 64 65if l: first ==# '#' 66call beancountcomplete#load_tags () 67return beancountcomplete#complete_basic ( b: beancount_tags , l: rest , '#' ) 68elseif l: first ==# '^' 69call beancountcomplete#load_links () 70return beancountcomplete#complete_basic ( b: beancount_links , l: rest , '^' ) 71elseif l: first ==# '"' 72call beancountcomplete#load_payees () 73return beancountcomplete#complete_basic ( b: beancount_payees , l: rest , '"' ) 74else 75call beancountcomplete#load_accounts () 76return beancountcomplete#complete_account ( a: base ) 77endif 78endfunction 79 80function ! beancountcomplete#get_root () abort 81if exists ( 'b:beancount_root' ) 82return b: beancount_root 83endif 84return expand ( '%' ) 85endfunction 86 87function ! beancountcomplete#load_everything () abort 88if s: using_python3 && ! exists ( 'b:beancount_loaded' ) 89let l: root = beancountcomplete#get_root () 90python 3 << EOF 91import vim 92from beancount import loader 93from beancount.core import data 94 95accounts = set() 96currencies = set() 97events = set() 98links = set() 99payees = set() 100tags = set() 101 102entries , errors, options_map = loader.load_file(vim.eval('l:root')) 103for index, entry in enumerate(entries): 104if isinstance ( entry , data . Open ): 105accounts .add(entry.account) 106if entry . currencies : 107currencies .update(entry.currencies) 108elif isinstance(entry, data.Commodity): 109currencies .add(entry.currency) 110elif isinstance(entry, data.Event): 111events .add(entry.type) 112elif isinstance(entry, data.Transaction): 113if entry . tags : 114tags .update(entry.tags) 115if entry . links : 116links .update(entry.links) 117if entry . payee : 118payees .add(entry.payee) 119 120vim .bindeval('b:')['beancount_accounts'] = sorted(accounts) 121vim .bindeval('b:')['beancount_currencies'] = sorted(currencies) 122vim .bindeval('b:')['beancount_events'] = sorted(events) 123vim .bindeval('b:')['beancount_links'] = sorted(links) 124vim .bindeval('b:')['beancount_payees'] = sorted(payees) 125vim .bindeval('b:')['beancount_tags'] = sorted(tags) 126vim .bindeval('b:')['beancount_loaded'] = 1 127EOF 128endif 129endfunction 130 131function ! beancountcomplete#load_accounts () abort 132if ! s: using_python3 && ! exists ( 'b:beancount_accounts' ) 133let l: root = beancountcomplete#get_root () 134let b: beancount_accounts = beancountcomplete#query_single ( l: root , 'select distinct account;' ) 135endif 136endfunction 137 138function ! beancountcomplete#load_tags () abort 139if ! s: using_python3 && ! exists ( 'b:beancount_tags' ) 140let l: root = beancountcomplete#get_root () 141let b: beancount_tags = beancountcomplete#query_single ( l: root , 'select distinct tags;' ) 142endif 143endfunction 144 145function ! beancountcomplete#load_links () abort 146if ! s: using_python3 && ! exists ( 'b:beancount_links' ) 147let l: root = beancountcomplete#get_root () 148let b: beancount_links = beancountcomplete#query_single ( l: root , 'select distinct links;' ) 149endif 150endfunction 151 152function ! beancountcomplete#load_currencies () abort 153if ! s: using_python3 && ! exists ( 'b:beancount_currencies' ) 154let l: root = beancountcomplete#get_root () 155let b: beancount_currencies = beancountcomplete#query_single ( l: root , 'select distinct currency;' ) 156endif 157endfunction 158 159function ! beancountcomplete#load_payees () abort 160if ! s: using_python3 && ! exists ( 'b:beancount_payees' ) 161let l: root = beancountcomplete#get_root () 162let b: beancount_payees = beancountcomplete#query_single ( l: root , 'select distinct payee;' ) 163endif 164endfunction 165 166" General completion function 167function ! beancountcomplete#complete_basic ( input , base , prefix ) abort 168let l: matches = filter ( copy ( a: input ), 's:startswith(v:val, a:base)' ) 169 170return map ( l: matches , 'a:prefix . v:val' ) 171endfunction 172 173" Complete account name. 174function ! beancountcomplete#complete_account ( base ) abort 175if g: beancount_account_completion ==? 'chunks' 176let l: pattern = '^\V' . substitute ( a: base , ':' , '\\[^:]\\*:' , 'g' ) . '\[^:]\*' 177else 178let l: pattern = '^\V\.\*' . substitute ( a: base , ':' , '\\.\\*:\\.\\*' , 'g' ) . '\.\*' 179endif 180 181let l: matches = [] 182let l: index = -1 183while 1 184let l: index = match ( b: beancount_accounts , l: pattern , l: index + 1 ) 185if l: index == -1 |break |endif 186call add ( l: matches , matchstr ( b: beancount_accounts [ l: index ], l: pattern )) 187endwhile 188 189if g: beancount_detailed_first 190let l: matches = reverse ( sort ( l: matches , 's:sort_accounts_by_depth' )) 191endif 192 193return l: matches 194endfunction 195 196function ! beancountcomplete#query_single ( root_file , query ) abort 197if s: using_python3 198python 3 << EOF 199import vim 200import subprocess 201 202#We intentionally want to ignore stderr so it doesn't mess up our query processing 203output = subprocess.check_output( 204 ['bean-query', vim.eval ('a:root_file'), vim.eval ('a:query')], 205stderr =subprocess.DEVNULL, 206text =True, 207) .splitlines() 208output = output[2:] 209result _list = sorted(y for y in (x.strip() for x in output) if y) 210EOF 211return py3eval ( 'result_list' ) 212else 213return [] 214endif 215endfunction