Class: Canopus::Patch
- Inherits:
-
Object
- Object
- Canopus::Patch
- Defined in:
- lib/canopus/patch.rb,
sig/canopus.rbs
Defined Under Namespace
Classes: Composite, Edit, Reload
Instance Attribute Summary collapse
-
#after ⇒ rope
readonly
Returns the value of attribute after.
-
#before ⇒ rope
readonly
Returns the value of attribute before.
-
#edits ⇒ Array[Edit]
readonly
Returns the value of attribute edits.
Instance Method Summary collapse
- #compose(other) ⇒ Composite
-
#initialize(before, after, changes) ⇒ Patch
constructor
A new instance of Patch.
- #inverse ⇒ Patch
- #map_offset(offset, bias: :right) ⇒ Integer
Constructor Details
#initialize(before, after, changes) ⇒ Patch
Returns a new instance of Patch.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/canopus/patch.rb', line 7 def initialize(before, after, changes) @before, @after = before, after delta = 0 @edits = changes.each_with_index.sort_by do |((range, _), index)| [range.begin, range.end + (range.exclude_end? ? 0 : 1), index] end.map do |((range, text), _)| ending = range.end + (range.exclude_end? ? 0 : 1) old = range.begin...ending start = range.begin + delta delta += text.bytesize - (ending - range.begin) Edit.new(old, start...(start + text.bytesize), before.byteslice(old).to_s.freeze, text.dup.freeze) end.freeze end |
Instance Attribute Details
#after ⇒ rope (readonly)
Returns the value of attribute after.
6 7 8 |
# File 'lib/canopus/patch.rb', line 6 def after @after end |
#before ⇒ rope (readonly)
Returns the value of attribute before.
6 7 8 |
# File 'lib/canopus/patch.rb', line 6 def before @before end |
#edits ⇒ Array[Edit] (readonly)
Returns the value of attribute edits.
6 7 8 |
# File 'lib/canopus/patch.rb', line 6 def edits @edits end |
Instance Method Details
#compose(other) ⇒ Composite
24 |
# File 'lib/canopus/patch.rb', line 24 def compose(other) = Composite.new([self, other]) |
#inverse ⇒ Patch
23 |
# File 'lib/canopus/patch.rb', line 23 def inverse = Patch.new(after, before, edits.map { |e| [e.new_range, e.old_text] }) |
#map_offset(offset, bias: :right) ⇒ Integer
20 21 22 |
# File 'lib/canopus/patch.rb', line 20 def map_offset(offset, bias: :right) Denebola::Anchor.new(offset, bias: bias).transform(@edits.map { |e| [e.old_range, e.new_text] }).offset end |