Skip to content

Commit 56fec37

Browse files
committed
Indent embedded views
1 parent 527e6fd commit 56fec37

File tree

3 files changed

+370
-1
lines changed

3 files changed

+370
-1
lines changed

Diff for: autoload/elixir/indent.vim

+110
Original file line numberDiff line numberDiff line change
@@ -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)
6667
endfunction
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
7082
function! s:_starts_with(text, expr, lnum)
@@ -156,6 +168,104 @@ function! s:find_last_pos(lnum, text, match)
156168
return -1
157169
endfunction
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 indent(pair_lnum)
181+
elseif a:context.text =~ '}}*>$'
182+
return -1
183+
elseif s:prev_ends_with(a:context, '[\|%{')
184+
return indent(a:context.prev_nb_lnum) + s:sw()
185+
elseif a:context.prev_nb_text =~ ',$'
186+
return indent(a:context.prev_nb_lnum)
187+
else
188+
return indent(pair_lnum) + s:sw()
189+
endif
190+
endif
191+
192+
" Multi-line opening tag -- >, />, or %> are on a different line that their opening <
193+
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]))
194+
if pair_lnum
195+
if a:context.text =~ '^\s\+\%\(>\|\/>\|%>\|}}>\)$'
196+
call s:debug("current line is a lone >, />, or %>")
197+
return indent(pair_lnum)
198+
elseif a:context.text =~ '\%\(>\|\/>\|%>\|}}>\)$'
199+
call s:debug("current line ends in >, />, or %>")
200+
if s:prev_ends_with(a:context, ',')
201+
return indent(a:context.prev_nb_lnum)
202+
else
203+
return -1
204+
endif
205+
else
206+
call s:debug("in the body of a multi-line opening tag")
207+
return indent(pair_lnum) + s:sw()
208+
endif
209+
endif
210+
211+
" Special cases
212+
if s:prev_ends_with(a:context, '^[^<]*do\s%>')
213+
call s:debug("prev line closes a multi-line do block")
214+
return indent(a:context.prev_nb_lnum)
215+
elseif a:context.prev_nb_text =~ 'do\s*%>$'
216+
call s:debug("prev line opens a do block")
217+
return indent(a:context.prev_nb_lnum) + s:sw()
218+
elseif a:context.text =~ '^\s\+<\/[a-zA-Z0-9\.\-_]\+>\|<% end %>'
219+
call s:debug("a single closing tag")
220+
if a:context.prev_nb_text =~ '^\s\+<[^%\/]*[^/]>.*<\/[a-zA-Z0-9\.\-_]\+>$'
221+
call s:debug("opening and closing tags are on the same line")
222+
return indent(a:context.prev_nb_lnum) - s:sw()
223+
elseif a:context.prev_nb_text =~ '^\s\+<[^%\/]*[^/]>\|\s\+>'
224+
call s:debug("prev line is opening html tag or single >")
225+
return indent(a:context.prev_nb_lnum)
226+
elseif s:prev_ends_with(a:context, '^[^<]*\%\(do\s\)\@<!%>')
227+
call s:debug("prev line closes a multi-line eex tag")
228+
return indent(a:context.prev_nb_lnum) - 2 * s:sw()
229+
else
230+
return indent(a:context.prev_nb_lnum) - s:sw()
231+
endif
232+
elseif a:context.text =~ '^\s*<%\s*\%(end\|else\|catch\|rescue\)\>.*%>'
233+
call s:debug("eex middle or closing eex tag")
234+
return indent(a:context.prev_nb_lnum) - s:sw()
235+
elseif a:context.prev_nb_text =~ '\s*<\/\|<% end %>$'
236+
call s:debug("prev is closing tag")
237+
return indent(a:context.prev_nb_lnum)
238+
elseif a:context.prev_nb_text =~ '^\s\+<[^%\/]*[^/]>.*<\/[a-zA-Z0-9\.\-_]\+>$'
239+
call s:debug("opening and closing tags are on the same line")
240+
return indent(a:context.prev_nb_lnum)
241+
elseif s:prev_ends_with(a:context, '\s\+\/>')
242+
call s:debug("prev ends with a single \>")
243+
return indent(a:context.prev_nb_lnum)
244+
elseif s:prev_ends_with(a:context, '^[^<]*\/>')
245+
call s:debug("prev line is closing a multi-line self-closing tag")
246+
return indent(a:context.prev_nb_lnum) - s:sw()
247+
elseif s:prev_ends_with(a:context, '^<*\/>')
248+
call s:debug("prev line is closing self-closing tag")
249+
return indent(a:context.prev_nb_lnum)
250+
elseif a:context.prev_nb_text =~ '^\s\+%\?>$'
251+
call s:debug("prev line is a single > or %>")
252+
return indent(a:context.prev_nb_lnum) + s:sw()
253+
endif
254+
255+
" Simple HTML (ie, opening tag is not split across lines)
256+
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]))
257+
if pair_lnum
258+
call s:debug("simple HTML")
259+
if a:context.text =~ '^\s\+<\/\w\+>$'
260+
return indent(pair_lnum)
261+
else
262+
return indent(pair_lnum) + s:sw()
263+
endif
264+
endif
265+
266+
return -1
267+
endfunction
268+
159269
function! elixir#indent#handle_top_of_file(context)
160270
if a:context.prev_nb_lnum == 0
161271
return 0

Diff for: indent/elixir.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let b:did_indent = 1
66
setlocal indentexpr=elixir#indent(v:lnum)
77

