Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Undo of actions taken by package results in corrupted display of buffer #7516

Closed

Description

@iarna

I wrote a package: https://atom.io/packages/joe-keybinds (github: https://github.com/iarna/atom-joe) and when I use the move operation it provides (https://github.com/iarna/atom-joe/blob/master/lib/joe.coffee#L56-L67) and then undo it with Cmd-Z, the resulting display of the buffer is corrupted. I can tell that the underlying buffer is fine because saving and reloading shows the undo occurred correctly. The display corruption shows duplicated lines where there are none, and things like what constitutes the end of the line no longer match the display.

The move operation uses transact to combine of the pieces of the move operation into a single undoable chunk.

Activity

  1. iarna commented on Jun 28, 2015

    @iarna
    Author

    example breakage

    Example of what I see ↑

  2. lee-dohm commented on Jun 28, 2015

    @lee-dohm
    Contributor

    Repro Steps

    1. Install joe-keybinds package
    2. Start Atom
    3. Open a file with some text in it (I used my init.coffee)
    4. Move to a random location
    5. Mark the start of a block using Ctrl+K B
    6. Move someplace later on the line
    7. Mark the end of a block using Ctrl+K K
    8. Move to the next line
    9. Move the block using Ctrl+K M
    10. Undo the action using Cmd+Z

    Expected: State of the file to return to where it was before the move
    Actual: The state of the file itself returns to where it was, but the displayed text includes an extra copy of the line to where the block was moved

    @maxbrunsfeld is there anything in particular that is wrong with the code being used in the package?

  3. self-assigned this
    on Jun 29, 2015
  4. azoerb commented on Jul 2, 2015

    @azoerb

    I'm not sure if this is the same error, but I've been getting similar weird behavior when using undo/redo in vim-mode. It seems to scatter lines all over the place, breaking them into seemingly random places. I can't get the correct state back afterwards. I'm not sure how to reproduce it consistently though.

  5. maxbrunsfeld commented on Jul 20, 2015

    @maxbrunsfeld
    Contributor

    This is happening because of two things:

    • When a Range object is passed to TextEditor::setTextInRange, that range object is stored in the text-editor's history.
    • This package atom-joe reuses Range objects, mutating their start and end points.

    The result is that the text-editor's history gets modified into an inconsistent state. I think that rewriting the blockMove method as follows fixes the problem:

      blockMove: ->
        return unless editor = atom.workspace.getActiveTextEditor()
        persistentBlock = getBlock(editor)
        moveFrom = persistentBlock.getBufferRange()
        toMove = editor.getTextInBufferRange(moveFrom)
        editor.transact ->
          editor.setTextInBufferRange(moveFrom, '')
          start = editor.getCursorBufferPosition()
          editor.insertText(toMove)
          end = editor.getCursorBufferPosition()
          persistentBlock.setBufferRange([start, end])

    We should probably be calling Object.freeze on these (maybe all) Range objects, since they are not intended to be mutated in this way.

    /cc @nathansobo should Ranges just always be frozen?

  6. maxbrunsfeld commented on Jul 20, 2015

    @maxbrunsfeld
    Contributor

    By the way @iarna thanks for this very detailed report; I'm sorry that you had to deal with this confusing behavior. Please let me know if you're still working on atom-joe and notice any other problems with the Atom API.

  7. nathansobo commented on Jul 21, 2015

    @nathansobo
    Contributor

    @maxbrunsfeld I do think we should freeze ranges and points as much as possible. I'm a bit concerned about the implications of doing it universally, since some packages mutate them for convenience. Starting with freezing stuff that goes into the history seems reasonable, and maybe exploring a way to freeze by default is also a good idea.

  8. iarna commented on Jul 21, 2015

    @iarna
    Author

    \o/ Awesome, glad to see this fixed! I'm sure I will continue to do even weirder things w/ my little module, so w/ luck I'll help find some more of the hard to find corner cases. =)

  9. lock commented on Jan 21, 2019

    @lock

    This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

  10. locked as resolved and limited conversation to collaborators on Jan 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions