blob: ff86c19fac4819816e56bd11961ad204bcec4dfd [file] [log] [blame]
Brad Bishop64c979e2019-11-04 13:55:29 -05001if exists("b:did_indent")
2 finish
3endif
4
5runtime! indent/sh.vim
6unlet b:did_indent
7
8setlocal indentexpr=BitbakeIndent(v:lnum)
9setlocal autoindent nolisp
10
11function s:is_python_func_def(lnum)
12 let stack = synstack(a:lnum, 1)
13 if len(stack) == 0
14 return 0
15 endif
16
17 let top = synIDattr(stack[0], "name")
18 echo top
19
20 return synIDattr(stack[0], "name") == "bbPyFuncDef"
21endfunction
22
23"""" begin modified from indent/python.vim, upstream commit 7a9bd7c1e0ce1baf5a02daf36eeae3638aa315c7
24"""" This copied code is licensed the same as Vim itself.
25setlocal indentkeys+=<:>,=elif,=except
26
27let s:keepcpo= &cpo
28set cpo&vim
29
30let s:maxoff = 50 " maximum number of lines to look backwards for ()
31
32function GetPythonIndent(lnum)
33
34 " If this line is explicitly joined: If the previous line was also joined,
35 " line it up with that one, otherwise add two 'shiftwidth'
36 if getline(a:lnum - 1) =~ '\\$'
37 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
38 return indent(a:lnum - 1)
39 endif
40 return indent(a:lnum - 1) + (exists("g:pyindent_continue") ? eval(g:pyindent_continue) : (shiftwidth() * 2))
41 endif
42
43 " If the start of the line is in a string don't change the indent.
44 if has('syntax_items')
45 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
46 return -1
47 endif
48
49 " Search backwards for the previous non-empty line.
50 let plnum = prevnonblank(v:lnum - 1)
51
52 if plnum == 0
53 " This is the first non-empty line, use zero indent.
54 return 0
55 endif
56
57 call cursor(plnum, 1)
58
59 " Identing inside parentheses can be very slow, regardless of the searchpair()
60 " timeout, so let the user disable this feature if he doesn't need it
61 let disable_parentheses_indenting = get(g:, "pyindent_disable_parentheses_indenting", 0)
62
63 if disable_parentheses_indenting == 1
64 let plindent = indent(plnum)
65 let plnumstart = plnum
66 else
67 " searchpair() can be slow sometimes, limit the time to 150 msec or what is
68 " put in g:pyindent_searchpair_timeout
69 let searchpair_stopline = 0
70 let searchpair_timeout = get(g:, 'pyindent_searchpair_timeout', 150)
71
72 " If the previous line is inside parenthesis, use the indent of the starting
73 " line.
74 " Trick: use the non-existing "dummy" variable to break out of the loop when
75 " going too far back.
76 let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
77 \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
78 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
79 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
80 \ searchpair_stopline, searchpair_timeout)
81 if parlnum > 0
82 if s:is_python_func_def(parlnum)
83 let parlnum = 0
84 let plindent = indent(plnum)
85 let plnumstart = plnum
86 else
87 let plindent = indent(parlnum)
88 let plnumstart = parlnum
89 endif
90 else
91 let plindent = indent(plnum)
92 let plnumstart = plnum
93 endif
94
95 " When inside parenthesis: If at the first line below the parenthesis add
96 " two 'shiftwidth', otherwise same as previous line.
97 " i = (a
98 " + b
99 " + c)
100 call cursor(a:lnum, 1)
101 let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
102 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
103 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
104 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
105 \ searchpair_stopline, searchpair_timeout)
106 if p > 0
107 if s:is_python_func_def(p)
108 let p = 0
109 else
110 if p == plnum
111 " When the start is inside parenthesis, only indent one 'shiftwidth'.
112 let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
113 \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
114 \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
115 \ . " =~ '\\(Comment\\|Todo\\|String\\)$'",
116 \ searchpair_stopline, searchpair_timeout)
117 if pp > 0
118 return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
119 endif
120 return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
121 endif
122 if plnumstart == p
123 return indent(plnum)
124 endif
125 return plindent
126 endif
127 endif
128
129 endif
130
131
132 " Get the line and remove a trailing comment.
133 " Use syntax highlighting attributes when possible.
134 let pline = getline(plnum)
135 let pline_len = strlen(pline)
136 if has('syntax_items')
137 " If the last character in the line is a comment, do a binary search for
138 " the start of the comment. synID() is slow, a linear search would take
139 " too long on a long line.
140 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
141 let min = 1
142 let max = pline_len
143 while min < max
144 let col = (min + max) / 2
145 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
146 let max = col
147 else
148 let min = col + 1
149 endif
150 endwhile
151 let pline = strpart(pline, 0, min - 1)
152 endif
153 else
154 let col = 0
155 while col < pline_len
156 if pline[col] == '#'
157 let pline = strpart(pline, 0, col)
158 break
159 endif
160 let col = col + 1
161 endwhile
162 endif
163
164 " If the previous line ended with a colon, indent this line
165 if pline =~ ':\s*$'
166 return plindent + shiftwidth()
167 endif
168
169 " If the previous line was a stop-execution statement...
170 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
171 " See if the user has already dedented
172 if indent(a:lnum) > indent(plnum) - shiftwidth()
173 " If not, recommend one dedent
174 return indent(plnum) - shiftwidth()
175 endif
176 " Otherwise, trust the user
177 return -1
178 endif
179
180 " If the current line begins with a keyword that lines up with "try"
181 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
182 let lnum = a:lnum - 1
183 while lnum >= 1
184 if getline(lnum) =~ '^\s*\(try\|except\)\>'
185 let ind = indent(lnum)
186 if ind >= indent(a:lnum)
187 return -1 " indent is already less than this
188 endif
189 return ind " line up with previous try or except
190 endif
191 let lnum = lnum - 1
192 endwhile
193 return -1 " no matching "try"!
194 endif
195
196 " If the current line begins with a header keyword, dedent
197 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
198
199 " Unless the previous line was a one-liner
200 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
201 return plindent
202 endif
203
204 " Or the user has already dedented
205 if indent(a:lnum) <= plindent - shiftwidth()
206 return -1
207 endif
208
209 return plindent - shiftwidth()
210 endif
211
212 " When after a () construct we probably want to go back to the start line.
213 " a = (b
214 " + c)
215 " here
216 if parlnum > 0
217 return plindent
218 endif
219
220 return -1
221
222endfunction
223
224let &cpo = s:keepcpo
225unlet s:keepcpo
226
227""" end of stuff from indent/python.vim
228
229
230let b:did_indent = 1
231
232
233function BitbakeIndent(lnum)
234 let stack = synstack(a:lnum, col("."))
235 if len(stack) == 0
236 return -1
237 endif
238
239 let name = synIDattr(stack[0], "name")
240
241 if index(["bbPyDefRegion", "bbPyFuncRegion"], name) != -1
242 let ret = GetPythonIndent(a:lnum)
243 return ret
244 endif
245
246 return -1
247 "return s:pythonIndentExpr()
248endfunction