[ruby/rdoc] Pull up handle_tab_width to RDoc::Parser

To share with the duplicate code in RDoc::Parser::Ruby#initialize.

https://github.com/ruby/rdoc/commit/27829ac119
This commit is contained in:
Nobuyoshi Nakada 2021-12-08 23:48:12 +09:00 committed by git
parent d055c44b0c
commit 1b67c58f41
3 changed files with 18 additions and 26 deletions

View File

@ -266,6 +266,23 @@ class RDoc::Parser
autoload :RubyTools, "#{__dir__}/parser/ruby_tools"
autoload :Text, "#{__dir__}/parser/text"
##
# Normalizes tabs in +body+
def handle_tab_width(body)
if /\t/ =~ body
tab_width = @options.tab_width
body.split(/\n/).map do |line|
1 while line.gsub!(/\t+/) do
b, e = $~.offset(0)
' ' * (tab_width * (e-b) - b % tab_width)
end
line
end.join "\n"
else
body
end
end
end
# simple must come first in order to show up last in the parsers list

View File

@ -1057,23 +1057,6 @@ class RDoc::Parser::C < RDoc::Parser
@singleton_classes[sclass_var] = class_name
end
##
# Normalizes tabs in +body+
def handle_tab_width(body)
if /\t/ =~ body
tab_width = @options.tab_width
body.split(/\n/).map do |line|
1 while line.gsub!(/\t+/) do
' ' * (tab_width * $&.length - $`.length % tab_width)
end && $~
line
end.join "\n"
else
body
end
end
##
# Loads the variable map with the given +name+ from the RDoc::Store, if
# present.

View File

@ -164,15 +164,7 @@ class RDoc::Parser::Ruby < RDoc::Parser
def initialize(top_level, file_name, content, options, stats)
super
if /\t/ =~ content then
tab_width = @options.tab_width
content = content.split(/\n/).map do |line|
1 while line.gsub!(/\t+/) {
' ' * (tab_width*$&.length - $`.length % tab_width)
} && $~
line
end.join("\n")
end
content = handle_tab_width(content)
@size = 0
@token_listeners = nil