Skip to content

Commit 296448f

Browse files
authored
Merge pull request #630 from aycabta/enable-line-numbers-option
Enable --line-numbers option
2 parents a27b086 + 802f5b2 commit 296448f

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

lib/rdoc/generator/markup.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ class RDoc::CodeObject
6565

6666
class RDoc::MethodAttr
6767

68-
@add_line_numbers = false
69-
70-
class << self
71-
##
72-
# Allows controlling whether <tt>#markup_code</tt> adds line numbers to
73-
# the source code.
74-
75-
attr_accessor :add_line_numbers
76-
end
77-
7868
##
7969
# Prepend +src+ with line numbers. Relies on the first line of a source
8070
# code listing having:
@@ -106,7 +96,7 @@ def add_line_numbers(src)
10696
##
10797
# Turns the method's token stream into HTML.
10898
#
109-
# Prepends line numbers if +add_line_numbers+ is true.
99+
# Prepends line numbers if +options.line_numbers+ is true.
110100

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

129-
add_line_numbers(src) if RDoc::MethodAttr.add_line_numbers
119+
add_line_numbers(src) if options.line_numbers
130120

131121
src
132122
end

test/test_rdoc_any_method.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ def test_markup_code
8585
assert_equal expected, @c2_a.markup_code
8686
end
8787

88+
def test_markup_code_with_line_numbers
89+
position_comment = "# File #{@file_name}, line 1"
90+
tokens = [
91+
{ :line_no => 1, :char_no => 0, :kind => :on_comment, :text => position_comment },
92+
{ :line_no => 1, :char_no => position_comment.size, :kind => :on_nl, :text => "\n" },
93+
{ :line_no => 2, :char_no => 0, :kind => :on_const, :text => 'A' },
94+
{ :line_no => 2, :char_no => 1, :kind => :on_nl, :text => "\n" },
95+
{ :line_no => 3, :char_no => 0, :kind => :on_const, :text => 'B' }
96+
]
97+
98+
@c2_a.collect_tokens
99+
@c2_a.add_tokens(*tokens)
100+
101+
assert_equal <<-EXPECTED.chomp, @c2_a.markup_code
102+
<span class="ruby-comment"># File xref_data.rb, line 1</span>
103+
<span class="ruby-constant">A</span>
104+
<span class="ruby-constant">B</span>
105+
EXPECTED
106+
107+
@options.line_numbers = true
108+
assert_equal <<-EXPECTED.chomp, @c2_a.markup_code
109+
<span class="ruby-comment"># File xref_data.rb</span>
110+
<span class="line-num">1</span> <span class="ruby-constant">A</span>
111+
<span class="line-num">2</span> <span class="ruby-constant">B</span>
112+
EXPECTED
113+
end
114+
88115
def test_markup_code_empty
89116
assert_equal '', @c2_a.markup_code
90117
end

0 commit comments

Comments
 (0)