* test/indent/ps-mode.ps: New file.
[bpt/emacs.git] / test / indent / ruby.rb
CommitLineData
34d1a133
SM
1if something_wrong? # ruby-move-to-block-skips-heredoc
2 ActiveSupport::Deprecation.warn(<<-eowarn)
3 boo hoo
4 end
5 eowarn
dca01b09 6 foo
34d1a133
SM
7end
8
c435f506
SM
9def foo
10 %^bar^
11end
12
f063063a 13# Percent literals.
dfbd787f 14b = %Q{This is a "string"}
f063063a 15c = %w!foo
dfbd787f 16 bar
f063063a
DG
17 baz!
18d = %(hello (nested) world)
19
20# Don't propertize percent literals inside strings.
21"(%s, %s)" % [123, 456]
22
5d03fb43
SM
23"abc/#{def}ghi"
24"abc\#{def}ghi"
25
f063063a
DG
26# Or inside comments.
27x = # "tot %q/to"; =
a9e4425b 28 y = 2 / 3
f063063a
DG
29
30# Regexp after whitelisted method.
31"abc".sub /b/, 'd'
32
33# Don't mis-match "sub" at the end of words.
34a = asub / aslb + bsub / bslb;
dfbd787f 35
f063063a
DG
36# Highlight the regexp after "if".
37x = toto / foo if /do bar/ =~ "dobar"
dfbd787f 38
1850913d
DG
39# Regexp options are highlighted.
40
41/foo/xi != %r{bar}mo.tee
42
34d1a133
SM
43bar(class: XXX) do # ruby-indent-keyword-label
44 foo
45end
46bar
47
48foo = [1, # ruby-deep-indent
49 2]
50
51foo = { # ruby-deep-indent-disabled
5cd9cda9 52 a: b
34d1a133
SM
53}
54
369bbf71 55foo = { a: b,
03d44565
SM
56 a1: b1
57 }
58
276bc333
DG
59foo({ # bug#16118
60 a: b,
61 c: d
5556c0ce 62 })
276bc333
DG
63
64bar = foo(
7e1549c9
DG
65 a, [
66 1,
67 ],
68 :qux => [
69 3
70 ])
276bc333
DG
71
72foo(
73 [
74 {
75 a: b
76 },
77 ],
78 {
79 c: d
80 }
81)
82
83foo([{
84 a: 2
85 },
86 {
87 b: 3
88 },
89 4
5556c0ce 90 ])
8c1ae481 91
34d1a133
SM
92foo = [ # ruby-deep-indent-disabled
93 1
94]
95
96foo( # ruby-deep-indent-disabled
97 a
98)
99
1a0a0a8a
DG
100# Multiline regexp.
101/bars
102 tees # toots
103 nfoos/
104
a9e4425b
SM
105def test1(arg)
106 puts "hello"
107end
108
109def test2 (arg)
110 a = "apple"
111
112 if a == 2
113 puts "hello"
114 else
115 puts "there"
116 end
117
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
127
b520f210
DG
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
a9e4425b
SM
139end
140
dfbd787f
SM
141# Some Cucumber code:
142Given /toto/ do
143 print "hello"
144end
88527bc0
DG
145
146# Bug#15208
147if something == :==
148 do_something
a09beb3d
DG
149
150 return false unless method == :+
151 x = y + z # Bug#16609
152
153 a = 1 ? 2 :(
154 2 + 3
155 )
88527bc0 156end
578c21bc 157
42ebc34e
DG
158# Bug#17097
159if x == :!=
160 something
161end
162
7ccae3b1
SM
163# Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
164d = 4 + 5 + # no '\' needed
165 6 + 7
166
167# Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
168e = 8 + 9 \
169 + 10 # '\' needed
170
1f923923
DG
171foo = obj.bar { |m| tee(m) } +
172 obj.qux { |m| hum(m) }
173
5cd9cda9
DG
174begin
175 foo
176ensure
177 bar
178end
179
578c21bc 180# Bug#15369
2f84ba10 181MSG = 'Separate every 3 digits in the integer portion of a number' \
34d1a133 182 'with underscores(_).'
5cd9cda9 183
7ccae3b1
SM
184class C
185 def foo
186 self.end
187 D.new.class
188 end
189end
5cd9cda9
DG
190
191a = foo(j, k) -
7ccae3b1 192 bar_tee
5cd9cda9
DG
193
194while a < b do # "do" is optional
195 foo
196end
197
198desc "foo foo" \
7ccae3b1 199 "bar bar"
7318480c
DG
200
201foo.
202 bar
203
2f84ba10 204# https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
7318480c
DG
205foo
206 .bar
2f84ba10
DG
207
208z = {
209 foo: {
210 a: "aaa",
211 b: "bbb"
212 }
213}
214
238150c8
DG
215foo if
216 bar
217
b68e2926
DG
218if foo?
219 bar
220end
221
1eda1d8d
DG
222method arg1, # bug#15594
223 method2 arg2,
224 arg3
225
226method? arg1,
227 arg2
228
229method! arg1,
230 arg2
231
bae91342
DG
232method !arg1,
233 arg2
234
235method [],
236 arg2
237
238method :foo,
239 :bar
240
241method (a + b),
242 c, :d => :e,
243 f: g
244
7e1549c9
DG
245desc "abc",
246 defg
247
8c1ae481
DG
248it "is a method call with block" do |asd|
249 foo
250end
251
252it("is too!") {
253 bar
f2351498 254 .qux
8c1ae481
DG
255}
256
257and_this_one(has) { |block, parameters|
258 tee
259}
260
c7e36328 261if foo &&
cfef16c0
DG
262 bar
263end
264
2f84ba10
DG
265foo +
266 bar
267
1d1c86da
DG
268foo and
269 bar
270
1f923923
DG
271foo > bar &&
272 tee < qux
273
274zux do
275 foo == bar and
276 tee == qux
277end
278
1d1c86da
DG
279foo ^
280 bar
281
18cacc39 282foo_bar_tee(1, 2, 3)
e2a67bd0
DG
283 .qux.bar
284 .tee
18cacc39 285
a6462ef5
DG
286foo do
287 bar
288 .tee
289end
290
291def bar
292 foo
293 .baz
294end
295
c8c605ac
DG
296# http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
297tee = if foo
298 bar
ce41edb4
DG
299 else
300 tee
c8c605ac
DG
301 end
302
3d42b968
DG
303a = b {
304 c
305}
306
307aa = bb do
308 cc
309end
310
41784d0b
DG
311foo :bar do
312 qux
313end
314
1f923923
DG
315foo do |*args|
316 tee
317end
318
319bar do |&block|
320 tee
321end
322
238150c8
DG
323foo = [1, 2, 3].map do |i|
324 i + 1
325end
55ee77a3 326
b420ccfc 327bar.foo do
18cacc39 328 bar
18cacc39 329end
1d1c86da 330
1f923923
DG
331bar.foo(tee) do
332 bar
333end
1d1c86da 334
1f923923 335bar.foo(tee) {
1d1c86da 336 bar
1f923923
DG
337}
338
339bar 1 do
340 foo 2 do
341 tee
342 end
1d1c86da 343end
d85783ca 344
7b08f97e
DG
345foo |
346 bar
347
1629a329
DG
348def qux
349 foo ||= begin
b520f210
DG
350 bar
351 tee
352 rescue
353 oomph
354 end
1629a329 355end
8212d9c0 356
fb3d479c
DG
357private def foo
358 bar
359end
360
8212d9c0
DG
361%^abc^
362ddd
35b249a6 363
e2a67bd0 364qux = foo.fee ?
35b249a6
DG
365 bar :
366 tee
7e1549c9
DG
367
368zoo.keep.bar!(
369 {x: y,
370 z: t})
371
372zoo
373 .lose(
e2a67bd0 374 q, p)
65a1da00 375
dfdb365c
DG
376a.records().map(&:b).zip(
377 foo)
378
379# FIXME: This is not consistent with the example below it, but this
9a89cfa2 380# offset only happens if the colon is at eol, which wouldn't be often.
dfdb365c
DG
381# Tokenizing `bar:' as `:bar =>' would be better, but it's hard to
382# distinguish from a variable reference inside a ternary operator.
65a1da00
DG
383foo(bar:
384 tee)
385
386foo(:bar =>
62f95022
DG
387 tee)
388
389{'a' => {
390 'b' => 'c',
391 'd' => %w(e f)
392 }
393}
1fc8f655
DG
394
395# Bug#17050
396
397return render json: {
398 errors: { base: [message] },
399 copying: copying
400 },
401 status: 400
402
403top test(
404 some,
405 top,
406 test)
407
408foo bar, {
409 tee: qux
410 }