8000 Auto style by nobu · Pull Request #13634 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Auto style #13634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 17, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Auto-style indent
  • Loading branch information
nobu committed Jun 17, 2025
commit c09619d91163ea7c042ee3b5de20c23eaf5a2aba
19 changes: 17 additions & 2 deletions tool/auto-style.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def with_clean_env
%r{\Asample/trick[^/]*/},
]

DIFFERENT_STYLE_FILES = %w[
addr2line.c io_buffer.c prism*.c scheduler.c
]

oldrev, newrev, pushref = ARGV
unless dry_run = pushref.empty?
branch = IO.popen(['git', 'rev-parse', '--symbolic', '--abbrev-ref', pushref], &:read).strip
Expand All @@ -194,14 +198,16 @@ def with_clean_env
exit
end

trailing = eofnewline = expandtab = false
trailing = eofnewline = expandtab = indent = false

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

trailing0 = false
expandtab0 = false
indent0 = false

src.gsub!(/^.*$/).with_index do |line, lineno|
trailing = trailing0 = true if line.sub!(/[ \t]+$/, '')
line
Expand All @@ -225,7 +231,15 @@ def with_clean_env
end
end

if trailing0 or eofnewline0 or expandtab0
if File.fnmatch?("*.[ch]", f, File::FNM_PATHNAME) &&
!DIFFERENT_STYLE_FILES.any? {|pat| File.fnmatch?(pat, f, File::FNM_PATHNAME)}
src.gsub!(/^\w+\([^(\n)]*?\)\K[ \t]*(?=\{$)/, "\n")
src.gsub!(/^([ \t]*)\}\K[ \t]*(?=else\b)/, "\n" '\1')
src.gsub!(/^[ \t]*\}\n\K\n+(?=[ \t]*else\b)/, '')
indent = indent0 = true
end

if trailing0 or eofnewline0 or expandtab0 or indent0
File.binwrite(f, src)
true
end
Expand All @@ -236,6 +250,7 @@ def with_clean_env
msg = [('remove trailing spaces' if trailing),
('append newline at EOF' if eofnewline),
('expand tabs' if expandtab),
('adjust indents' if indent),
].compact
message = "* #{msg.join(', ')}. [ci skip]"
if expandtab
Expand Down
0