robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #40228 from zeertzjq/vim-ff7fcb8710431c696

master
1.4 KiB51 linesraw
1" Vim indent file
2" Language: beancount
3" Maintainer: Nathan Grigg
4" Latest Revision: 2017-03-20
5
6if exists('b:did_indent')
7    finish
8endif
9let b:did_indent = 1
10
11setlocal indentexpr=GetBeancountIndent(v:lnum)
12
13let b:undo_indent = "setl inde<"
14
15if exists('*GetBeancountIndent')
16    finish
17endif
18
19function! s:IsDirective(str)
20    return a:str =~# '\v^\s*(\d{4}-\d{2}-\d{2}|pushtag|poptag|option|plugin|include)'
21endfunction
22
23function! s:IsPosting(str)
24    return a:str =~# '\v^\s*[A-Z]\w+:'
25endfunction
26
27function! s:IsMetadata(str)
28    return a:str =~# '\v^\s*[a-z][a-zA-Z0-9\-_]+:'
29endfunction
30
31function! s:IsTransaction(str)
32    " The final \S represents the flag (e.g. * or !).
33    return a:str =~# '\v^\s*\d{4}-\d{2}-\d{2}\s+(txn\s+)?\S(\s|$)'
34endfunction
35
36function GetBeancountIndent(line_num)
37    let l:this_line = getline(a:line_num)
38    let l:prev_line = getline(a:line_num - 1)
39    " Don't touch comments
40    if l:this_line =~# '\v^\s*;' | return -1 | endif
41    " This is a new directive or previous line is blank.
42    if l:prev_line =~# '^\s*$' || s:IsDirective(l:this_line) | return 0 | endif
43    " Previous line is transaction or this is a posting.
44    if s:IsTransaction(l:prev_line) || s:IsPosting(l:this_line) | return &shiftwidth | endif
45    if s:IsMetadata(l:this_line)
46        let l:this_indent = indent(a:line_num - 1)
47        if ! s:IsMetadata(l:prev_line) | let l:this_indent += &shiftwidth | endif
48        return l:this_indent
49    endif
50    return -1
51endfunction