* test/indent/ps-mode.ps: New file.
[bpt/emacs.git] / test / indent / ruby.rb
... / ...
CommitLineData
1if something_wrong? # ruby-move-to-block-skips-heredoc
2 ActiveSupport::Deprecation.warn(<<-eowarn)
3 boo hoo
4 end
5 eowarn
6 foo
7end
8
9def foo
10 %^bar^
11end
12
13# Percent literals.
14b = %Q{This is a "string"}
15c = %w!foo
16 bar
17 baz!
18d = %(hello (nested) world)
19
20# Don't propertize percent literals inside strings.
21"(%s, %s)" % [123, 456]
22
23"abc/#{def}ghi"
24"abc\#{def}ghi"
25
26# Or inside comments.
27x = # "tot %q/to"; =
28 y = 2 / 3
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;
35
36# Highlight the regexp after "if".
37x = toto / foo if /do bar/ =~ "dobar"
38
39# Regexp options are highlighted.
40
41/foo/xi != %r{bar}mo.tee
42
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
52 a: b
53}
54
55foo = { a: b,
56 a1: b1
57 }
58
59foo({ # bug#16118
60 a: b,
61 c: d
62 })
63
64bar = foo(
65 a, [
66 1,
67 ],
68 :qux => [
69 3
70 ])
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
90 ])
91
92foo = [ # ruby-deep-indent-disabled
93 1
94]
95
96foo( # ruby-deep-indent-disabled
97 a
98)
99
100# Multiline regexp.
101/bars
102 tees # toots
103 nfoos/
104
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
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
139end
140
141# Some Cucumber code:
142Given /toto/ do
143 print "hello"
144end
145
146# Bug#15208
147if something == :==
148 do_something
149
150 return false unless method == :+
151 x = y + z # Bug#16609
152
153 a = 1 ? 2 :(
154 2 + 3
155 )
156end
157
158# Bug#17097
159if x == :!=
160 something
161end
162
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
171foo = obj.bar { |m| tee(m) } +
172 obj.qux { |m| hum(m) }
173
174begin
175 foo
176ensure
177 bar
178end
179
180# Bug#15369
181MSG = 'Separate every 3 digits in the integer portion of a number' \
182 'with underscores(_).'
183
184class C
185 def foo
186 self.end
187 D.new.class
188 end
189end
190
191a = foo(j, k) -
192 bar_tee
193
194while a < b do # "do" is optional
195 foo
196end
197
198desc "foo foo" \
199 "bar bar"
200
201foo.
202 bar
203
204# https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
205foo
206 .bar
207
208z = {
209 foo: {
210 a: "aaa",
211 b: "bbb"
212 }
213}
214
215foo if
216 bar
217
218if foo?
219 bar
220end
221
222method arg1, # bug#15594
223 method2 arg2,
224 arg3
225
226method? arg1,
227 arg2
228
229method! arg1,
230 arg2
231
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
245desc "abc",
246 defg
247
248it "is a method call with block" do |asd|
249 foo
250end
251
252it("is too!") {
253 bar
254 .qux
255}
256
257and_this_one(has) { |block, parameters|
258 tee
259}
260
261if foo &&
262 bar
263end
264
265foo +
266 bar
267
268foo and
269 bar
270
271foo > bar &&
272 tee < qux
273
274zux do
275 foo == bar and
276 tee == qux
277end
278
279foo ^
280 bar
281
282foo_bar_tee(1, 2, 3)
283 .qux.bar
284 .tee
285
286foo do
287 bar
288 .tee
289end
290
291def bar
292 foo
293 .baz
294end
295
296# http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
297tee = if foo
298 bar
299 else
300 tee
301 end
302
303a = b {
304 c
305}
306
307aa = bb do
308 cc
309end
310
311foo :bar do
312 qux
313end
314
315foo do |*args|
316 tee
317end
318
319bar do |&block|
320 tee
321end
322
323foo = [1, 2, 3].map do |i|
324 i + 1
325end
326
327bar.foo do
328 bar
329end
330
331bar.foo(tee) do
332 bar
333end
334
335bar.foo(tee) {
336 bar
337}
338
339bar 1 do
340 foo 2 do
341 tee
342 end
343end
344
345foo |
346 bar
347
348def qux
349 foo ||= begin
350 bar
351 tee
352 rescue
353 oomph
354 end
355end
356
357private def foo
358 bar
359end
360
361%^abc^
362ddd
363
364qux = foo.fee ?
365 bar :
366 tee
367
368zoo.keep.bar!(
369 {x: y,
370 z: t})
371
372zoo
373 .lose(
374 q, p)
375
376a.records().map(&:b).zip(
377 foo)
378
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.
383foo(bar:
384 tee)
385
386foo(:bar =>
387 tee)
388
389{'a' => {
390 'b' => 'c',
391 'd' => %w(e f)
392 }
393}
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 }