porcupine icon indicating copy to clipboard operation
porcupine copied to clipboard

textutils.Change.end is confusing

Open Akuli opened this issue 2 years ago • 0 comments

Bugs with handling change events are difficult to reproduce, so it's important that using them correctly is easy. Porcupine has a dataclass textutils.Change that contains:

    # These should be Tuple[int, int], but they can't be because converting to
    # json and back turns tuples to lists
    start: List[int]
    end: List[int]
    old_text_len: int
    new_text: str

Let's say you replace hello with hi on line 7 with a 4-space indentation before it:

  • old_text_len is 5
  • new_text is "hi"
  • start is [7, 4]
  • What is end? Is it the "old end" [7, 9] (after spaces and hello) or the "new end" [7, 6] (after spaces and hi)?

Turns out that end is the "old end". It kinda makes sense: the "new end" can be computed from start and new_text, but the "old end" can't when this edit erases text from multiple lines.

Instead of just renaming the field to old_end, I think I should have two end fields, old_end and new_end. I usually dislike redundant fields because it's easy to update one and forget to update the other, but that doesn't really apply here because Changes are not meant to be mutated.

Akuli avatar Aug 06 '22 20:08 Akuli