Skip to content

Commit 7352c0e

Browse files
committed
Fix ghost method line no
1 parent 2db7aec commit 7352c0e

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

lib/rdoc/comment.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class RDoc::Comment
2323

2424
attr_accessor :location
2525

26+
##
27+
# Line where this Comment was written
28+
29+
attr_accessor :line
30+
2631
##
2732
# For duck-typing when merging classes at load time
2833

lib/rdoc/parser/ruby.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def collect_first_comment
244244
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
245245
first_line = true
246246
first_comment_tk_kind = nil
247+
line_no = nil
247248

248249
tk = get_tk
249250

@@ -260,6 +261,7 @@ def collect_first_comment
260261
break if first_comment_tk_kind and not first_comment_tk_kind === tk[:kind]
261262
first_comment_tk_kind = tk[:kind]
262263

264+
line_no = tk[:line_no] if first_line
263265
first_line = false
264266
comment << comment_body
265267
tk = get_tk
@@ -273,7 +275,7 @@ def collect_first_comment
273275

274276
unget_tk tk
275277

276-
new_comment comment
278+
new_comment comment, line_no
277279
end
278280

279281
##
@@ -666,8 +668,9 @@ def make_message message
666668
##
667669
# Creates a comment with the correct format
668670

669-
def new_comment comment
671+
def new_comment comment, line_no = nil
670672
c = RDoc::Comment.new comment, @top_level, :ruby
673+
c.line = line_no
671674
c.format = @markup
672675
c
673676
end
@@ -1058,13 +1061,14 @@ def parse_constant_body container, constant, is_array_or_hash # :nodoc:
10581061
def parse_comment container, tk, comment
10591062
return parse_comment_tomdoc container, tk, comment if @markup == 'tomdoc'
10601063
column = tk[:char_no]
1061-
line_no = tk[:line_no]
1064+
line_no = comment.line.nil? ? tk[:line_no] : comment.line
10621065

10631066
comment.text = comment.text.sub(/(^# +:?)(singleton-)(method:)/, '\1\3')
10641067
singleton = !!$~
10651068

10661069
co =
10671070
if (comment.text = comment.text.sub(/^# +:?method: *(\S*).*?\n/i, '')) && !!$~ then
1071+
line_no += $`.count("\n")
10681072
parse_comment_ghost container, comment.text, $1, column, line_no, comment
10691073
elsif (comment.text = comment.text.sub(/# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i, '')) && !!$~ then
10701074
parse_comment_attr container, $1, $3, comment
@@ -1776,8 +1780,10 @@ def parse_statements(container, single = NORMAL, current_method = nil,
17761780
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
17771781
end
17781782

1783+
line_no = nil
17791784
while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do
17801785
comment_body = retrieve_comment_body(tk)
1786+
line_no = tk[:line_no] if comment.empty?
17811787
comment += comment_body
17821788
comment << "\n" unless comment_body =~ /\n\z/
17831789

@@ -1787,7 +1793,7 @@ def parse_statements(container, single = NORMAL, current_method = nil,
17871793
tk = get_tk
17881794
end
17891795

1790-
comment = new_comment comment
1796+
comment = new_comment comment, line_no
17911797

17921798
unless comment.empty? then
17931799
look_for_directives_in container, comment

test/rdoc/test_rdoc_parser_ruby.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ class Foo
776776

777777
blah = foo.method_list.first
778778
assert_equal 'Foo#blah', blah.full_name
779+
assert_equal 3, blah.line
779780
assert_equal @top_level, blah.file
780781
end
781782

@@ -825,6 +826,7 @@ class Foo
825826
blah = foo.method_list.first
826827
assert_equal 'Foo#yields', blah.full_name
827828
assert_equal 'yields(name)', blah.call_seq
829+
assert_equal 3, blah.line
828830
assert_equal @top_level, blah.file
829831
end
830832

@@ -1323,7 +1325,7 @@ def test_parse_comment_method
13231325
assert_equal 'foo', foo.name
13241326
assert_equal 'my method', foo.comment.text
13251327
assert_equal @top_level, foo.file
1326-
assert_equal 1, foo.line
1328+
assert_equal 2, foo.line
13271329

13281330
assert_equal [], foo.aliases
13291331
assert_nil foo.block_params
@@ -1344,8 +1346,8 @@ def test_parse_comment_method
13441346

13451347
stream = [
13461348
{
1347-
:line_no => 1, :char_no => 1, :kind => :on_comment,
1348-
:text => "# File #{@top_level.relative_name}, line 1"
1349+
:line_no => 2, :char_no => 1, :kind => :on_comment,
1350+
:text => "# File #{@top_level.relative_name}, line 2"
13491351
},
13501352
{ :line_no => 0, :char_no => 0, :kind => :on_nl, :text => "\n" },
13511353
{ :line_no => 1, :char_no => 1, :kind => :on_sp, :text => '' }

0 commit comments

Comments
 (0)