aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Grigg2015-04-15 16:01:07 -0700
committerNathan Grigg2015-04-15 16:01:07 -0700
commitbf312bc7c1aec0344da46fa660d0e6e7ba68dcf5 (patch)
tree0e7963685b84fd117ed0e597e12138ac9c10395a
parentf4c6ce93598bcc1789a206b899be35b2b7a5194d (diff)
downloadvim-beancount-bf312bc7c1aec0344da46fa660d0e6e7ba68dcf5.tar.gz
Improve indent algorithm
The new algorithm relies less on returning '-1'. It also supports metadata
-rw-r--r--indent/beancount.vim31
1 files changed, 25 insertions, 6 deletions
diff --git a/indent/beancount.vim b/indent/beancount.vim
index 686cbdc..29d7d9e 100644
--- a/indent/beancount.vim
+++ b/indent/beancount.vim
@@ -9,16 +9,35 @@ if exists("*GetBeancountIndent")
finish
endif
+function! s:IsDirective(str)
+ return a:str =~ '\v^\s*(\d{4}-\d{2}-\d{2}|pushtag|poptag|option|plugin|include)'
+endfunction
+
+function! s:IsPost(str)
+ return a:str =~ '\v^\s*(Assets|Liabilities|Expenses|Equity|Income):'
+endfunction
+
+function! s:IsMetadata(str)
+ return a:str =~ '\v^\s*\w+:\s'
+endfunction
+
+function! s:IsTransaction(str)
+ return a:str =~ '\v^\s*\d{4}-\d{2}-\d{2}\s+(txn\s+)?\S\s'
+endfunction
+
function GetBeancountIndent(line_num)
let this_line = getline(a:line_num)
let prev_line = getline(a:line_num - 1)
+ " Don't touch comments
+ if this_line =~ '\v^\s*;' | return -1 | endif
" This is a new directive or previous line is blank.
- if this_line =~ '\v^\s*\d{4}-\d{2}-\d{2}' || prev_line =~ '^\s*$'
- return 0
- endif
- " Previous line is the beginning of a transaction.
- if prev_line =~ '\v^\s*\d{4}-\d{2}-\d{2}\s+(txn\s+)?.\s+'
- return &shiftwidth
+ if prev_line =~ '^\s*$' || s:IsDirective(this_line) | return 0 | endif
+ " Previous line is transaction or this is a posting.
+ if s:IsTransaction(prev_line) || s:IsPost(this_line) | return &sw | endif
+ if s:IsMetadata(this_line)
+ let this_indent = indent(a:line_num - 1)
+ if ! s:IsMetadata(prev_line) | let this_indent += &sw | endif
+ return this_indent
endif
return -1
endfunction