Skip to content

Commit 1229efb

Browse files
committed
Add attributes to the search index
To make this work correctly, add anchors to the generated html. RDoc uses the same class for methods and attributes so most all of the plumbing was already there.
1 parent 242686d commit 1229efb

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Main (3.0.0.alpha)
3939
* [#356](https://github.com/rails/sdoc/pull/356) Redesign "Constants" section [@jonathanhefner](https://github.com/jonathanhefner)
4040
* [#357](https://github.com/rails/sdoc/pull/357) Support permalinking constants [@jonathanhefner](https://github.com/jonathanhefner)
4141
* [#358](https://github.com/rails/sdoc/pull/358) Add constants to search index [@jonathanhefner](https://github.com/jonathanhefner)
42+
* [#370](https://github.com/rails/sdoc/pull/370) Add attributes to search index [@earlopain](https://github.com/earlopain)
4243

4344
2.6.1
4445
=====

lib/rdoc/generator/template/rails/_context.rhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<h2 class="content__divider">Attributes</h2>
7878
<table border='0' cellpadding='5'>
7979
<% attributes.each do |attrib| %>
80-
<tr valign='top'>
80+
<tr valign='top' id='<%= attrib.aref %>'>
8181
<td class='attr-rw'>
8282
[<%= attrib.rw %>]
8383
</td>

lib/sdoc/search_index.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def to_json(*)
1313
end
1414

1515
def generate(rdoc_modules)
16-
rdoc_objects = rdoc_modules + rdoc_modules.flat_map(&:constants) + rdoc_modules.flat_map(&:method_list)
16+
rdoc_objects = rdoc_modules +
17+
rdoc_modules.flat_map(&:constants) +
18+
rdoc_modules.flat_map(&:method_list) +
19+
rdoc_modules.flat_map(&:attributes)
1720

1821
# RDoc duplicates member instances when modules are aliased by assigning to
1922
# a constant. For example, `MyBar = Foo::Bar` will duplicate all of

spec/search_index_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
top_level = rdoc_top_level_for <<~RUBY
77
# This is FooBar.
88
class FooBar
9+
# This is #lorem_ipsum.
10+
attr_reader :lorem_ipsum
11+
912
# This is +BAZ_QUX+.
1013
BAZ_QUX = true
1114
@@ -16,6 +19,7 @@ def hoge_fuga; end
1619

1720
ngrams =
1821
SDoc::SearchIndex.derive_ngrams("FooBar") |
22+
SDoc::SearchIndex.derive_ngrams("FooBar#lorem_ipsum") |
1923
SDoc::SearchIndex.derive_ngrams("FooBar::BAZ_QUX") |
2024
SDoc::SearchIndex.derive_ngrams("FooBar#hoge_fuga")
2125

@@ -26,30 +30,33 @@ def hoge_fuga; end
2630
_(search_index["ngrams"].keys.sort).must_equal ngrams.sort
2731
_(search_index["ngrams"].values.max).must_equal search_index["weights"].length - 1
2832

29-
_(search_index["entries"].length).must_equal 3
33+
_(search_index["entries"].length).must_equal 4
3034
search_index["entries"].each do |entry|
3135
_(entry.length).must_be :<=, 6
3236
_(entry[0]).must_be_kind_of Array # Fingerprint
3337
_(entry[1]).must_be :<, 1.0 # Tiebreaker bonus
3438
_(entry[3]).must_equal "FooBar" # Module name
3539
end
3640

37-
module_entry, method_entry, constant_entry = search_index["entries"].sort_by { |entry| entry[4].to_s }
41+
module_entry, method_entry, attr_entry, constant_entry = search_index["entries"].sort_by { |entry| entry[4].to_s }
3842

3943
# URL
4044
_(module_entry[2]).must_equal "classes/FooBar.html"
4145
_(constant_entry[2]).must_equal "classes/FooBar.html#constant-BAZ_QUX"
4246
_(method_entry[2]).must_equal "classes/FooBar.html#method-i-hoge_fuga"
47+
_(attr_entry[2]).must_equal "classes/FooBar.html#attribute-i-lorem_ipsum"
4348

4449
# Member label
4550
_(module_entry[4]).must_be_nil
4651
_(constant_entry[4]).must_equal "::BAZ_QUX"
4752
_(method_entry[4]).must_equal "#hoge_fuga()"
53+
_(attr_entry[4]).must_equal "#lorem_ipsum"
4854

4955
# Description
5056
_(module_entry[5]).must_equal "This is <code>FooBar</code>."
5157
_(constant_entry[5]).must_equal "This is <code>BAZ_QUX</code>."
5258
_(method_entry[5]).must_equal "This is <code>hoge_fuga</code>."
59+
_(attr_entry[5]).must_equal "This is <code>lorem_ipsum</code>."
5360
end
5461
end
5562

0 commit comments

Comments
 (0)