Skip to content

Commit 0042db2

Browse files
committed
Auto-style indent
1 parent 21c7131 commit 0042db2

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

tool/auto-style.rb

100644100755
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ def with_clean_env
173173
%r{\Asample/trick[^/]*/},
174174
]
175175

176+
DIFFERENT_ORIGIN_FILES = %w[
177+
addr2line.c io_buffer.c prism*.c scheduler.c
178+
]
179+
176180
oldrev, newrev, pushref = ARGV
177181
unless dry_run = pushref.empty?
178182
branch = IO.popen(['git', 'rev-parse', '--symbolic', '--abbrev-ref', pushref], &:read).strip
@@ -194,14 +198,16 @@ def with_clean_env
194198
exit
195199
end
196200

197-
trailing = eofnewline = expandtab = false
201+
trailing = eofnewline = expandtab = indent = false
198202

199203
edited_files = files.select do |f|
200204
src = File.binread(f) rescue next
201205
eofnewline = eofnewline0 = true if src.sub!(/(?<!\A|\n)\z/, "\n")
202206

203207
trailing0 = false
204208
expandtab0 = false
209+
indent0 = false
210+
205211
src.gsub!(/^.*$/).with_index do |line, lineno|
206212
trailing = trailing0 = true if line.sub!(/[ \t]+$/, '')
207213
line
@@ -225,7 +231,15 @@ def with_clean_env
225231
end
226232
end
227233

228-
if trailing0 or eofnewline0 or expandtab0
234+
if File.fnmatch?("*.[ch]", f, File::FNM_PATHNAME) &&
235+
!DIFFERENT_ORIGIN_FILES.any? {|pat| File.fnmatch?(pat, f, File::FNM_PATHNAME)}
236+
src.gsub!(/^\w+\([^(\n)]*?\)\K[ \t]*(?=\{$)/, "\n")
237+
src.gsub!(/^([ \t]*)\}\K[ \t]*(?=else\b)/, "\n" '\1')
238+
src.gsub!(/^[ \t]*\}\n\K\n+(?=[ \t]*else\b)/, '')
239+
indent = indent0 = true
240+
end
241+
242+
if trailing0 or eofnewline0 or expandtab0 or indent0
229243
File.binwrite(f, src)
230244
true
231245
end
@@ -236,6 +250,7 @@ def with_clean_env
236250
msg = [('remove trailing spaces' if trailing),
237251
('append newline at EOF' if eofnewline),
238252
('expand tabs' if expandtab),
253+
('adjust indents' if indent),
239254
].compact
240255
message = "* #{msg.join(', ')}. [ci skip]"
241256
if expandtab

0 commit comments

Comments
 (0)