Skip to main content

module analysis::diff::edits::TextEdits

rascal-Not specified

Intermediate representation for file creation, removal and changing, including textual (string) rewriting.

Usage​

import analysis::diff::edits::TextEdits;

Dependencies​

extend analysis::diff::edits::FileSystemChanges;

Description​

Document Edits can be produced by source-to-source transformation tools, and then executed via Execute Document Edits in the REPL or Apply Documents Edits by the IDE.

Benefits​

  • Document edits can be attached to Code Actions and error IDEServices-Messages, to achieve interactive source code rewriting utilities.
  • Document edits can be tested via Execute Document Edits
  • Document edits can be "high fidelity", avoiding unnecessary damage to a source text.

Pitfalls​

  • Code edits depend on a specific state of the source file that may be transient while editing. Use the Code Action interface to avoid racing for the state of the source file.

alias DocumentEdit​

For compatibility we rename FileSystemChange to DocumentEdit

FileSystemChange

data FileSystemChange​

For some FileSystemChanges we know exactly what happened.

data FileSystemChange  
= changed(loc file, list[TextEdit] edits)
;

function changed​

Shorthand for file changes.

FileSystemChange changed(list[TextEdit] edits:[replace(loc l, str _), *_])

data TextEdit​

String rewriting operations

data TextEdit  
= replace(loc range, str replacement)
;

The core operation is to replace a substring with another. The replace operator uses a loc value to point to a range inside a string, and a str as its replacement.

Benefits​

function delete​

Deletion is replacement with an empty string.

TextEdit delete(loc range)

function insertBefore​

Inserting before a given range.

TextEdit insertBefore(loc range, str insertion, str separator=" ")

Benefits​

  • Use separator="\n" to introduce a newline after the inserted text instead of a space.

function insertAfter​

Inserting after a given range.

TextEdit insertAfter(loc range, str insertion, str separator=" ")

Benefits​

  • Use separator="\n" to introduce a newline after the inserted text instead of a space.