Parent

Parser::Source::Rewriter

{Rewriter} performs the heavy lifting in the source rewriting process. It schedules code updates to be performed in the correct order and verifies that no two updates clobber each other, that is, attempt to modify the same part of code.

If it is detected that one update clobbers another one, an `:error` and a `:note` diagnostics describing both updates are generated and passed to the diagnostic engine. After that, an exception is raised.

The default diagnostic engine consumer simply prints the diagnostics to `stderr`.

@!attribute [r] source_buffer

@return [Source::Buffer]

@!attribute [r] diagnostics

@return [Diagnostic::Engine]

@api public

Attributes

diagnostics[R]
source_buffer[R]

Public Class Methods

new(source_buffer) click to toggle source

@param [Source::Buffer] source_buffer

# File lib/parser/source/rewriter.rb, line 31
def initialize(source_buffer)
  @diagnostics = Diagnostic::Engine.new
  @diagnostics.consumer = lambda do |diag|
    $stderr.puts diag.render
  end

  @source_buffer = source_buffer
  @queue         = []
  @clobber       = 0
end

Public Instance Methods

insert_after(range, content) click to toggle source

Inserts new code after the given source range.

@param [Range] range @param [String] content @return [Rewriter] self @raise [RuntimeError] when clobbering is detected

# File lib/parser/source/rewriter.rb, line 73
def insert_after(range, content)
  append Rewriter::Action.new(range.end, content)
end
insert_before(range, content) click to toggle source

Inserts new code before the given source range.

@param [Range] range @param [String] content @return [Rewriter] self @raise [RuntimeError] when clobbering is detected

# File lib/parser/source/rewriter.rb, line 61
def insert_before(range, content)
  append Rewriter::Action.new(range.begin, content)
end
process() click to toggle source

Applies all scheduled changes to the `source_buffer` and returns modified source as a new string.

@return [String]

# File lib/parser/source/rewriter.rb, line 95
def process
  adjustment = 0
  source     = @source_buffer.source.dup

  sorted_queue = @queue.sort_by.with_index do |action, index|
    [action.range.begin_pos, index]
  end

  sorted_queue.each do |action|
    begin_pos = action.range.begin_pos + adjustment
    end_pos   = begin_pos + action.range.length

    source[begin_pos...end_pos] = action.replacement

    adjustment += (action.replacement.length - action.range.length)
  end

  source
end
remove(range) click to toggle source

Removes the source range.

@param [Range] range @return [Rewriter] self @raise [RuntimeError] when clobbering is detected

# File lib/parser/source/rewriter.rb, line 49
def remove(range)
  append Rewriter::Action.new(range, '')
end
replace(range, content) click to toggle source

Replaces the code of the source range `range` with `content`.

@param [Range] range @param [String] content @return [Rewriter] self @raise [RuntimeError] when clobbering is detected

# File lib/parser/source/rewriter.rb, line 85
def replace(range, content)
  append Rewriter::Action.new(range, content)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.