Skip to content

Commit 9edb4fc

Browse files
committed
RDoc::Parser::Ruby no longer skips over classes when the container's documentation is suppressed. This could place methods in the wrong class or attach them to Object. Issue #55
Add RDoc::CodeObject#ignore to facilitate hiding of classes via stopdoc
1 parent c9f6896 commit 9edb4fc

File tree

8 files changed

+132
-15
lines changed

8 files changed

+132
-15
lines changed

History.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* `ri []` and other special methods now work properly. Issue #52 by
77
ddebernardy.
88
* `ri` now has space between class comments from multiple files.
9+
* The stopdoc directive no longer creates Object references. Issue #55 by
10+
Simon Chiang
911

1012
=== 3.8 / 2011-06-29
1113

lib/rdoc/code_object.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def initialize
114114
@done_documenting = false
115115
@force_documentation = false
116116
@received_nodoc = false
117+
@ignored = false
117118
end
118119

119120
##
@@ -139,6 +140,13 @@ def comment=(comment)
139140
end
140141
end
141142

143+
##
144+
# Should this CodeObject be shown in documentation?
145+
146+
def display?
147+
@document_self and not @ignore
148+
end
149+
142150
##
143151
# Enables or disables documentation of this CodeObject's children unless it
144152
# has been turned off by :enddoc:
@@ -195,6 +203,11 @@ def each_parent
195203
self
196204
end
197205

206+
##
207+
# File name where this CodeObject was found.
208+
#
209+
# See also RDoc::Context#in_files
210+
198211
def file_name
199212
return unless @file
200213

@@ -220,6 +233,34 @@ def full_name= full_name
220233
@full_name = full_name
221234
end
222235

236+
##
237+
# Use this to ignore a CodeObject and all its children until found again
238+
# (#record_location is called). An ignored item will not be shown in
239+
# documentation.
240+
#
241+
# See github issue #55
242+
#--
243+
# The ignored status is temporary in order to allow implementation details
244+
# to be hidden. At the end of processing a file RDoc allows all classes
245+
# and modules to add documentation.
246+
#
247+
# If a class was ignored (via stopdoc) then reopened later with additional
248+
# documentation it should be shown. If a class was ignored and never
249+
# reopened it should not be shown. The ignore flag allows this to occur.
250+
251+
def ignore
252+
@ignored = true
253+
254+
stop_doc
255+
end
256+
257+
##
258+
# Has this class been ignored?
259+
260+
def ignored?
261+
@ignored
262+
end
263+
223264
##
224265
# File name of our parent
225266

@@ -238,6 +279,7 @@ def parent_name
238279
# Records the RDoc::TopLevel (file) where this code object was defined
239280

240281
def record_location top_level
282+
@ignored = false
241283
@file = top_level
242284
end
243285

@@ -250,6 +292,7 @@ def start_doc
250292

251293
@document_self = true
252294
@document_children = true
295+
@ignored = false
253296
end
254297

255298
##

lib/rdoc/context.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ def add_class class_type, given_name, superclass = '::Object'
423423
if klass then
424424
# if TopLevel, it may not be registered in the classes:
425425
enclosing.classes_hash[name] = klass
426+
426427
# update the superclass if needed
427428
if superclass then
428429
existing = klass.superclass

lib/rdoc/generator/darkfish.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def get_sorted_module_list(classes)
192192
top_level = klass.full_name.gsub( /::.*/, '' )
193193
[nscounts[top_level] * -1, klass.full_name]
194194
end.select do |klass|
195-
klass.document_self
195+
klass.display?
196196
end
197197
end
198198

lib/rdoc/parser/ruby.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ def parse_class(container, single, tk, comment)
611611

612612
cls_type = single == SINGLE ? RDoc::SingleClass : RDoc::NormalClass
613613
cls = declaration_context.add_class cls_type, given_name, superclass
614+
cls.ignore unless container.document_children
614615

