Class: Spoom::Source::Rewriter
- Inherits:
-
Object
- Object
- Spoom::Source::Rewriter
- Defined in:
- lib/spoom/source/rewriter.rb
Instance Method Summary collapse
-
#<<(other) ⇒ Object
: (Edit) -> void.
-
#initialize ⇒ Rewriter
constructor
: -> void.
-
#rewrite!(bytes) ⇒ Object
: (Array) -> void.
Constructor Details
#initialize ⇒ Rewriter
: -> void
150 151 152 |
# File 'lib/spoom/source/rewriter.rb', line 150 def initialize @edits = [] #: Array[Edit] end |
Instance Method Details
#<<(other) ⇒ Object
: (Edit) -> void
155 156 157 |
# File 'lib/spoom/source/rewriter.rb', line 155 def <<(other) @edits << other end |
#rewrite!(bytes) ⇒ Object
: (Array) -> void
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/spoom/source/rewriter.rb', line 160 def rewrite!(bytes) # To avoid remapping positions after each edit, # we sort the changes by position and apply them in reverse order. # When ranges are equal, preserve the original order @edits.each_with_index.sort_by do |(edit, idx)| [edit.range, idx] end.reverse_each do |(edit, _)| edit.apply(bytes) end end |