Merge from origin/emacs-24
[emacs.git] / test / indent / ruby.rb
blob82cc63f916839a82f3a91f57100f0177b94f3f82
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 bar(class: XXX) do              # ruby-indent-keyword-label
44   foo
45 end
46 bar
48 foo = [1,                       # ruby-deep-indent
49        2]
51 foo = {                         # ruby-deep-indent-disabled
52   a: b
55 foo = { a: b,
56         a1: b1
57       }
59 foo({                           # bug#16118
60       a: b,
61       c: d
62     })
64 bar = foo(
65   a, [
66     1,
67   ],
68   :qux => [
69     3
70   ])
72 foo(
73   [
74     {
75       a: b
76     },
77   ],
78   {
79     c: d
80   }
83 foo([{
84        a: 2
85      },
86      {
87        b: 3
88      },
89      4
90     ])
92 foo = [                         # ruby-deep-indent-disabled
93   1
96 foo(                            # ruby-deep-indent-disabled
97   a
100 # Multiline regexp.
101 /bars
102  tees # toots
103  nfoos/
105 def test1(arg)
106   puts "hello"
109 def test2 (arg)
110   a = "apple"
112   if a == 2
113     puts "hello"
114   else
115     puts "there"
116   end
118   if a == 2 then
119     puts "hello"
120   elsif a == 3
121     puts "hello3"
122   elsif a == 3 then
123     puts "hello3"
124   else
125     puts "there"
126   end
128   b = case a
129       when "a"
130         6
131       # Support for this syntax was removed in Ruby 1.9, so we
132       # probably don't need to handle it either.
133       # when "b" :
134       #   7
135       # when "c" : 2
136       when "d"  then 4
137       else 5
138       end
141 # Some Cucumber code:
142 Given /toto/ do
143   print "hello"
146 # Bug#15208
147 if something == :==
148   do_something
150   return false unless method == :+
151   x = y + z # Bug#16609
153   a = 1 ? 2 :(
154     2 + 3
155   )
158 # Bug#17097
159 if x == :!=
160   something
163 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
164 d = 4 + 5 +      # no '\' needed
165     6 + 7
167 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
168 e = 8 + 9   \
169     + 10         # '\' needed
171 foo = obj.bar { |m| tee(m) } +
172       obj.qux { |m| hum(m) }
174 begin
175   foo
176 ensure
177   bar
180 # Bug#15369
181 MSG = 'Separate every 3 digits in the integer portion of a number' \
182       'with underscores(_).'
184 class C
185   def foo
186     self.end
187     D.new.class
188   end
191 a = foo(j, k) -
192     bar_tee
194 while a < b do # "do" is optional
195   foo
198 desc "foo foo" \
199      "bar bar"
201 foo.
202   bar
204 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
206   .bar
208 z = {
209   foo: {
210     a: "aaa",
211     b: "bbb"
212   }
215 foo if
216   bar
218 if foo?
219   bar
222 method arg1,                   # bug#15594
223        method2 arg2,
224                arg3
226 method? arg1,
227         arg2
229 method! arg1,
230         arg2
232 method !arg1,
233        arg2
235 method [],
236        arg2
238 method :foo,
239        :bar
241 method (a + b),
242        c, :d => :e,
243        f: g
245 desc "abc",
246      defg
248 it "is a method call with block" do |asd|
249   foo
252 it("is too!") {
253   bar
254     .qux
257 and_this_one(has) { |block, parameters|
258   tee
261 if foo &&
262    bar
265 foo +
266   bar
268 foo and
269   bar
271 foo > bar &&
272   tee < qux
274 zux do
275   foo == bar and
276     tee == qux
279 foo ^
280   bar
282 foo_bar_tee(1, 2, 3)
283   .qux.bar
284   .tee
286 foo do
287   bar
288     .tee
291 def bar
292   foo
293     .baz
296 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
297 tee = if foo
298         bar
299       else
300         tee
301       end
303 a = b {
304   c
307 aa = bb do
308   cc
311 foo :bar do
312   qux
315 foo do |*args|
316   tee
319 bar do |&block|
320   tee
323 foo = [1, 2, 3].map do |i|
324   i + 1
327 bar.foo do
328   bar
331 bar.foo(tee) do
332   bar
335 bar.foo(tee) {
336   bar
339 bar 1 do
340   foo 2 do
341     tee
342   end
345 foo |
346   bar
348 def qux
349   foo ||= begin
350             bar
351             tee
352           rescue
353             oomph
354           end
357 private def foo
358   bar
361 %^abc^
364 qux = foo.fee ?
365         bar :
366         tee
368 zoo.keep.bar!(
369   {x: y,
370    z: t})
373   .lose(
374     q, p)
376 a.records().map(&:b).zip(
377   foo)
379 # FIXME: This is not consistent with the example below it, but this
380 # offset only happens if the colon is at eol, which wouldn't be often.
381 # Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
382 # distinguish from a variable reference inside a ternary operator.
383 foo(bar:
384       tee)
386 foo(:bar =>
387     tee)
389 {'a' => {
390    'b' => 'c',
391    'd' => %w(e f)
395 # Bug#17050
397 return render json: {
398                 errors: { base: [message] },
399                 copying: copying
400               },
401               status: 400
403 top test(
404       some,
405       top,
406       test)
408 foo bar, {
409       tee: qux
410     }