Implement user option ruby-align-chained-calls
[emacs.git] / test / indent / ruby.rb
blob49ed92f8fdcec87483db4efa05bdfa08367365ba
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
140 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
141 d = 4 + 5 +      # no '\' needed
142     6 + 7
144 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
145 e = 8 + 9   \
146     + 10         # '\' needed
148 foo = obj.bar { |m| tee(m) } +
149       obj.qux { |m| hum(m) }
151 begin
152   foo
153 ensure
154   bar
157 # Bug#15369
158 MSG = 'Separate every 3 digits in the integer portion of a number' \
159       'with underscores(_).'
161 class C
162   def foo
163     self.end
164     D.new.class
165   end
168 a = foo(j, k) -
169     bar_tee
171 while a < b do # "do" is optional
172   foo
175 desc "foo foo" \
176      "bar bar"
178 foo.
179   bar
181 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
183   .bar
185 z = {
186   foo: {
187     a: "aaa",
188     b: "bbb"
189   }
192 foo if
193   bar
195 if foo?
196   bar
199 method arg1,                   # bug#15594
200        method2 arg2,
201                arg3
203 method? arg1,
204         arg2
206 method! arg1,
207         arg2
209 method !arg1,
210        arg2
212 method [],
213        arg2
215 method :foo,
216        :bar
218 method (a + b),
219        c, :d => :e,
220        f: g
222 desc "abc",
223      defg
225 it "is a method call with block" do |asd|
226   foo
229 it("is too!") {
230   bar
231     .qux
234 and_this_one(has) { |block, parameters|
235   tee
238 if foo &&
239    bar
242 foo +
243   bar
245 foo and
246   bar
248 foo > bar &&
249   tee < qux
251 zux do
252   foo == bar and
253     tee == qux
256 foo ^
257   bar
259 foo_bar_tee(1, 2, 3)
260   .qux.bar
261   .tee
263 foo do
264   bar
265     .tee
268 def bar
269   foo
270     .baz
273 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
274 tee = if foo
275         bar
276       else
277         tee
278       end
280 a = b {
281   c
284 aa = bb do
285   cc
288 foo :bar do
289   qux
292 foo do |*args|
293   tee
296 bar do |&block|
297   tee
300 foo = [1, 2, 3].map do |i|
301   i + 1
304 bar.foo do
305   bar
308 bar.foo(tee) do
309   bar
312 bar.foo(tee) {
313   bar
316 bar 1 do
317   foo 2 do
318     tee
319   end
322 foo |
323   bar
325 def qux
326   foo ||= begin
327             bar
328             tee
329           rescue
330             oomph
331           end
334 private def foo
335   bar
338 %^abc^
341 qux = foo.fee ?
342         bar :
343         tee
345 zoo.keep.bar!(
346   {x: y,
347    z: t})
350   .lose(
351     q, p)
353 foo(bar:
354       tee)
356 foo(:bar =>
357       tee)