615616
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
616617
cls.record_location @top_level
@@ -1309,11 +1310,7 @@ def parse_statements(container, single = NORMAL, current_method = nil,
13091310
keep_comment = true
13101311

13111312
when TkCLASS then
1312-
if container.document_children then
1313-
parse_class container, single, tk, comment
1314-
else
1315-
nest += 1
1316-
end
1313+
parse_class container, single, tk, comment
13171314

13181315
when TkMODULE then
13191316
if container.document_children then
@@ -1504,6 +1501,7 @@ def parse_top_level_statements container
15041501

15051502
# HACK move if to RDoc::Context#comment=
15061503
container.comment = comment if container.document_self unless comment.empty?
1504+
15071505
parse_statements container, NORMAL, nil, comment
15081506
end
15091507

test/test_rdoc_code_object.rb

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ def test_comment_equals_encoding_blank
6767
assert_equal Encoding::UTF_8, @co.comment.encoding
6868
end
6969

70+
def test_display_eh_document_self
71+
assert @co.display?
72+
73+
@co.stop_doc
74+
75+
refute @co.display?
76+
end
77+
78+
def test_display_eh_ignore
79+
assert @co.display?
80+
81+
@co.ignore
82+
83+
refute @co.display?
84+
85+
@co.stop_doc
86+
87+
refute @co.display?
88+
end
89+
7090
def test_document_children_equals
7191
@co.document_children = false
7292
refute @co.document_children
@@ -156,6 +176,22 @@ def test_full_name_equals
156176
assert_nil @co.instance_variable_get(:@full_name)
157177
end
158178

179+
def test_ignore
180+
@co.ignore
181+
182+
refute @co.document_self
183+
refute @co.document_children
184+
assert @co.ignored?
185+
end
186+
187+
def test_ignore_eh
188+
refute @co.ignored?
189+
190+
@co.ignore
191+
192+
assert @co.ignored?
193+
end
194+
159195
def test_line
160196
@c1_m.line = 5
161197

@@ -202,10 +238,16 @@ def test_received_ndoc
202238
end
203239

204240
def test_record_location
205-
c = RDoc::CodeObject.new
206-
c.record_location @xref_data
241+
@co.record_location @xref_data
242+
243+
assert_equal 'xref_data.rb', @co.file.relative_name
244+
end
245+
246+
def test_record_location_ignored
247+
@co.ignore
248+
@co.record_location @xref_data
207249

208-
assert_equal 'xref_data.rb', c.file.relative_name
250+
refute @co.ignored?
209251
end
210252

211253
def test_start_doc
@@ -218,6 +260,16 @@ def test_start_doc
218260
assert @co.document_children
219261
end
220262

263+
def test_start_doc_ignored
264+
@co.ignore
265+
266+
@co.start_doc
267+
268+
assert @co.document_self
269+
assert @co.document_children
270+
refute @co.ignored?
271+
end
272+
221273
def test_stop_doc
222274
@co.document_self = true
223275
@co.document_children = true

test/test_rdoc_generator_darkfish.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ def setup
3838

3939
@top_level = RDoc::TopLevel.new 'file.rb'
4040
@klass = @top_level.add_class RDoc::NormalClass, 'Object'
41+
4142
@meth = RDoc::AnyMethod.new nil, 'method'
4243
@meth_bang = RDoc::AnyMethod.new nil, 'method!'
4344
@attr = RDoc::Attr.new nil, 'attr', 'RW', ''
4445

4546
@klass.add_method @meth
4647
@klass.add_method @meth_bang
4748
@klass.add_attribute @attr
49+
50+
@ignored = @top_level.add_class RDoc::NormalClass, 'Ignored'
51+
@ignored.ignore
4852
end
4953

5054
def teardown
@@ -83,6 +87,8 @@ def test_generate
8387
File.read('Object.html'))
8488
assert_match(/<meta content="text\/html; charset=#{encoding}"/,
8589
File.read('file_rb.html'))
90+
91+
refute_match(/Ignored/, File.read('index.html'))
8692
end
8793

8894
def test_generate_dry_run

test/test_rdoc_parser_ruby.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -748,12 +748,7 @@ def test_parse_class_stopdoc
748748

749749
@parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment
750750

751-
foo = @top_level.classes.first
752-
assert_equal 'Foo', foo.full_name
753-
assert_equal 'my class', foo.comment
754-
assert_equal [@top_level], foo.in_files
755-
assert_equal 0, foo.offset
756-
assert_equal 1, foo.line
751+
assert_empty @top_level.classes.first.comment
757752
end
758753

759754
def test_parse_multi_ghost_methods
@@ -2178,6 +2173,26 @@ def test_parse_top_level_statements_stopdoc
21782173
assert_empty @top_level.comment
21792174
end
21802175

2176+
def test_parse_top_level_statements_stopdoc_integration
2177+
content = <<-CONTENT
2178+
# :stopdoc:
2179+
2180+
class Example
2181+
def method_name
2182+
end
2183+
end
2184+
CONTENT
2185+
2186+
util_parser content
2187+
2188+
@parser.parse_top_level_statements @top_level
2189+
2190+
assert_equal 1, @top_level.classes.length
2191+
assert_empty @top_level.modules
2192+
2193+
assert @top_level.find_module_named('Example').ignored?
2194+
end
2195+
21812196
def test_parse_yield_in_braces_with_parens
21822197
klass = RDoc::NormalClass.new 'Foo'
21832198
klass.parent = @top_level

0 commit comments

Comments
 (0)