Skip to main content

module analysis::diff::edits::TextEdits

rascal-0.40.16

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

Usage

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

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.

data DocumentEdit

File changing operations.

data DocumentEdit  
= removed(loc file)
| created(loc file)
| renamed(loc from, loc to)
| changed(loc file, list[TextEdit] edits)
;

function changed

Shorthand for file changes.

DocumentEdit 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.

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=" ")

function insertAfter

Inserting after a given range.

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