Module: Spoom::Source

Defined in:
lib/spoom/source/rewriter.rb

Overview

This module provides a simple API to rewrite source code.

Using a Rewriter, you can build a list of changes to apply to a source file and apply them all at once. Edits are applied from bottom to top, so that the line numbers are not remapped after each edit.

The source code is represented as an array of bytes, so that it can be manipulated in place. The client is responsible for string <-> bytes conversions and encoding handling.

bytes = "def foo; end".bytes

rewriter = Spoom::Source::Rewriter.new
rewriter << Spoom::Source::Replace.new(4, 6, "baz")
rewriter << Spoom::Source::Insert.new(0, "def bar; end\n")
rewriter.rewrite!(bytes)

puts bytes.pack("C*") # => "def bar; end\ndef baz; end"

Defined Under Namespace

Classes: Delete, Edit, Insert, PositionError, Replace, Rewriter