Skip to content

Commit

Permalink
Merge pull request #370 from Earlopain/search-attributes
Browse files Browse the repository at this point in the history
Add attributes to the search index
  • Loading branch information
zzak authored Nov 1, 2024
2 parents b5e7497 + 1229efb commit 7de756f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Main (3.0.0.alpha)
* [#356](https://github.com/rails/sdoc/pull/356) Redesign "Constants" section [@jonathanhefner](https://github.com/jonathanhefner)
* [#357](https://github.com/rails/sdoc/pull/357) Support permalinking constants [@jonathanhefner](https://github.com/jonathanhefner)
* [#358](https://github.com/rails/sdoc/pull/358) Add constants to search index [@jonathanhefner](https://github.com/jonathanhefner)
* [#370](https://github.com/rails/sdoc/pull/370) Add attributes to search index [@earlopain](https://github.com/earlopain)

2.6.1
=====
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/template/rails/_context.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<h2 class="content__divider">Attributes</h2>
<table border='0' cellpadding='5'>
<% attributes.each do |attrib| %>
<tr valign='top'>
<tr valign='top' id='<%= attrib.aref %>'>
<td class='attr-rw'>
[<%= attrib.rw %>]
</td>
Expand Down
5 changes: 4 additions & 1 deletion lib/sdoc/search_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def to_json(*)
end

def generate(rdoc_modules)
rdoc_objects = rdoc_modules + rdoc_modules.flat_map(&:constants) + rdoc_modules.flat_map(&:method_list)
rdoc_objects = rdoc_modules +
rdoc_modules.flat_map(&:constants) +
rdoc_modules.flat_map(&:method_list) +
rdoc_modules.flat_map(&:attributes)

# RDoc duplicates member instances when modules are aliased by assigning to
# a constant. For example, `MyBar = Foo::Bar` will duplicate all of
Expand Down
11 changes: 9 additions & 2 deletions spec/search_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
top_level = rdoc_top_level_for <<~RUBY
# This is FooBar.
class FooBar
# This is #lorem_ipsum.
attr_reader :lorem_ipsum
# This is +BAZ_QUX+.
BAZ_QUX = true
Expand All @@ -16,6 +19,7 @@ def hoge_fuga; end

ngrams =
SDoc::SearchIndex.derive_ngrams("FooBar") |
SDoc::SearchIndex.derive_ngrams("FooBar#lorem_ipsum") |
SDoc::SearchIndex.derive_ngrams("FooBar::BAZ_QUX") |
SDoc::SearchIndex.derive_ngrams("FooBar#hoge_fuga")

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

_(search_index["entries"].length).must_equal 3
_(search_index["entries"].length).must_equal 4
search_index["entries"].each do |entry|
_(entry.length).must_be :<=, 6
_(entry[0]).must_be_kind_of Array # Fingerprint
_(entry[1]).must_be :<, 1.0 # Tiebreaker bonus
_(entry[3]).must_equal "FooBar" # Module name
end

module_entry, method_entry, constant_entry = search_index["entries"].sort_by { |entry| entry[4].to_s }
module_entry, method_entry, attr_entry, constant_entry = search_index["entries"].sort_by { |entry| entry[4].to_s }

# URL
_(module_entry[2]).must_equal "classes/FooBar.html"
_(constant_entry[2]).must_equal "classes/FooBar.html#constant-BAZ_QUX"
_(method_entry[2]).must_equal "classes/FooBar.html#method-i-hoge_fuga"
_(attr_entry[2]).must_equal "classes/FooBar.html#attribute-i-lorem_ipsum"

# Member label
_(module_entry[4]).must_be_nil
_(constant_entry[4]).must_equal "::BAZ_QUX"
_(method_entry[4]).must_equal "#hoge_fuga()"
_(attr_entry[4]).must_equal "#lorem_ipsum"

# Description
_(module_entry[5]).must_equal "This is <code>FooBar</code>."
_(constant_entry[5]).must_equal "This is <code>BAZ_QUX</code>."
_(method_entry[5]).must_equal "This is <code>hoge_fuga</code>."
_(attr_entry[5]).must_equal "This is <code>lorem_ipsum</code>."
end
end

Expand Down

0 comments on commit 7de756f

Please sign in to comment.