Class: Steep::Services::ContentChange

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/services/content_change.rb

Defined Under Namespace

Classes: Position

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range: nil, text:) ⇒ ContentChange

Returns a new instance of ContentChange.



25
26
27
28
# File 'lib/steep/services/content_change.rb', line 25

def initialize(range: nil, text:)
  @range = range
  @text = text
end

Instance Attribute Details

#rangeObject (readonly)

Returns the value of attribute range.



23
24
25
# File 'lib/steep/services/content_change.rb', line 23

def range
  @range
end

#textObject (readonly)

Returns the value of attribute text.



23
24
25
# File 'lib/steep/services/content_change.rb', line 23

def text
  @text
end

Class Method Details

.string(string) ⇒ Object



40
41
42
# File 'lib/steep/services/content_change.rb', line 40

def self.string(string)
  new(text: string)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



30
31
32
# File 'lib/steep/services/content_change.rb', line 30

def ==(other)
  other.is_a?(ContentChange) && other.range == range && other.text == text
end

#apply_to(text) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/steep/services/content_change.rb', line 44

def apply_to(text)
  if range
    text = text.dup
    start_pos, end_pos = range

    buf = RBS::Buffer.new(name: :_, content: text)
    start_pos = buf.loc_to_pos([start_pos.line, start_pos.column])
    end_pos = buf.loc_to_pos([end_pos.line, end_pos.column])

    text[start_pos...end_pos] = self.text
    text
  else
    self.text
  end
end

#hashObject



36
37
38
# File 'lib/steep/services/content_change.rb', line 36

def hash
  self.class.hash ^ range.hash ^ text.hash
end