def self.adjust_indentation! lines, indent = 0, tab_size = 0
return if lines.empty?
if (tab_size = tab_size.to_i) > 0 && (lines.join.include? TAB)
full_tab_space = ' ' * tab_size
lines.map! do |line|
next line if line.empty?
line.sub!(TabIndentRx) { full_tab_space * $&.length } if line.start_with? TAB
if line.include? TAB
spaces_added = 0
line.gsub!(TabRx) {
if (offset = ($~.begin 0) + spaces_added) % tab_size == 0
spaces_added += (tab_size - 1)
full_tab_space
else
unless (spaces = tab_size - offset % tab_size) == 1
spaces_added += (spaces - 1)
end
' ' * spaces
end
}
else
line
end
end
end
return unless indent && (indent = indent.to_i) > -1
gutter_width = nil
lines.each do |line|
next if line.empty?
if (line_indent = line.length - line.lstrip.length) == 0
gutter_width = nil
break
else
unless gutter_width && line_indent > gutter_width
gutter_width = line_indent
end
end
end
if indent == 0
if gutter_width
lines.map! {|line| line.empty? ? line : line[gutter_width..-1] }
end
else
padding = ' ' * indent
if gutter_width
lines.map! {|line| line.empty? ? line : padding + line[gutter_width..-1] }
else
lines.map! {|line| line.empty? ? line : padding + line }
end
end
nil
end