blob: 55edd23928c2646f6de412f5bb3846aceeae94ba [file] [log] [blame]
Gregory Anders1cc4cae2024-07-15 20:00:48 +02001" Language: Typst
2" Maintainer: Gregory Anders
3" Last Change: 2024-07-14
4" Based on: https://github.com/kaarmu/typst.vim
5
6function! typst#indentexpr() abort
7 let l:lnum = v:lnum
8 let s:sw = shiftwidth()
9
10 let [l:plnum, l:pline] = s:get_prev_nonblank(l:lnum - 1)
11 if l:plnum == 0 | return 0 | endif
12
13 let l:line = getline(l:lnum)
14 let l:ind = indent(l:plnum)
15
16 let l:synname = synIDattr(synID(l:lnum, 1, 1), 'name')
17
18 " Use last indent for block comments
19 if l:synname == 'typstCommentBlock'
20 return l:ind
21 endif
22
23 if l:pline =~ '\v[{[(]\s*$'
24 let l:ind += s:sw
25 endif
26
27 if l:line =~ '\v^\s*[}\])]'
28 let l:ind -= s:sw
29 endif
30
31 return l:ind
32endfunction
33
34" Gets the previous non-blank line that is not a comment.
35function! s:get_prev_nonblank(lnum) abort
36 let l:lnum = prevnonblank(a:lnum)
37 let l:line = getline(l:lnum)
38
39 while l:lnum > 0 && l:line =~ '^\s*//'
40 let l:lnum = prevnonblank(l:lnum - 1)
41 let l:line = getline(l:lnum)
42 endwhile
43
44 return [l:lnum, s:remove_comments(l:line)]
45endfunction
46
47" Removes comments from the given line.
48function! s:remove_comments(line) abort
49 return substitute(a:line, '\s*//.*', '', '')
50endfunction