Skip to content

Commit

Permalink
[ruby/rdoc] Pull up handle_tab_width to RDoc::Parser
Browse files Browse the repository at this point in the history
To share with the duplicate code in RDoc::Parser::Ruby#initialize.

ruby/rdoc@27829ac119
  • Loading branch information
nobu authored and matzbot committed Nov 27, 2022
1 parent d055c44 commit 1b67c58
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
17 changes: 17 additions & 0 deletions lib/rdoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,23 @@ def initialize top_level, file_name, content, options, stats
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
Expand Down
17 changes: 0 additions & 17 deletions lib/rdoc/parser/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1057,23 +1057,6 @@ def handle_singleton sclass_var, class_var
@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.
Expand Down
10 changes: 1 addition & 9 deletions lib/rdoc/parser/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1b67c58

Please sign in to comment.