Recognize iuwu-mod after an escaped newline
[emacs.git] / test / indent / ruby.rb
blob49478602e41414f9dbd0e68ec110c0340c704bda
1 if something_wrong?             # ruby-move-to-block-skips-heredoc
2   ActiveSupport::Deprecation.warn(<<-eowarn)
3   boo hoo
4   end
5   eowarn
6   foo
8   foo(<<~squiggly)
9   end
10   squiggly
11 end
13 def foo
14   %^bar^
15 end
17 # Percent literals.
18 b = %Q{This is a "string"}
19 c = %w!foo
20  bar
21  baz!
22 d = %(hello (nested) world)
24 # Don't propertize percent literals inside strings.
25 "(%s, %s)" % [123, 456]
27 "abc/#{def}ghi"
28 "abc\#{def}ghi"
30 # Or inside comments.
31 x = # "tot %q/to"; =
32   y = 2 / 3
34 # Regexp after whitelisted method.
35 "abc".sub /b/, 'd'
37 # Don't mis-match "sub" at the end of words.
38 a = asub / aslb + bsub / bslb;
40 # Highlight the regexp after "if".
41 x = toto / foo if /do bar/ =~ "dobar"
43 # Regexp options are highlighted.
45 /foo/xi != %r{bar}mo.tee
47 foo { /"tee/
48   bar { |qux| /'fee"/ }         # bug#20026
51 bar(class: XXX) do              # ruby-indent-keyword-label
52   foo
53 end
54 bar
56 foo = [1,                       # ruby-deep-indent
57        2]
59 foo = {                         # ruby-deep-indent-disabled
60   a: b
63 foo = { a: b,
64         a1: b1
65       }
67 foo({                           # bug#16118
68       a: b,
69       c: d
70     })
72 bar = foo(
73   a, [
74     1,
75   ],
76   :qux => [
77     3
78   ])
80 foo(
81   [
82     {
83       a: b
84     },
85   ],
86   {
87     c: d
88   }
91 foo([{
92        a: 2
93      },
94      {
95        b: 3
96      },
97      4
98     ])
100 foo = [                         # ruby-deep-indent-disabled
101   1
104 foo(                            # ruby-deep-indent-disabled
105   a
108 # Multiline regexp.
109 /bars
110  tees # toots
111  nfoos/
113 def test1(arg)
114   puts "hello"
117 def test2 (arg)
118   a = "apple"
120   if a == 2
121     puts "hello"
122   else
123     puts "there"
124   end
126   if a == 2 then
127     puts "hello"
128   elsif a == 3
129     puts "hello3"
130   elsif a == 3 then
131     puts "hello3"
132   else
133     puts "there"
134   end
136   b = case a
137       when "a"
138         6
139       # Support for this syntax was removed in Ruby 1.9, so we
140       # probably don't need to handle it either.
141       # when "b" :
142       #   7
143       # when "c" : 2
144       when "d"  then 4
145       else 5
146       end
149 # Some Cucumber code:
150 Given /toto/ do
151   print "hello"
154 # Bug#15208
155 if something == :==
156   do_something
158   return false unless method == :+
159   x = y + z # Bug#16609
161   a = 1 ? 2 :(
162     2 + 3
163   )
166 # Bug#17097
167 if x == :!=
168   something
171 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
172 d = 4 + 5 +      # no '\' needed
173     6 + 7
175 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
176 e = 8 + 9   \
177     + 10         # '\' needed
179 foo = obj.bar { |m| tee(m) } +
180       obj.qux { |m| hum(m) }
182 begin
183   foo
184 ensure
185   bar
188 # Bug#15369
189 MSG = 'Separate every 3 digits in the integer portion of a number' \
190       'with underscores(_).'
192 class C
193   def foo
194     self.end
195     D.new.class
196   end
199 a = foo(j, k) -
200     bar_tee
202 while a < b do # "do" is optional
203   foo
206 desc "foo foo" \
207      "bar bar"
209 foo.
210   bar
212 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
214   .bar
216 z = {
217   foo: {
218     a: "aaa",
219     b: "bbb"
220   }
223 foo if
224   bar
226 fail "stuff" \
227   unless all_fine?
229 if foo?
230   bar
233 method arg1,                   # bug#15594
234        method2 arg2,
235                arg3
237 method? arg1,
238         arg2
240 method! arg1,
241         arg2
243 method !arg1,
244        arg2
246 method [],
247        arg2
249 method :foo,
250        :bar
252 method (a + b),
253        c, :d => :e,
254        f: g
256 desc "abc",
257      defg
259 it "is a method call with block" do |asd|
260   foo
263 it("is too!") {
264   bar
265     .qux
268 and_this_one(has) { |block, parameters|
269   tee
272 if foo &&
273    bar
276 foo +
277   bar
279 foo and
280   bar
282 foo > bar &&
283   tee < qux
285 zux do
286   foo == bar and
287     tee == qux
290 foo ^
291   bar
293 foo_bar_tee(1, 2, 3)
294   .qux.bar
295   .tee
297 foo do
298   bar
299     .tee
302 def bar
303   foo
304     .baz
307 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
308 tee = if foo
309         bar
310       else
311         tee
312       end
314 a = b {
315   c
318 aa = bb do
319   cc
322 foo :bar do
323   qux
326 foo do |*args|
327   tee
330 bar do |&block|
331   tee
334 foo = [1, 2, 3].map do |i|
335   i + 1
338 bar.foo do
339   bar
342 bar.foo(tee) do
343   bar
346 bar.foo(tee) {
347   bar
350 bar 1 do
351   foo 2 do
352     tee
353   end
356 foo |
357   bar
359 def qux
360   foo ||= begin
361             bar
362             tee
363           rescue
364             oomph
365           end
368 private def foo
369   bar
372 %^abc^
375 qux = foo.fee ?
376         bar :
377         tee
379 zoo.keep.bar!(
380   {x: y,
381    z: t})
384   .lose(
385     q, p)
387 a.records().map(&:b).zip(
388   foo)
390 # FIXME: This is not consistent with the example below it, but this
391 # offset only happens if the colon is at eol, which wouldn't be often.
392 # Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
393 # distinguish from a variable reference inside a ternary operator.
394 foo(bar:
395       tee)
397 foo(:bar =>
398     tee)
400 {'a' => {
401    'b' => 'c',
402    'd' => %w(e f)
406 # Bug#17050
408 return render json: {
409                 errors: { base: [message] },
410                 copying: copying
411               },
412               status: 400
414 top test(
415       some,
416       top,
417       test)
419 foo bar, {
420       tee: qux
421     }