* lisp/progmodes/ruby-mode.el (ruby-smie--bosp): Anything that goes
[bpt/emacs.git] / test / indent / ruby.rb
1 if something_wrong? # ruby-move-to-block-skips-heredoc
2 ActiveSupport::Deprecation.warn(<<-eowarn)
3 boo hoo
4 end
5 eowarn
6 end
7
8 # Percent literals.
9 b = %Q{This is a "string"}
10 c = %w!foo
11 bar
12 baz!
13 d = %(hello (nested) world)
14
15 # Don't propertize percent literals inside strings.
16 "(%s, %s)" % [123, 456]
17
18 # Or inside comments.
19 x = # "tot %q/to"; =
20 y = 2 / 3
21
22 # Regexp after whitelisted method.
23 "abc".sub /b/, 'd'
24
25 # Don't mis-match "sub" at the end of words.
26 a = asub / aslb + bsub / bslb;
27
28 # Highlight the regexp after "if".
29 x = toto / foo if /do bar/ =~ "dobar"
30
31 bar(class: XXX) do # ruby-indent-keyword-label
32 foo
33 end
34 bar
35
36 foo = [1, # ruby-deep-indent
37 2]
38
39 foo = { # ruby-deep-indent-disabled
40 a: b
41 }
42
43 foo = { a: b,
44 a1: b1
45 }
46
47 foo({
48 a: b,
49 c: d
50 })
51
52 foo = [ # ruby-deep-indent-disabled
53 1
54 ]
55
56 foo( # ruby-deep-indent-disabled
57 a
58 )
59
60 # Multiline regexp.
61 /bars
62 tees # toots
63 nfoos/
64
65 def test1(arg)
66 puts "hello"
67 end
68
69 def test2 (arg)
70 a = "apple"
71
72 if a == 2
73 puts "hello"
74 else
75 puts "there"
76 end
77
78 if a == 2 then
79 puts "hello"
80 elsif a == 3
81 puts "hello3"
82 elsif a == 3 then
83 puts "hello3"
84 else
85 puts "there"
86 end
87
88 case a
89 when "a"
90 6
91 # Support for this syntax was removed in Ruby 1.9, so we
92 # probably don't need to handle it either.
93 # when "b" :
94 # 7
95 # when "c" : 2
96 when "d" then 4
97 else 5
98 end
99 end
100
101 # Some Cucumber code:
102 Given /toto/ do
103 print "hello"
104 end
105
106 # Bug#15208
107 if something == :==
108 do_something
109 end
110
111 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
112 d = 4 + 5 + # no '\' needed
113 6 + 7
114
115 # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
116 e = 8 + 9 \
117 + 10 # '\' needed
118
119 begin
120 foo
121 ensure
122 bar
123 end
124
125 # Bug#15369
126 MSG = 'Separate every 3 digits in the integer portion of a number' \
127 'with underscores(_).'
128
129 class C
130 def foo
131 self.end
132 D.new.class
133 end
134 end
135
136 a = foo(j, k) -
137 bar_tee
138
139 while a < b do # "do" is optional
140 foo
141 end
142
143 desc "foo foo" \
144 "bar bar"
145
146 foo.
147 bar
148
149 # https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
150 foo
151 .bar
152
153 z = {
154 foo: {
155 a: "aaa",
156 b: "bbb"
157 }
158 }
159
160 foo if
161 bar
162
163 if foo?
164 bar
165 end
166
167 method arg1, # bug#15594
168 method2 arg2,
169 arg3
170
171 method? arg1,
172 arg2
173
174 method! arg1,
175 arg2
176
177 method !arg1,
178 arg2
179
180 method [],
181 arg2
182
183 method :foo,
184 :bar
185
186 method (a + b),
187 c, :d => :e,
188 f: g
189
190 it "is a method call with block" do |asd|
191 foo
192 end
193
194 it("is too!") {
195 bar
196 }
197
198 and_this_one(has) { |block, parameters|
199 tee
200 }
201
202 if foo &&
203 bar
204 end
205
206 foo +
207 bar
208
209 foo_bar_tee(1, 2, 3)
210 .qux
211 .bar
212
213 foo do
214 bar
215 .tee
216 end
217
218 def bar
219 foo
220 .baz
221 end
222
223 # http://stackoverflow.com/questions/17786563/emacs-ruby-mode-if-expressions-indentation
224 tee = if foo
225 bar
226 end
227
228 # Examples below still fail with `ruby-use-smie' on:
229
230 foo = [1, 2, 3].map do |i|
231 i + 1
232 end
233
234 bar.foo do # "." is parent to "do"; it shouldn't be.
235 bar
236 end
237
238 foo :bar do
239 qux
240 end