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

.from_lsp(changes) ⇒ Object



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

def self.from_lsp(changes)
  changes.map do |change|
    content_range =
      if range = change[:range]
        [
          range[:start].yield_self {|pos| Services::ContentChange::Position.new(line: pos[:line] + 1, column: pos[:character]) },
          end_pos = range[:end].yield_self {|pos| Services::ContentChange::Position.new(line: pos[:line] + 1, column: pos[:character]) }
        ] #: range
      end
    Services::ContentChange.new(range: content_range, text: change[:text])
  end
end

.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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/steep/services/content_change.rb', line 57

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

    buf = RBS::Buffer.new(name: Pathname("_"), 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