raz / editor / vim / indent / raz.vim

173 lines · 5.2 KiB · raw · history · last change fc966ca0

1" Vim indent file for raz, by shiftwidth(): a block's statements one
2" level in from the word that opens it; end, until, else and elsif at
3" that word's level; match arms at the level of match; procedures,
4" sections (const, type, var) and module bodies at the left margin.
5" The text of a comment or of a raw string, and a line that goes on with
6" the expression of the line before, keep the indent they have.
7
8if exists("b:did_indent")
9 finish
10endif
11let b:did_indent = 1
12
13setlocal indentexpr=GetRazIndent(v:lnum)
14setlocal indentkeys=o,O,0=end,0=else,0=elsif,0=until,0=\|,!^F
15setlocal nolisp nosmartindent
16
17if exists("*GetRazIndent")
18 finish
19endif
20
21" searchpair() needs groups that don't capture
22let s:opener = '\C\<\%(if\|while\|for\|match\|record\|variant\|begin\|repeat\|code\)\>'
23let s:closer = '\C\<\%(end\|until\)\>'
24let s:skip = 'synIDattr(synID(line("."), col("."), 1), "name") =~? "string\\|char\\|comment"'
25let s:decl = '^\s*\%(module\|import\|link\|extern\|proc\|const\|type\|var\|begin\|code\)\>'
26
27" The line's code, without strings, characters and comments.
28function! s:Code(lnum)
29 let line = getline(a:lnum)
30 " a line that starts inside a comment: its code is after the comment
31 if s:InComment(a:lnum)
32 let line = line =~ '\*)' ? substitute(line, '^.\{-}\*)', '', '') : ''
33 endif
34 " and inside a raw string, after the string
35 if s:InRaw(a:lnum)
36 let line = line =~ '`' ? substitute(line, '^[^`]*`', '0', '') : ''
37 endif
38 " one pattern, so that whichever comes first is taken: a " in a
39 " character, a ' in a string; strings become 0, comments go
40 let line = substitute(line,
41 \ '"\%([^"\\]\|\\.\)*"\|''\%([^''\\]\|\\.\)*''\|`[^`]*`\|(\*.\{-}\*)',
42 \ '\=submatch(0)[0] == "(" ? "" : "0"', 'g')
43 let line = substitute(line, '(\*.*$', '', '')
44 let line = substitute(line, '`.*$', '0', '')
45 return substitute(line, '^\s*\(.\{-}\)\s*$', '\1', '')
46endfunction
47
48" Whether the expression on lnum goes on to the next line.
49function! s:Continues(lnum)
50 let code = s:Code(a:lnum)
51 if len(substitute(code, '[^([{]', '', 'g')) > len(substitute(code, '[^)\]}]', '', 'g'))
52 return 1
53 endif
54 return code =~# '\%(,\|:=\|[-+*/&|=#<>]\|\<and\|\<or\)$'
55endfunction
56
57let s:leading = '^\s*\%(or\|and\)\>'
58
59" The first line of the statement that ends on lnum.
60function! s:Start(lnum)
61 let l = a:lnum
62 while 1
63 let p = prevnonblank(l - 1)
64 if p == 0 || !s:Continues(p) && getline(l) !~# s:leading && !s:InRaw(l)
65 return l
66 endif
67 let l = p
68 endwhile
69endfunction
70
71" The line itself, or where the comment it ends began, if it holds
72" nothing but comment.
73function! s:Reference(lnum)
74 let l = a:lnum
75 if s:Code(l) != ''
76 return l
77 endif
78 while l > 1 && getline(l) !~ '(\*'
79 let l -= 1
80 endwhile
81 return l
82endfunction
83
84" How many blocks the code opens, less those it closes.
85function! s:Opens(code)
86 let n = 0
87 let i = 0
88 while 1
89 let m = matchstrpos(a:code, '\C\<\%(if\|while\|for\|record\|variant\|begin\|repeat\|code\|end\|until\)\>', i)
90 if m[1] < 0
91 return n
92 endif
93 let n += (m[0] ==# 'end' || m[0] ==# 'until') ? -1 : 1
94 let i = m[2]
95 endwhile
96endfunction
97
98function! s:InComment(lnum)
99 let col = match(getline(a:lnum), '\S') + 1
100 return col > 0 && synIDattr(synID(a:lnum, col, 1), "name") =~? 'comment'
101 \ && getline(a:lnum) !~ '^\s*(\*'
102endfunction
103
104" Whether the line starts inside a raw string, which only a raw string
105" can, since it alone runs over lines; a backquote first on the line
106" closes one if what follows it isn't in a string.
107function! s:IsRaw(lnum, col)
108 return synIDattr(synID(a:lnum, a:col, 1), "name") ==# 'razRawString'
109endfunction
110
111function! s:InRaw(lnum)
112 return s:IsRaw(a:lnum, 1) && (getline(a:lnum) !~ '^`' || !s:IsRaw(a:lnum, 2))
113endfunction
114
115function! GetRazIndent(lnum)
116 let cur = getline(a:lnum)
117 if s:InComment(a:lnum) || s:InRaw(a:lnum)
118 return -1
119 endif
120 if cur =~# s:decl && cur !~# '^\s*proc\s*('
121 return 0
122 endif
123 " a comment before a declaration goes with it, at the margin
124 if cur =~ '^\s*(\*'
125 let l = a:lnum
126 while l < line('$') && getline(l) !~ '\*)'
127 let l += 1
128 endwhile
129 if getline(nextnonblank(l + 1)) =~# s:decl
130 return 0
131 endif
132 endif
133 if cur =~# '^\s*\%(end\|until\)\>'
134 call cursor(a:lnum, 1)
135 let l = searchpair(s:opener, '', s:closer, 'bW', s:skip)
136 return l > 0 ? indent(l) : 0
137 endif
138 let prev = prevnonblank(a:lnum - 1)
139 if prev == 0
140 return 0
141 endif
142 if cur =~# '^\s*\%(else\|elsif\)\>'
143 call cursor(a:lnum, 1)
144 let l = searchpair(s:opener, '', s:closer, 'bW', s:skip)
145 return l > 0 ? indent(l) : indent(prev)
146 endif
147 if cur =~# '^\s*|'
148 call cursor(a:lnum, 1)
149 let l = searchpair(s:opener, '', s:closer, 'bW', s:skip)
150 return l > 0 ? indent(l) : indent(prev)
151 endif
152 if s:Continues(prev) || cur =~# s:leading
153 return -1
154 endif
155 if s:Code(prev) == ''
156 return indent(s:Reference(prev))
157 endif
158
159 let start = s:Start(prev)
160 let ind = indent(start)
161 let code = join(map(range(start, prev), 's:Code(v:val)'), ' ')
162 if code =~# '^\%(const\|type\|var\)$' || code =~# '^else\>' && s:Opens(code) >= 0
163 \ || code =~# '^|.*:$' || code =~# '^elsif\>.*\<then$'
164 return ind + shiftwidth()
165 endif
166 if code =~# '^|' || code =~# '\<match\>.*\<of$'
167 return ind
168 endif
169 if s:Opens(code) > 0
170 return ind + shiftwidth()
171 endif
172 return ind
173endfunction