Class TextEditBuffer

java.lang.Object
com.vaadin.copilot.rewriter.TextEditBuffer

public final class TextEditBuffer extends Object
Records edits against an immutable source string by character offset, then applies them in a single pass.

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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(String source)
    Applies every recorded edit to the given source.
    void
    delete(int start, int end)
    Deletes the text in the given range.
    void
    deleteLine(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.
    void
    insert(int offset, String text)
    Inserts text at the given offset.
    boolean
     
    void
    replace(int start, int end, String text)
    Replaces the text in the given range.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TextEditBuffer

      public TextEditBuffer()
  • Method Details

    • insert

      public void insert(int offset, String text)
      Inserts text at the given offset.
      Parameters:
      offset - the offset to insert at
      text - the text to insert
    • replace

      public void replace(int start, int end, String text)
      Replaces the text in the given range.
      Parameters:
      start - the start offset, inclusive
      end - the end offset, exclusive
      text - the replacement text
    • delete

      public void delete(int start, int end)
      Deletes the text in the given range.
      Parameters:
      start - the start offset, inclusive
      end - the end offset, exclusive
    • deleteLine

      public void deleteLine(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.

      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 to
      start - the start offset, inclusive
      end - the end offset, exclusive
    • isEmpty

      public boolean isEmpty()
      Returns:
      true if no edits have been recorded
    • apply

      public String apply(String source)
      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