Revert two accidental commits
[emacs.git] / test / manual / indent / ruby.rb
blobb038512b114cf0e0d58a0301519590a19ddda9b9
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/#{ddf}ghi"
28 "abc\#{ddf}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 qux :+,
172     bar,
173     :[]=,
174     bar,
175     :a
177 b = $:
178 c = ??
180 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
181 d = 4 + 5 +      # no '\' needed
182     6 + 7
184 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
185 e = 8 + 9   \
186     + 10         # '\' needed
188 foo = obj.bar { |m| tee(m) } +
189       obj.qux { |m| hum(m) }
191 begin
192   foo
193 ensure
194   bar
197 # Bug#15369
198 MSG = 'Separate every 3 digits in the integer portion of a number' \
199       'with underscores(_).'
201 class C
202   def foo
203     self.end
204     D.new.class
205   end
207   def begin
208   end
211 a = foo(j, k) -
212     bar_tee
214 while a < b do # "do" is optional
215   foo
218 desc "foo foo" \
219      "bar bar"
221 foo.
222   bar
224 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
225 foo # comment intended to confuse the tokenizer
226   .bar
228 z = {
229   foo: {
230     a: "aaa",
231     b: "bbb"
232   }
235 foo if
236   bar
238 fail "stuff" \
239   unless all_fine?
241 if foo?
242   bar
245 method arg1,                   # bug#15594
246        method2 arg2,
247                arg3
249 method? arg1,
250         arg2
252 method! arg1,
253         arg2
255 method !arg1,
256        arg2
258 method [],
259        arg2
261 method :foo,
262        :bar
264 method (a + b),
265        c, :d => :e,
266        f: g
268 desc "abc",
269      defg
271 it "is a method call with block" do |asd|
272   foo
275 it("is too!") {
276   bar
277     .qux
280 and_this_one(has) { |block, parameters|
281   tee
284 if foo &&
285    bar
288 foo +
289   bar
291 foo and
292   bar
294 foo > bar &&
295   tee < qux
297 zux do
298   foo == bar &&
299     tee == qux
301   a = 3 and
302     b = 4
305 foo + bar ==
306   tee + qux
308 1 .. 2 &&
309      3
311 3 < 4 +
312     5
314 10 << 4 ^
315   20
317 100 + 2 >>
318   3
320 2 ** 10 /
321   2
323 foo ^
324   bar
326 foo_bar_tee(1, 2, 3)
327   .qux&.bar
328   .tee.bar
329   &.tee
331 foo do
332   bar
333     .tee
336 def bar
337   foo
338     .baz
341 abc(foo
342       .bar,
343     tee
344       .qux)
346 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
347 tee = if foo
348         bar
349       else
350         tee
351       end
353 a = b {
354   c
357 aa = bb do
358   cc
361 foo :bar do
362   qux
365 foo do |*args|
366   tee
369 bar do |&block|
370   tee
373 foo = [1, 2, 3].map do |i|
374   i + 1
377 bar.foo do
378   bar
381 bar.foo(tee) do
382   bar
385 bar.foo(tee) {
386   bar
389 bar 1 do
390   foo 2 do
391     tee
392   end
395 foo |
396   bar
398 def qux
399   foo ||= begin
400             bar
401             tee
402           rescue
403             oomph
404           end
407 private def foo
408   bar
411 %^abc^
414 qux = foo.fee ?
415         bar :
416         tee
418 zoo.keep.bar!(
419   {x: y,
420    z: t})
423   .lose(
424     q, p)
426 a.records().map(&:b).zip(
427   foo)
429 foo1 =
430   subject.update(
431     1
432   )
434 foo2 =
435   subject.
436     update(
437       2
438     )
440 # FIXME: This is not consistent with the example below it, but this
441 # offset only happens if the colon is at eol, which wouldn't be often.
442 # Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
443 # distinguish from a variable reference inside a ternary operator.
444 foo(bar:
445       tee)
447 foo(:bar =>
448     tee)
450 regions = foo(
451   OpenStruct.new(id: 0, name: "foo") => [
452     10
453   ]
456 {'a' => {
457    'b' => 'c',
458    'd' => %w(e f)
462 # Bug#17050
464 return render json: {
465                 errors: { base: [message] },
466                 copying: copying
467               },
468               status: 400
470 top test(
471       some,
472       top,
473       test)
475 foo bar, {
476       tee: qux
477     }