Skip to content

Commit 94dea0b

Browse files
authored
Merge pull request #702 from aycabta/normalize-comment-should-check-language
Normalization of comment should check language
2 parents 3a8bd10 + ca68ba1 commit 94dea0b

11 files changed

+73
-53
lines changed

lib/rdoc/comment.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class RDoc::Comment
4848
# Creates a new comment with +text+ that is found in the RDoc::TopLevel
4949
# +location+.
5050

51-
def initialize text = nil, location = nil
51+
def initialize text = nil, location = nil, language = nil
5252
@location = location
5353
@text = text.nil? ? nil : text.dup
54+
@language = language
5455

5556
@document = nil
5657
@format = 'rdoc'

lib/rdoc/parser/c.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def do_includes
446446
next unless cls = @classes[c]
447447
m = @known_classes[m] || m
448448

449-
comment = RDoc::Comment.new '', @top_level
449+
comment = RDoc::Comment.new '', @top_level, :c
450450
incl = cls.add_include RDoc::Include.new(m, comment)
451451
incl.record_location @top_level
452452
end
@@ -564,7 +564,7 @@ def find_alias_comment class_name, new_name, old_name
564564
\s*"#{Regexp.escape new_name}"\s*,
565565
\s*"#{Regexp.escape old_name}"\s*\);%xm
566566

567-
RDoc::Comment.new($1 || '', @top_level)
567+
RDoc::Comment.new($1 || '', @top_level, :c)
568568
end
569569

570570
##
@@ -603,7 +603,7 @@ def find_attr_comment var_name, attr_name, read = nil, write = nil
603603
''
604604
end
605605

606-
RDoc::Comment.new comment, @top_level
606+
RDoc::Comment.new comment, @top_level, :c
607607
end
608608

609609
##
@@ -643,7 +643,7 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
643643

644644
case type
645645
when :func_def
646-
comment = RDoc::Comment.new args[0], @top_level
646+
comment = RDoc::Comment.new args[0], @top_level, :c
647647
body = args[1]
648648
offset, = args[2]
649649

@@ -673,7 +673,7 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
673673

674674
body
675675
when :macro_def
676-
comment = RDoc::Comment.new args[0], @top_level
676+
comment = RDoc::Comment.new args[0], @top_level, :c
677677
body = args[1]
678678
offset, = args[2]
679679

@@ -780,7 +780,7 @@ def find_class_comment class_name, class_mod
780780
comment = ''
781781
end
782782

783-
comment = RDoc::Comment.new comment, @top_level
783+
comment = RDoc::Comment.new comment, @top_level, :c
784784
comment.normalize
785785

786786
look_for_directives_in class_mod, comment
@@ -825,7 +825,7 @@ def find_const_comment(type, const_name, class_name = nil)
825825
table[const_name] ||
826826
''
827827

828-
RDoc::Comment.new comment, @top_level
828+
RDoc::Comment.new comment, @top_level, :c
829829
end
830830

831831
##
@@ -856,7 +856,7 @@ def find_override_comment class_name, meth_obj
856856

857857
return unless comment
858858

859-
RDoc::Comment.new comment, @top_level
859+
RDoc::Comment.new comment, @top_level, :c
860860
end
861861

862862
##
@@ -990,7 +990,7 @@ def handle_constants(type, var_name, const_name, definition)
990990

991991
new_comment = "#{$1}#{new_comment.lstrip}"
992992

993-
new_comment = RDoc::Comment.new new_comment, @top_level
993+
new_comment = RDoc::Comment.new new_comment, @top_level, :c
994994

995995
con = RDoc::Constant.new const_name, new_definition, new_comment
996996
else

lib/rdoc/parser/ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def make_message message
667667
# Creates a comment with the correct format
668668

669669
def new_comment comment
670-
c = RDoc::Comment.new comment, @top_level
670+
c = RDoc::Comment.new comment, @top_level, :ruby
671671
c.format = @markup
672672
c
673673
end

lib/rdoc/text.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
module RDoc::Text
1212

13+
attr_accessor :language
14+
1315
##
1416
# Maps markup formats to classes that can parse them. If the format is
1517
# unknown, "rdoc" format is used.
@@ -111,8 +113,12 @@ def markup text
111113
def normalize_comment text
112114
return text if text.empty?
113115

114-
text = strip_stars text
115-
text = strip_hashes text
116+
case language
117+
when :ruby
118+
text = strip_hashes text
119+
when :c
120+
text = strip_stars text
121+
end
116122
text = expand_tabs text
117123
text = flush_left text
118124
text = strip_newlines text

