Skip to content

Commit ec980bc

Browse files
committed
"expand tabs" only when the file wasn't tab-indented
1 parent 918a7c3 commit ec980bc

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

bin/auto-style.rb

+13-6
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,19 @@ def with_clean_env
165165
expandtab0 = false
166166
updated_lines = vcs.updated_lines(f)
167167
if !updated_lines.empty? && (f.end_with?('.c') || f.end_with?('.h') || f == 'insns.def') && EXPANDTAB_IGNORED_FILES.all? { |re| !f.match(re) }
168-
src.gsub!(/^.*$/).with_index do |line, lineno|
169-
if updated_lines.include?(lineno) && line.start_with?("\t") # last-committed line with hard tabs
170-
expandtab = expandtab0 = true
171-
line.sub(/\A\t+/) { |tabs| ' ' * (8 * tabs.length) }
172-
else
173-
line
168+
# If and only if unedited lines did not have tab indentation, prevent introducing tab indentation to the file.
169+
expandtab_allowed = src.each_line.with_index.all? do |line, lineno|
170+
updated_lines.include?(lineno) || !line.start_with?("\t")
171+
end
172+
173+
if expandtab_allowed
174+
src.gsub!(/^.*$/).with_index do |line, lineno|
175+
if updated_lines.include?(lineno) && line.start_with?("\t") # last-committed line with hard tabs
176+
expandtab = expandtab0 = true
177+
line.sub(/\A\t+/) { |tabs| ' ' * (8 * tabs.length) }
178+
else
179+
line
180+
end
174181
end
175182
end
176183
end

0 commit comments

Comments
 (0)