Class: RBS::Rewriter
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/rbs/rewriter.rb |
Class Method Summary
- .new(buffer) ⇒ Rewriter constructor
Instance Attribute Summary
- #buffer readonly
Instance Method Summary
Constructor Details
.new(buffer) ⇒ Rewriter
Instance Attribute Details
#buffer (readonly)
[ GitHub ]# File 'lib/rbs/rewriter.rb', line 5
attr_reader :buffer
Instance Method Details
#add_comment(*locations, content:)
[ GitHub ]# File 'lib/rbs/rewriter.rb', line 25
def add_comment(*locations, content:) earliest = locations.min_by(&:start_pos) or raise "At least one location is required" insert_pos = earliest.start_pos indent = " " * earliest.start_column formatted = format_comment(content, indent) loc = Location.new(buffer, insert_pos, insert_pos) rewrite(loc, "#{formatted}\n#{indent}") end
#delete_comment(comment)
[ GitHub ]#format_comment(content, indent) (private)
[ GitHub ]# File 'lib/rbs/rewriter.rb', line 63
def format_comment(content, indent) content.lines.map do |line| line = line.chomp line.empty? ? "#" : "# #{line}" end.join("\n#{indent}") end
#replace_comment(comment, content:)
[ GitHub ]# File 'lib/rbs/rewriter.rb', line 36
def replace_comment(comment, content:) location = comment.location or raise "Comment must have a location" indent = " " * location.start_column rewrite(location, format_comment(content, indent)) end
#rewrite(location, string)
[ GitHub ]# File 'lib/rbs/rewriter.rb', line 14
def rewrite(location, string) @rewrites.each do |existing_location, _| if location.start_pos < existing_location.end_pos && existing_location.start_pos < location.end_pos raise "Overlapping rewrites: #{existing_location} and #{location}" end end @rewrites << [location, string] self end
#string
[ GitHub ]# File 'lib/rbs/rewriter.rb', line 51
def string result = buffer.content.dup @rewrites.sort_by { |location, _| location.start_pos }.reverse_each do |location, replacement| result[location.start_pos...location.end_pos] = replacement end result end