module analysis::diff::edits::ExecuteTextEdits
A semantics for text and file patching based on ((TextEdits-FileSystemChange)) and ((TextEdits-TextEdit)) operators.
Usage
import analysis::diff::edits::ExecuteTextEdits;
Dependencies
extend analysis::diff::edits::TextEdits;
import DateTime;
import IO;
import List;
import String;
Description
This module provides the same functionality as IDEServices for executing FileSystemChanges. Instead of deferring to the IDE to execute the patches and include them in the undo stack and preview modes, this code simply directly patches strings and files.
Benefits
- This module is very useful for batch execution of large scale renovation rewrites,
- or for testing refactoring code without all the UI dependencies.
function executeDocumentEdits
Execute file changes, including in-file edits if present.
void executeDocumentEdits(list[FileSystemChange] edits)
function executeFileSystemChanges
Execute file changes, including in-file edits if present.
void executeFileSystemChanges(list[FileSystemChange] edits)
function executeFileSystemChange
void executeFileSystemChange(removed(loc f))
void executeFileSystemChange(created(loc f))
void executeFileSystemChange(renamed(loc from, loc to))
void executeFileSystemChange(changed(loc file))
function executeFileSystemChange
Edit a file according to the given ((TextEdits-TextEdit)) instructions
void executeFileSystemChange(changed(loc file, list[TextEdit] edits))
function executeTextEdits
Edit a string according to the given ((TextEdits-TextEdit)) instructions
str executeTextEdits(str content, list[TextEdit] edits)
If you have a patch in the shape of a list of TextEdits, then this function applies it for you.
Good sources of correct TextEdit:
Benefits
- fast way to reconstruct an entire file based on a list of changes to that file.
- the
strinterface helps with unit testing of refactoring tools without file IO. - use Execute File System Change for a file based interface to the same functionality.
Pitfalls
- the list of edits must be sorted on the offset, or this algorithm will not have the desired effect.
- the individual edits' range must not overlap, or this algorithm will not have the desired effect.
- the text edits must have been derived earlier from the current state of the string. There is a clear
semantic dependency between
contentandeditsthat the caller of the function must respect to get the desired effect. Not doing so would be like throwing a patch of file A on a completely different file B.