test/minitest_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ def block *contents
9797
# Creates an RDoc::Comment with +text+ which was defined on +top_level+.
9898
# By default the comment has the 'rdoc' format.
9999

100-
def comment text, top_level = @top_level
101-
RDoc::Comment.new text, top_level
100+
def comment text, top_level = @top_level, language = nil
101+
comment = RDoc::Comment.new text, top_level, language
102+
comment
102103
end
103104

104105
##

test/test_rdoc_class_module.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ def test_add_comment
99
tl3 = @store.add_file 'three.rb'
1010

1111
cm = RDoc::ClassModule.new 'Klass'
12-
comment_tl1 = RDoc::Comment.new('# comment 1')
12+
comment_tl1 = RDoc::Comment.new('# comment 1', @top_level, :ruby)
1313
cm.add_comment comment_tl1, tl1
1414

1515
assert_equal [[comment_tl1, tl1]], cm.comment_location
1616
assert_equal 'comment 1', cm.comment.text
1717

18-
comment_tl2 = RDoc::Comment.new('# comment 2')
18+
comment_tl2 = RDoc::Comment.new('# comment 2', @top_level, :ruby)
1919
cm.add_comment comment_tl2, tl2
2020

2121
assert_equal [[comment_tl1, tl1], [comment_tl2, tl2]], cm.comment_location
2222
assert_equal "comment 1\n---\ncomment 2", cm.comment
2323

24-
comment_tl3 = RDoc::Comment.new('# * comment 3')
24+
comment_tl3 = RDoc::Comment.new('# * comment 3', @top_level, :ruby)
2525
cm.add_comment comment_tl3, tl3
2626

2727
assert_equal [[comment_tl1, tl1],
@@ -42,11 +42,13 @@ def test_add_comment_duplicate
4242
tl1 = @store.add_file 'one.rb'
4343

4444
cm = RDoc::ClassModule.new 'Klass'
45-
cm.add_comment '# comment 1', tl1
46-
cm.add_comment '# comment 2', tl1
45+
comment1 = RDoc::Comment.new('# comment 1', @top_level, :ruby)
46+
comment2 = RDoc::Comment.new('# comment 2', @top_level, :ruby)
47+
cm.add_comment comment1, tl1
48+
cm.add_comment comment2, tl1
4749

48-
assert_equal [['comment 1', tl1],
49-
['comment 2', tl1]], cm.comment_location
50+
assert_equal [[comment1, tl1],
51+
[comment2, tl1]], cm.comment_location
5052
end
5153

5254
def test_add_comment_stopdoc
@@ -66,17 +68,17 @@ def test_ancestors
6668

6769
def test_comment_equals
6870
cm = RDoc::ClassModule.new 'Klass'
69-
cm.comment = '# comment 1'
71+
cm.comment = RDoc::Comment.new('# comment 1', @top_level, :ruby)
7072

71-
assert_equal 'comment 1', cm.comment
73+
assert_equal 'comment 1', cm.comment.to_s
7274

73-
cm.comment = '# comment 2'
75+
cm.comment = RDoc::Comment.new('# comment 2', @top_level, :ruby)
7476

75-
assert_equal "comment 1\n---\ncomment 2", cm.comment
77+
assert_equal "comment 1\n---\ncomment 2", cm.comment.to_s
7678

77-
cm.comment = "# * comment 3"
79+
cm.comment = RDoc::Comment.new('# * comment 3', @top_level, :ruby)
7880

79-
assert_equal "comment 1\n---\ncomment 2\n---\n* comment 3", cm.comment
81+
assert_equal "comment 1\n---\ncomment 2\n---\n* comment 3", cm.comment.to_s
8082
end
8183

8284
def test_comment_equals_comment

test/test_rdoc_comment.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def test_normalize
241241
@comment.text = <<-TEXT
242242
# comment
243243
TEXT
244+
@comment.language = :ruby
244245

245246
assert_same @comment, @comment.normalize
246247

test/test_rdoc_context_section.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def setup
1111
@klass = @top_level.add_class RDoc::NormalClass, 'Object'
1212

1313
@S = RDoc::Context::Section
14-
@s = @S.new @klass, 'section', comment('# comment', @top_level)
14+
@s = @S.new @klass, 'section', comment('# comment', @top_level, :ruby)
1515
end
1616

1717
def test_add_comment

test/test_rdoc_parser_c.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ def test_find_modifiers_nodoc
13811381
end
13821382

13831383
def test_find_modifiers_yields
1384-
comment = RDoc::Comment.new <<-COMMENT
1384+
comment = RDoc::Comment.new <<-COMMENT, @top_level, :c
13851385
/* :yields: a, b
13861386
*
13871387
* Blah

0 commit comments

Comments
 (0)