Refactor comment_location from Array to Hash#1634
Merged
Conversation
Convert ClassModule#comment_location from an Array of [comment, location]
pairs to a Hash of { location => [comments] }. Each location maps to an
array of comments, so a class reopened in the same file preserves all
its comments in the rendered documentation.
Key changes:
- add_comment appends to an array per location instead of pushing pairs
- C parser special-case delete_if removed (hash key handles dedup)
- parse uses flat_map to flatten per-location comment arrays into docs
- marshal_load/merge use group_by(&:file) to reconstruct arrays
- search_snippet, documented?, from_module updated for new shape
Collaborator
|
🚀 Preview deployment available at: https://5326d8ea.rdoc-6cd.pages.dev (commit: 791572b) |
tompng
approved these changes
Mar 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Convert
ClassModule#comment_locationfrom an Array of[comment, location]pairs to a Hash of{ location => [comments] }.Motivation
When a class like
RDocis documented across multiple files (lib/rdoc.rb,lib/rdoc/rubygems_hook.rb, etc.), each file contributes a comment. With the old Array of[comment, location]pairs, there was no efficient way to look up or replace comments by file. This matters for server live-reload (#1620): when a user editslib/rdoc.rb, we need to clear that file's comment and re-parse — but preserve comments from other files in their original order. The Array structure made this fragile because removing and re-appending an entry moved it to the end, changing the order comments appeared on the rendered page.The Hash structure solves this:
{ location => [comments] }allows O(1) lookup by file, and Ruby hashes preserve insertion order — replacing a key's value keeps it in position.A secondary benefit: a class reopened in the same file now preserves all its comments:
With the old Array, the C parser had a special-case
delete_ifto deduplicate same-location entries, while Ruby parsers accumulated duplicates inconsistently. With the new Hash, each location maps to an array of comments — both comments are preserved and rendered, matching the cross-file behavior.Changes
add_comment: appends to array per location —(@comment_location[location] ||= []) << commentparse: usesflat_mapto flatten per-location comment arrays into Document partsmarshal_load/merge: usegroup_by(&:file)to reconstruct arrays from Documentsdocumented?,search_snippet,from_module: updated for new value shapei18n/text.rb: handles Hash with array valuesdelete_ifspecial case removed (hash key naturally deduplicates by location)