class Solargraph::Source::Change
A change to be applied to text.
Attributes
new_text[R]
@return [String]
range[R]
@return [Range]
Public Class Methods
new(range, new_text)
click to toggle source
@param range [Range] The starting and ending positions of the change.
If nil, the original text will be overwritten.
@param new_text
[String] The text to be changed.
# File lib/solargraph/source/change.rb, line 19 def initialize range, new_text @range = range @new_text = new_text end
Public Instance Methods
repair(text)
click to toggle source
Repair an update by replacing the new text with similarly formatted whitespace.
@param text [String] The text to be changed. @return [String] The updated text.
# File lib/solargraph/source/change.rb, line 55 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)
click to toggle source
Write the change to the specified text.
@param text [String] The text to be changed. @param nullable [Boolean] If true, minor changes that could generate
syntax errors will be repaired.
@return [String] The updated text.
# File lib/solargraph/source/change.rb, line 30 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
Private Instance Methods
commit(text, insert)
click to toggle source
# File lib/solargraph/source/change.rb, line 72 def commit text, insert start_offset = Position.to_offset(text, range.start) end_offset = Position.to_offset(text, range.ending) (start_offset == 0 ? '' : text[0..start_offset-1].to_s) + normalize(insert) + text[end_offset..-1].to_s end