@@ -20,6 +20,7 @@ function! elixir#indent#indent(lnum)
2020 call cursor (lnum, 0 )
2121
2222 let handlers = [
23+ \' inside_embedded_view',
2324 \' top_of_file',
2425 \' starts_with_string_continuation',
2526 \' following_trailing_binary_operator',
@@ -65,6 +66,17 @@ function! s:prev_starts_with(context, expr)
6566 return s: _starts_with (a: context .prev_nb_text, a: expr , a: context .prev_nb_lnum)
6667endfunction
6768
69+ function ! s: in_embedded_view ()
70+ let groups = map (synstack (line (' .' ), col (' .' )), " synIDattr(v:val, 'name')" )
71+ for group in [' elixirPhoenixESigil' , ' elixirLiveViewSigil' , ' elixirSurfaceSigil' ]
72+ if index (groups, group) >= 0
73+ return 1
74+ endif
75+ endfor
76+
77+ return 0
78+ endfunction
79+
6880" Returns 0 or 1 based on whether or not the text starts with the given
6981" expression and is not a string or comment
7082function ! s: _starts_with (text, expr , lnum)
@@ -156,6 +168,98 @@ function! s:find_last_pos(lnum, text, match)
156168 return -1
157169endfunction
158170
171+ function ! elixir#indent#handle_inside_embedded_view (context)
172+ if ! s: in_embedded_view ()
173+ return -1
174+ endif
175+
176+ " Multi-line Surface data delimiters
177+ let pair_lnum = searchpair (' {{' , ' ' , ' }}' , ' bW' , " line('.') == " .a: context .lnum." || s:is_string_or_comment(line('.'), col('.'))" , max ([0 , a: context .lnum - g: elixir_indent_max_lookbehind ]))
178+ if pair_lnum
179+ if a: context .text = ~ ' }}$'
180+ return -1
181+ else
182+ return indent (pair_lnum) + s: sw ()
183+ endif
184+ endif
185+
186+ " Multi-line opening tag -- >, />, or %> are on a different line that their opening <
187+ let pair_lnum = searchpair (' ^\s\+<.*[^>]$' , ' ' , ' ^[^<]*[/%}]\?>$' , ' bW' , " line('.') == " .a: context .lnum." || s:is_string_or_comment(line('.'), col('.'))" , max ([0 , a: context .lnum - g: elixir_indent_max_lookbehind ]))
188+ if pair_lnum
189+ if a: context .text = ~ ' ^\s\+\%\(>\|\/>\|%>\|}}>\)$'
190+ call s: debug (" current line is a lone >, />, or %>" )
191+ return indent (pair_lnum)
192+ elseif a: context .text = ~ ' \%\(>\|\/>\|%>\|}}>\)$'
193+ call s: debug (" current line ends in >, />, or %>" )
194+ if s: prev_ends_with (a: context , ' ,' )
195+ return indent (a: context .prev_nb_lnum)
196+ else
197+ return -1
198+ endif
199+ else
200+ call s: debug (" in the body of a multi-line opening tag" )
201+ return indent (pair_lnum) + s: sw ()
202+ endif
203+ endif
204+
205+ " Special cases
206+ if s: prev_ends_with (a: context , ' ^[^<]*do\s%>' )
207+ call s: debug (" prev line closes a multi-line do block" )
208+ return indent (a: context .prev_nb_lnum)
209+ elseif a: context .prev_nb_text = ~ ' do\s*%>$'
210+ call s: debug (" prev line opens a do block" )
211+ return indent (a: context .prev_nb_lnum) + s: sw ()
212+ elseif a: context .text = ~ ' ^\s\+<\/[a-zA-Z0-9\.\-_]\+>\|<% end %>'
213+ call s: debug (" a single closing tag" )
214+ if a: context .prev_nb_text = ~ ' ^\s\+<[^%\/]*[^/]>.*<\/[a-zA-Z0-9\.\-_]\+>$'
215+ call s: debug (" opening and closing tags are on the same line" )
216+ return indent (a: context .prev_nb_lnum) - s: sw ()
217+ elseif a: context .prev_nb_text = ~ ' ^\s\+<[^%\/]*[^/]>\|\s\+>'
218+ call s: debug (" prev line is opening html tag or single >" )
219+ return indent (a: context .prev_nb_lnum)
220+ elseif s: prev_ends_with (a: context , ' ^[^<]*\%\(do\s\)\@<!%>' )
221+ call s: debug (" prev line closes a multi-line eex tag" )
222+ return indent (a: context .prev_nb_lnum) - 2 * s: sw ()
223+ else
224+ return indent (a: context .prev_nb_lnum) - s: sw ()
225+ endif
226+ elseif a: context .text = ~ ' ^\s*<%\s*\%(end\|else\|catch\|rescue\)\>.*%>'
227+ call s: debug (" eex middle or closing eex tag" )
228+ return indent (a: context .prev_nb_lnum) - s: sw ()
229+ elseif a: context .prev_nb_text = ~ ' \s*<\/\|<% end %>$'
230+ call s: debug (" prev is closing tag" )
231+ return indent (a: context .prev_nb_lnum)
232+ elseif a: context .prev_nb_text = ~ ' ^\s\+<[^%\/]*[^/]>.*<\/[a-zA-Z0-9\.\-_]\+>$'
233+ call s: debug (" opening and closing tags are on the same line" )
234+ return indent (a: context .prev_nb_lnum)
235+ elseif s: prev_ends_with (a: context , ' \s\+\/>' )
236+ call s: debug (" prev ends with a single \> " )
237+ return indent (a: context .prev_nb_lnum)
238+ elseif s: prev_ends_with (a: context , ' ^[^<]*\/>' )
239+ call s: debug (" prev line is closing a multi-line self-closing tag" )
240+ return indent (a: context .prev_nb_lnum) - s: sw ()
241+ elseif s: prev_ends_with (a: context , ' ^<*\/>' )
242+ call s: debug (" prev line is closing self-closing tag" )
243+ return indent (a: context .prev_nb_lnum)
244+ elseif a: context .prev_nb_text = ~ ' ^\s\+%\?>$'
245+ call s: debug (" prev line is a single > or %>" )
246+ return indent (a: context .prev_nb_lnum) + s: sw ()
247+ endif
248+
249+ " Simple HTML (ie, opening tag is not split across lines)
250+ let pair_lnum = searchpair (' ^\s\+<[^%\/].*[^\/>]>$' , ' ' , ' ^\s\+<\/\w\+>$' , ' bW' , " line('.') == " .a: context .lnum." || s:is_string_or_comment(line('.'), col('.'))" , max ([0 , a: context .lnum - g: elixir_indent_max_lookbehind ]))
251+ if pair_lnum
252+ call s: debug (" simple HTML" )
253+ if a: context .text = ~ ' ^\s\+<\/\w\+>$'
254+ return indent (pair_lnum)
255+ else
256+ return indent (pair_lnum) + s: sw ()
257+ endif
258+ endif
259+
260+ return -1
261+ endfunction
262+
159263function ! elixir#indent#handle_top_of_file (context)
160264 if a: context .prev_nb_lnum == 0
161265 return 0
0 commit comments