Fix bug#17050
[emacs.git] / test / indent / ruby.rb
blob9948243314562e206a774b72f38c0676a6b03fa6
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
9 # Percent literals.
10 b = %Q{This is a "string"}
11 c = %w!foo
12  bar
13  baz!
14 d = %(hello (nested) world)
16 # Don't propertize percent literals inside strings.
17 "(%s, %s)" % [123, 456]
19 # Or inside comments.
20 x = # "tot %q/to"; =
21   y = 2 / 3
23 # Regexp after whitelisted method.
24 "abc".sub /b/, 'd'
26 # Don't mis-match "sub" at the end of words.
27 a = asub / aslb + bsub / bslb;
29 # Highlight the regexp after "if".
30 x = toto / foo if /do bar/ =~ "dobar"
32 # Regexp options are highlighted.
34 /foo/xi != %r{bar}mo.tee
36 bar(class: XXX) do              # ruby-indent-keyword-label
37   foo
38 end
39 bar
41 foo = [1,                       # ruby-deep-indent
42        2]
44 foo = {                         # ruby-deep-indent-disabled
45   a: b
48 foo = { a: b,
49         a1: b1
50       }
52 foo({                           # bug#16118
53       a: b,
54       c: d
55     })
57 bar = foo(
58   a, [
59     1,
60   ],
61   :qux => [
62     3
63   ])
65 foo(
66   [
67     {
68       a: b
69     },
70   ],
71   {
72     c: d
73   }
76 foo([{
77        a: 2
78      },
79      {
80        b: 3
81      },
82      4
83     ])
85 foo = [                         # ruby-deep-indent-disabled
86   1
89 foo(                            # ruby-deep-indent-disabled
90   a
93 # Multiline regexp.
94 /bars
95  tees # toots
96  nfoos/
98 def test1(arg)
99   puts "hello"
102 def test2 (arg)
103   a = "apple"
105   if a == 2
106     puts "hello"
107   else
108     puts "there"
109   end
111   if a == 2 then
112     puts "hello"
113   elsif a == 3
114     puts "hello3"
115   elsif a == 3 then
116     puts "hello3"
117   else
118     puts "there"
119   end
121   b = case a
122       when "a"
123         6
124       # Support for this syntax was removed in Ruby 1.9, so we
125       # probably don't need to handle it either.
126       # when "b" :
127       #   7
128       # when "c" : 2
129       when "d"  then 4
130       else 5
131       end
134 # Some Cucumber code:
135 Given /toto/ do
136   print "hello"
139 # Bug#15208
140 if something == :==
141   do_something
143   return false unless method == :+
144   x = y + z # Bug#16609
146   a = 1 ? 2 :(
147     2 + 3
148   )
151 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
152 d = 4 + 5 +      # no '\' needed
153     6 + 7
155 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
156 e = 8 + 9   \
157     + 10         # '\' needed
159 foo = obj.bar { |m| tee(m) } +
160       obj.qux { |m| hum(m) }
162 begin
163   foo
164 ensure
165   bar
168 # Bug#15369
169 MSG = 'Separate every 3 digits in the integer portion of a number' \
170       'with underscores(_).'
172 class C
173   def foo
174     self.end
175     D.new.class
176   end
179 a = foo(j, k) -
180     bar_tee
182 while a < b do # "do" is optional
183   foo
186 desc "foo foo" \
187      "bar bar"
189 foo.
190   bar
192 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
194   .bar
196 z = {
197   foo: {
198     a: "aaa",
199     b: "bbb"
200   }
203 foo if
204   bar
206 if foo?
207   bar
210 method arg1,                   # bug#15594
211        method2 arg2,
212                arg3
214 method? arg1,
215         arg2
217 method! arg1,
218         arg2
220 method !arg1,
221        arg2
223 method [],
224        arg2
226 method :foo,
227        :bar
229 method (a + b),
230        c, :d => :e,
231        f: g
233 desc "abc",
234      defg
236 it "is a method call with block" do |asd|
237   foo
240 it("is too!") {
241   bar
242     .qux
245 and_this_one(has) { |block, parameters|
246   tee
249 if foo &&
250    bar
253 foo +
254   bar
256 foo and
257   bar
259 foo > bar &&
260   tee < qux
262 zux do
263   foo == bar and
264     tee == qux
267 foo ^
268   bar
270 foo_bar_tee(1, 2, 3)
271   .qux.bar
272   .tee
274 foo do
275   bar
276     .tee
279 def bar
280   foo
281     .baz
284 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
285 tee = if foo
286         bar
287       else
288         tee
289       end
291 a = b {
292   c
295 aa = bb do
296   cc
299 foo :bar do
300   qux
303 foo do |*args|
304   tee
307 bar do |&block|
308   tee
311 foo = [1, 2, 3].map do |i|
312   i + 1
315 bar.foo do
316   bar
319 bar.foo(tee) do
320   bar
323 bar.foo(tee) {
324   bar
327 bar 1 do
328   foo 2 do
329     tee
330   end
333 foo |
334   bar
336 def qux
337   foo ||= begin
338             bar
339             tee
340           rescue
341             oomph
342           end
345 private def foo
346   bar
349 %^abc^
352 qux = foo.fee ?
353         bar :
354         tee
356 zoo.keep.bar!(
357   {x: y,
358    z: t})
361   .lose(
362     q, p)
364 a.records().map(&:b).zip(
365   foo)
367 # FIXME: This is not consistent with the example below it, but this
368 # offset only happens if the colon is at eol, which wouldn't be often.
369 # Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
370 # distinguish from a variable reference inside a ternary operator.
371 foo(bar:
372       tee)
374 foo(:bar =>
375     tee)
377 {'a' => {
378    'b' => 'c',
379    'd' => %w(e f)
383 # Bug#17050
385 return render json: {
386                 errors: { base: [message] },
387                 copying: copying
388               },
389               status: 400
391 top test(
392       some,
393       top,
394       test)
396 foo bar, {
397       tee: qux
398     }