1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
if polyglot#init#is_disabled(expand('<sfile>:p'), 'mermaid', 'indent/mermaid.vim')
finish
endif
let s:indent_next_line_keywords = [
\ '^classDiagram',
\ '^classDiagram-v2',
\ '^erDiagram',
\ '^gantt',
\ '^graph',
\ '^flowchart',
\ '^pie',
\ '^sequenceDiagram',
\ '^stateDiagram',
\ '^stateDiagram-v2',
\ 'class.*{',
\ 'subgraph',
\ 'loop',
\ 'alt',
\ 'else',
\ 'opt',
\ 'par',
\ 'and',
\ 'rect',
\ '[^a-z]activate',
\ ]
let s:deindent_current_line_keywords = [
\ '^\s\+classDiagram',
\ '^\s\+classDiagram-v2',
\ '^\s\+erDiagram',
\ '^\s\+gantt',
\ '^\s\+graph',
\ '^\s\+flowchart',
\ '^\s\+pie',
\ '^\s\+sequenceDiagram',
\ '^\s\+stateDiagram',
\ '^\s\+stateDiagram-v2',
\ '^\s\+end$',
\ '^\s\+}$',
\ '^\s\+deactivate$'
\ ]
function! s:matches_any_word_in(text, list)
return empty(filter(copy(a:list), {_idx, val -> matchstr(a:text, val) != ""})) == v:false
endfunction
function! MermaidIndent()
let sw = shiftwidth()
let indent_level = indent(v:lnum - 1) / sw
let current_line = getline(v:lnum)
let line_above = getline(v:lnum - 1)
" TODO don't incorrect indent when has empty lines inside graph
if s:matches_any_word_in(current_line, s:deindent_current_line_keywords)
return (indent_level - 1) * sw
endif
if s:matches_any_word_in(line_above, s:indent_next_line_keywords)
return (indent_level + 1) * sw
endif
return indent_level * sw
endfunction
|