Class TextEditBuffer
This is the output half of a rewriter that parses read-only: locate nodes in the parse tree, record edits at their offsets, splice once. Because the original text is never re-printed, everything the edits do not touch is preserved exactly, including formatting, comments and line endings.
Contrast with JavaRewriterMerger, which reconstructs output by
diffing a mutated JavaParser AST against the original text and blanks whole
lines for removals. Offsets are character-precise, so deleting a
multi-line expression such as a trailing lambda removes exactly that
expression.
Overlapping edits are rejected rather than silently resolved, since their result would depend on application order. Several insertions at the same offset are allowed and are applied in the order they were recorded.
Instances are not thread safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionApplies every recorded edit to the given source.voiddelete(int start, int end) Deletes the text in the given range.voiddeleteLine(String source, int start, int end) Deletes the given range along with the line it sits on, when removing it would otherwise leave a blank line behind.voidInserts text at the given offset.booleanisEmpty()voidReplaces the text in the given range.
-
Constructor Details
-
TextEditBuffer
public TextEditBuffer()
-
-
Method Details
-
insert
Inserts text at the given offset.- Parameters:
offset- the offset to insert attext- the text to insert
-
replace
Replaces the text in the given range.- Parameters:
start- the start offset, inclusiveend- the end offset, exclusivetext- the replacement text
-
delete
public void delete(int start, int end) Deletes the text in the given range.- Parameters:
start- the start offset, inclusiveend- the end offset, exclusive
-
deleteLine
Deletes the given range along with the line it sits on, when removing it would otherwise leave a blank line behind.Use this for deleting a whole statement or declaration. Leading indentation is removed when only whitespace precedes the range on its line, and the trailing line break is removed when only whitespace follows it. A range sharing its line with other code loses only the range itself.
- Parameters:
source- the source the offsets refer tostart- the start offset, inclusiveend- the end offset, exclusive
-
isEmpty
public boolean isEmpty()- Returns:
- true if no edits have been recorded
-
apply
Applies every recorded edit to the given source.- Parameters:
source- the source the offsets refer to- Returns:
- the edited text, identical to the input when no edits were recorded
- Throws:
CopilotException- if two edits modify overlapping ranges
-