Module: Ast::Merge::TrailingGroups::AlignmentSort
- Included in:
- FileAlignerBase
- Defined in:
- lib/ast/merge/trailing_groups/alignment_sort.rb
Overview
Pattern B — Alignment-based template-only positioning.
Convenience helpers for gems that build an alignment array and sort template-only entries to their correct positions. This is the pattern used by dotenv-merge, rbs-merge, markdown-merge, markly-merge, and commonmarker-merge.
Typical usage
include Ast::Merge::TrailingGroups::AlignmentSort
def sort_alignment(alignment, dest_size)
sort_alignment_with_template_position(alignment, dest_size)
end
Hooks
Override #template_only_sort_key to customize the sort key for
template-only entries. The default appends them after all
destination-backed entries, ordered by template_index.
Instance Method Summary collapse
-
#dest_only_sort_key(entry) ⇒ Array
Sort key for destination-only entries.
-
#match_sort_key(entry) ⇒ Array
Sort key for matched entries.
-
#sort_alignment_with_template_position(alignment, dest_size) ⇒ Array<Hash>
Sort an alignment array so that: - Matched and dest-only entries preserve destination order - Template-only entries appear after all destination-backed entries, in template order.
-
#template_only_sort_key(entry, _dest_size) ⇒ Array
Sort key for template-only entries.
Instance Method Details
#dest_only_sort_key(entry) ⇒ Array
Sort key for destination-only entries.
Default: interleave with matches by destination index. Override for gems with special dest-only handling (e.g. freeze blocks in rbs-merge).
77 78 79 |
# File 'lib/ast/merge/trailing_groups/alignment_sort.rb', line 77 def dest_only_sort_key(entry) [0, entry[:dest_index], 1, 0] end |
#match_sort_key(entry) ⇒ Array
Sort key for matched entries.
Default: sort by destination index. Override for gems that need a more complex sort (e.g. rbs-merge's 4-tuple key).
65 66 67 |
# File 'lib/ast/merge/trailing_groups/alignment_sort.rb', line 65 def match_sort_key(entry) [0, entry[:dest_index], 0, entry[:template_index] || 0] end |
#sort_alignment_with_template_position(alignment, dest_size) ⇒ Array<Hash>
Sort an alignment array so that:
- Matched and dest-only entries preserve destination order
- Template-only entries appear after all destination-backed entries, in template order
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ast/merge/trailing_groups/alignment_sort.rb', line 40 def sort_alignment_with_template_position(alignment, dest_size) alignment.sort_by! do |entry| case entry[:type] when :match match_sort_key(entry) when :dest_only dest_only_sort_key(entry) when :template_only template_only_sort_key(entry, dest_size) else # simplecov:disable defensive [999, 0, 0, 0] # simplecov:enable end end end |
#template_only_sort_key(entry, _dest_size) ⇒ Array
Sort key for template-only entries.
Default: append after all destination-backed entries, ordered by template index. Override for gems that need position-aware interleaving (future enhancement).
90 91 92 |
# File 'lib/ast/merge/trailing_groups/alignment_sort.rb', line 90 def template_only_sort_key(entry, _dest_size) [2, entry[:template_index], 0, 0] end |