Skip to content

Commit d29a9fa

Browse files
committed
Add line information to code objects created by RDoc::Parser::Ruby (offset is broken)
1 parent 2161c20 commit d29a9fa

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

lib/rdoc/parser/ruby.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ def initialize(top_level, file_name, content, options, stats)
174174
@scanner.exception_on_syntax_error = false
175175
@prev_seek = nil
176176

177+
@encoding = @options.encoding if Object.const_defined? :Encoding
178+
177179
reset
178180
end
179181

@@ -449,6 +451,9 @@ def make_message message
449451
# +comment+.
450452

451453
def parse_attr(context, single, tk, comment)
454+
column = tk.char_no
455+
line_no = tk.line_no
456+
452457
args = parse_symbol_arg 1
453458
if args.size > 0 then
454459
name = args[0]
@@ -464,6 +469,8 @@ def parse_attr(context, single, tk, comment)
464469

465470
att = RDoc::Attr.new get_tkread, name, rw, comment, single == SINGLE
466471
att.record_location @top_level
472+
att.offset = column
473+
att.line = line_no
467474

468475
read_documentation_modifiers att, RDoc::ATTR_MODIFIERS
469476

@@ -480,6 +487,9 @@ def parse_attr(context, single, tk, comment)
480487
# comment for each to +comment+.
481488

482489
def parse_attr_accessor(context, single, tk, comment)
490+
column = tk.char_no
491+
line_no = tk.line_no
492+
483493
args = parse_symbol_arg
484494
rw = "?"
485495

@@ -498,6 +508,8 @@ def parse_attr_accessor(context, single, tk, comment)
498508
for name in args
499509
att = RDoc::Attr.new get_tkread, name, rw, comment, single == SINGLE
500510
att.record_location @top_level
511+
att.offset = column
512+
att.line = line_no
501513

502514
context.add_attribute att
503515
@stats.add_attribute att
@@ -508,6 +520,9 @@ def parse_attr_accessor(context, single, tk, comment)
508520
# Parses an +alias+ in +context+ with +comment+
509521

510522
def parse_alias(context, single, tk, comment)
523+
column = tk.char_no
524+
line_no = tk.line_no
525+
511526
skip_tkspace
512527

513528
if TkLPAREN === peek_tk then
@@ -534,6 +549,8 @@ def parse_alias(context, single, tk, comment)
534549
al = RDoc::Alias.new(get_tkread, old_name, new_name, comment,
535550
single == SINGLE)
536551
al.record_location @top_level
552+
al.offset = column
553+
al.line = line_no
537554

538555
read_documentation_modifiers al, RDoc::ATTR_MODIFIERS
539556
context.add_alias al if al.document_self
@@ -586,6 +603,9 @@ def parse_call_parameters(tk)
586603
# Parses a class in +context+ with +comment+
587604

588605
def parse_class(container, single, tk, comment)
606+
column = tk.char_no
607+
line_no = tk.line_no
608+
589609
declaration_context = container
590610
container, name_t, given_name = get_class_or_module container
591611

@@ -606,6 +626,9 @@ def parse_class(container, single, tk, comment)
606626

607627
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
608628
cls.record_location @top_level
629+
cls.offset = column
630+
cls.line = line_no
631+
609632
cls.comment = comment if cls.document_self
610633

611634
@top_level.add_to_classes_or_modules cls
@@ -622,6 +645,9 @@ def parse_class(container, single, tk, comment)
622645
unless other then
623646
other = container.add_module RDoc::NormalModule, name
624647
other.record_location @top_level
648+
other.offset = column
649+
other.line = line_no
650+
625651
other.comment = comment
626652
end
627653

@@ -639,7 +665,6 @@ def parse_class(container, single, tk, comment)
639665
read_documentation_modifiers other, RDoc::CLASS_MODIFIERS
640666
parse_statements(other, SINGLE)
641667
end
642-
643668
else
644669
warn("Expected class name or '<<'. Got #{name_t.class}: #{name_t.text.inspect}")
645670
end
@@ -649,6 +674,9 @@ def parse_class(container, single, tk, comment)
649674
# Parses a constant in +context+ with +comment+
650675

651676
def parse_constant(container, tk, comment)
677+
column = tk.char_no
678+
line_no = tk.line_no
679+
652680
name = tk.name
653681
skip_tkspace false
654682

@@ -721,6 +749,8 @@ def parse_constant(container, tk, comment)
721749

722750
con = RDoc::Constant.new name, res, comment
723751
con.record_location @top_level
752+
con.offset = column
753+
con.line = line_no
724754
read_documentation_modifiers con, RDoc::CONSTANT_MODIFIERS
725755

726756
@stats.add_constant con
@@ -745,6 +775,8 @@ def parse_comment(container, tk, comment)
745775
meth = RDoc::GhostMethod.new get_tkread, name
746776
meth.record_location @top_level
747777
meth.singleton = singleton
778+
meth.offset = column
779+
meth.line = line_no
748780

749781
meth.start_collecting_tokens
750782
indent = TkSPACE.new nil, 1, 1
@@ -777,6 +809,8 @@ def parse_comment(container, tk, comment)
777809
# TODO authorize 'singleton-attr...'?
778810
att = RDoc::Attr.new get_tkread, name, rw, comment
779811
att.record_location @top_level
812+
att.offset = column
813+
att.line = line_no
780814

