Skip to content

Enable --line-numbers option #630

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 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 2 additions & 12 deletions lib/rdoc/generator/markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ class RDoc::CodeObject

class RDoc::MethodAttr

@add_line_numbers = false

class << self
##
# Allows controlling whether <tt>#markup_code</tt> adds line numbers to
# the source code.

attr_accessor :add_line_numbers
end

##
# Prepend +src+ with line numbers. Relies on the first line of a source
# code listing having:
Expand Down Expand Up @@ -106,7 +96,7 @@ def add_line_numbers(src)
##
# Turns the method's token stream into HTML.
#
# Prepends line numbers if +add_line_numbers+ is true.
# Prepends line numbers if +options.line_numbers+ is true.

def markup_code
return '' unless @token_stream
Expand All @@ -126,7 +116,7 @@ def markup_code
end
src.gsub!(/^#{' ' * indent}/, '') if indent > 0

add_line_numbers(src) if RDoc::MethodAttr.add_line_numbers
add_line_numbers(src) if options.line_numbers

src
end
Expand Down
27 changes: 27 additions & 0 deletions test/test_rdoc_any_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ def test_markup_code
assert_equal expected, @c2_a.markup_code
end

def test_markup_code_with_line_numbers
position_comment = "# File #{@file_name}, line 1"
tokens = [
{ :line_no => 1, :char_no => 0, :kind => :on_comment, :text => position_comment },
{ :line_no => 1, :char_no => position_comment.size, :kind => :on_nl, :text => "\n" },
{ :line_no => 2, :char_no => 0, :kind => :on_const, :text => 'A' },
{ :line_no => 2, :char_no => 1, :kind => :on_nl, :text => "\n" },
{ :line_no => 3, :char_no => 0, :kind => :on_const, :text => 'B' }
]

@c2_a.collect_tokens
@c2_a.add_tokens(*tokens)

assert_equal <<-EXPECTED.chomp, @c2_a.markup_code
<span class="ruby-comment"># File xref_data.rb, line 1</span>
<span class="ruby-constant">A</span>
<span class="ruby-constant">B</span>
EXPECTED

@options.line_numbers = true
assert_equal <<-EXPECTED.chomp, @c2_a.markup_code
<span class="ruby-comment"># File xref_data.rb</span>
<span class="line-num">1</span> <span class="ruby-constant">A</span>
<span class="line-num">2</span> <span class="ruby-constant">B</span>
EXPECTED
end

def test_markup_code_empty
assert_equal '', @c2_a.markup_code
end
Expand Down