From 0577ae89ddbb04512c6d621afc3f574eec0383d0 Mon Sep 17 00:00:00 2001 From: Jason Chu Date: Mon, 7 Nov 2016 05:40:19 +0000 Subject: Remove errant print statement --- autoload/beancount.vim | 1 - 1 file changed, 1 deletion(-) (limited to 'autoload') diff --git a/autoload/beancount.vim b/autoload/beancount.vim index ecd423b..50a1b33 100644 --- a/autoload/beancount.vim +++ b/autoload/beancount.vim @@ -174,7 +174,6 @@ import os # We intentionally want to ignore stderr so it doesn't mess up our query processing output = subprocess.check_output(['bean-query', vim.eval('a:root_file'), vim.eval('a:query')], stderr=open(os.devnull, 'w')).split('\n') -print(output) output = output[2:] result_list = [y for y in (x.strip() for x in output) if y] -- cgit v1.2.3-2-gb3c3 From 1935bd1a60416da97b0f4a2e39f327496baa7421 Mon Sep 17 00:00:00 2001 From: Jason Chu Date: Mon, 7 Nov 2016 05:40:33 +0000 Subject: Add support for python3 Using beancount in process instead of shelling out to bean-query. --- autoload/beancount.vim | 61 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) (limited to 'autoload') diff --git a/autoload/beancount.vim b/autoload/beancount.vim index 50a1b33..aea0a42 100644 --- a/autoload/beancount.vim +++ b/autoload/beancount.vim @@ -1,3 +1,12 @@ +function! s:UsingPython3() + if has('python3') + return 1 + endif + return 0 +endfunction + +let s:using_python3 = s:UsingPython3() + " Equivalent to python's startswith " Matches based on user's ignorecase preference function! s:startswith(string, prefix) @@ -69,6 +78,9 @@ function! beancount#complete(findstart, base) return beancount#complete_basic(s:directives, a:base, '') endif + " If we are using python3, now is a good time to load everything + call beancount#load_everything() + let l:two_tokens = searchpos('\S\+\s', "bn", line("."))[1] let l:prev_token = strpart(getline("."), l:two_tokens, getpos(".")[2] - l:two_tokens) " Match curriences if previous token is number @@ -101,36 +113,75 @@ function! s:get_root() return expand('%') endfunction +function! beancount#load_everything() + if s:using_python3 + let l:root = s:get_root() +python3 << EOF +import vim +from beancount import loader +from beancount.core import data + +accounts = set() +currencies = set() +links = set() +payees = set() +tags = set() + +entries, errors, options_map = loader.load_file(vim.eval('l:root')) +for index, entry in enumerate(entries): + if isinstance(entry, data.Open): + accounts.add(entry.account) + if entry.currencies: + currencies.update(entry.currencies) + elif isinstance(entry, data.Commodity): + currencies.add(entry.currency) + elif isinstance(entry, data.Transaction): + if entry.tags: + tags.update(entry.tags) + if entry.links: + links.update(entry.links) + if entry.payee: + payees.add(entry.payee) + +vim.command('let b:beancount_accounts = [{}]'.format(','.join(repr(x) for x in sorted(accounts)))) +vim.command('let b:beancount_currencies = [{}]'.format(','.join(repr(x) for x in sorted(currencies)))) +vim.command('let b:beancount_links = [{}]'.format(','.join(repr(x) for x in sorted(links)))) +vim.command('let b:beancount_payees = [{}]'.format(','.join(repr(x) for x in sorted(payees)))) +vim.command('let b:beancount_tags = [{}]'.format(','.join(repr(x) for x in sorted(tags)))) +EOF + endif +endfunction + function! beancount#load_accounts() - if !exists('b:beancount_accounts') + if !s:using_python3 && !exists('b:beancount_accounts') let l:root = s:get_root() let b:beancount_accounts = beancount#find_accounts(l:root) endif endfunction function! beancount#load_tags() - if !exists('b:beancount_tags') + if !s:using_python3 && !exists('b:beancount_tags') let l:root = s:get_root() let b:beancount_tags = beancount#find_tags(l:root) endif endfunction function! beancount#load_links() - if !exists('b:beancount_links') + if !s:using_python3 && !exists('b:beancount_links') let l:root = s:get_root() let b:beancount_links = beancount#find_links(l:root) endif endfunction function! beancount#load_currencies() - if !exists('b:beancount_currencies') + if !s:using_python3 && !exists('b:beancount_currencies') let l:root = s:get_root() let b:beancount_currencies = beancount#find_currencies(l:root) endif endfunction function! beancount#load_payees() - if !exists('b:beancount_payees') + if !s:using_python3 && !exists('b:beancount_payees') let l:root = s:get_root() let b:beancount_payees = beancount#find_payees(l:root) endif -- cgit v1.2.3-2-gb3c3 From 8b48dce57cd6715da4c9890a88a222c948887803 Mon Sep 17 00:00:00 2001 From: Jason Chu Date: Mon, 7 Nov 2016 06:21:37 +0000 Subject: Fix off-by-one error in initial line matching --- autoload/beancount.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'autoload') diff --git a/autoload/beancount.vim b/autoload/beancount.vim index aea0a42..1f067b5 100644 --- a/autoload/beancount.vim +++ b/autoload/beancount.vim @@ -72,9 +72,9 @@ function! beancount#complete(findstart, base) endif endif - let l:partial_line = strpart(getline("."), 0, getpos(".")[2]) + let l:partial_line = strpart(getline("."), 0, getpos(".")[2]-1) " Match directive types - if l:partial_line =~# '^\d\d\d\d\(-\|/\)\d\d\1\d\d \S*$' + if l:partial_line =~# '^\d\d\d\d\(-\|/\)\d\d\1\d\d $' return beancount#complete_basic(s:directives, a:base, '') endif -- cgit v1.2.3-2-gb3c3 From 60b5600e0d71d82aa1b1736b14167fc77482da02 Mon Sep 17 00:00:00 2001 From: Jason Chu Date: Mon, 7 Nov 2016 06:25:40 +0000 Subject: Add support for completing event types Only works in python3 because bean-query doesn't expose event types --- autoload/beancount.vim | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'autoload') diff --git a/autoload/beancount.vim b/autoload/beancount.vim index 1f067b5..3f3b930 100644 --- a/autoload/beancount.vim +++ b/autoload/beancount.vim @@ -81,6 +81,15 @@ function! beancount#complete(findstart, base) " If we are using python3, now is a good time to load everything call beancount#load_everything() + " Split out the first character (for cases where we don't want to match the + " leading character: ", #, etc) + let l:first = strpart(a:base, 0, 1) + let l:rest = strpart(a:base, 1) + + if l:partial_line =~# '^\d\d\d\d\(-\|/\)\d\d\1\d\d event $' && l:first == '"' + return beancount#complete_basic(b:beancount_events, l:rest, '"') + endif + let l:two_tokens = searchpos('\S\+\s', "bn", line("."))[1] let l:prev_token = strpart(getline("."), l:two_tokens, getpos(".")[2] - l:two_tokens) " Match curriences if previous token is number @@ -89,8 +98,6 @@ function! beancount#complete(findstart, base) return beancount#complete_basic(b:beancount_currencies, a:base, '') endif - let l:first = strpart(a:base, 0, 1) - let l:rest = strpart(a:base, 1) if l:first == "#" call beancount#load_tags() return beancount#complete_basic(b:beancount_tags, l:rest, '#') @@ -123,6 +130,7 @@ from beancount.core import data accounts = set() currencies = set() +events = set() links = set() payees = set() tags = set() @@ -135,6 +143,8 @@ for index, entry in enumerate(entries): currencies.update(entry.currencies) elif isinstance(entry, data.Commodity): currencies.add(entry.currency) + elif isinstance(entry, data.Event): + events.add(entry.type) elif isinstance(entry, data.Transaction): if entry.tags: tags.update(entry.tags) @@ -145,6 +155,7 @@ for index, entry in enumerate(entries): vim.command('let b:beancount_accounts = [{}]'.format(','.join(repr(x) for x in sorted(accounts)))) vim.command('let b:beancount_currencies = [{}]'.format(','.join(repr(x) for x in sorted(currencies)))) +vim.command('let b:beancount_events = [{}]'.format(','.join(repr(x) for x in sorted(events)))) vim.command('let b:beancount_links = [{}]'.format(','.join(repr(x) for x in sorted(links)))) vim.command('let b:beancount_payees = [{}]'.format(','.join(repr(x) for x in sorted(payees)))) vim.command('let b:beancount_tags = [{}]'.format(','.join(repr(x) for x in sorted(tags)))) -- cgit v1.2.3-2-gb3c3 From 2023c8bab6d345f9c4219cf6bd2fb1442f797763 Mon Sep 17 00:00:00 2001 From: Jason Chu Date: Sun, 13 Nov 2016 22:20:21 +0000 Subject: Add caching back to python3 autocomplete implementation --- autoload/beancount.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'autoload') diff --git a/autoload/beancount.vim b/autoload/beancount.vim index 3f3b930..9d18252 100644 --- a/autoload/beancount.vim +++ b/autoload/beancount.vim @@ -121,7 +121,7 @@ function! s:get_root() endfunction function! beancount#load_everything() - if s:using_python3 + if s:using_python3 && !exists('b:beancount_loaded') let l:root = s:get_root() python3 << EOF import vim @@ -159,6 +159,7 @@ vim.command('let b:beancount_events = [{}]'.format(','.join(repr(x) for x in sor vim.command('let b:beancount_links = [{}]'.format(','.join(repr(x) for x in sorted(links)))) vim.command('let b:beancount_payees = [{}]'.format(','.join(repr(x) for x in sorted(payees)))) vim.command('let b:beancount_tags = [{}]'.format(','.join(repr(x) for x in sorted(tags)))) +vim.command('let b:beancount_loaded = v:true'.format(','.join(repr(x) for x in sorted(tags)))) EOF endif endfunction -- cgit v1.2.3-2-gb3c3