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
148 149 150 |
# File 'lib/spoom/source/rewriter.rb', line 148 def initialize @edits = [] #: Array[Edit] end |
Instance Method Details
#<<(other) ⇒ Object
: (Edit) -> void
153 154 155 |
# File 'lib/spoom/source/rewriter.rb', line 153 def <<(other) @edits << other end |
#rewrite!(bytes) ⇒ Object
: (Array) -> void
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/spoom/source/rewriter.rb', line 158 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 |