781815
container.add_attribute att
782816

@@ -904,6 +938,8 @@ def parse_meta_method(container, single, tk, comment)
904938

905939
meth = RDoc::MetaMethod.new get_tkread, name
906940
meth.record_location @top_level
941+
meth.offset = column
942+
meth.line = line_no
907943
meth.singleton = singleton
908944

909945
remove_token_listener self
@@ -1049,6 +1085,8 @@ def parse_method(container, single, tk, comment)
10491085
end
10501086

10511087
meth.record_location @top_level
1088+
meth.offset = column
1089+
meth.line = line_no
10521090

10531091
meth.start_collecting_tokens
10541092
indent = TkSPACE.new nil, 1, 1

test/test_rdoc_parser_ruby.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ def test_parse_alias
310310
assert_equal klass, alas.parent
311311
assert_equal 'comment', alas.comment
312312
assert_equal @top_level, alas.file
313+
assert_equal 0, alas.offset
314+
assert_equal 1, alas.line
313315
end
314316

315317
def test_parse_alias_singleton
@@ -361,6 +363,8 @@ def test_parse_attr
361363
assert_equal 'foo', foo.name
362364
assert_equal 'my attr', foo.comment
363365
assert_equal @top_level, foo.file
366+
assert_equal 0, foo.offset
367+
assert_equal 1, foo.line
364368
end
365369

366370
def test_parse_attr_accessor
@@ -382,6 +386,8 @@ def test_parse_attr_accessor
382386
assert_equal 'RW', foo.rw
383387
assert_equal 'my attr', foo.comment
384388
assert_equal @top_level, foo.file
389+
assert_equal 0, foo.offset
390+
assert_equal 1, foo.line
385391

386392
bar = klass.attributes.last
387393
assert_equal 'bar', bar.name
@@ -540,6 +546,9 @@ def test_parse_class
540546
foo = @top_level.classes.first
541547
assert_equal 'Foo', foo.full_name
542548
assert_equal 'my method', foo.comment
549+
assert_equal [@top_level], foo.in_files
550+
assert_equal 0, foo.offset
551+
assert_equal 1, foo.line
543552
end
544553

545554
def test_parse_class_ghost_method
@@ -705,6 +714,10 @@ def foo; end
705714
assert_equal %w[A], RDoc::TopLevel.classes.map { |c| c.full_name }
706715
assert_equal %w[A::B A::d], RDoc::TopLevel.modules.map { |c| c.full_name }
707716

717+
b = RDoc::TopLevel.modules.first
718+
assert_equal 2, b.offset # HACK should be 10
719+
assert_equal 2, b.line
720+
708721
# make sure method/alias was not added to enclosing class/module
709722
a = RDoc::TopLevel.all_classes_hash['A']
710723
assert_empty a.method_list
@@ -843,6 +856,8 @@ def test_parse_comment_attr
843856
assert_equal 'RW', foo.rw
844857
assert_equal 'my attr', foo.comment
845858
assert_equal @top_level, foo.file
859+
assert_equal 0, foo.offset
860+
assert_equal 1, foo.line
846861

847862
assert_equal nil, foo.viewer
848863
assert_equal true, foo.document_children
@@ -872,6 +887,8 @@ def test_parse_comment_method
872887
assert_equal 'foo', foo.name
873888
assert_equal 'my method', foo.comment
874889
assert_equal @top_level, foo.file
890+
assert_equal 0, foo.offset
891+
assert_equal 1, foo.line
875892

876893
assert_equal [], foo.aliases
877894
assert_equal nil, foo.block_params
@@ -899,6 +916,25 @@ def test_parse_comment_method
899916
assert_equal stream, foo.token_stream
900917
end
901918

919+
def test_parse_constant
920+
util_top_level
921+
922+
klass = @top_level.add_class RDoc::NormalClass, 'Foo'
923+
924+
util_parser "A = v"
925+
926+
tk = @parser.get_tk
927+
928+
@parser.parse_constant klass, tk, ''
929+
930+
foo = klass.constants.first
931+
932+
assert_equal 'A', foo.name
933+
assert_equal @top_level, foo.file
934+
assert_equal 0, foo.offset
935+
assert_equal 1, foo.line
936+
end
937+
902938
def test_parse_constant_attrasgn
903939
util_top_level
904940

@@ -959,6 +995,8 @@ def test_parse_meta_method
959995
assert_equal 'foo', foo.name
960996
assert_equal 'my method', foo.comment
961997
assert_equal @top_level, foo.file
998+
assert_equal 0, foo.offset
999+
assert_equal 1, foo.line
9621000

9631001
assert_equal [], foo.aliases
9641002
assert_equal nil, foo.block_params
@@ -1118,6 +1156,8 @@ def test_parse_method
11181156
assert_equal 'foo', foo.name
11191157
assert_equal 'my method', foo.comment
11201158
assert_equal @top_level, foo.file
1159+
assert_equal 0, foo.offset
1160+
assert_equal 1, foo.line
11211161

11221162
assert_equal [], foo.aliases
11231163
assert_equal nil, foo.block_params

0 commit comments

Comments
 (0)