Fix bug#16609
[emacs.git] / test / indent / ruby.rb
blobcf6bcba8c393a88e70b02e8aef4892dcb6c0a998
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 bar(class: XXX) do              # ruby-indent-keyword-label
33   foo
34 end
35 bar
37 foo = [1,                       # ruby-deep-indent
38        2]
40 foo = {                         # ruby-deep-indent-disabled
41   a: b
44 foo = { a: b,
45         a1: b1
46       }
48 foo({                           # bug#16118
49       a: b,
50       c: d
51     })
53 bar = foo(
54   a, [
55     1,
56   ],
57   :qux => [
58     3
59   ])
61 foo(
62   [
63     {
64       a: b
65     },
66   ],
67   {
68     c: d
69   }
72 foo([{
73        a: 2
74      },
75      {
76        b: 3
77      },
78      4
79     ])
81 foo = [                         # ruby-deep-indent-disabled
82   1
85 foo(                            # ruby-deep-indent-disabled
86   a
89 # Multiline regexp.
90 /bars
91  tees # toots
92  nfoos/
94 def test1(arg)
95   puts "hello"
96 end
98 def test2 (arg)
99   a = "apple"
101   if a == 2
102     puts "hello"
103   else
104     puts "there"
105   end
107   if a == 2 then
108     puts "hello"
109   elsif a == 3
110     puts "hello3"
111   elsif a == 3 then
112     puts "hello3"
113   else
114     puts "there"
115   end
117   b = case a
118       when "a"
119         6
120       # Support for this syntax was removed in Ruby 1.9, so we
121       # probably don't need to handle it either.
122       # when "b" :
123       #   7
124       # when "c" : 2
125       when "d"  then 4
126       else 5
127       end
130 # Some Cucumber code:
131 Given /toto/ do
132   print "hello"
135 # Bug#15208
136 if something == :==
137   do_something
139   return false unless method == :+
140   x = y + z # Bug#16609
142   a = 1 ? 2 :(
143     2 + 3
144   )
147 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
148 d = 4 + 5 +      # no '\' needed
149     6 + 7
151 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
152 e = 8 + 9   \
153     + 10         # '\' needed
155 foo = obj.bar { |m| tee(m) } +
156       obj.qux { |m| hum(m) }
158 begin
159   foo
160 ensure
161   bar
164 # Bug#15369
165 MSG = 'Separate every 3 digits in the integer portion of a number' \
166       'with underscores(_).'
168 class C
169   def foo
170     self.end
171     D.new.class
172   end
175 a = foo(j, k) -
176     bar_tee
178 while a < b do # "do" is optional
179   foo
182 desc "foo foo" \
183      "bar bar"
185 foo.
186   bar
188 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
190   .bar
192 z = {
193   foo: {
194     a: "aaa",
195     b: "bbb"
196   }
199 foo if
200   bar
202 if foo?
203   bar
206 method arg1,                   # bug#15594
207        method2 arg2,
208                arg3
210 method? arg1,
211         arg2
213 method! arg1,
214         arg2
216 method !arg1,
217        arg2
219 method [],
220        arg2
222 method :foo,
223        :bar
225 method (a + b),
226        c, :d => :e,
227        f: g
229 desc "abc",
230      defg
232 it "is a method call with block" do |asd|
233   foo
236 it("is too!") {
237   bar
238     .qux
241 and_this_one(has) { |block, parameters|
242   tee
245 if foo &&
246    bar
249 foo +
250   bar
252 foo and
253   bar
255 foo > bar &&
256   tee < qux
258 zux do
259   foo == bar and
260     tee == qux
263 foo ^
264   bar
266 foo_bar_tee(1, 2, 3)
267   .qux.bar
268   .tee
270 foo do
271   bar
272     .tee
275 def bar
276   foo
277     .baz
280 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
281 tee = if foo
282         bar
283       else
284         tee
285       end
287 a = b {
288   c
291 aa = bb do
292   cc
295 foo :bar do
296   qux
299 foo do |*args|
300   tee
303 bar do |&block|
304   tee
307 foo = [1, 2, 3].map do |i|
308   i + 1
311 bar.foo do
312   bar
315 bar.foo(tee) do
316   bar
319 bar.foo(tee) {
320   bar
323 bar 1 do
324   foo 2 do
325     tee
326   end
329 foo |
330   bar
332 def qux
333   foo ||= begin
334             bar
335             tee
336           rescue
337             oomph
338           end
341 private def foo
342   bar
345 %^abc^
348 qux = foo.fee ?
349         bar :
350         tee
352 zoo.keep.bar!(
353   {x: y,
354    z: t})
357   .lose(
358     q, p)
360 foo(bar:
361       tee)
363 foo(:bar =>
364       tee)