Class: Canopus::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/canopus/patch.rb,
sig/canopus.rbs

Defined Under Namespace

Classes: Composite, Edit, Reload

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(before, after, changes) ⇒ Patch

Returns a new instance of Patch.

Parameters:

  • before (rope)
  • after (rope)
  • changes (changes)


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.

Returns:

  • (rope)


6
7
8
# File 'lib/canopus/patch.rb', line 6

def after
  @after
end

#before ⇒ rope (readonly)

Returns the value of attribute before.

Returns:

  • (rope)


6
7
8
# File 'lib/canopus/patch.rb', line 6

def before
  @before
end

#edits ⇒ Array[Edit] (readonly)

Returns the value of attribute edits.

Returns:



6
7
8
# File 'lib/canopus/patch.rb', line 6

def edits
  @edits
end

Instance Method Details

#compose(other) ⇒ Composite

Parameters:

  • other (patch)

Returns:



24
# File 'lib/canopus/patch.rb', line 24

def compose(other) = Composite.new([self, other])

#inverse ⇒ Patch

Returns:



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

Parameters:

  • offset (Integer)
  • bias: (bias) (defaults to: :right)

Returns:

  • (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