88
setlocal indentkeys+==after,=catch,=do,=else,=end,=rescue,
9-
setlocal indentkeys+=*<Return>,=->,=\|>,=<>,0},0],0)
9+
setlocal indentkeys+=*<Return>,=->,=\|>,=<>,0},0],0),>
1010

1111
" TODO: @jbodah 2017-02-27: all operators should cause reindent when typed
1212

Diff for: spec/indent/embedded_views_spec.rb

+259
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Indenting embedded views' do
6+
i <<~EOF
7+
def render(assigns) do
8+
~L"""
9+
<div>
10+
Some content
11+
</div>
12+
"""
13+
end
14+
EOF
15+
16+
i <<~EOF
17+
def render(assigns) do
18+
~H"""
19+
<div class="theres a-/ in the class names from tailwind">
20+
<div class="some more classes">
21+
This is immediately nested
22+
</div>
23+
</div>
24+
"""
25+
end
26+
EOF
27+
28+
i <<~EOF
29+
def render(assigns) do
30+
~L"""
31+
<div id="123456">
32+
Some content
33+
</div>
34+
"""
35+
end
36+
EOF
37+
38+
i <<~EOF
39+
def render(assigns) do
40+
~L"""
41+
<div
42+
id="123456"
43+
>
44+
Some content
45+
</div>
46+
"""
47+
end
48+
EOF
49+
50+
i <<~EOF
51+
def render(assigns) do
52+
~L"""
53+
<div />
54+
<p>Some paragraph</p>
55+
"""
56+
end
57+
EOF
58+
59+
i <<~EOF
60+
def render(assigns) do
61+
~L"""
62+
<div>
63+
it
64+
<div>
65+
keeps
66+
<div>
67+
nesting
68+
</div>
69+
</div>
70+
</div>
71+
"""
72+
end
73+
EOF
74+
75+
i <<~EOF
76+
def render(assgins) do
77+
~L"""
78+
<div>
79+
<%= for i <- iter do %>
80+
<div><%= i %></div>
81+
<% end %>
82+
</div>
83+
"""
84+
end
85+
EOF
86+
87+
i <<~EOF
88+
def render(assigns) do
89+
~L"""
90+
<%= live_component @socket,
91+
Component,
92+
id: "<%= @id %>",
93+
user: @user do
94+
%>
95+
96+
<main>
97+
<header>
98+
<h1>Some Header</h1>
99+
</header>
100+
<section>
101+
<h1>Some Section</h1>
102+
<p>
103+
I'm some text
104+
</p>
105+
</section>
106+
</main>
107+
108+
<% end %>
109+
"""
110+
end
111+
EOF
112+
113+
i <<~EOF
114+
def render(assigns) do
115+
~L"""
116+
<%= render_component,
117+
@socket,
118+
Component do %>
119+
120+
<p>Multi-line opening eex tag that takes a block</p>
121+
<% end %>
122+
"""
123+
end
124+
EOF
125+
126+
i <<~EOF
127+
def render(assigns) do
128+
~L"""
129+
<div>
130+
<%= render_component,
131+
@socket,
132+
Component %>
133+
</div>
134+
135+
<%= render_component,
136+
@socket,
137+
Component %>
138+
<p>Multi-line single eex tag</p>
139+
"""
140+
end
141+
EOF
142+
143+
i <<~EOF
144+
def render(assigns) do
145+
~H"""
146+
<Component
147+
foo={{
148+
foo: [
149+
'one',
150+
'two',
151+
'three'
152+
],
153+
bar: %{
154+
"foo" => "bar"
155+
}
156+
}}
157+
/>
158+
"""
159+
end
160+
EOF
161+
162+
i <<~EOF
163+
def render(assigns) do
164+
~L"""
165+
<%= live_component @socket,
166+
Component,
167+
id: "<%= @id %>",
168+
team: @team do
169+
%>
170+
171+
<div>
172+
<div>
173+
<div>
174+
A deeply nested tree
175+
<div>
176+
with trailing whitespace
177+
178+
</div>
179+
</div>
180+
</div>
181+
</div>
182+
183+
<div id="id-ends-with-greater-than->"
184+
propWithEexTag="<%= @id %>"
185+
anotherProp="foo"
186+
/>
187+
188+
<%= for i <- iter do %>
189+
<div><%= i %></div>
190+
<% end %>
191+
192+
<div
193+
opts={{
194+
opt1: "optA",
195+
opt2: "optB"
196+
}}
197+
id="hi"
198+
bye="hi" />
199+
200+
<ul>
201+
<li :for={{ item <- @items }}>
202+
{{ item }}
203+
</li>
204+
</ul>
205+
206+
<div id="hi">
207+
Hi <p>hi</p>
208+
I'm ok, ok?
209+
<div>
210+
hi there!
211+
</div>
212+
<div>
213+
<div>
214+
<p>hi</p>
215+
<hr />
216+
</div>
217+
</div>
218+
</div>
219+
220+
<Some.Surface.Component />
221+
222+
<Another
223+
prop="prop"
224+
prop2="prop2"
225+
>
226+
<div>content</div>
227+
</Another>
228+
229+
<div foo />
230+
231+
<div>hi</div>
232+
233+
<div>
234+
<div>
235+
content
236+
</div>
237+
<div />
238+
<div>
239+
content in new div after a self-closing div
240+
</div>
241+
</div>
242+
243+
<p
244+
id="<%= @id %>"
245+
class="multi-line opening single letter p tag"
246+
>
247+
<%= @solo.eex_tag %>
248+
<Nested
249+
prop="nested"
250+
>
251+
content
252+
</Nested>
253+
</p>
254+
255+
<% end %>
256+
"""
257+
end
258+
EOF
259+
end

0 commit comments

Comments
 (0)