package file-rewriter
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=bcc2177b21daf06d379806045cab266ef9017968645dd8fb445594222e0f1d07
sha512=e7c546d98d1b0da412d63366cce777cc50a24f89c60ce9af67bb6698e6c12a620480daaefb5e186d6d5daf6143ac2b65a6f26a8daea0588055cfcfef90c13401
doc/file-rewriter/File_rewriter/index.html
Module File_rewriter
Source
A file rewriter is an abstraction used by programs that wants to perform some tweaks to an existing file, such as small rewrites or refactoring.
The way they express their changes is by adding substitution to an existing file contents. Add the end, all substitutions are applied and a new contents can be produced.
Number of bytes from the beginning of the input. The first byte has offset 0
.
A range refers to a chunk of the file, from start (included) to stop (excluded). Ranges may be used to remove chunks of the file, or replace them with some alternative text.
t
is a mutable type that holds the original contents of the file being rewritten, as well as all the edits that are enqueued to it over time (inserts, replaces and removals).
Initialize a new t
from an original contents, initially with no edits. The path
supplied is only used for context awareness and error messages, but no I/O occurs on disk.
Rewrites
This may come as a surprise, but offsets refer to the offsets in the original contents, not the ongoing rewritten buffer. Thus, it is OK to insert several texts at the same offset. The order of insertion decides which inserts are applied first. Raises Invalid_argument
if offset
is not a valid offset in t's original_contents
, indicating a programming error that needs to be fixed.
Replace an entire section denoted by the given range by an alternative text. The range refers to the range in the original contents, not the ongoing rewritten buffer. Through the use of this module it is invalid to request replaces
with overlapping ranges, and doing so will cause exception Invalid_rewrites
to be raised during contents
. Raises Invalid_argument
if range
is not a valid range in t's original_contents
, indicating a programming error that needs to be fixed.
This is a small convenient wrapper that is equivalent to replacing a range by the empty string. Raises Invalid_argument
if range
is not a valid range in t's original_contents
, indicating a programming error that needs to be fixed.
You may decide at a given moment that you want to discard all the edits inserted so far, and just start over. This has the same effect than starting fresh with a newly created t
.
Output
Build the final result, with all the substitutions applied. If the substitutions requested causes conflicts, this function will raise Invalid_rewrites
. The exception is not really meant to be caught in a production path, because it would usually be indicative of a programming error. If you prefer a result type, see contents_result
.
Same as contents
but wraps the output into a result type rather than raising an exception.