Class: Ibex::Runtime::CST::TextEdit

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/cst/text_edit.rb

Overview

One byte-oriented source replacement.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start:, delete_length:, insert_text:) ⇒ TextEdit

Returns a new instance of TextEdit.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
# File 'lib/ibex/runtime/cst/text_edit.rb', line 62

def initialize(start:, delete_length:, insert_text:)
  raise ArgumentError, "start must be non-negative" if start.negative?
  raise ArgumentError, "delete_length must be non-negative" if delete_length.negative?

  @start = start
  @delete_length = delete_length
  @insert_text = insert_text.b.freeze
  freeze
end

Instance Attribute Details

#delete_lengthObject (readonly)

: Integer



58
59
60
# File 'lib/ibex/runtime/cst/text_edit.rb', line 58

def delete_length
  @delete_length
end

#insert_textObject (readonly)

: String



59
60
61
# File 'lib/ibex/runtime/cst/text_edit.rb', line 59

def insert_text
  @insert_text
end

#startObject (readonly)

: Integer



57
58
59
# File 'lib/ibex/runtime/cst/text_edit.rb', line 57

def start
  @start
end

Class Method Details

.normalize(edits) ⇒ Object

Sort and merge adjacent edits while rejecting intersecting source ranges. All edits remain expressed against the unedited source.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/ibex/runtime/cst/text_edit.rb', line 13

def normalize(edits)
  raise ArgumentError, "edits must contain only TextEdit values" unless edits.all?(TextEdit)

  merge_adjacent(
    edits.each_with_index.sort_by do |edit, index|
      [edit.start, edit.delete_length.zero? ? 0 : 1, index]
    end.map(&:first)
  ).freeze
end

Instance Method Details

#rangeObject



73
# File 'lib/ibex/runtime/cst/text_edit.rb', line 73

def range = @start...(@start + @delete_length)