class Solargraph::Source::Change
A change to be applied to text.
Attributes
@return [String]
@return [Range]
Public Class Methods
Source
# File lib/solargraph/source/change.rb, line 19 def initialize range, new_text @range = range @new_text = new_text end
@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.
Public Instance Methods
Source
# 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
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.
Source
# 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
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.
Private Instance Methods
Source
# File lib/solargraph/source/change.rb, line 75 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
@param text [String] @param insert [String] @return [String]