blob: baf765a30bdf44179a6fbcc9cbb5f2e6a1dcaa8f [file] [log] [blame]
Gregory Anders1cc4cae2024-07-15 20:00:48 +02001" Language: Typst
2" Maintainer: Gregory Anders
Luca Saccarola421ed142024-10-21 22:01:10 +02003" Last Change: 2024 Oct 21
Gregory Anders1cc4cae2024-07-15 20:00:48 +02004" 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
Luca Saccarola421ed142024-10-21 22:01:10 +020034function typst#foldexpr()
35 let line = getline(v:lnum)
36
37 " Whenever the user wants to fold nested headers under the parent
38 let nested = get(g:, "typst_foldnested", 1)
39
40 " Regular headers
41 let depth = match(line, '\(^=\+\)\@<=\( .*$\)\@=')
42
43 " Do not fold nested regular headers
44 if depth > 1 && !nested
45 let depth = 1
46 endif
47
48 if depth > 0
49 " check syntax, it should be typstMarkupHeading
50 let syncode = synstack(v:lnum, 1)
51 if len(syncode) > 0 && synIDattr(syncode[0], 'name') ==# 'typstMarkupHeading'
52 return ">" . depth
53 endif
54 endif
55
56 return "="
57endfunction
58
Gregory Anders1cc4cae2024-07-15 20:00:48 +020059" Gets the previous non-blank line that is not a comment.
60function! s:get_prev_nonblank(lnum) abort
61 let l:lnum = prevnonblank(a:lnum)
62 let l:line = getline(l:lnum)
63
64 while l:lnum > 0 && l:line =~ '^\s*//'
65 let l:lnum = prevnonblank(l:lnum - 1)
66 let l:line = getline(l:lnum)
67 endwhile
68
69 return [l:lnum, s:remove_comments(l:line)]
70endfunction
71
72" Removes comments from the given line.
73function! s:remove_comments(line) abort
74 return substitute(a:line, '\s*//.*', '', '')
75endfunction