From 1229efbeb5dca1169d20450c53c5db850738afa2 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:16:30 +0100 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + lib/rdoc/generator/template/rails/_context.rhtml | 2 +- lib/sdoc/search_index.rb | 5 ++++- spec/search_index_spec.rb | 11 +++++++++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25d5a060..20e42d13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ===== diff --git a/lib/rdoc/generator/template/rails/_context.rhtml b/lib/rdoc/generator/template/rails/_context.rhtml index cc0dc73c..d4b1bfab 100644 --- a/lib/rdoc/generator/template/rails/_context.rhtml +++ b/lib/rdoc/generator/template/rails/_context.rhtml @@ -77,7 +77,7 @@
[<%= attrib.rw %>] | diff --git a/lib/sdoc/search_index.rb b/lib/sdoc/search_index.rb index 58526940..7cfd3230 100644 --- a/lib/sdoc/search_index.rb +++ b/lib/sdoc/search_index.rb @@ -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 diff --git a/spec/search_index_spec.rb b/spec/search_index_spec.rb index 34c55d34..11c60b2b 100644 --- a/spec/search_index_spec.rb +++ b/spec/search_index_spec.rb @@ -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 @@ -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") @@ -26,7 +30,7 @@ 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 @@ -34,22 +38,25 @@ def hoge_fuga; end _(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