/
rules.vim
201 lines (187 loc) · 5.64 KB
/
rules.vim
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
let s:basic_rules_raw = {
\ '&{}': #{
\ increase: ['{[^}]*$'],
\ decrease: ['^}'],
\ },
\ '&[]': #{
\ increase: ['\[[^\]]*$'],
\ decrease: ['^\]'],
\ },
\ '&()': #{
\ increase: ['([^)]*$'],
\ decrease: ['^)'],
\ },
\ '&<>': #{
\ increase: ['<[^>]*$'],
\ decrease: ['^>'],
\ },
\ '&tag': #{
\ increase: [
\ '\v\<(%(area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr))@!([\-_\.A-Za-z0-9]*)(\s|\>)@=[^\>]*\>(.*\<\/[^/>]*\>)@!$',
\ '\v^\>'
\ ],
\ decrease: [
\ '\v^\<\/([\-_\.A-Za-z0-9]*)\s*\>',
\ '\v^\/>'
\ ],
\ },
\ '&s_str': #{
\ string: ['\v''@<=([^''\\]|\\.)*''@=']
\ },
\ '&d_str': #{
\ string: ['\v"@<=([^"\\]|\\.)*"@=']
\ },
\ '&cml_comment': #{
\ indentone: ['\/\*\(\(\*\/\)\@!.\)*$']
\ }
\ }
let s:default_rule_key = "&default"
let s:default_rule_raw = #{
\ inherit: ['&{}', '&[]', '&()']
\ }
let s:rules_raw = {
\ 'c': #{
\ comment: ['//', '^\*'],
\ inherit: ['&{}', '&()', '&[]', '&s_str', '&d_str', '&cml_comment'],
\ increase: ['\v<%(case|default)>.*\:\s*$'],
\ decrease: ['\v<%(case|default)>.*\:\s*$'],
\ indentnext: ['\v<%(if|while|for|switch)>((\;)@!.)*$'],
\ },
\ 'cpp': #{
\ inherit: ['c'],
\ },
\ 'lua': #{
\ comment: ['--'],
\ inherit: ['&{}', '&()', '&s_str', '&d_str'],
\ increase: ['\v<%(if|else|while|for|function|then|do|repeat)>((<%(end|until)>)@!.)*$'],
\ decrease: ['^\v<%(then|do|elseif|else|end|until)>']
\ },
\ 'vim': #{
\ inherit: ['&s_str', '&d_str'],
\ increase: ['\v<%(function|if|else|elseif|while|for|try|catch|finally|augroup)>((<%(endif|endfor|endfunction|endtry|END)>)@!.)*$'],
\ decrease: ['\v^<%(endif|elseif|catch|finally|endfor|endfunction|endtry|endwhile)>', '^augroup\s\+END'],
\ },
\ 'python': #{
\ comment: ['#'],
\ inherit: ['&{}', '&()', '&[]', '&s_str', '&d_str'],
\ increase: ['\v<%(def|class|for|if|elif|else|while|try|with|finally|except|async|match|case)>.*\:\s*$'],
\ decrease: ['\v<%(elif|else|finally|except)>.*\:\s*$'],
\ indentasmax: v:true,
\ },
\ 'html': #{
\ inherit: ['&tag', '&s_str', '&d_str'],
\ },
\ 'vue': #{
\ inherit: ['html', 'javascript'],
\ },
\ 'json': #{
\ comment: ['//'],
\ inherit: ['&{}', '&[]', '&d_str'],
\ },
\ 'css': #{
\ comment: ['^\*'],
\ inherit: ['&{}', '&s_str', '&d_str', '&cml_comment'],
\ },
\ 'less': #{
\ inherit: ['css'],
\ },
\ 'scss': #{
\ inherit: ['css'],
\ },
\ 'javascript': #{
\ comment: ['//', '^\*', '&cml_comment'],
\ increase: ['\v<%(case|default)>.*\:\s*$'],
\ decrease: ['\v<%(case|default)>.*\:\s*$'],
\ inherit: ['&{}', '&()', '&[]', '&s_str', '&d_str'],
\ },
\ 'typescript': #{
\ inherit: ['javascript', '&<>'],
\ },
\ 'javascriptreact': #{
\ inherit: ['javascript', '&tag'],
\ },
\ 'typescriptreact': #{
\ inherit: ['typescript', '&tag'],
\ },
\ 'rust': #{
\ comment: ['//', '^*'],
\ inherit: ['&{}', '&()', '&[]', '&s_str', '&d_str', '&cml_comment'],
\ },
\ 'yaml': #{
\ comment: ['#'],
\ inherit: ['&{}', '&()', '&s_str', '&d_str'],
\ increase: ['\v(\:|\-)\s?(\&\w+)?$'],
\ },
\ }
let s:cache_key = g:tmindent
let s:rule_cache = {}
function s:raw_get(lang, conf) abort
let maybe_basic = get(s:basic_rules_raw, a:lang, v:null)
if maybe_basic isnot v:null
return maybe_basic
endif
let conf_rules = get(a:conf, "rules", {})
let maybe_rule = get(conf_rules, a:lang, get(s:rules_raw, a:lang, v:null))
if maybe_rule isnot v:null
return maybe_rule
endif
let default_rule = get(a:conf, "default_rule", s:default_rule_raw)
return default_rule
endfunction
function s:get(key, or_build, conf) abort
if !has_key(s:rule_cache, a:key)
let rule = s:build(a:or_build, a:conf)
let s:rule_cache[a:key] = rule
endif
return s:rule_cache[a:key]
endfunction
function s:build(raw_rule, conf) abort
let res = #{
\ comment: get(a:raw_rule, "comment", []),
\ string: get(a:raw_rule, "string", []),
\ increase: get(a:raw_rule, "increase", []),
\ decrease: get(a:raw_rule, "decrease", []),
\ unindented: get(a:raw_rule, "unindented", []),
\ indentnext: get(a:raw_rule, "indentnext", []),
\ indentone: get(a:raw_rule, "indentone", []),
\ indentasmax: get(a:raw_rule, "indentasmax", v:null)
\ }
let inherits = get(a:raw_rule, "inherit", [])
for i in inherits
let rhs = s:get(i, s:raw_get(i, a:conf), a:conf)
for p in get(rhs, "comment", [])
call add(res.comment, p)
endfor
for p in get(rhs, "string", [])
call add(res.string, p)
endfor
for p in get(rhs, "increase", [])
call add(res.increase, p)
endfor
for p in get(rhs, "decrease", [])
call add(res.decrease, p)
endfor
for p in get(rhs, "unindented", [])
call add(res.unindented, p)
endfor
for p in get(rhs, "indentnext", [])
call add(res.indentnext, p)
endfor
for p in get(rhs, "indentone", [])
call add(res.indentone, p)
endfor
let indentasmax = get(rhs, "indentasmax", v:null)
if indentasmax != v:null
let res.indentasmax = indentasmax
endif
endfor
return res
endfunction
function tmindent#rules#get(lang) abort
let user_conf = g:tmindent
if user_conf isnot s:cache_key
let s:cache_key = user_conf
let s:rule_cache = {}
endif
return s:get(a:lang, s:raw_get(a:lang, user_conf), user_conf)
endfunction