A better fix for bug#21303
[emacs.git] / test / indent / ruby.rb
blobdec6de98605044ac4926220656d2d17673d72f06
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 def foo
10   %^bar^
11 end
13 # Percent literals.
14 b = %Q{This is a "string"}
15 c = %w!foo
16  bar
17  baz!
18 d = %(hello (nested) world)
20 # Don't propertize percent literals inside strings.
21 "(%s, %s)" % [123, 456]
23 "abc/#{def}ghi"
24 "abc\#{def}ghi"
26 # Or inside comments.
27 x = # "tot %q/to"; =
28   y = 2 / 3
30 # Regexp after whitelisted method.
31 "abc".sub /b/, 'd'
33 # Don't mis-match "sub" at the end of words.
34 a = asub / aslb + bsub / bslb;
36 # Highlight the regexp after "if".
37 x = toto / foo if /do bar/ =~ "dobar"
39 # Regexp options are highlighted.
41 /foo/xi != %r{bar}mo.tee
43 foo { /"tee/
44   bar { |qux| /'fee"/ }         # bug#20026
47 bar(class: XXX) do              # ruby-indent-keyword-label
48   foo
49 end
50 bar
52 foo = [1,                       # ruby-deep-indent
53        2]
55 foo = {                         # ruby-deep-indent-disabled
56   a: b
59 foo = { a: b,
60         a1: b1
61       }
63 foo({                           # bug#16118
64       a: b,
65       c: d
66     })
68 bar = foo(
69   a, [
70     1,
71   ],
72   :qux => [
73     3
74   ])
76 foo(
77   [
78     {
79       a: b
80     },
81   ],
82   {
83     c: d
84   }
87 foo([{
88        a: 2
89      },
90      {
91        b: 3
92      },
93      4
94     ])
96 foo = [                         # ruby-deep-indent-disabled
97   1
100 foo(                            # ruby-deep-indent-disabled
101   a
104 # Multiline regexp.
105 /bars
106  tees # toots
107  nfoos/
109 def test1(arg)
110   puts "hello"
113 def test2 (arg)
114   a = "apple"
116   if a == 2
117     puts "hello"
118   else
119     puts "there"
120   end
122   if a == 2 then
123     puts "hello"
124   elsif a == 3
125     puts "hello3"
126   elsif a == 3 then
127     puts "hello3"
128   else
129     puts "there"
130   end
132   b = case a
133       when "a"
134         6
135       # Support for this syntax was removed in Ruby 1.9, so we
136       # probably don't need to handle it either.
137       # when "b" :
138       #   7
139       # when "c" : 2
140       when "d"  then 4
141       else 5
142       end
145 # Some Cucumber code:
146 Given /toto/ do
147   print "hello"
150 # Bug#15208
151 if something == :==
152   do_something
154   return false unless method == :+
155   x = y + z # Bug#16609
157   a = 1 ? 2 :(
158     2 + 3
159   )
162 # Bug#17097
163 if x == :!=
164   something
167 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
168 d = 4 + 5 +      # no '\' needed
169     6 + 7
171 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
172 e = 8 + 9   \
173     + 10         # '\' needed
175 foo = obj.bar { |m| tee(m) } +
176       obj.qux { |m| hum(m) }
178 begin
179   foo
180 ensure
181   bar
184 # Bug#15369
185 MSG = 'Separate every 3 digits in the integer portion of a number' \
186       'with underscores(_).'
188 class C
189   def foo
190     self.end
191     D.new.class
192   end
195 a = foo(j, k) -
196     bar_tee
198 while a < b do # "do" is optional
199   foo
202 desc "foo foo" \
203      "bar bar"
205 foo.
206   bar
208 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
210   .bar
212 z = {
213   foo: {
214     a: "aaa",
215     b: "bbb"
216   }
219 foo if
220   bar
222 if foo?
223   bar
226 method arg1,                   # bug#15594
227        method2 arg2,
228                arg3
230 method? arg1,
231         arg2
233 method! arg1,
234         arg2
236 method !arg1,
237        arg2
239 method [],
240        arg2
242 method :foo,
243        :bar
245 method (a + b),
246        c, :d => :e,
247        f: g
249 desc "abc",
250      defg
252 it "is a method call with block" do |asd|
253   foo
256 it("is too!") {
257   bar
258     .qux
261 and_this_one(has) { |block, parameters|
262   tee
265 if foo &&
266    bar
269 foo +
270   bar
272 foo and
273   bar
275 foo > bar &&
276   tee < qux
278 zux do
279   foo == bar and
280     tee == qux
283 foo ^
284   bar
286 foo_bar_tee(1, 2, 3)
287   .qux.bar
288   .tee
290 foo do
291   bar
292     .tee
295 def bar
296   foo
297     .baz
300 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
301 tee = if foo
302         bar
303       else
304         tee
305       end
307 a = b {
308   c
311 aa = bb do
312   cc
315 foo :bar do
316   qux
319 foo do |*args|
320   tee
323 bar do |&block|
324   tee
327 foo = [1, 2, 3].map do |i|
328   i + 1
331 bar.foo do
332   bar
335 bar.foo(tee) do
336   bar
339 bar.foo(tee) {
340   bar
343 bar 1 do
344   foo 2 do
345     tee
346   end
349 foo |
350   bar
352 def qux
353   foo ||= begin
354             bar
355             tee
356           rescue
357             oomph
358           end
361 private def foo
362   bar
365 %^abc^
368 qux = foo.fee ?
369         bar :
370         tee
372 zoo.keep.bar!(
373   {x: y,
374    z: t})
377   .lose(
378     q, p)
380 a.records().map(&:b).zip(
381   foo)
383 # FIXME: This is not consistent with the example below it, but this
384 # offset only happens if the colon is at eol, which wouldn't be often.
385 # Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
386 # distinguish from a variable reference inside a ternary operator.
387 foo(bar:
388       tee)
390 foo(:bar =>
391     tee)
393 {'a' => {
394    'b' => 'c',
395    'd' => %w(e f)
399 # Bug#17050
401 return render json: {
402                 errors: { base: [message] },
403                 copying: copying
404               },
405               status: 400
407 top test(
408       some,
409       top,
410       test)
412 foo bar, {
413       tee: qux
414     }