Class: Solargraph::Source::Change

Inherits:
Object
  • Object
show all
Includes:
EncodingFixes
Defined in:
lib/solargraph/source/change.rb

Overview

A change to be applied to text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EncodingFixes

normalize

Constructor Details

#initialize(range, new_text) ⇒ Change


17
18
19
20
# File 'lib/solargraph/source/change.rb', line 17

def initialize range, new_text
  @range = range
  @new_text = new_text
end

Instance Attribute Details

#new_textString (readonly)


12
13
14
# File 'lib/solargraph/source/change.rb', line 12

def new_text
  @new_text
end

#rangeRange (readonly)


9
10
11
# File 'lib/solargraph/source/change.rb', line 9

def range
  @range
end

Instance Method Details

#repair(text) ⇒ String

Repair an update by replacing the new text with similarly formatted whitespace.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/solargraph/source/change.rb', line 53

def repair text
  fixed = new_text.gsub(/[^\s]/, ' ')
  if range.nil?
    fixed
  else
    result = commit text, fixed
    off = Position.to_offset(text, range.start)
    match = result[0, off].match(/[\.:]+\z/)
    if match
      result = result[0, off].sub(/#{match[0]}\z/, ' ' * match[0].length) + result[off..-1]
    end
    result
  end
end

#write(text, nullable = false) ⇒ String

Write the change to the specified text.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/solargraph/source/change.rb', line 28

def write text, nullable = false
  if nullable and !range.nil? and new_text.match(/[\.\[\{\(@\$:]$/)
    [':', '@'].each do |dupable|
      next unless new_text == dupable
      offset = Position.to_offset(text, range.start)
      if text[offset - 1] == dupable
        p = Position.from_offset(text, offset - 1)
        r = Change.new(Range.new(p, range.start), ' ')
        text = r.write(text)
      end
      break
    end
    commit text, "#{new_text[0..-2]} "
  elsif range.nil?
    new_text
  else
    commit text, new_text
  end
end