344f16b3d13ac9871863c5d47ada727b4ea21643
[bpt/emacs.git] / test / indent / ruby.rb
1 if something_wrong? # ruby-move-to-block-skips-heredoc
2 ActiveSupport::Deprecation.warn(<<-eowarn)
3 boo hoo
4 end
5 eowarn
6 foo
7 end
8
9 # Percent literals.
10 b = %Q{This is a "string"}
11 c = %w!foo
12 bar
13 baz!
14 d = %(hello (nested) world)
15
16 # Don't propertize percent literals inside strings.
17 "(%s, %s)" % [123, 456]
18
19 # Or inside comments.
20 x = # "tot %q/to"; =
21 y = 2 / 3
22
23 # Regexp after whitelisted method.
24 "abc".sub /b/, 'd'
25
26 # Don't mis-match "sub" at the end of words.
27 a = asub / aslb + bsub / bslb;
28
29 # Highlight the regexp after "if".
30 x = toto / foo if /do bar/ =~ "dobar"
31
32 bar(class: XXX) do # ruby-indent-keyword-label
33 foo
34 end
35 bar
36
37 foo = [1, # ruby-deep-indent
38 2]
39
40 foo = { # ruby-deep-indent-disabled
41 a: b
42 }
43
44 foo = { a: b,
45 a1: b1
46 }
47
48 foo({ # bug#16118
49 a: b,
50 c: d
51 })
52
53 bar = foo(
54 a, [
55 1,
56 ],
57 :qux => [
58 3
59 ])
60
61 foo(
62 [
63 {
64 a: b
65 },
66 ],
67 {
68 c: d
69 }
70 )
71
72 foo([{
73 a: 2
74 },
75 {
76 b: 3
77 },
78 4
79 ])
80
81 foo = [ # ruby-deep-indent-disabled
82 1
83 ]
84
85 foo( # ruby-deep-indent-disabled
86 a
87 )
88
89 # Multiline regexp.
90 /bars
91 tees # toots
92 nfoos/
93
94 def test1(arg)
95 puts "hello"
96 end
97
98 def test2 (arg)
99 a = "apple"
100
101 if a == 2
102 puts "hello"
103 else
104 puts "there"
105 end
106
107 if a == 2 then
108 puts "hello"
109 elsif a == 3
110 puts "hello3"
111 elsif a == 3 then
112 puts "hello3"
113 else
114 puts "there"
115 end
116
117 b = case a
118 when "a"
119 6
120 # Support for this syntax was removed in Ruby 1.9, so we
121 # probably don't need to handle it either.
122 # when "b" :
123 # 7
124 # when "c" : 2
125 when "d" then 4
126 else 5
127 end
128 end
129
130 # Some Cucumber code:
131 Given /toto/ do
132 print "hello"
133 end
134
135 # Bug#15208
136 if something == :==
137 do_something
138
139 return false unless method == :+
140 x = y + z # Bug#16609
141
142 a = 1 ? 2 :(
143 2 + 3
144 )
145 end
146
147 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
148 d = 4 + 5 + # no '\' needed
149 6 + 7
150
151 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
152 e = 8 + 9 \
153 + 10 # '\' needed
154
155 foo = obj.bar { |m| tee(m) } +
156 obj.qux { |m| hum(m) }
157
158 begin
159 foo
160 ensure
161 bar
162 end
163
164 # Bug#15369
165 MSG = 'Separate every 3 digits in the integer portion of a number' \
166 'with underscores(_).'
167
168 class C
169 def foo
170 self.end
171 D.new.class
172 end
173 end
174
175 a = foo(j, k) -
176 bar_tee
177
178 while a < b do # "do" is optional
179 foo
180 end
181
182 desc "foo foo" \
183 "bar bar"
184
185 foo.
186 bar
187
188 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
189 foo
190 .bar
191
192 z = {
193 foo: {
194 a: "aaa",
195 b: "bbb"
196 }
197 }
198
199 foo if
200 bar
201
202 if foo?
203 bar
204 end
205
206 method arg1, # bug#15594
207 method2 arg2,
208 arg3
209
210 method? arg1,
211 arg2
212
213 method! arg1,
214 arg2
215
216 method !arg1,
217 arg2
218
219 method [],
220 arg2
221
222 method :foo,
223 :bar
224
225 method (a + b),
226 c, :d => :e,
227 f: g
228
229 desc "abc",
230 defg
231
232 it "is a method call with block" do |asd|
233 foo
234 end
235
236 it("is too!") {
237 bar
238 .qux
239 }
240
241 and_this_one(has) { |block, parameters|
242 tee
243 }
244
245 if foo &&
246 bar
247 end
248
249 foo +
250 bar
251
252 foo and
253 bar
254
255 foo > bar &&
256 tee < qux
257
258 zux do
259 foo == bar and
260 tee == qux
261 end
262
263 foo ^
264 bar
265
266 foo_bar_tee(1, 2, 3)
267 .qux.bar
268 .tee
269
270 foo do
271 bar
272 .tee
273 end
274
275 def bar
276 foo
277 .baz
278 end
279
280 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
281 tee = if foo
282 bar
283 else
284 tee
285 end
286
287 a = b {
288 c
289 }
290
291 aa = bb do
292 cc
293 end
294
295 foo :bar do
296 qux
297 end
298
299 foo do |*args|
300 tee
301 end
302
303 bar do |&block|
304 tee
305 end
306
307 foo = [1, 2, 3].map do |i|
308 i + 1
309 end
310
311 bar.foo do
312 bar
313 end
314
315 bar.foo(tee) do
316 bar
317 end
318
319 bar.foo(tee) {
320 bar
321 }
322
323 bar 1 do
324 foo 2 do
325 tee
326 end
327 end
328
329 foo |
330 bar
331
332 def qux
333 foo ||= begin
334 bar
335 tee
336 rescue
337 oomph
338 end
339 end
340
341 private def foo
342 bar
343 end
344
345 %^abc^
346 ddd
347
348 qux = foo.fee ?
349 bar :
350 tee
351
352 zoo.keep.bar!(
353 {x: y,
354 z: t})
355
356 zoo
357 .lose(
358 q, p)
359
360 a.records().map(&:b).zip(
361 foo)
362
363 # FIXME: This is not consistent with the example below it, but this
364 # ofset only happens if the colon is at eol, which wouldn't be often.
365 # Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
366 # distinguish from a variable reference inside a ternary operator.
367 foo(bar:
368 tee)
369
370 foo(:bar =>
371 tee)
372
373 {'a' => {
374 'b' => 'c',
375 'd' => %w(e f)
376 }
377 }